Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(resource): add resource for amplify app #291

Merged
merged 3 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
}