-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgroup.go
More file actions
141 lines (119 loc) · 3.71 KB
/
group.go
File metadata and controls
141 lines (119 loc) · 3.71 KB
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
package gghelper
import (
"fmt"
"github.com/aws/aws-sdk-go/service/greengrass"
)
// CreateGroup - create a new Greengrass group
func (ggSession *GreengrassSession) CreateGroup(name string) error {
// fmt.Printf("creategroup: %v\n", groupOutput)
thingOutput, err := ggSession.CreateCore(name)
if err != nil {
fmt.Printf("CreateCore error: %v\n", err)
}
fmt.Printf("Created core '%s'\n", name)
coreDefinition, err := ggSession.greengrass.CreateCoreDefinition(&greengrass.CreateCoreDefinitionInput{
Name: &name,
})
if err != nil {
fmt.Printf("CreateCoreDefinition error: %v\n", err)
return err
}
fmt.Printf("Created core definition\n")
definitionInput := greengrass.CreateCoreDefinitionVersionInput{
CoreDefinitionId: coreDefinition.Id,
Cores: []*greengrass.Core{
&greengrass.Core{
CertificateArn: ggSession.keyCertOutput.CertificateArn,
Id: coreDefinition.Id,
ThingArn: thingOutput.ThingArn,
},
},
}
definitionVersion, err := ggSession.greengrass.CreateCoreDefinitionVersion(&definitionInput)
if err != nil {
fmt.Printf("CreateCoreDefinitionVersion error: %v\n", err)
return err
}
fmt.Printf("Created core definition version\n")
groupOutput, err := ggSession.greengrass.CreateGroup(&greengrass.CreateGroupInput{Name: &name})
if err != nil {
fmt.Printf("creategroup error: %v\n", err)
return err
}
fmt.Printf("Created group\n")
// Update group configuration
ggSession.config.CoreDefinition.ID = *definitionVersion.Id
ggSession.config.CoreDefinition.VersionArn = *definitionVersion.Arn
ggSession.config.Group.ID = *groupOutput.Id
ggSession.updateGroup()
return nil
}
func (ggSession *GreengrassSession) updateGroup() error {
input := &greengrass.CreateGroupVersionInput{
GroupId: &ggSession.config.Group.ID,
}
if ggSession.config.CoreDefinition.VersionArn != "" {
input.CoreDefinitionVersionArn = &ggSession.config.CoreDefinition.VersionArn
}
if ggSession.config.DeviceDefinition.VersionArn != "" {
input.DeviceDefinitionVersionArn = &ggSession.config.DeviceDefinition.VersionArn
}
if ggSession.config.FunctionDefinition.VersionArn != "" {
input.FunctionDefinitionVersionArn = &ggSession.config.FunctionDefinition.VersionArn
}
if ggSession.config.LoggerDefinition.VersionArn != "" {
input.LoggerDefinitionVersionArn = &ggSession.config.LoggerDefinition.VersionArn
}
if ggSession.config.SubscriptionDefinition.VersionArn != "" {
input.SubscriptionDefinitionVersionArn = &ggSession.config.SubscriptionDefinition.VersionArn
}
groupVersion, err := ggSession.greengrass.CreateGroupVersion(input)
if err != nil {
fmt.Printf("updateGroup error: %v\n", err)
return err
}
ggSession.config.Group.Arn = *groupVersion.Arn
ggSession.config.Group.Version = *groupVersion.Version
fmt.Printf("Updated group version\n")
return nil
}
// ListGroup - list the group definition
func (ggSession *GreengrassSession) ListGroup(name string) error {
var id string
if name != "" {
listOutput, err := ggSession.greengrass.ListGroups(&greengrass.ListGroupsInput{})
if err != nil {
return err
}
for _, v := range listOutput.Groups {
if name == *v.Name {
id = *v.Id
break
}
}
if id == "" {
return fmt.Errorf("group %s not found", name)
}
} else {
id = ggSession.config.Group.ID
}
group, err := ggSession.greengrass.GetGroup(&greengrass.GetGroupInput{
GroupId: &id,
})
if err != nil {
return err
}
fmt.Printf("group: %v\n", group)
if group.LatestVersion == nil {
return nil
}
groupVersion, err := ggSession.greengrass.GetGroupVersion(&greengrass.GetGroupVersionInput{
GroupId: group.Id,
GroupVersionId: group.LatestVersion,
})
if err != nil {
return err
}
fmt.Printf("group version: %v\n", groupVersion)
return nil
}