@@ -31,63 +31,105 @@ type AWSS3ImportTrigger struct {
31
31
}
32
32
33
33
const (
34
- ArduinoPrefix = "/arduino/s3-importer"
35
- IoTApiKey = ArduinoPrefix + "/iot/api-key"
36
- IoTApiSecret = ArduinoPrefix + "/iot/api-secret"
37
- IoTApiOrgId = ArduinoPrefix + "/iot/org-id"
38
- IoTApiTags = ArduinoPrefix + "/iot/filter/tags"
39
- SamplesResoSec = ArduinoPrefix + "/iot/samples-resolution-seconds"
40
- SamplesReso = ArduinoPrefix + "/iot/samples-resolution"
41
- Scheduling = ArduinoPrefix + "/iot/scheduling"
42
- DestinationS3Bucket = ArduinoPrefix + "/destination-bucket"
34
+ GlobalArduinoPrefix = "/arduino/s3-importer"
35
+
36
+ // Compatibility parameters for backward compatibility
37
+ IoTApiKey = GlobalArduinoPrefix + "/iot/api-key"
38
+ IoTApiSecret = GlobalArduinoPrefix + "/iot/api-secret"
39
+ IoTApiOrgId = GlobalArduinoPrefix + "/iot/org-id"
40
+ IoTApiTags = GlobalArduinoPrefix + "/iot/filter/tags"
41
+ SamplesResoSec = GlobalArduinoPrefix + "/iot/samples-resolution-seconds"
42
+ SamplesReso = GlobalArduinoPrefix + "/iot/samples-resolution"
43
+ Scheduling = GlobalArduinoPrefix + "/iot/scheduling"
44
+ DestinationS3Bucket = GlobalArduinoPrefix + "/destination-bucket"
45
+
46
+ // Per stack parameters
47
+ PerStackArduinoPrefix = "/arduino/s3-exporter/" + parameters .StackName
48
+ IoTApiKeyStack = PerStackArduinoPrefix + "/iot/api-key"
49
+ IoTApiSecretStack = PerStackArduinoPrefix + "/iot/api-secret"
50
+ IoTApiOrgIdStack = PerStackArduinoPrefix + "/iot/org-id"
51
+ IoTApiTagsStack = PerStackArduinoPrefix + "/iot/filter/tags"
52
+ SamplesResoStack = PerStackArduinoPrefix + "/iot/samples-resolution"
53
+ SchedulingStack = PerStackArduinoPrefix + "/iot/scheduling"
54
+ DestinationS3BucketStack = PerStackArduinoPrefix + "/destination-bucket"
55
+
43
56
SamplesResolutionSeconds = 300
44
57
DefaultTimeExtractionWindowMinutes = 60
45
58
)
46
59
47
60
func HandleRequest (ctx context.Context , event * AWSS3ImportTrigger ) (* string , error ) {
48
61
49
62
logger := logrus .NewEntry (logrus .New ())
63
+ stackName := os .Getenv ("STACK_NAME" )
50
64
65
+ var apikey * string
66
+ var apiSecret * string
67
+ var destinationS3Bucket * string
51
68
var tags * string
69
+ var orgId * string
70
+ var err error
52
71
53
72
logger .Infoln ("------ Reading parameters from SSM" )
54
73
paramReader , err := parameters .New ()
55
74
if err != nil {
56
75
return nil , err
57
76
}
58
- apikey , err := paramReader .ReadConfig (IoTApiKey )
59
- if err != nil {
60
- logger .Error ("Error reading parameter " + IoTApiKey , err )
61
- }
62
- apiSecret , err := paramReader .ReadConfig (IoTApiSecret )
63
- if err != nil {
64
- logger .Error ("Error reading parameter " + IoTApiSecret , err )
65
- }
66
- destinationS3Bucket , err := paramReader .ReadConfig (DestinationS3Bucket )
67
- if err != nil || destinationS3Bucket == nil || * destinationS3Bucket == "" {
68
- logger .Error ("Error reading parameter " + DestinationS3Bucket , err )
77
+
78
+ if stackName != "" {
79
+ logger .Infoln ("------ Configured stack: " + stackName )
80
+ apikey , err = paramReader .ReadConfigByStack (IoTApiKeyStack , stackName )
81
+ if err != nil {
82
+ logger .Error ("Error reading parameter " + IoTApiKeyStack , err )
83
+ }
84
+ apiSecret , err = paramReader .ReadConfigByStack (IoTApiSecretStack , stackName )
85
+ if err != nil {
86
+ logger .Error ("Error reading parameter " + IoTApiSecretStack , err )
87
+ }
88
+ destinationS3Bucket , err = paramReader .ReadConfigByStack (DestinationS3BucketStack , stackName )
89
+ if err != nil || destinationS3Bucket == nil || * destinationS3Bucket == "" {
90
+ logger .Error ("Error reading parameter " + DestinationS3BucketStack , err )
91
+ }
92
+ orgId , _ = paramReader .ReadConfigByStack (IoTApiOrgIdStack , stackName )
93
+ tagsParam , _ := paramReader .ReadConfigByStack (IoTApiTagsStack , stackName )
94
+ if tagsParam != nil {
95
+ tags = tagsParam
96
+ }
97
+ } else {
98
+ apikey , err = paramReader .ReadConfig (IoTApiKey )
99
+ if err != nil {
100
+ logger .Error ("Error reading parameter " + IoTApiKey , err )
101
+ }
102
+ apiSecret , err = paramReader .ReadConfig (IoTApiSecret )
103
+ if err != nil {
104
+ logger .Error ("Error reading parameter " + IoTApiSecret , err )
105
+ }
106
+ destinationS3Bucket , err = paramReader .ReadConfig (DestinationS3Bucket )
107
+ if err != nil || destinationS3Bucket == nil || * destinationS3Bucket == "" {
108
+ logger .Error ("Error reading parameter " + DestinationS3Bucket , err )
109
+ }
110
+ orgId , _ = paramReader .ReadConfig (IoTApiOrgId )
111
+ tagsParam , _ := paramReader .ReadConfig (IoTApiTags )
112
+ if tagsParam != nil {
113
+ tags = tagsParam
114
+ }
69
115
}
70
- origId , _ := paramReader . ReadConfig ( IoTApiOrgId )
116
+
71
117
organizationId := ""
72
- if origId != nil {
73
- organizationId = * origId
118
+ if orgId != nil {
119
+ organizationId = * orgId
74
120
}
75
121
if apikey == nil || apiSecret == nil {
76
122
return nil , errors .New ("key and secret are required" )
77
123
}
78
- tagsParam , _ := paramReader .ReadConfig (IoTApiTags )
79
- if tagsParam != nil {
80
- tags = tagsParam
81
- }
82
124
83
125
// Resolve resolution
84
- resolution , err := configureExtractionResolution (logger , paramReader )
126
+ resolution , err := configureExtractionResolution (logger , paramReader , stackName )
85
127
if err != nil {
86
128
return nil , err
87
129
}
88
130
89
131
// Resolve scheduling
90
- extractionWindowMinutes , err := configureDataExtractionTimeWindow (logger , paramReader )
132
+ extractionWindowMinutes , err := configureDataExtractionTimeWindow (logger , paramReader , stackName )
91
133
if err != nil {
92
134
return nil , err
93
135
}
@@ -98,7 +140,7 @@ func HandleRequest(ctx context.Context, event *AWSS3ImportTrigger) (*string, err
98
140
resolution = & defReso
99
141
}
100
142
101
- logger .Infoln ("------ Running import... " )
143
+ logger .Infoln ("------ Running import" )
102
144
if event .Dev || os .Getenv ("DEV" ) == "true" {
103
145
logger .Infoln ("Running in dev mode" )
104
146
os .Setenv ("IOT_API_URL" , "https://api2.oniudra.cc" )
@@ -129,8 +171,14 @@ func HandleRequest(ctx context.Context, event *AWSS3ImportTrigger) (*string, err
129
171
return & message , nil
130
172
}
131
173
132
- func configureExtractionResolution (logger * logrus.Entry , paramReader * parameters.ParametersClient ) (* int , error ) {
133
- resolution , err := paramReader .ReadIntConfig (SamplesResoSec )
174
+ func configureExtractionResolution (logger * logrus.Entry , paramReader * parameters.ParametersClient , stack string ) (* int , error ) {
175
+ var resolution * int
176
+ var err error
177
+ if stack != "" {
178
+ resolution , err = paramReader .ReadIntConfigByStack (SamplesResoStack , stack )
179
+ } else {
180
+ resolution , err = paramReader .ReadIntConfig (SamplesReso )
181
+ }
134
182
if err != nil {
135
183
// Possibly this parameter is not set. Try SamplesReso
136
184
res , err := paramReader .ReadConfig (SamplesReso )
@@ -160,8 +208,14 @@ func configureExtractionResolution(logger *logrus.Entry, paramReader *parameters
160
208
return resolution , nil
161
209
}
162
210
163
- func configureDataExtractionTimeWindow (logger * logrus.Entry , paramReader * parameters.ParametersClient ) (* int , error ) {
164
- schedule , err := paramReader .ReadConfig (Scheduling )
211
+ func configureDataExtractionTimeWindow (logger * logrus.Entry , paramReader * parameters.ParametersClient , stack string ) (* int , error ) {
212
+ var schedule * string
213
+ var err error
214
+ if stack != "" {
215
+ schedule , err = paramReader .ReadConfigByStack (SchedulingStack , stack )
216
+ } else {
217
+ schedule , err = paramReader .ReadConfig (Scheduling )
218
+ }
165
219
if err != nil {
166
220
logger .Error ("Error reading parameter " + Scheduling , err )
167
221
return nil , err
0 commit comments