11package sumologic
22
33import (
4+ "errors"
45 "fmt"
56 "log"
67 "strconv"
@@ -23,7 +24,7 @@ func resourceSumologicGenericPollingSource() *schema.Resource {
2324 Required : true ,
2425 ForceNew : true ,
2526 ValidateFunc : validation .StringInSlice ([]string {"AwsS3Bucket" , "AwsElbBucket" , "AwsCloudFrontBucket" ,
26- "AwsCloudTrailBucket" , "AwsS3AuditBucket" , "AwsCloudWatch" , "AwsXRay" }, false ),
27+ "AwsCloudTrailBucket" , "AwsS3AuditBucket" , "AwsCloudWatch" , "AwsInventory" , " AwsXRay" }, false ),
2728 }
2829 pollingSource .Schema ["scan_interval" ] = & schema.Schema {
2930 Type : schema .TypeInt ,
@@ -77,7 +78,7 @@ func resourceSumologicGenericPollingSource() *schema.Resource {
7778 Type : schema .TypeString ,
7879 Required : true ,
7980 ValidateFunc : validation .StringInSlice ([]string {"S3BucketPathExpression" , "CloudWatchPath" ,
80- "AwsXRayPath" }, false ),
81+ "AwsInventoryPath" , " AwsXRayPath" }, false ),
8182 },
8283 "bucket_name" : {
8384 Type : schema .TypeString ,
@@ -133,20 +134,20 @@ func resourceSumologicGenericPollingSource() *schema.Resource {
133134}
134135
135136func resourceSumologicGenericPollingSourceCreate (d * schema.ResourceData , meta interface {}) error {
136-
137137 c := meta .(* Client )
138138
139139 if d .Id () == "" {
140- source := resourceToGenericPollingSource (d )
141- sourceID , err := c .CreatePollingSource (source , d .Get ("collector_id" ).(int ))
142-
140+ source , err := resourceToGenericPollingSource (d )
143141 if err != nil {
144142 return err
145143 }
146144
147- id := strconv .Itoa (sourceID )
145+ sourceID , err := c .CreatePollingSource (source , d .Get ("collector_id" ).(int ))
146+ if err != nil {
147+ return err
148+ }
148149
149- d .SetId (id )
150+ d .SetId (strconv . Itoa ( sourceID ) )
150151 }
151152
152153 return resourceSumologicGenericPollingSourceRead (d , meta )
@@ -155,10 +156,12 @@ func resourceSumologicGenericPollingSourceCreate(d *schema.ResourceData, meta in
155156func resourceSumologicGenericPollingSourceUpdate (d * schema.ResourceData , meta interface {}) error {
156157 c := meta .(* Client )
157158
158- source := resourceToGenericPollingSource (d )
159-
160- err := c .UpdatePollingSource (source , d .Get ("collector_id" ).(int ))
159+ source , err := resourceToGenericPollingSource (d )
160+ if err != nil {
161+ return err
162+ }
161163
164+ err = c .UpdatePollingSource (source , d .Get ("collector_id" ).(int ))
162165 if err != nil {
163166 return err
164167 }
@@ -201,7 +204,7 @@ func resourceSumologicGenericPollingSourceRead(d *schema.ResourceData, meta inte
201204 return nil
202205}
203206
204- func resourceToGenericPollingSource (d * schema.ResourceData ) PollingSource {
207+ func resourceToGenericPollingSource (d * schema.ResourceData ) ( PollingSource , error ) {
205208 source := resourceToSource (d )
206209 source .Type = "Polling"
207210
@@ -213,15 +216,25 @@ func resourceToGenericPollingSource(d *schema.ResourceData) PollingSource {
213216 URL : d .Get ("url" ).(string ),
214217 }
215218
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+
216229 pollingResource := PollingResource {
217230 ServiceType : d .Get ("content_type" ).(string ),
218- Authentication : getPollingAuthentication ( d ) ,
219- Path : getPollingPathSettings ( d ) ,
231+ Authentication : authSettings ,
232+ Path : pathSettings ,
220233 }
221234
222235 pollingSource .ThirdPartyRef .Resources = append (pollingSource .ThirdPartyRef .Resources , pollingResource )
223236
224- return pollingSource
237+ return pollingSource , nil
225238}
226239
227240func getPollingThirdPartyPathAttributes (pollingResource []PollingResource ) []map [string ]interface {} {
@@ -280,29 +293,35 @@ func getPollingTagFilters(d *schema.ResourceData) []TagFilter {
280293 return filters
281294}
282295
283- func getPollingAuthentication (d * schema.ResourceData ) PollingAuthentication {
296+ func getPollingAuthentication (d * schema.ResourceData ) ( PollingAuthentication , error ) {
284297 auths := d .Get ("authentication" ).([]interface {})
285298 authSettings := PollingAuthentication {}
286299
287300 if len (auths ) > 0 {
288301 auth := auths [0 ].(map [string ]interface {})
289302 switch authType := auth ["type" ].(string ); authType {
290303 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+ }
291308 authSettings .Type = "S3BucketAuthentication"
292309 authSettings .AwsID = auth ["access_key" ].(string )
293310 authSettings .AwsKey = auth ["secret_key" ].(string )
294311 case "AWSRoleBasedAuthentication" :
295312 authSettings .Type = "AWSRoleBasedAuthentication"
296313 authSettings .RoleARN = auth ["role_arn" ].(string )
297314 default :
298- 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 )
299318 }
300319 }
301320
302- return authSettings
321+ return authSettings , nil
303322}
304323
305- func getPollingPathSettings (d * schema.ResourceData ) PollingPath {
324+ func getPollingPathSettings (d * schema.ResourceData ) ( PollingPath , error ) {
306325 pathSettings := PollingPath {}
307326 paths := d .Get ("path" ).([]interface {})
308327
@@ -313,8 +332,8 @@ func getPollingPathSettings(d *schema.ResourceData) PollingPath {
313332 pathSettings .Type = "S3BucketPathExpression"
314333 pathSettings .BucketName = path ["bucket_name" ].(string )
315334 pathSettings .PathExpression = path ["path_expression" ].(string )
316- case "CloudWatchPath" :
317- pathSettings .Type = "CloudWatchPath"
335+ case "CloudWatchPath" , "AwsInventoryPath" :
336+ pathSettings .Type = pathType
318337 rawLimitToRegions := path ["limit_to_regions" ].([]interface {})
319338 LimitToRegions := make ([]string , len (rawLimitToRegions ))
320339 for i , v := range rawLimitToRegions {
@@ -328,7 +347,9 @@ func getPollingPathSettings(d *schema.ResourceData) PollingPath {
328347 }
329348 pathSettings .LimitToRegions = LimitToRegions
330349 pathSettings .LimitToNamespaces = LimitToNamespaces
331- pathSettings .TagFilters = getPollingTagFilters (d )
350+ if pathType == "CloudWatchPath" {
351+ pathSettings .TagFilters = getPollingTagFilters (d )
352+ }
332353 case "AwsXRayPath" :
333354 pathSettings .Type = "AwsXRayPath"
334355 rawLimitToRegions := path ["limit_to_regions" ].([]interface {})
@@ -338,9 +359,11 @@ func getPollingPathSettings(d *schema.ResourceData) PollingPath {
338359 }
339360 pathSettings .LimitToRegions = LimitToRegions
340361 default :
341- log .Printf ("[ERROR] Unknown resourceType in path: %v" , pathType )
362+ errorMessage := fmt .Sprintf ("[ERROR] Unknown resourceType in path: %v" , pathType )
363+ log .Print (errorMessage )
364+ return pathSettings , errors .New (errorMessage )
342365 }
343366 }
344367
345- return pathSettings
368+ return pathSettings , nil
346369}
0 commit comments