Skip to content

Commit ba6d810

Browse files
committed
use generic polling source instead of regular one
1 parent 1e661a5 commit ba6d810

File tree

3 files changed

+62
-46
lines changed

3 files changed

+62
-46
lines changed

sumologic/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func Provider() terraform.ResourceProvider {
4444
"sumologic_s3_source": resourceSumologicGenericPollingSource(),
4545
"sumologic_s3_audit_source": resourceSumologicGenericPollingSource(),
4646
"sumologic_cloudwatch_source": resourceSumologicGenericPollingSource(),
47+
"sumologic_aws_inventory_source": resourceSumologicGenericPollingSource(),
4748
"sumologic_cloudtrail_source": resourceSumologicGenericPollingSource(),
4849
"sumologic_elb_source": resourceSumologicGenericPollingSource(),
4950
"sumologic_cloudfront_source": resourceSumologicGenericPollingSource(),

sumologic/resource_sumologic_generic_polling_source.go

Lines changed: 54 additions & 29 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 resourceSumologicGenericPollingSource() *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 resourceSumologicGenericPollingSource() *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,
@@ -131,20 +134,20 @@ func resourceSumologicGenericPollingSource() *schema.Resource {
131134
}
132135

133136
func resourceSumologicGenericPollingSourceCreate(d *schema.ResourceData, meta interface{}) error {
134-
135137
c := meta.(*Client)
136138

137139
if d.Id() == "" {
138-
source := resourceToGenericPollingSource(d)
139-
sourceID, err := c.CreatePollingSource(source, d.Get("collector_id").(int))
140-
140+
source, err := resourceToGenericPollingSource(d)
141141
if err != nil {
142142
return err
143143
}
144144

145-
id := strconv.Itoa(sourceID)
145+
sourceID, err := c.CreatePollingSource(source, d.Get("collector_id").(int))
146+
if err != nil {
147+
return err
148+
}
146149

147-
d.SetId(id)
150+
d.SetId(strconv.Itoa(sourceID))
148151
}
149152

150153
return resourceSumologicGenericPollingSourceRead(d, meta)
@@ -153,10 +156,12 @@ func resourceSumologicGenericPollingSourceCreate(d *schema.ResourceData, meta in
153156
func resourceSumologicGenericPollingSourceUpdate(d *schema.ResourceData, meta interface{}) error {
154157
c := meta.(*Client)
155158

156-
source := resourceToGenericPollingSource(d)
157-
158-
err := c.UpdatePollingSource(source, d.Get("collector_id").(int))
159+
source, err := resourceToGenericPollingSource(d)
160+
if err != nil {
161+
return err
162+
}
159163

164+
err = c.UpdatePollingSource(source, d.Get("collector_id").(int))
160165
if err != nil {
161166
return err
162167
}
@@ -199,7 +204,7 @@ func resourceSumologicGenericPollingSourceRead(d *schema.ResourceData, meta inte
199204
return nil
200205
}
201206

202-
func resourceToGenericPollingSource(d *schema.ResourceData) PollingSource {
207+
func resourceToGenericPollingSource(d *schema.ResourceData) (PollingSource, error) {
203208
source := resourceToSource(d)
204209
source.Type = "Polling"
205210

@@ -211,15 +216,25 @@ func resourceToGenericPollingSource(d *schema.ResourceData) PollingSource {
211216
URL: d.Get("url").(string),
212217
}
213218

219+
authSettings, errAuthSettings := getPollingAuthentication(d)
220+
if errAuthSettings != nil {
221+
return pollingSource, errAuthSettings
222+
}
223+
224+
pathSettings, errPathSettings := getPollingPathSettings(d)
225+
if errPathSettings != nil {
226+
return pollingSource, errPathSettings
227+
}
228+
214229
pollingResource := PollingResource{
215230
ServiceType: d.Get("content_type").(string),
216-
Authentication: getPollingAuthentication(d),
217-
Path: getPollingPathSettings(d),
231+
Authentication: authSettings,
232+
Path: pathSettings,
218233
}
219234

220235
pollingSource.ThirdPartyRef.Resources = append(pollingSource.ThirdPartyRef.Resources, pollingResource)
221236

222-
return pollingSource
237+
return pollingSource, nil
223238
}
224239

225240
func getPollingThirdPartyPathAttributes(pollingResource []PollingResource) []map[string]interface{} {
@@ -278,29 +293,35 @@ func getPollingTagFilters(d *schema.ResourceData) []TagFilter {
278293
return filters
279294
}
280295

281-
func getPollingAuthentication(d *schema.ResourceData) PollingAuthentication {
296+
func getPollingAuthentication(d *schema.ResourceData) (PollingAuthentication, error) {
282297
auths := d.Get("authentication").([]interface{})
283298
authSettings := PollingAuthentication{}
284299

285300
if len(auths) > 0 {
286301
auth := auths[0].(map[string]interface{})
287302
switch authType := auth["type"].(string); authType {
288303
case "S3BucketAuthentication":
304+
if d.Get("content_type").(string) == "AwsInventory" {
305+
return authSettings, errors.New(
306+
fmt.Sprintf("[ERROR] Unsupported authType: %v for AwsInventory source", authType))
307+
}
289308
authSettings.Type = "S3BucketAuthentication"
290309
authSettings.AwsID = auth["access_key"].(string)
291310
authSettings.AwsKey = auth["secret_key"].(string)
292311
case "AWSRoleBasedAuthentication":
293312
authSettings.Type = "AWSRoleBasedAuthentication"
294313
authSettings.RoleARN = auth["role_arn"].(string)
295314
default:
296-
log.Printf("[ERROR] Unknown authType: %v", authType)
315+
errorMessage := fmt.Sprintf("[ERROR] Unknown authType: %v", authType)
316+
log.Print(errorMessage)
317+
return authSettings, errors.New(errorMessage)
297318
}
298319
}
299320

300-
return authSettings
321+
return authSettings, nil
301322
}
302323

303-
func getPollingPathSettings(d *schema.ResourceData) PollingPath {
324+
func getPollingPathSettings(d *schema.ResourceData) (PollingPath, error) {
304325
pathSettings := PollingPath{}
305326
paths := d.Get("path").([]interface{})
306327

@@ -311,8 +332,8 @@ func getPollingPathSettings(d *schema.ResourceData) PollingPath {
311332
pathSettings.Type = "S3BucketPathExpression"
312333
pathSettings.BucketName = path["bucket_name"].(string)
313334
pathSettings.PathExpression = path["path_expression"].(string)
314-
case "CloudWatchPath":
315-
pathSettings.Type = "CloudWatchPath"
335+
case "CloudWatchPath", "AwsInventoryPath":
336+
pathSettings.Type = pathType
316337
rawLimitToRegions := path["limit_to_regions"].([]interface{})
317338
LimitToRegions := make([]string, len(rawLimitToRegions))
318339
for i, v := range rawLimitToRegions {
@@ -326,11 +347,15 @@ func getPollingPathSettings(d *schema.ResourceData) PollingPath {
326347
}
327348
pathSettings.LimitToRegions = LimitToRegions
328349
pathSettings.LimitToNamespaces = LimitToNamespaces
329-
pathSettings.TagFilters = getPollingTagFilters(d)
350+
if pathType == "CloudWatchPath" {
351+
pathSettings.TagFilters = getPollingTagFilters(d)
352+
}
330353
default:
331-
log.Printf("[ERROR] Unknown resourceType in path: %v", pathType)
354+
errorMessage := fmt.Sprintf("[ERROR] Unknown resourceType in path: %v", pathType)
355+
log.Print(errorMessage)
356+
return pathSettings, errors.New(errorMessage)
332357
}
333358
}
334359

335-
return pathSettings
360+
return pathSettings, nil
336361
}

sumologic/resource_sumologic_polling_source.go

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ func resourceSumologicPollingSource() *schema.Resource {
2121
"We are deprecating the generic sumologic polling source and in turn creating individual sources for each of the content_type currently supported."
2222

2323
pollingSource.Schema["content_type"] = &schema.Schema{
24-
Type: schema.TypeString,
25-
Required: true,
26-
ForceNew: true,
27-
ValidateFunc: validation.StringInSlice([]string{"AwsS3Bucket", "AwsElbBucket", "AwsCloudFrontBucket",
28-
"AwsCloudTrailBucket", "AwsS3AuditBucket", "AwsCloudWatch", "AwsXRay"}, false),
24+
Type: schema.TypeString,
25+
Required: true,
26+
ForceNew: true,
27+
ValidateFunc: validation.StringInSlice([]string{"AwsS3Bucket", "AwsElbBucket", "AwsCloudFrontBucket", "AwsCloudTrailBucket", "AwsS3AuditBucket", "AwsCloudWatch"}, false),
2928
}
3029
pollingSource.Schema["scan_interval"] = &schema.Schema{
3130
Type: schema.TypeInt,
@@ -76,10 +75,9 @@ func resourceSumologicPollingSource() *schema.Resource {
7675
Elem: &schema.Resource{
7776
Schema: map[string]*schema.Schema{
7877
"type": {
79-
Type: schema.TypeString,
80-
Required: true,
81-
ValidateFunc: validation.StringInSlice([]string{"S3BucketPathExpression", "CloudWatchPath",
82-
"AwsXRayPath"}, false),
78+
Type: schema.TypeString,
79+
Required: true,
80+
ValidateFunc: validation.StringInSlice([]string{"S3BucketPathExpression", "CloudWatchPath"}, false),
8381
},
8482
"bucket_name": {
8583
Type: schema.TypeString,
@@ -331,14 +329,6 @@ func getPathSettings(d *schema.ResourceData) PollingPath {
331329
pathSettings.LimitToRegions = LimitToRegions
332330
pathSettings.LimitToNamespaces = LimitToNamespaces
333331
pathSettings.TagFilters = getTagFilters(d)
334-
case "AwsXRayPath":
335-
pathSettings.Type = "AwsXRayPath"
336-
rawLimitToRegions := path["limit_to_regions"].([]interface{})
337-
LimitToRegions := make([]string, len(rawLimitToRegions))
338-
for i, v := range rawLimitToRegions {
339-
LimitToRegions[i] = v.(string)
340-
}
341-
pathSettings.LimitToRegions = LimitToRegions
342332
default:
343333
log.Printf("[ERROR] Unknown resourceType in path: %v", pathType)
344334
}

0 commit comments

Comments
 (0)