|
| 1 | +package resources |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/gotidy/ptr" |
| 9 | + |
| 10 | + "github.com/aws/aws-sdk-go-v2/service/s3control" |
| 11 | + |
| 12 | + "github.com/ekristen/libnuke/pkg/registry" |
| 13 | + "github.com/ekristen/libnuke/pkg/resource" |
| 14 | + "github.com/ekristen/libnuke/pkg/types" |
| 15 | + |
| 16 | + "github.com/ekristen/aws-nuke/v3/pkg/nuke" |
| 17 | +) |
| 18 | + |
| 19 | +const S3AccessGrantsGrantResource = "S3AccessGrantsGrant" |
| 20 | + |
| 21 | +func init() { |
| 22 | + registry.Register(®istry.Registration{ |
| 23 | + Name: S3AccessGrantsGrantResource, |
| 24 | + Scope: nuke.Account, |
| 25 | + Resource: &S3AccessGrantsGrant{}, |
| 26 | + Lister: &S3AccessGrantsGrantLister{}, |
| 27 | + }) |
| 28 | +} |
| 29 | + |
| 30 | +type S3AccessGrantsGrantLister struct{} |
| 31 | + |
| 32 | +func (l *S3AccessGrantsGrantLister) List(ctx context.Context, o interface{}) ([]resource.Resource, error) { |
| 33 | + opts := o.(*nuke.ListerOpts) |
| 34 | + svc := s3control.NewFromConfig(*opts.Config) |
| 35 | + var resources []resource.Resource |
| 36 | + |
| 37 | + res, err := svc.ListAccessGrants(ctx, &s3control.ListAccessGrantsInput{ |
| 38 | + AccountId: opts.AccountID, |
| 39 | + }) |
| 40 | + if err != nil { |
| 41 | + if strings.Contains(err.Error(), "AccessGrantsInstanceNotExistsError") { |
| 42 | + return resources, nil |
| 43 | + } else { |
| 44 | + return nil, err |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + for _, p := range res.AccessGrantsList { |
| 49 | + resources = append(resources, &S3AccessGrantsGrant{ |
| 50 | + svc: svc, |
| 51 | + ID: p.AccessGrantId, |
| 52 | + GrantScope: p.GrantScope, |
| 53 | + GranteeType: ptr.String(string(p.Grantee.GranteeType)), |
| 54 | + GranteeID: p.Grantee.GranteeIdentifier, |
| 55 | + CreatedAt: p.CreatedAt, |
| 56 | + }) |
| 57 | + } |
| 58 | + |
| 59 | + return resources, nil |
| 60 | +} |
| 61 | + |
| 62 | +type S3AccessGrantsGrant struct { |
| 63 | + svc *s3control.Client |
| 64 | + ID *string `description:"The ID of the access grant."` |
| 65 | + GrantScope *string `description:"The scope of the access grant."` |
| 66 | + GranteeType *string `description:"The type of the grantee, (e.g. IAM)."` |
| 67 | + GranteeID *string `description:"The ARN of the grantee."` |
| 68 | + CreatedAt *time.Time `description:"The date and time the access grant was created."` |
| 69 | +} |
| 70 | + |
| 71 | +func (r *S3AccessGrantsGrant) Remove(ctx context.Context) error { |
| 72 | + _, err := r.svc.DeleteAccessGrant(ctx, &s3control.DeleteAccessGrantInput{ |
| 73 | + AccessGrantId: r.ID, |
| 74 | + }) |
| 75 | + return err |
| 76 | +} |
| 77 | + |
| 78 | +func (r *S3AccessGrantsGrant) Properties() types.Properties { |
| 79 | + return types.NewPropertiesFromStruct(r) |
| 80 | +} |
| 81 | + |
| 82 | +func (r *S3AccessGrantsGrant) String() string { |
| 83 | + return *r.ID |
| 84 | +} |
0 commit comments