Skip to content

Commit

Permalink
fix listing
Browse files Browse the repository at this point in the history
  • Loading branch information
Isan-Rivkin committed Jul 18, 2023
1 parent 007c7d4 commit da7422f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
9 changes: 8 additions & 1 deletion cmd/cloudcontrol.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
55 changes: 39 additions & 16 deletions lib/awsu/cloudcontrol.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,47 @@ 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"
)

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 {
return &CCResourceWrapper{
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 {
Expand All @@ -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
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}

0 comments on commit da7422f

Please sign in to comment.