Skip to content

Commit

Permalink
Merge pull request #291 from ekristen/amplify
Browse files Browse the repository at this point in the history
feat(resource): add resource for amplify app
  • Loading branch information
ekristen authored Sep 11, 2024
2 parents 7283abb + 12407b7 commit 8c6b514
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
91 changes: 91 additions & 0 deletions resources/amplify-app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package resources

import (
"context"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/amplify"

"github.com/ekristen/libnuke/pkg/registry"
"github.com/ekristen/libnuke/pkg/resource"
"github.com/ekristen/libnuke/pkg/types"

"github.com/ekristen/aws-nuke/v3/pkg/nuke"
)

const AmplifyAppResource = "AmplifyApp"

func init() {
registry.Register(&registry.Registration{
Name: AmplifyAppResource,
Scope: nuke.Account,
Lister: &AmplifyAppLister{},
})
}

type AmplifyAppLister struct{}

func (l *AmplifyAppLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) {
opts := o.(*nuke.ListerOpts)

svc := amplify.New(opts.Session)
resources := make([]resource.Resource, 0)

params := &amplify.ListAppsInput{
MaxResults: aws.Int64(100),
}

for {
output, err := svc.ListApps(params)
if err != nil {
return nil, err
}

for _, item := range output.Apps {
resources = append(resources, &AmplifyApp{
svc: svc,
AppID: item.AppId,
Name: item.Name,
Tags: item.Tags,
})
}

if output.NextToken == nil {
break
}

params.NextToken = output.NextToken
}

return resources, nil
}

type AmplifyApp struct {
svc *amplify.Amplify
AppID *string
Name *string
Tags map[string]*string
}

func (r *AmplifyApp) Remove(_ context.Context) error {
_, err := r.svc.DeleteApp(&amplify.DeleteAppInput{
AppId: r.AppID,
})

return err
}

func (r *AmplifyApp) String() string {
return *r.AppID
}

func (r *AmplifyApp) Properties() types.Properties {
properties := types.NewProperties()
for key, tag := range r.Tags {
properties.SetTag(&key, tag)
}
properties.
Set("AppID", r.AppID).
Set("Name", r.Name)
return properties
}
31 changes: 31 additions & 0 deletions resources/amplify-app_mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package resources

import (
"testing"

"github.com/gotidy/ptr"
"github.com/stretchr/testify/assert"
)

var app = &AmplifyApp{
Name: ptr.String("app1"),
AppID: ptr.String("appId1"),
Tags: map[string]*string{
"key1": ptr.String("value1"),
},
}

func Test_AmplifyApp_Properties(t *testing.T) {
a := assert.New(t)

properties := app.Properties()
a.Equal("app1", properties.Get("Name"))
a.Equal("appId1", properties.Get("AppID"))
a.Equal("value1", properties.Get("tag:key1"))
}

func Test_AmplifyApp_Stringer(t *testing.T) {
a := assert.New(t)

a.Equal("appId1", app.String())
}

0 comments on commit 8c6b514

Please sign in to comment.