-
Notifications
You must be signed in to change notification settings - Fork 4
/
session.go
104 lines (91 loc) · 2.66 KB
/
session.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
package gghelper
import (
"fmt"
"io"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/greengrass"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/iot"
"github.com/aws/aws-sdk-go/service/lambda"
)
var policyDocument = `{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:*",
"greengrass:*"
],
"Resource": [
"*"
]
}
]
}`
type ggHelperConfig struct {
Core struct {
CertArn string `json:"cert_arn"`
CertID string `json:"cert_id"`
ThingArn string `json:"thing_arn"`
ThingName string `json:"thing_name"`
}
}
// GreengrassSession is an object for Greengrass sessions
type GreengrassSession struct {
session *session.Session
greengrass *greengrass.Greengrass
iam *iam.IAM
iot *iot.IoT
lambda *lambda.Lambda
config ggGroupConfig
ggconfig GreengrassConfig
keyCertOutput *iot.CreateKeysAndCertificateOutput
}
// NewGreengrassSession is the constructor for Greengrass interactions
func NewGreengrassSession(sess *session.Session) *GreengrassSession {
ggSession := new(GreengrassSession)
ggSession.session = sess
ggSession.greengrass = greengrass.New(sess)
ggSession.iot = iot.New(sess)
ggSession.iam = iam.New(sess)
ggSession.lambda = lambda.New(sess)
return ggSession
}
// WriteGGConfig - write the Greengrass Core config file
func (ggSession *GreengrassSession) WriteGGConfig(w io.Writer) {
ggSession.ggconfig.Write(w)
}
// LoadGroupConfig - load the Greengrass group config file
func (ggSession *GreengrassSession) LoadGroupConfig(filename string) error {
return ggSession.config.LoadConfig(filename)
}
// WriteGroupConfig - write the Greengrass group config file
func (ggSession *GreengrassSession) WriteGroupConfig(filename string) error {
return ggSession.config.WriteConfig(filename)
}
// Cleanup - delete configuration for a Greengrass session object
func (ggSession *GreengrassSession) Cleanup() error {
inactive := "INACTIVE"
ggSession.iot.UpdateCertificate(&iot.UpdateCertificateInput{
CertificateId: ggSession.keyCertOutput.CertificateId,
NewStatus: &inactive,
//NewStatus: &iot.CertificateStatusInactive,
})
_, err := ggSession.iot.DetachThingPrincipal(&iot.DetachThingPrincipalInput{
Principal: ggSession.keyCertOutput.CertificateId,
ThingName: &ggSession.config.Core.ThingName,
})
if err != nil {
fmt.Printf("DetachPrincipalPolicy error: %v\n", err)
return err
}
_, err = ggSession.iot.DeleteCertificate(&iot.DeleteCertificateInput{
CertificateId: ggSession.keyCertOutput.CertificateId,
})
if err != nil {
fmt.Printf("cleanup: %+v\n", err)
return err
}
return nil
}