-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.go
261 lines (228 loc) · 7.19 KB
/
resource.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Copyright 2020 Amazon.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package resource
import (
"errors"
"fmt"
"strings"
"github.com/aws-cloudformation/cloudformation-cli-go-plugin/cfn/handler"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/iot"
"github.com/aws/aws-sdk-go/service/s3"
)
// Create handles the Create event from the Cloudformation service.
func Create(req handler.Request, prevModel *Model, currentModel *Model) (handler.ProgressEvent, error) {
// Add your code here:
// * Make API calls (use req.Session)
// * Mutate the model
// * Check/set any callback context (req.CallbackContext / response.CallbackContext)
iotSvc := iot.New(req.Session)
s3Svc := s3.New(req.Session)
testKey := req.LogicalResourceID
_, err := s3Svc.PutObject(&s3.PutObjectInput{
Bucket: currentModel.Bucket,
Key: &testKey,
Body: strings.NewReader("test"),
})
if err != nil {
aerr, ok := err.(awserr.Error)
if ok {
fmt.Printf("%v", aerr)
}
response := handler.ProgressEvent{
OperationStatus: handler.Failed,
Message: "Bucket is not accessible",
}
return response, nil
}
_, err = s3Svc.DeleteObject(&s3.DeleteObjectInput{
Bucket: currentModel.Bucket,
Key: &testKey,
})
active := false
if currentModel.Status != nil && strings.Compare(*currentModel.Status, "ACTIVE") == 0 {
active = true
}
res, err := iotSvc.CreateKeysAndCertificate(&iot.CreateKeysAndCertificateInput{
SetAsActive: &active,
})
if err != nil {
aerr, ok := err.(awserr.Error)
if ok {
fmt.Printf("%v", aerr)
}
response := handler.ProgressEvent{
OperationStatus: handler.Failed,
Message: fmt.Sprintf("Failed: %s", aerr.Error()),
}
return response, nil
}
var key string
key = fmt.Sprintf("%s.pem", *res.CertificateId)
_, err = s3Svc.PutObject(&s3.PutObjectInput{
Bucket: currentModel.Bucket,
Key: &key,
Body: strings.NewReader(*res.CertificatePem),
})
key = fmt.Sprintf("%s.key", *res.CertificateId)
_, err = s3Svc.PutObject(&s3.PutObjectInput{
Bucket: currentModel.Bucket,
Key: &key,
Body: strings.NewReader(*res.KeyPair.PrivateKey),
})
currentModel.Arn = res.CertificateArn
currentModel.Id = res.CertificateId
response := handler.ProgressEvent{
OperationStatus: handler.Success,
Message: "Created certificate",
ResourceModel: currentModel,
}
return response, nil
}
// Read handles the Read event from the Cloudformation service.
func Read(req handler.Request, prevModel *Model, currentModel *Model) (handler.ProgressEvent, error) {
// Add your code here:
// * Make API calls (use req.Session)
// * Mutate the model
// * Check/set any callback context (req.CallbackContext / response.CallbackContext)
/*
// Construct a new handler.ProgressEvent and return it
response := handler.ProgressEvent{
OperationStatus: handler.Success,
Message: "Read complete",
ResourceModel: currentModel,
}
return response, nil
*/
// Not implemented, return an empty handler.ProgressEvent
// and an error
return handler.ProgressEvent{}, errors.New("Not implemented: Read")
}
// Update handles the Update event from the Cloudformation service.
func Update(req handler.Request, prevModel *Model, currentModel *Model) (handler.ProgressEvent, error) {
// Add your code here:
// * Make API calls (use req.Session)
// * Mutate the model
// * Check/set any callback context (req.CallbackContext / response.CallbackContext)
iotSvc := iot.New(req.Session)
_, err := iotSvc.UpdateCertificate(&iot.UpdateCertificateInput{
CertificateId: currentModel.Id,
NewStatus: currentModel.Status,
})
if err != nil {
aerr, ok := err.(awserr.Error)
if ok {
fmt.Printf("%v", aerr)
}
response := handler.ProgressEvent{
OperationStatus: handler.Failed,
Message: aerr.Error(),
}
return response, nil
}
// Not implemented, return an empty handler.ProgressEvent
// and an error
response := handler.ProgressEvent{
OperationStatus: handler.Success,
Message: "Certificate updated successfully",
ResourceModel: currentModel,
}
return response, nil
}
// Delete handles the Delete event from the Cloudformation service.
func Delete(req handler.Request, prevModel *Model, currentModel *Model) (handler.ProgressEvent, error) {
// Add your code here:
// * Make API calls (use req.Session)
// * Mutate the model
// * Check/set any callback context (req.CallbackContext / response.CallbackContext)
iotSvc := iot.New(req.Session)
s3Svc := s3.New(req.Session)
inactiveStatus := "INACTIVE"
iotSvc.UpdateCertificate(&iot.UpdateCertificateInput{
CertificateId: currentModel.Id,
NewStatus: &inactiveStatus,
})
var key string
key = fmt.Sprintf("%s.pem", *currentModel.Id)
_, err := s3Svc.DeleteObject(&s3.DeleteObjectInput{
Bucket: currentModel.Bucket,
Key: &key,
})
if err != nil {
aerr, ok := err.(awserr.Error)
if ok {
fmt.Printf("%v", aerr)
}
response := handler.ProgressEvent{
OperationStatus: handler.Failed,
Message: "Bucket is not accessible",
}
return response, nil
}
key = fmt.Sprintf("%s.key", *currentModel.Id)
_, err = s3Svc.DeleteObject(&s3.DeleteObjectInput{
Bucket: currentModel.Bucket,
Key: &key,
})
if err != nil {
aerr, ok := err.(awserr.Error)
if ok {
fmt.Printf("%v", aerr)
}
response := handler.ProgressEvent{
OperationStatus: handler.Failed,
Message: "Bucket is not accessible",
}
return response, nil
}
_, err = iotSvc.DeleteCertificate(&iot.DeleteCertificateInput{
CertificateId: currentModel.Id,
})
if err != nil {
aerr, ok := err.(awserr.Error)
if ok {
fmt.Printf("%v", aerr)
}
response := handler.ProgressEvent{
OperationStatus: handler.Failed,
Message: fmt.Sprintf("Failed deleting the certificate: %s", aerr.Error()),
}
return response, nil
}
response := handler.ProgressEvent{
OperationStatus: handler.Success,
Message: "Certificate delete successfully",
ResourceModel: currentModel,
}
return response, nil
}
// List handles the List event from the Cloudformation service.
func List(req handler.Request, prevModel *Model, currentModel *Model) (handler.ProgressEvent, error) {
// Add your code here:
// * Make API calls (use req.Session)
// * Mutate the model
// * Check/set any callback context (req.CallbackContext / response.CallbackContext)
/*
// Construct a new handler.ProgressEvent and return it
response := handler.ProgressEvent{
OperationStatus: handler.Success,
Message: "List complete",
ResourceModel: currentModel,
}
return response, nil
*/
// Not implemented, return an empty handler.ProgressEvent
// and an error
return handler.ProgressEvent{}, errors.New("Not implemented: List")
}