forked from theaaf/cloudformation-media-services
-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_string.go
44 lines (37 loc) · 922 Bytes
/
random_string.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
package main
import (
"crypto/rand"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
)
func init() {
RegisterType("Custom::RandomString", RandomString)
}
func RandomString(request *CustomResourceRequest, cfg aws.Config) (*Success, error) {
switch request.RequestType {
case "Create", "Update":
var input struct{}
if err := ReshapeProps(request.ResourceProperties, &input); err != nil {
return nil, err
}
// Crockford's base32 encoding: https://www.crockford.com/wrmg/base32.html
b := make([]byte, 12)
if _, err := rand.Read(b); err != nil {
return nil, err
}
ret := ""
const chars = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"
for _, b := range b {
ret += string(chars[int(b)%len(chars)])
}
return &Success{
PhysicalResourceId: ret,
Data: map[string]interface{}{
"String": ret,
},
}, nil
case "Delete":
return nil, nil
}
return nil, fmt.Errorf("unexpected request type")
}