Skip to content

Commit

Permalink
Merge pull request #16 from trek10inc/change_structure
Browse files Browse the repository at this point in the history
Change structure
  • Loading branch information
Jeff authored Nov 16, 2020
2 parents 21b4fbd + 4b3b29c commit d9abf78
Show file tree
Hide file tree
Showing 243 changed files with 1,846 additions and 1,682 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,6 @@ OPTIONS:
## Future Work

Although AWSets is in a place where it provides solid resource coverage and works well for a lot of use cases, there is more work to be done:
* Supporting more AWS resources and relationships - 200+ is a good start, but there are many more to go
* Supporting more AWS resources and relationships - 300+ is a good start, but there are many more to go
* In addition to supporting more resources, existing resources may have some gaps. For example, some resources require secondary calls to get Tags
* Improve relationship building - AWSets should be able to match a DynamoDB table to a Lambda Function when the DDB table is passed in via environment variable
33 changes: 33 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package awsets

import "github.com/trek10inc/awsets/resource"

// Cacher is an interface that defines the necessary functions for an AWSets
// cache.
type Cacher interface {
Initialize(accountId string) error
IsCached(region string, kind ListerName) bool
SaveGroup(kind ListerName, group *resource.Group) error
LoadGroup(region string, kind ListerName) (*resource.Group, error)
}

// NoOpCache is the default cache provided by AWSets. It does nothing, and
// will never load nor save any data.
type NoOpCache struct {
}

func (c NoOpCache) Initialize(accountId string) error {
return nil
}

func (c NoOpCache) IsCached(region string, kind ListerName) bool {
return false
}

func (c NoOpCache) SaveGroup(kind ListerName, group *resource.Group) error {
return nil
}

func (c NoOpCache) LoadGroup(region string, kind ListerName) (*resource.Group, error) {
return resource.NewGroup(), nil
}
26 changes: 15 additions & 11 deletions cmd/awsets/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/cheggaaa/pb/v3"
"github.com/trek10inc/awsets"
"github.com/trek10inc/awsets/cmd/awsets/cache"
"github.com/trek10inc/awsets/option"
"github.com/trek10inc/awsets/context"
"github.com/urfave/cli/v2"
)

Expand Down Expand Up @@ -111,9 +111,13 @@ var listCmd = &cli.Command{
log.Fatalf("failed to open cache: %v", err)
}

statusChan := make(chan option.StatusUpdate)
options := []option.Option{
option.WithStatus(statusChan),
statusChan := make(chan context.StatusUpdate)
options := []awsets.Option{
awsets.WithAWSConfig(awscfg),
awsets.WithStatus(statusChan),
awsets.WithRegions(regions),
awsets.WithListers(listers),
awsets.WithCache(bc),
}
verbose := c.Bool("verbose")
showProgress := c.Bool("show-progress")
Expand All @@ -132,20 +136,20 @@ var listCmd = &cli.Command{
bar = pb.StartNew(update.TotalJobs)
}
switch update.Type {
case option.StatusLogInfo:
case context.StatusLogInfo:
if verbose {
fmt.Fprintf(os.Stdout, "%s - %s - %s\n", update.Region, update.Lister, update.Message)
}
case option.StatusLogDebug:
case context.StatusLogDebug:
if verbose {
fmt.Fprintf(os.Stdout, "%s - %s - %s\n", update.Region, update.Lister, update.Message)
}
case option.StatusLogError:
case context.StatusLogError:
fmt.Fprintf(os.Stderr, "%s - %s - %s\n", update.Region, update.Lister, update.Message)
case option.StatusProcessing:
case option.StatusComplete:
case context.StatusProcessing:
case context.StatusComplete:
fallthrough
case option.StatusCompleteWithError:
case context.StatusCompleteWithError:
if bar != nil {
bar.Increment()
}
Expand All @@ -154,7 +158,7 @@ var listCmd = &cli.Command{
}
}()

rg, err := awsets.List(awscfg, regions, listers, bc, options...)
rg, err := awsets.List(options...)
if err != nil {
return fmt.Errorf("failed to list resources: %w", err)
}
Expand Down
57 changes: 57 additions & 0 deletions context/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package context

import (
"context"

"github.com/aws/aws-sdk-go-v2/aws"
)

type AWSetsCtx struct {
AWSCfg aws.Config
AccountId string
WorkerId int
Context context.Context
Lister string
StatusChan chan<- StatusUpdate
TotalJobs int
}

func (c *AWSetsCtx) Region() string {
return c.AWSCfg.Region
}

func (c *AWSetsCtx) Copy(region string) *AWSetsCtx {

cop := &AWSetsCtx{
AWSCfg: c.AWSCfg.Copy(),
AccountId: c.AccountId,
Context: c.Context,
StatusChan: c.StatusChan,
Lister: c.Lister,
WorkerId: c.WorkerId,
TotalJobs: c.TotalJobs,
}
cop.AWSCfg.Region = region
return cop
}

func (c *AWSetsCtx) SendStatus(statusType StatusType, msg string) {
if c.StatusChan == nil {
return
}
su := StatusUpdate{
Type: statusType,
Lister: c.Lister,
Region: c.Region(),
Message: msg,
WorkerId: c.WorkerId,
TotalJobs: c.TotalJobs,
}
c.StatusChan <- su
}

func (c *AWSetsCtx) Close() {
if c.StatusChan != nil {
close(c.StatusChan)
}
}
2 changes: 1 addition & 1 deletion option/status.go → context/status.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package option
package context

type StatusType string

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.15

require (
github.com/aws/aws-sdk-go-v2 v0.29.0
github.com/aws/aws-sdk-go-v2/config v0.2.0
github.com/aws/aws-sdk-go-v2/service/accessanalyzer v0.29.0
github.com/aws/aws-sdk-go-v2/service/acm v0.29.0
github.com/aws/aws-sdk-go-v2/service/amplify v0.29.0
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
github.com/aws/aws-sdk-go-v2 v0.27.0/go.mod h1:F9alGopZD03PJvZJSuXC9d0nQuvbmgcKgeIrEmLZG3s=
github.com/aws/aws-sdk-go-v2 v0.29.0 h1:V/KKvuMO2hwHRg2SXJc5aasBHhD1AWbS6KMWg/Ueq1w=
github.com/aws/aws-sdk-go-v2 v0.29.0/go.mod h1:4d1/Ee0vCwCF7BfG1hCT3zu82493cRy5+VZ8JHvMPf0=
github.com/aws/aws-sdk-go-v2/config v0.2.0 h1:63AqNmJaCHhvrXcG9AXU8pr0wbQGlkzxQO+C3N0r54Y=
github.com/aws/aws-sdk-go-v2/config v0.2.0/go.mod h1:iswkkZ9YWgy1Pk1E9pmtxnFrqtNijgJDfa0NBnWvsSA=
github.com/aws/aws-sdk-go-v2/credentials v0.1.2 h1:Sls7iKWkeOIGiTxXBWW+8fGTCWQN9SzTVK+FLBndd9w=
github.com/aws/aws-sdk-go-v2/credentials v0.1.2/go.mod h1:mOKm5nJa7J2wCRHkF4A7JB4ZdKU9BvomvD0SZuWIZso=
github.com/aws/aws-sdk-go-v2/ec2imds v0.1.2 h1:4QZ0UHyjocD64tjjp7oDb2/moaISTwch76dEPJB9YAI=
github.com/aws/aws-sdk-go-v2/ec2imds v0.1.2/go.mod h1:rEc/qkcAcM4YE0NPLf+H+2GcVajFlbkNAGnL0CwiGZM=
github.com/aws/aws-sdk-go-v2/service/accessanalyzer v0.29.0 h1:BiLOKk5Hh7ZQAXwdbIh5n+s82S0qtx+HTyOAxN8BEAE=
github.com/aws/aws-sdk-go-v2/service/accessanalyzer v0.29.0/go.mod h1:aF+4BdVGQrxtef1NSQXQWtXXq3pf/rIbJ48BlULsHMk=
github.com/aws/aws-sdk-go-v2/service/acm v0.29.0 h1:d1D0uHB+Mkqu/1xj7MlBVDddPNhPjzQ7Nl2UntOIkSY=
Expand Down Expand Up @@ -152,6 +159,7 @@ github.com/aws/aws-sdk-go-v2/service/sqs v0.29.0 h1:tNUvLwVXDkJB6esfAcYhW68P/q12
github.com/aws/aws-sdk-go-v2/service/sqs v0.29.0/go.mod h1:JCxLIhmZPNP/RY5R07RcwbofC2tMhx+V5uc8/ZiWjs8=
github.com/aws/aws-sdk-go-v2/service/ssm v0.29.0 h1:FgBPiadv4E0RuR7TRHVbw3+CxW1RF0jv7Hp5ilaLkOE=
github.com/aws/aws-sdk-go-v2/service/ssm v0.29.0/go.mod h1:Bxd06cEL72MMPAI1dVti/9NqCeJUh+rhjJYTUdhzbCI=
github.com/aws/aws-sdk-go-v2/service/sts v0.27.0/go.mod h1:0N+L7fP2Zm4s8Ib+ZBCjtX4caWMiMG5xV6RB6ec8x/c=
github.com/aws/aws-sdk-go-v2/service/sts v0.29.0 h1:EOEsrzOQh+xU4lKbrkRoTybsP704I32GczRFsW6apEw=
github.com/aws/aws-sdk-go-v2/service/sts v0.29.0/go.mod h1:zV0Fx4GE1wPZJ3iHn1g7UxoPb+uJfqOkvBp3UQAq3bc=
github.com/aws/aws-sdk-go-v2/service/transfer v0.29.0 h1:raEKBms1Gtl2weiyYd5agYSLVmH7XpcdglExNNw7aE8=
Expand All @@ -164,6 +172,7 @@ github.com/aws/aws-sdk-go-v2/service/wafv2 v0.29.0 h1:yfg6krvud6WkJhqwqGzajKgHGb
github.com/aws/aws-sdk-go-v2/service/wafv2 v0.29.0/go.mod h1:pnmyxxSC+z6KunladbhxN4SNE2b4MUJCSH8xpJltgc4=
github.com/aws/aws-sdk-go-v2/service/workspaces v0.29.0 h1:dA848fqnKn474Z58UeW8qQbQcLcPipPcin/l31rqQKY=
github.com/aws/aws-sdk-go-v2/service/workspaces v0.29.0/go.mod h1:SdzetrxOP9fSS+RK4WRmSReQHGEZREIujmQIYe4RaNE=
github.com/awslabs/smithy-go v0.2.0/go.mod h1:hPOQwnmBLHsUphH13tVSjQhTAFma0/0XoZGbBcOuABI=
github.com/awslabs/smithy-go v0.2.1/go.mod h1:hPOQwnmBLHsUphH13tVSjQhTAFma0/0XoZGbBcOuABI=
github.com/awslabs/smithy-go v0.3.0 h1:I1EQ1P+VtxpuNnGYymATewaKrlnaYQwFvO8lNTsafbs=
github.com/awslabs/smithy-go v0.3.0/go.mod h1:hPOQwnmBLHsUphH13tVSjQhTAFma0/0XoZGbBcOuABI=
Expand All @@ -183,6 +192,7 @@ github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
Expand Down
10 changes: 5 additions & 5 deletions lister/accessanalyzer_analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package lister
import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/accessanalyzer"
"github.com/trek10inc/awsets/option"
"github.com/trek10inc/awsets/context"
"github.com/trek10inc/awsets/resource"
)

Expand All @@ -19,20 +19,20 @@ func (l AWSAccessAnalyzerAnalyzer) Types() []resource.ResourceType {
return []resource.ResourceType{resource.AccessAnalyzerAnalyzer}
}

func (l AWSAccessAnalyzerAnalyzer) List(cfg option.AWSetsConfig) (*resource.Group, error) {
svc := accessanalyzer.NewFromConfig(cfg.AWSCfg)
func (l AWSAccessAnalyzerAnalyzer) List(ctx context.AWSetsCtx) (*resource.Group, error) {
svc := accessanalyzer.NewFromConfig(ctx.AWSCfg)

rg := resource.NewGroup()
err := Paginator(func(nt *string) (*string, error) {
req, err := svc.ListAnalyzers(cfg.Context, &accessanalyzer.ListAnalyzersInput{
req, err := svc.ListAnalyzers(ctx.Context, &accessanalyzer.ListAnalyzersInput{
MaxResults: aws.Int32(100),
NextToken: nt,
})
if err != nil {
return nil, err
}
for _, v := range req.Analyzers {
r := resource.New(cfg, resource.AccessAnalyzerAnalyzer, v.Name, v.Name, v)
r := resource.New(ctx, resource.AccessAnalyzerAnalyzer, v.Name, v.Name, v)
rg.AddResource(r)
}
return req.NextToken, nil
Expand Down
14 changes: 7 additions & 7 deletions lister/acm_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/acm"
"github.com/trek10inc/awsets/arn"
"github.com/trek10inc/awsets/option"
"github.com/trek10inc/awsets/context"
"github.com/trek10inc/awsets/resource"
)

Expand All @@ -22,12 +22,12 @@ func (l AWSAcmCertificate) Types() []resource.ResourceType {
return []resource.ResourceType{resource.AcmCertificate}
}

func (l AWSAcmCertificate) List(cfg option.AWSetsConfig) (*resource.Group, error) {
svc := acm.NewFromConfig(cfg.AWSCfg)
func (l AWSAcmCertificate) List(ctx context.AWSetsCtx) (*resource.Group, error) {
svc := acm.NewFromConfig(ctx.AWSCfg)

rg := resource.NewGroup()
err := Paginator(func(nt *string) (*string, error) {
req, err := svc.ListCertificates(cfg.Context, &acm.ListCertificatesInput{
req, err := svc.ListCertificates(ctx.Context, &acm.ListCertificatesInput{
MaxItems: aws.Int32(100),
NextToken: nt,
})
Expand All @@ -36,15 +36,15 @@ func (l AWSAcmCertificate) List(cfg option.AWSetsConfig) (*resource.Group, error
}
for _, cert := range req.CertificateSummaryList {

res, err := svc.DescribeCertificate(cfg.Context, &acm.DescribeCertificateInput{CertificateArn: cert.CertificateArn})
res, err := svc.DescribeCertificate(ctx.Context, &acm.DescribeCertificateInput{CertificateArn: cert.CertificateArn})
if err != nil {
return nil, fmt.Errorf("unable to describe certificate %s: %w", *cert.CertificateArn, err)
}
//if arn.IsArnP(res.Certificate.CertificateArn) {
certArn := arn.ParseP(res.Certificate.CertificateArn)
r := resource.New(cfg, resource.AcmCertificate, certArn.ResourceId, certArn.ResourceId, res.Certificate)
r := resource.New(ctx, resource.AcmCertificate, certArn.ResourceId, certArn.ResourceId, res.Certificate)
//}
tagRes, err := svc.ListTagsForCertificate(cfg.Context, &acm.ListTagsForCertificateInput{
tagRes, err := svc.ListTagsForCertificate(ctx.Context, &acm.ListTagsForCertificateInput{
CertificateArn: cert.CertificateArn,
})
if err != nil {
Expand Down
18 changes: 9 additions & 9 deletions lister/amplify_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/amplify"
"github.com/trek10inc/awsets/arn"
"github.com/trek10inc/awsets/option"
"github.com/trek10inc/awsets/context"
"github.com/trek10inc/awsets/resource"
)

Expand All @@ -26,25 +26,25 @@ func (l AWSAmplifyApp) Types() []resource.ResourceType {
}
}

func (l AWSAmplifyApp) List(cfg option.AWSetsConfig) (*resource.Group, error) {
func (l AWSAmplifyApp) List(ctx context.AWSetsCtx) (*resource.Group, error) {

svc := amplify.NewFromConfig(cfg.AWSCfg)
svc := amplify.NewFromConfig(ctx.AWSCfg)
rg := resource.NewGroup()

err := Paginator(func(nt *string) (*string, error) {
apps, err := svc.ListApps(cfg.Context, &amplify.ListAppsInput{
apps, err := svc.ListApps(ctx.Context, &amplify.ListAppsInput{
MaxResults: aws.Int32(100),
NextToken: nt,
})
if err != nil {
return nil, fmt.Errorf("failed to list amplify apps: %w", err)
}
for _, v := range apps.Apps {
r := resource.New(cfg, resource.AmplifyApp, v.AppId, v.Name, v)
r := resource.New(ctx, resource.AmplifyApp, v.AppId, v.Name, v)

// add Amplify Branches
err = Paginator(func(nt2 *string) (*string, error) {
branches, err := svc.ListBranches(cfg.Context, &amplify.ListBranchesInput{
branches, err := svc.ListBranches(ctx.Context, &amplify.ListBranchesInput{
AppId: v.AppId,
MaxResults: aws.Int32(50),
NextToken: nt2,
Expand All @@ -54,7 +54,7 @@ func (l AWSAmplifyApp) List(cfg option.AWSetsConfig) (*resource.Group, error) {
}
for _, branch := range branches.Branches {
branchArn := arn.ParseP(branch.BranchArn)
branchR := resource.New(cfg, resource.AmplifyBranch, branchArn.ResourceId, branch.DisplayName, branch)
branchR := resource.New(ctx, resource.AmplifyBranch, branchArn.ResourceId, branch.DisplayName, branch)
branchR.AddRelation(resource.AmplifyApp, v.AppId, "")
rg.AddResource(branchR)
}
Expand All @@ -66,7 +66,7 @@ func (l AWSAmplifyApp) List(cfg option.AWSetsConfig) (*resource.Group, error) {

// add Amplify Domains
err = Paginator(func(nt2 *string) (*string, error) {
domains, err := svc.ListDomainAssociations(cfg.Context, &amplify.ListDomainAssociationsInput{
domains, err := svc.ListDomainAssociations(ctx.Context, &amplify.ListDomainAssociationsInput{
AppId: v.AppId,
MaxResults: aws.Int32(50),
NextToken: nt2,
Expand All @@ -75,7 +75,7 @@ func (l AWSAmplifyApp) List(cfg option.AWSetsConfig) (*resource.Group, error) {
return nil, fmt.Errorf("failed to list domains for app %s: %w", *v.AppId, err)
}
for _, domain := range domains.DomainAssociations {
domainR := resource.New(cfg, resource.AmplifyDomain, domain.DomainName, domain.DomainName, domain)
domainR := resource.New(ctx, resource.AmplifyDomain, domain.DomainName, domain.DomainName, domain)
domainR.AddRelation(resource.AmplifyApp, v.AppId, "")
rg.AddResource(domainR)
}
Expand Down
Loading

0 comments on commit d9abf78

Please sign in to comment.