11package sumologic
22
33import (
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
133136func 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
153156func 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
225240func 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}
0 commit comments