-
Notifications
You must be signed in to change notification settings - Fork 4
/
deleteregion.go
170 lines (156 loc) · 4.96 KB
/
deleteregion.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package gghelper
import (
"fmt"
"github.com/aws/aws-sdk-go/service/greengrass"
"github.com/aws/aws-sdk-go/service/iot"
)
// DeleteRegion - delete iot/greengrass objects from region
func (ggSession *GreengrassSession) DeleteRegion() error {
things, err := ggSession.iot.ListThings(&iot.ListThingsInput{})
if err != nil {
fmt.Printf("Err: ListThings - %v\n", err)
return err
}
// fmt.Printf("things: %v\n", things)
for _, t := range things.Things {
thingName := *t.ThingName
// fmt.Printf("ThingName %s\n", *t.ThingName)
principals, err := ggSession.iot.ListThingPrincipals(&iot.ListThingPrincipalsInput{
ThingName: &thingName,
})
if err != nil {
fmt.Printf("Error ListThingPrincipals - %v\n", err)
return err
}
for _, p := range principals.Principals {
// fmt.Printf("principal %s\n", *p)
_, err = ggSession.iot.DetachThingPrincipal(&iot.DetachThingPrincipalInput{
ThingName: &thingName,
Principal: p,
})
if err != nil {
return err
}
fmt.Printf("Deleted principal %s from thing %s\n", *p, thingName)
}
_, err = ggSession.iot.DeleteThing(&iot.DeleteThingInput{
ThingName: &thingName,
})
if err != nil {
return err
}
fmt.Printf("Deleted thing %s\n", thingName)
}
// Delete policies
policies, err := ggSession.iot.ListPolicies(&iot.ListPoliciesInput{})
for _, p := range policies.Policies {
// fmt.Printf("Policy %s\n", *p)
principals, _ := ggSession.iot.ListPolicyPrincipals(&iot.ListPolicyPrincipalsInput{
PolicyName: p.PolicyName,
})
for _, principal := range principals.Principals {
ggSession.iot.DetachPrincipalPolicy(&iot.DetachPrincipalPolicyInput{
PolicyName: p.PolicyName,
Principal: principal,
})
fmt.Printf("Detached policy principal %s from %s\n", *principal, *p.PolicyName)
}
_, err = ggSession.iot.DeletePolicy(&iot.DeletePolicyInput{
PolicyName: p.PolicyName,
})
if err != nil {
return err
}
fmt.Printf("Deleted policy %s\n", *p.PolicyName)
}
// Delete certificates
certificates, err := ggSession.iot.ListCertificates(&iot.ListCertificatesInput{})
if err != nil {
fmt.Printf("Err: ListCertificates - %v\n", err)
return err
}
inactive := "INACTIVE"
for _, cert := range certificates.Certificates {
ggSession.iot.UpdateCertificate(&iot.UpdateCertificateInput{
CertificateId: cert.CertificateId,
NewStatus: &inactive,
})
ggSession.iot.DeleteCertificate(&iot.DeleteCertificateInput{
CertificateId: cert.CertificateId,
})
fmt.Printf("Deleting certificate: %s\n", *cert.CertificateId)
}
// Delete groups
groups, err := ggSession.greengrass.ListGroups(&greengrass.ListGroupsInput{})
if err != nil {
fmt.Printf("Err: ListGroups - %v\n", err)
return err
}
for _, g := range groups.Groups {
ggSession.greengrass.ResetDeployments(&greengrass.ResetDeploymentsInput{
GroupId: g.Id,
})
_, err = ggSession.greengrass.DeleteGroup(&greengrass.DeleteGroupInput{
GroupId: g.Id,
})
if err != nil {
fmt.Printf("Err: DeleteGroup - %v\n", err)
return err
}
fmt.Printf("Deleted group: %s\n", *g.Id)
}
// Delete core definitions
cores, err := ggSession.greengrass.ListCoreDefinitions(&greengrass.ListCoreDefinitionsInput{})
if err != nil {
fmt.Printf("Err: ListCoreDefinitions - %v\n", err)
return err
}
for _, core := range cores.Definitions {
ggSession.greengrass.DeleteCoreDefinition(&greengrass.DeleteCoreDefinitionInput{
CoreDefinitionId: core.Id,
})
if err != nil {
fmt.Printf("Err: DeleteCoreDefinition - %v\n", err)
return err
}
fmt.Printf("Deleted core definition: %s\n", *core.Id)
}
// Delete subscriptions
subs, err := ggSession.greengrass.ListSubscriptionDefinitions(&greengrass.ListSubscriptionDefinitionsInput{})
for _, sub := range subs.Definitions {
ggSession.greengrass.DeleteSubscriptionDefinition(&greengrass.DeleteSubscriptionDefinitionInput{
SubscriptionDefinitionId: sub.Id,
})
if err != nil {
fmt.Printf("Err: DeleteSubscriptionDefinition - %v\n", err)
return err
}
fmt.Printf("Deleted subscription definition: %s\n", *sub.Id)
}
// Delete functions
functions, err := ggSession.greengrass.ListFunctionDefinitions(&greengrass.ListFunctionDefinitionsInput{})
for _, function := range functions.Definitions {
ggSession.greengrass.DeleteFunctionDefinition(&greengrass.DeleteFunctionDefinitionInput{
FunctionDefinitionId: function.Id,
})
if err != nil {
fmt.Printf("Err: DeleteFunctionDefinition - %v\n", err)
return err
}
fmt.Printf("Deleted function definition: %s\n", *function.Id)
}
// Delete loggers
loggers, err := ggSession.greengrass.ListLoggerDefinitions(&greengrass.ListLoggerDefinitionsInput{})
for _, logger := range loggers.Definitions {
ggSession.greengrass.DeleteLoggerDefinition(&greengrass.DeleteLoggerDefinitionInput{
LoggerDefinitionId: logger.Id,
})
if err != nil {
fmt.Printf("Err: DeleteLoggerDefinition - %v\n", err)
return err
}
fmt.Printf("Deleted logger definition: %s\n", *logger.Id)
}
ggSession.config.ClearConfig()
return nil
}