Skip to content

Commit 5362aad

Browse files
committed
Add tf support for Aws Inventory source
1 parent ab979ba commit 5362aad

File tree

1 file changed

+53
-25
lines changed

1 file changed

+53
-25
lines changed

sumologic/resource_sumologic_polling_source.go

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sumologic
22

33
import (
4+
"errors"
45
"fmt"
56
"log"
67
"strconv"
@@ -19,10 +20,11 @@ func resourceSumologicPollingSource() *schema.Resource {
1920
}
2021

2122
pollingSource.Schema["content_type"] = &schema.Schema{
22-
Type: schema.TypeString,
23-
Required: true,
24-
ForceNew: true,
25-
ValidateFunc: validation.StringInSlice([]string{"AwsS3Bucket", "AwsElbBucket", "AwsCloudFrontBucket", "AwsCloudTrailBucket", "AwsS3AuditBucket", "AwsCloudWatch"}, false),
23+
Type: schema.TypeString,
24+
Required: true,
25+
ForceNew: true,
26+
ValidateFunc: validation.StringInSlice([]string{"AwsS3Bucket", "AwsElbBucket", "AwsCloudFrontBucket",
27+
"AwsCloudTrailBucket", "AwsS3AuditBucket", "AwsCloudWatch", "AwsInventory"}, false),
2628
}
2729
pollingSource.Schema["scan_interval"] = &schema.Schema{
2830
Type: schema.TypeInt,
@@ -73,9 +75,10 @@ func resourceSumologicPollingSource() *schema.Resource {
7375
Elem: &schema.Resource{
7476
Schema: map[string]*schema.Schema{
7577
"type": {
76-
Type: schema.TypeString,
77-
Required: true,
78-
ValidateFunc: validation.StringInSlice([]string{"S3BucketPathExpression", "CloudWatchPath"}, false),
78+
Type: schema.TypeString,
79+
Required: true,
80+
ValidateFunc: validation.StringInSlice([]string{"S3BucketPathExpression", "CloudWatchPath",
81+
"AwsInventoryPath"}, false),
7982
},
8083
"bucket_name": {
8184
Type: schema.TypeString,
@@ -135,9 +138,12 @@ func resourceSumologicPollingSourceCreate(d *schema.ResourceData, meta interface
135138
c := meta.(*Client)
136139

137140
if d.Id() == "" {
138-
source := resourceToPollingSource(d)
139-
sourceID, err := c.CreatePollingSource(source, d.Get("collector_id").(int))
141+
source, err := resourceToPollingSource(d)
142+
if err != nil {
143+
return err
144+
}
140145

146+
sourceID, err := c.CreatePollingSource(source, d.Get("collector_id").(int))
141147
if err != nil {
142148
return err
143149
}
@@ -153,10 +159,12 @@ func resourceSumologicPollingSourceCreate(d *schema.ResourceData, meta interface
153159
func resourceSumologicPollingSourceUpdate(d *schema.ResourceData, meta interface{}) error {
154160
c := meta.(*Client)
155161

156-
source := resourceToPollingSource(d)
157-
158-
err := c.UpdatePollingSource(source, d.Get("collector_id").(int))
162+
source, err := resourceToPollingSource(d)
163+
if err != nil {
164+
return err
165+
}
159166

167+
err = c.UpdatePollingSource(source, d.Get("collector_id").(int))
160168
if err != nil {
161169
return err
162170
}
@@ -199,7 +207,7 @@ func resourceSumologicPollingSourceRead(d *schema.ResourceData, meta interface{}
199207
return nil
200208
}
201209

202-
func resourceToPollingSource(d *schema.ResourceData) PollingSource {
210+
func resourceToPollingSource(d *schema.ResourceData) (PollingSource, error) {
203211
source := resourceToSource(d)
204212
source.Type = "Polling"
205213

@@ -211,15 +219,25 @@ func resourceToPollingSource(d *schema.ResourceData) PollingSource {
211219
URL: d.Get("url").(string),
212220
}
213221

222+
authSettings, errAuthSettings := getAuthentication(d)
223+
if errAuthSettings != nil {
224+
return pollingSource, errAuthSettings
225+
}
226+
227+
pathSettings, errPathSettings := getPathSettings(d)
228+
if errPathSettings != nil {
229+
return pollingSource, errPathSettings
230+
}
231+
214232
pollingResource := PollingResource{
215233
ServiceType: d.Get("content_type").(string),
216-
Authentication: getAuthentication(d),
217-
Path: getPathSettings(d),
234+
Authentication: authSettings,
235+
Path: pathSettings,
218236
}
219237

220238
pollingSource.ThirdPartyRef.Resources = append(pollingSource.ThirdPartyRef.Resources, pollingResource)
221239

222-
return pollingSource
240+
return pollingSource, nil
223241
}
224242

225243
func getThirdPartyPathAttributes(pollingResource []PollingResource) []map[string]interface{} {
@@ -278,29 +296,35 @@ func getTagFilters(d *schema.ResourceData) []TagFilter {
278296
return filters
279297
}
280298

281-
func getAuthentication(d *schema.ResourceData) PollingAuthentication {
299+
func getAuthentication(d *schema.ResourceData) (PollingAuthentication, error) {
282300
auths := d.Get("authentication").([]interface{})
283301
authSettings := PollingAuthentication{}
284302

285303
if len(auths) > 0 {
286304
auth := auths[0].(map[string]interface{})
287305
switch authType := auth["type"].(string); authType {
288306
case "S3BucketAuthentication":
307+
if d.Get("content_type").(string) == "AwsInventory" {
308+
return authSettings, errors.New(
309+
fmt.Sprintf("[ERROR] Unsupported authType: %v for AwsInventory source", authType))
310+
}
289311
authSettings.Type = "S3BucketAuthentication"
290312
authSettings.AwsID = auth["access_key"].(string)
291313
authSettings.AwsKey = auth["secret_key"].(string)
292314
case "AWSRoleBasedAuthentication":
293315
authSettings.Type = "AWSRoleBasedAuthentication"
294316
authSettings.RoleARN = auth["role_arn"].(string)
295317
default:
296-
log.Printf("[ERROR] Unknown authType: %v", authType)
318+
errorMessage := fmt.Sprintf("[ERROR] Unknown authType: %v", authType)
319+
log.Print(errorMessage)
320+
return authSettings, errors.New(errorMessage)
297321
}
298322
}
299323

300-
return authSettings
324+
return authSettings, nil
301325
}
302326

303-
func getPathSettings(d *schema.ResourceData) PollingPath {
327+
func getPathSettings(d *schema.ResourceData) (PollingPath, error) {
304328
pathSettings := PollingPath{}
305329
paths := d.Get("path").([]interface{})
306330

@@ -311,8 +335,8 @@ func getPathSettings(d *schema.ResourceData) PollingPath {
311335
pathSettings.Type = "S3BucketPathExpression"
312336
pathSettings.BucketName = path["bucket_name"].(string)
313337
pathSettings.PathExpression = path["path_expression"].(string)
314-
case "CloudWatchPath":
315-
pathSettings.Type = "CloudWatchPath"
338+
case "CloudWatchPath", "AwsInventoryPath":
339+
pathSettings.Type = pathType
316340
rawLimitToRegions := path["limit_to_regions"].([]interface{})
317341
LimitToRegions := make([]string, len(rawLimitToRegions))
318342
for i, v := range rawLimitToRegions {
@@ -326,11 +350,15 @@ func getPathSettings(d *schema.ResourceData) PollingPath {
326350
}
327351
pathSettings.LimitToRegions = LimitToRegions
328352
pathSettings.LimitToNamespaces = LimitToNamespaces
329-
pathSettings.TagFilters = getTagFilters(d)
353+
if pathType == "CloudWatchPath" {
354+
pathSettings.TagFilters = getTagFilters(d)
355+
}
330356
default:
331-
log.Printf("[ERROR] Unknown resourceType in path: %v", pathType)
357+
errorMessage := fmt.Sprintf("[ERROR] Unknown resourceType in path: %v", pathType)
358+
log.Print(errorMessage)
359+
return pathSettings, errors.New(errorMessage)
332360
}
333361
}
334362

335-
return pathSettings
363+
return pathSettings, nil
336364
}

0 commit comments

Comments
 (0)