Skip to content

Commit 858ad6a

Browse files
authored
Merge pull request #69 from SumoLogic/apoorv-inventory-source
terraform support for inventory source
2 parents 62ae7b5 + ad1862e commit 858ad6a

File tree

4 files changed

+139
-24
lines changed

4 files changed

+139
-24
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ DEPRECATIONS:
55

66
FEATURES:
77

8+
* **New Resource:** sumologic_aws_inventory_source (GH-69)
9+
* **New Resource:** sumologic_aws_xray_source (GH-68)
810
* **New Resource:** sumologic_s3_source (GH-64)
911
* **New Resource:** sumologic_s3_audit_source (GH-64)
1012
* **New Resource:** sumologic_cloudwatch_source (GH-64)

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_aws_xray_source": resourceSumologicGenericPollingSource(),
4849
"sumologic_cloudtrail_source": resourceSumologicGenericPollingSource(),
4950
"sumologic_elb_source": resourceSumologicGenericPollingSource(),

sumologic/resource_sumologic_generic_polling_source.go

Lines changed: 47 additions & 24 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"
@@ -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

135136
func 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
155156
func 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

227240
func 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
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
layout: "sumologic"
3+
page_title: "SumoLogic: sumologic_aws_inventory_source"
4+
description: |-
5+
Provides a Sumologic AWS Inventory source.
6+
---
7+
8+
# sumologic_aws_inventory_source
9+
Provides a Sumologic AWS Inventory source to collect AWS resource inventory data.
10+
11+
__IMPORTANT:__ The AWS credentials are stored in plain-text in the state. This is a potential security issue.
12+
13+
## Example Usage
14+
```hcl
15+
resource "sumologic_aws_inventory_source" "terraform_aws_inventory_source" {
16+
name = "AWS Inventory"
17+
description = "My description"
18+
category = "aws/terraform_aws_inventory"
19+
content_type = "AwsInventory"
20+
scan_interval = 300000
21+
paused = false
22+
collector_id = "${sumologic_collector.collector.id}"
23+
24+
authentication {
25+
type = "AWSRoleBasedAuthentication"
26+
role_arn = "arn:aws:iam::01234567890:role/sumo-role"
27+
}
28+
29+
path {
30+
type = "AwsInventoryPath"
31+
limit_to_regions = ["us-west-2"]
32+
limit_to_namespaces = ["AWS/RDS","AWS/EC2"]
33+
}
34+
}
35+
36+
resource "sumologic_collector" "collector" {
37+
name = "my-collector"
38+
description = "Just testing this"
39+
}
40+
```
41+
42+
## Argument reference
43+
44+
In addition to the common properties, the following arguments are supported:
45+
46+
- `content_type` - (Required) The content-type of the collected data. This has to be `AwsInventoryPath` for AWS Inventory source.
47+
- `scan_interval` - (Required) Time interval in milliseconds of scans for new data. The minimum value is 1000 milliseconds. Currently this value is not respected.
48+
- `paused` - (Required) When set to true, the scanner is paused. To disable, set to false.
49+
- `authentication` - (Required) Authentication details to access AWS `Describe*` APIs.
50+
+ `type` - (Required) Must be `AWSRoleBasedAuthentication`
51+
+ `role_arn` - (Required) Your AWS role ARN. More details [here](https://help.sumologic.com/03Send-Data/Sources/02Sources-for-Hosted-Collectors/Amazon-Web-Services/Grant-Access-to-an-AWS-Product#iam-role).
52+
- `path` - (Required) The location to scan for new data.
53+
+ `type` - (Required) type of polling source. This has to be `AwsInventoryPath` for AWS Inventory source.
54+
+ `limit_to_regions` - (Optional) List of Amazon regions.
55+
+ `limit_to_namespaces` - (Optional) List of namespaces. By default all namespaces are selected. You can also choose a subset from
56+
+ AWS/EC2
57+
+ AWS/AutoScaling
58+
+ AWS/EBS
59+
+ AWS/ELB
60+
+ AWS/ApplicationELB
61+
+ AWS/NetworkELB
62+
+ AWS/Lambda
63+
+ AWS/RDS
64+
+ AWS/Dynamodb
65+
+ AWS/ECS
66+
+ AWS/Elasticache
67+
+ AWS/Redshift
68+
+ AWS/Kinesis
69+
70+
### See also
71+
* [Common Source Properties](https://github.com/terraform-providers/terraform-provider-sumologic/tree/master/website#common-source-properties)
72+
73+
## Attributes Reference
74+
The following attributes are exported:
75+
76+
- `id` - The internal ID of the source.
77+
78+
## Import
79+
AWS Inventory sources can be imported using the collector and source IDs (`collector/source`), e.g.:
80+
81+
```hcl
82+
terraform import sumologic_aws_inventory_source.test 123/456
83+
```
84+
85+
AWS Inventory sources can be imported using the collector name and source name (`collectorName/sourceName`), e.g.:
86+
87+
```hcl
88+
terraform import sumologic_aws_inventory_source.test my-test-collector/my-test-source
89+
```

0 commit comments

Comments
 (0)