/
destroy.go
124 lines (102 loc) · 3.71 KB
/
destroy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package provider
import (
"fmt"
"strings"
"github.com/aws/aws-sdk-go-v2/service/cloudformation"
"github.com/common-fate/clio"
"github.com/common-fate/cloudform/deployer"
"github.com/common-fate/common-fate/pkg/cfaws"
"github.com/common-fate/common-fate/pkg/cliconfig"
"github.com/common-fate/common-fate/pkg/client"
"github.com/common-fate/provider-registry-sdk-go/pkg/handlerclient"
"github.com/common-fate/provider-registry-sdk-go/pkg/providerregistrysdk"
"github.com/urfave/cli/v2"
)
var destroyCommand = cli.Command{
Name: "destroy",
Description: "Quickstart all-in-one command to destroy a provider deployment",
Usage: "Quickstart all-in-one command to destroy a provider deployment",
Flags: []cli.Flag{
&cli.StringFlag{Name: "handler-id", Usage: "The Handler ID to remove", Required: true},
&cli.StringFlag{Name: "target-group-id", Usage: "Override the ID of the Target Group which will be deleted"},
&cli.BoolFlag{Name: "delete-cloudformation-stack", Usage: "Delete the CloudFormation stack for the Handler", Value: true},
&cli.BoolFlag{Name: "confirm", Aliases: []string{"y"}, Usage: "Confirm the deletion of resources"},
},
Action: func(c *cli.Context) error {
ctx := c.Context
awsConfig, err := cfaws.ConfigFromContextOrDefault(ctx)
if err != nil {
return err
}
cfg, err := cliconfig.Load()
if err != nil {
return err
}
// the client needs to be constructed as early as possible in the
// CLI command, because client.FromConfig() returns an error
// prompting the user to run 'cf login' if they are unauthenticated.
cf, err := client.FromConfig(ctx, cfg)
if err != nil {
return err
}
// make an admin API call. Even though we don't use the response,
// this will cause the CLI wizard to fail early if the auth token
// is expired, or if the user is not an administrator.
_, err = cf.AdminListHandlersWithResponse(ctx)
if err != nil {
return err
}
handlerID := c.String("handler-id")
providerRuntime, err := handlerclient.NewLambdaRuntime(c.Context, handlerID)
if err != nil {
return err
}
var desc *providerregistrysdk.DescribeResponse
desc, err = providerRuntime.Describe(c.Context)
if err != nil {
// log errors but still continue, as the handler may be in a totally invalid
// state which prevents us from calling the Describe API
clio.Errorf("Error when describing Handler Lambda function (continuing with deletion anyway): %s", err.Error())
}
d := deployer.NewFromConfig(awsConfig)
if c.Bool("delete-cloudformation-stack") {
//check to see if cloudformation stack exists
client := cloudformation.NewFromConfig(awsConfig)
res, err := client.DescribeStacks(ctx, &cloudformation.DescribeStacksInput{
StackName: &handlerID,
})
if err != nil {
return err
}
if len(res.Stacks) == 0 {
return fmt.Errorf("could not find stack %s", handlerID)
}
clio.Infof("Deleting CloudFormation stack '%s'", handlerID)
_, err = d.Delete(ctx, deployer.DeleteOpts{
StackName: handlerID,
})
if err != nil {
return err
}
}
targetgroupID := c.String("target-group-id")
if targetgroupID == "" {
targetgroupID = strings.TrimPrefix(handlerID, "cf-handler-")
}
clio.Infof("Deleting Target Group '%s'", targetgroupID)
_, err = cf.AdminDeleteTargetGroupWithResponse(ctx, targetgroupID)
if err != nil {
return err
}
clio.Infof("Deleting Handler '%s'", targetgroupID)
_, err = cf.AdminDeleteHandlerWithResponse(ctx, handlerID)
if err != nil {
return err
}
clio.Successf("Handler '%s' has been removed", handlerID)
if desc != nil {
clio.Infof("You can deploy this handler again by running:\ncf provider deploy -p %s --handler-id %s", desc.Provider, handlerID)
}
return nil
},
}