forked from ekristen/aws-nuke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiottwinmaker-component-type.go
140 lines (115 loc) · 3.43 KB
/
iottwinmaker-component-type.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
package resources
import (
"context"
"fmt"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/iottwinmaker"
"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 IoTTwinMakerComponentTypeResource = "IoTTwinMakerComponentType"
func init() {
registry.Register(®istry.Registration{
Name: IoTTwinMakerComponentTypeResource,
Scope: nuke.Account,
Lister: &IoTTwinMakerComponentTypeLister{},
})
}
type IoTTwinMakerComponentTypeLister struct{}
func (l *IoTTwinMakerComponentTypeLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) {
opts := o.(*nuke.ListerOpts)
svc := iottwinmaker.New(opts.Session)
resources := make([]resource.Resource, 0)
// Require to have workspaces identifiers to query components
workspaceListResponse, err := ListWorkspacesComponentType(svc)
if err != nil {
return nil, err
}
for _, workspaceResponse := range workspaceListResponse {
params := &iottwinmaker.ListComponentTypesInput{
WorkspaceId: workspaceResponse.WorkspaceId,
MaxResults: aws.Int64(25),
}
for {
resp, err := svc.ListComponentTypes(params)
if err != nil {
return nil, err
}
for _, item := range resp.ComponentTypeSummaries {
// We must filter out amazon-owned component types when querying tags,
// because their ARN format causes ListTagsForResource to fail with validation error
tags := make(map[string]*string)
if !strings.Contains(*item.Arn, "AmazonOwnedTypesWorkspace") {
tagResp, err := svc.ListTagsForResource(
&iottwinmaker.ListTagsForResourceInput{
ResourceARN: item.Arn,
})
if err != nil {
return nil, err
}
tags = tagResp.Tags
}
resources = append(resources, &IoTTwinMakerComponentType{
svc: svc,
ID: item.ComponentTypeId,
arn: item.Arn,
Tags: tags,
WorkspaceID: workspaceResponse.WorkspaceId,
})
}
if resp.NextToken == nil {
break
}
params.NextToken = resp.NextToken
}
}
return resources, nil
}
// Utility function to list workspaces
func ListWorkspacesComponentType(svc *iottwinmaker.IoTTwinMaker) ([]*iottwinmaker.WorkspaceSummary, error) {
resources := make([]*iottwinmaker.WorkspaceSummary, 0)
params := &iottwinmaker.ListWorkspacesInput{
MaxResults: aws.Int64(25),
}
for {
resp, err := svc.ListWorkspaces(params)
if err != nil {
return nil, err
}
resources = append(resources, resp.WorkspaceSummaries...)
if resp.NextToken == nil {
break
}
params.NextToken = resp.NextToken
}
return resources, nil
}
type IoTTwinMakerComponentType struct {
svc *iottwinmaker.IoTTwinMaker
ID *string
Tags map[string]*string
WorkspaceID *string
arn *string
}
func (r *IoTTwinMakerComponentType) Filter() error {
if strings.Contains(*r.arn, "AmazonOwnedTypesWorkspace") {
return fmt.Errorf("cannot delete pre-defined component type")
}
return nil
}
func (r *IoTTwinMakerComponentType) Properties() types.Properties {
return types.NewPropertiesFromStruct(r)
}
func (r *IoTTwinMakerComponentType) Remove(_ context.Context) error {
_, err := r.svc.DeleteComponentType(&iottwinmaker.DeleteComponentTypeInput{
ComponentTypeId: r.ID,
WorkspaceId: r.WorkspaceID,
})
return err
}
func (r *IoTTwinMakerComponentType) String() string {
return *r.ID
}