Skip to content

Commit

Permalink
feat: adding termination protection setting
Browse files Browse the repository at this point in the history
style: fixing lint issues

style: apply suggestions from code review

Co-authored-by: Erik Kristensen <[email protected]>

style: apply suggestions from code review

style: Apply suggestions from code review

Co-authored-by: Erik Kristensen <[email protected]>
  • Loading branch information
danielbojczuk and ekristen committed Jun 4, 2024
1 parent b3cbb0c commit 6c404bd
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 81 deletions.
89 changes: 52 additions & 37 deletions resources/quicksight-subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/ekristen/aws-nuke/pkg/nuke"
"github.com/ekristen/libnuke/pkg/registry"
"github.com/ekristen/libnuke/pkg/resource"
libsettings "github.com/ekristen/libnuke/pkg/settings"
"github.com/ekristen/libnuke/pkg/types"
)

Expand All @@ -32,13 +33,14 @@ type QuickSightSubscriptionLister struct {
quicksightService quicksightiface.QuickSightAPI
}

type QuicksightSubscription struct {
type QuickSightSubscription struct {
svc quicksightiface.QuickSightAPI
accountId *string
accountID *string
name *string
notificationEmail *string
edition *string
status *string
settings *libsettings.Setting
}

func (l *QuickSightSubscriptionLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) {
Expand All @@ -62,12 +64,12 @@ func (l *QuickSightSubscriptionLister) List(_ context.Context, o interface{}) ([
if err != nil {
return nil, err
}
accountId := callerID.Account
accountID := callerID.Account

var resources []resource.Resource

describeSubscriptionOutput, err := quicksightSvc.DescribeAccountSubscription(&quicksight.DescribeAccountSubscriptionInput{
AwsAccountId: accountId,
AwsAccountId: accountID,
})

if err != nil {
Expand All @@ -78,15 +80,15 @@ func (l *QuickSightSubscriptionLister) List(_ context.Context, o interface{}) ([
return resources, nil
}

//The account name is only available some time later after the Subscription creation.
// The account name is only available some time later after the Subscription creation.
subscriptionName := subscriptionNameWhenNotAvailable
if describeSubscriptionOutput.AccountInfo.AccountName != nil {
subscriptionName = *describeSubscriptionOutput.AccountInfo.AccountName
}

resources = append(resources, &QuicksightSubscription{
resources = append(resources, &QuickSightSubscription{
svc: quicksightSvc,
accountId: accountId,
accountID: accountID,
name: &subscriptionName,
notificationEmail: describeSubscriptionOutput.AccountInfo.NotificationEmail,
edition: describeSubscriptionOutput.AccountInfo.Edition,
Expand All @@ -96,32 +98,16 @@ func (l *QuickSightSubscriptionLister) List(_ context.Context, o interface{}) ([
return resources, nil
}

func (s *QuicksightSubscription) Remove(_ context.Context) error {
terminateProtectionEnabled := false

describeSettingsOutput, err := subscription.svc.DescribeAccountSettings(&quicksight.DescribeAccountSettingsInput{
AwsAccountId: subscription.accountId,
})
if err != nil {
return err
}

if *describeSettingsOutput.AccountSettings.TerminationProtectionEnabled {
updateSettingsInput := quicksight.UpdateAccountSettingsInput{
AwsAccountId: subscription.accountId,
DefaultNamespace: describeSettingsOutput.AccountSettings.DefaultNamespace,
NotificationEmail: describeSettingsOutput.AccountSettings.NotificationEmail,
TerminationProtectionEnabled: &terminateProtectionEnabled,
}

_, err = subscription.svc.UpdateAccountSettings(&updateSettingsInput)
func (r *QuickSightSubscription) Remove(_ context.Context) error {
if r.settings != nil && r.settings.Get("DisableTerminationProtection").(bool) {
err := r.DisableTerminationProtection()
if err != nil {
return err
}
}

_, err = subscription.svc.DeleteAccountSubscription(&quicksight.DeleteAccountSubscriptionInput{
AwsAccountId: subscription.accountId,
_, err := r.svc.DeleteAccountSubscription(&quicksight.DeleteAccountSubscriptionInput{
AwsAccountId: r.accountID,
})
if err != nil {
return err
Expand All @@ -130,28 +116,57 @@ func (s *QuicksightSubscription) Remove(_ context.Context) error {
return nil
}

func (r *QuicksightSubscription) Properties() types.Properties {
func (r *QuickSightSubscription) Properties() types.Properties {
properties := types.NewProperties()
properties.Set("Edition", subscription.edition).
Set("NotificationEmail", subscription.notificationEmail).
Set("Name", subscription.name).
Set("Status", subscription.status)
properties.Set("Edition", r.edition).
Set("NotificationEmail", r.notificationEmail).
Set("Name", r.name).
Set("Status", r.status)

return properties
}

func (r *QuicksightSubscription) String() string {
return *subscription.name
func (r *QuickSightSubscription) String() string {
return *r.name
}

func (r *QuicksightSubscription) Filter() error {
func (r *QuickSightSubscription) Filter() error {
if *r.status != "ACCOUNT_CREATED" {
return fmt.Errorf("subscription is not active")
}

//Since the subscription name is an important value to identify the resource, it will wait till it is available
// Since the subscription name is an important value to identify the resource, it will wait till it is available
if *r.name == subscriptionNameWhenNotAvailable {
return fmt.Errorf("subscription name is not available yet")
}
return nil
}

func (r *QuickSightSubscription) Settings(setting *libsettings.Setting) {
r.settings = setting
}

func (r *QuickSightSubscription) DisableTerminationProtection() error {
terminateProtectionEnabled := false
describeSettingsOutput, err := r.svc.DescribeAccountSettings(&quicksight.DescribeAccountSettingsInput{
AwsAccountId: r.accountID,
})
if err != nil {
return err
}

if *describeSettingsOutput.AccountSettings.TerminationProtectionEnabled {
updateSettingsInput := quicksight.UpdateAccountSettingsInput{
AwsAccountId: r.accountID,
DefaultNamespace: describeSettingsOutput.AccountSettings.DefaultNamespace,
NotificationEmail: describeSettingsOutput.AccountSettings.NotificationEmail,
TerminationProtectionEnabled: &terminateProtectionEnabled,
}

_, err = r.svc.UpdateAccountSettings(&updateSettingsInput)
if err != nil {
return err
}
}
return nil
}
Loading

0 comments on commit 6c404bd

Please sign in to comment.