diff --git a/cmd/cloudcontrol.go b/cmd/cloudcontrol.go index 65611cc..af0f4b5 100644 --- a/cmd/cloudcontrol.go +++ b/cmd/cloudcontrol.go @@ -82,10 +82,17 @@ var cloudcontrolCmd = &cobra.Command{ log.WithError(err).Fatalf("failed matching resources") } fmt.Printf("listing matched resource %s\n", resourceType.String()) - _, err = api.ListResources(resourceType) + resourceList, err := api.ListResources(resourceType) if err != nil { log.WithError(err).Fatalf("failed listing resource %s", inputType) } + for _, r := range resourceList.Resources { + id, err := r.GetIdentifier() + if err != nil { + panic(fmt.Errorf("failed parsing identifier %s", err)) + } + fmt.Printf("ID: %s\nProperties: %s\n", id, r.GetRawProperties()) + } default: log.Fatalf("unknown action %s", action) } diff --git a/lib/awsu/cloudcontrol.go b/lib/awsu/cloudcontrol.go index b358009..6ee28e9 100644 --- a/lib/awsu/cloudcontrol.go +++ b/lib/awsu/cloudcontrol.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/aws/aws-sdk-go-v2/service/cloudcontrol" + cctypes "github.com/aws/aws-sdk-go-v2/service/cloudcontrol/types" "github.com/aws/aws-sdk-go-v2/service/cloudformation" "github.com/aws/aws-sdk-go/aws" log "github.com/sirupsen/logrus" @@ -12,17 +13,20 @@ import ( type CCResourceDescriber interface { IsDescribed() bool + IsShallowDescribe() bool GetType() *CCResourceProperty GetTypeName() string GetIdentifier() (string, error) GetRawDescribed() *cloudcontrol.GetResourceOutput + GetRawShallowDescribed() *cctypes.ResourceDescription GetRawProperties() string } // CCResourceWrapper implementeds CCResourceDescriber interface type CCResourceWrapper struct { - RawResource *cloudcontrol.GetResourceOutput - Type *CCResourceProperty + RawResource *cloudcontrol.GetResourceOutput + RawResourceList *cctypes.ResourceDescription + Type *CCResourceProperty } func NewResourceFromGetOutput(output *cloudcontrol.GetResourceOutput, inputType *CCResourceProperty) CCResourceDescriber { @@ -30,14 +34,18 @@ func NewResourceFromGetOutput(output *cloudcontrol.GetResourceOutput, inputType RawResource: output, Type: inputType, } -} -func NewResourceFromListOutput(output *cloudcontrol.ListResourcesOutput, inputType *CCResourceProperty) CCResourceDescriber { - return nil - // return &CCResourceWrapper{ - // RawResource: output, - // Type: inputType, - // } +} +func NewResourceFromListOutput(output *cloudcontrol.ListResourcesOutput, inputType *CCResourceProperty) []CCResourceDescriber { + var resources []CCResourceDescriber + for _, r := range output.ResourceDescriptions { + capture := r + resources = append(resources, &CCResourceWrapper{ + RawResourceList: &capture, + Type: inputType, + }) + } + return resources } type CCResourcesList struct { @@ -48,10 +56,18 @@ func (cc *CCResourceWrapper) GetRawDescribed() *cloudcontrol.GetResourceOutput { return cc.RawResource } +func (cc *CCResourceWrapper) GetRawShallowDescribed() *cctypes.ResourceDescription { + return cc.RawResourceList +} + func (cc *CCResourceWrapper) IsDescribed() bool { return cc.RawResource != nil } +func (cc *CCResourceWrapper) IsShallowDescribe() bool { + return cc.RawResourceList != nil +} + func (cc *CCResourceWrapper) GetType() *CCResourceProperty { return cc.Type } @@ -66,14 +82,20 @@ func (cc *CCResourceWrapper) GetIdentifier() (string, error) { if cc.IsDescribed() { return aws.StringValue(cc.RawResource.ResourceDescription.Identifier), nil } + if cc.IsShallowDescribe() { + return aws.StringValue(cc.RawResourceList.Identifier), nil + } return "", fmt.Errorf("resource not described") } func (cc *CCResourceWrapper) GetRawProperties() string { - if !cc.IsDescribed() { - return "" + if cc.IsDescribed() { + return aws.StringValue(cc.GetRawDescribed().ResourceDescription.Properties) + } + if cc.IsShallowDescribe() { + return aws.StringValue(cc.RawResourceList.Properties) } - return aws.StringValue(cc.GetRawDescribed().ResourceDescription.Properties) + return "" } type CloudControlAPI interface { @@ -143,19 +165,20 @@ func (cc *CloudControlClient) ListResources(resource *CCResourceProperty) (*CCRe input := &cloudcontrol.ListResourcesInput{ TypeName: aws.String(resource.String()), } + var result []CCResourceDescriber for { resp, err := cc.client().ListResources(context.Background(), input) if err != nil { - return nil, err + return nil, fmt.Errorf("listing resources: %w", err) } + result = append(result, NewResourceFromListOutput(resp, resource)...) for _, r := range resp.ResourceDescriptions { - log.Infof("Resource: %s Props %s", *r.Identifier, *r.Properties) + log.Debugf("Resource: %s Props %s", *r.Identifier, *r.Properties) } if resp.NextToken == nil { break } input.NextToken = resp.NextToken } - return nil, nil - // return NewResourceFromListOutput(resp, resource), nil + return &CCResourcesList{Resources: result}, nil }