-
Notifications
You must be signed in to change notification settings - Fork 4
/
subscription.go
120 lines (99 loc) · 3.37 KB
/
subscription.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
package gghelper
import (
"fmt"
"github.com/aws/aws-sdk-go/service/greengrass"
"github.com/satori/go.uuid"
)
func (ggSession *GreengrassSession) mapSubToArn(name string) string {
if name == "cloud" {
return name
}
// Check if name is a lambda function
if val, ok := ggSession.config.LambdaFunctions[name]; ok {
return val.Arn
}
// Todo: Check if name is a device
return ""
}
// CreateSub - create a greengrass subscription
func (ggSession *GreengrassSession) CreateSub(source, target, subject string) error {
sourceArn := ggSession.mapSubToArn(source)
targetArn := ggSession.mapSubToArn(target)
newUUID, err := uuid.NewV4()
if err != nil {
return err
}
uuidString := newUUID.String()
// Check if we need to create the initial version
if ggSession.config.SubscriptionDefinition.ID == "" {
newSubscription, err := ggSession.greengrass.CreateSubscriptionDefinition(&greengrass.CreateSubscriptionDefinitionInput{
InitialVersion: &greengrass.SubscriptionDefinitionVersion{
Subscriptions: []*greengrass.Subscription{
&greengrass.Subscription{
Source: &sourceArn,
Target: &targetArn,
Subject: &subject,
Id: &uuidString,
},
},
},
})
if err != nil {
return err
}
fmt.Printf("Created new subscription\n")
ggSession.config.SubscriptionDefinition.ID = *newSubscription.Id
ggSession.config.SubscriptionDefinition.VersionArn = *newSubscription.LatestVersionArn
ggSession.updateGroup()
return nil
}
// Add subscription to existing
subscription, _ := ggSession.greengrass.GetSubscriptionDefinition(&greengrass.GetSubscriptionDefinitionInput{
SubscriptionDefinitionId: &ggSession.config.SubscriptionDefinition.ID,
})
subscriptionVersion, _ := ggSession.greengrass.GetSubscriptionDefinitionVersion(&greengrass.GetSubscriptionDefinitionVersionInput{
SubscriptionDefinitionId: subscription.Id,
SubscriptionDefinitionVersionId: subscription.LatestVersion,
})
subscriptions := subscriptionVersion.Definition.Subscriptions
subscriptions = append(subscriptions, &greengrass.Subscription{
Source: &sourceArn,
Target: &targetArn,
Subject: &subject,
Id: &uuidString,
})
output, err := ggSession.greengrass.CreateSubscriptionDefinitionVersion(&greengrass.CreateSubscriptionDefinitionVersionInput{
SubscriptionDefinitionId: subscription.Id,
Subscriptions: subscriptions,
})
if err != nil {
return err
}
ggSession.config.SubscriptionDefinition.VersionArn = *output.Arn
fmt.Printf("Updated subscription\n")
ggSession.updateGroup()
return nil
}
// ListSub - list a greengrass subscription
func (ggSession *GreengrassSession) ListSub() error {
if ggSession.config.SubscriptionDefinition.ID == "" {
fmt.Printf("No initial subscription defined\n")
return nil
}
subscription, err := ggSession.greengrass.GetSubscriptionDefinition(&greengrass.GetSubscriptionDefinitionInput{
SubscriptionDefinitionId: &ggSession.config.SubscriptionDefinition.ID,
})
if err != nil {
return err
}
fmt.Printf("subscription: %v\n", subscription)
subscriptionVersion, err := ggSession.greengrass.GetSubscriptionDefinitionVersion(&greengrass.GetSubscriptionDefinitionVersionInput{
SubscriptionDefinitionId: subscription.Id,
SubscriptionDefinitionVersionId: subscription.LatestVersion,
})
if err != nil {
return err
}
fmt.Printf("subscription version: %v\n", subscriptionVersion)
return nil
}