Skip to content

Commit bd9ed6b

Browse files
authored
Merge pull request #678 from SumoLogic/CSRE-183_Adding_Data_Forwarding_Resource
Csre 183 adding data forwarding resource
2 parents 6b36856 + 74f8587 commit bd9ed6b

File tree

4 files changed

+282
-0
lines changed

4 files changed

+282
-0
lines changed

sumologic/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ func Provider() terraform.ResourceProvider {
9292
"sumologic_folder": resourceSumologicFolder(),
9393
"sumologic_content": resourceSumologicContent(),
9494
"sumologic_scheduled_view": resourceSumologicScheduledView(),
95+
"sumologic_data_forwarding_destination": resourceSumologicDataForwardingDestination(),
9596
"sumologic_partition": resourceSumologicPartition(),
9697
"sumologic_field_extraction_rule": resourceSumologicFieldExtractionRule(),
9798
"sumologic_connection": resourceSumologicConnection(),
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
package sumologic
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
5+
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
6+
"log"
7+
)
8+
9+
func resourceSumologicDataForwardingDestination() *schema.Resource {
10+
return &schema.Resource{
11+
12+
Create: resourceSumologicDataForwardingDestinationCreate,
13+
Read: resourceSumologicDataForwardingDestinationRead,
14+
Update: resourceSumologicDataForwardingDestinationUpdate,
15+
Delete: resourceSumologicDataForwardingDestinationDelete,
16+
Importer: &schema.ResourceImporter{
17+
State: schema.ImportStatePassthrough,
18+
},
19+
20+
Schema: map[string]*schema.Schema{
21+
22+
"destination_name": {
23+
Type: schema.TypeString,
24+
Required: true,
25+
ValidateFunc: validation.StringLenBetween(1, 255),
26+
},
27+
"description": {
28+
Type: schema.TypeString,
29+
Optional: true,
30+
},
31+
"bucket_name": {
32+
Type: schema.TypeString,
33+
Required: true,
34+
},
35+
"authentication": {
36+
Type: schema.TypeList,
37+
Required: true,
38+
MaxItems: 1,
39+
Elem: &schema.Resource{
40+
Schema: map[string]*schema.Schema{
41+
"type": {
42+
Type: schema.TypeString,
43+
Required: true,
44+
ValidateFunc: validation.StringInSlice([]string{"RoleBased", "AccessKey"}, false),
45+
},
46+
"role_arn": {
47+
Type: schema.TypeString,
48+
Optional: true,
49+
},
50+
"access_key": {
51+
Type: schema.TypeString,
52+
Optional: true,
53+
},
54+
"secret_key": {
55+
Type: schema.TypeString,
56+
Optional: true,
57+
},
58+
},
59+
},
60+
},
61+
"s3_region": {
62+
Type: schema.TypeString,
63+
Optional: true,
64+
},
65+
"s3_server_side_encryption": {
66+
Type: schema.TypeBool,
67+
Optional: true,
68+
},
69+
},
70+
}
71+
72+
}
73+
74+
func resourceSumologicDataForwardingDestinationCreate(d *schema.ResourceData, meta interface{}) error {
75+
c := meta.(*Client)
76+
77+
if d.Id() == "" {
78+
dataForwardingDestination := resourceToDataForwardingDestination(d)
79+
createdDataForwardingDestination, err := c.CreateDataForwardingDestination(dataForwardingDestination)
80+
81+
if err != nil {
82+
return err
83+
}
84+
85+
d.SetId(createdDataForwardingDestination.ID)
86+
87+
}
88+
89+
return resourceSumologicDataForwardingDestinationUpdate(d, meta)
90+
}
91+
92+
func resourceSumologicDataForwardingDestinationUpdate(d *schema.ResourceData, meta interface{}) error {
93+
94+
dataForwardingDestination := resourceToDataForwardingDestination(d)
95+
96+
c := meta.(*Client)
97+
err := c.UpdateDataForwardingDestination(dataForwardingDestination)
98+
99+
if err != nil {
100+
return err
101+
}
102+
103+
return resourceSumologicDataForwardingDestinationRead(d, meta)
104+
}
105+
106+
func resourceSumologicDataForwardingDestinationRead(d *schema.ResourceData, meta interface{}) error {
107+
c := meta.(*Client)
108+
109+
id := d.Id()
110+
dataForwardingDestination, err := c.getDataForwardingDestination(id)
111+
112+
if err != nil {
113+
return err
114+
}
115+
116+
if dataForwardingDestination == nil {
117+
log.Printf("[WARN] Data Forwarding destination not found, removing from state: %v - %v", id, err)
118+
d.SetId("")
119+
120+
return nil
121+
}
122+
123+
d.Set("destination_name", dataForwardingDestination.DestinationName)
124+
d.Set("description", dataForwardingDestination.Description)
125+
d.Set("bucket_name", dataForwardingDestination.BucketName)
126+
d.Set("S3_region", dataForwardingDestination.S3Region)
127+
d.Set("S3_server_side_encryption", dataForwardingDestination.S3ServerSideEncryption)
128+
129+
return nil
130+
}
131+
132+
func resourceSumologicDataForwardingDestinationDelete(d *schema.ResourceData, meta interface{}) error {
133+
c := meta.(*Client)
134+
id := d.Id()
135+
136+
return c.DeleteDataForwardingDestination(id)
137+
}
138+
139+
func resourceToDataForwardingDestination(d *schema.ResourceData) DataForwardingDestination {
140+
141+
authentication := extractAuthenticationDetails(d.Get("authentication").([]interface{}))
142+
143+
return DataForwardingDestination{
144+
ID: d.Id(),
145+
DestinationName: d.Get("destination_name").(string),
146+
Description: d.Get("description").(string),
147+
BucketName: d.Get("bucket_name").(string),
148+
AccessMethod: authentication["type"].(string),
149+
AccessKey: authentication["access_key"].(string),
150+
SecretKey: authentication["secret_key"].(string),
151+
RoleArn: authentication["role_arn"].(string),
152+
S3Region: d.Get("s3_region").(string),
153+
S3ServerSideEncryption: d.Get("s3_server_side_encryption").(bool),
154+
}
155+
}
156+
157+
func extractAuthenticationDetails(authenticationList []interface{}) map[string]interface{} {
158+
if len(authenticationList) > 0 {
159+
authMap := authenticationList[0].(map[string]interface{})
160+
return authMap
161+
}
162+
return map[string]interface{}{
163+
"type": nil,
164+
"role_arn": nil,
165+
"access_key": nil,
166+
"secret_key": nil,
167+
}
168+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package sumologic
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
)
7+
8+
func (s *Client) getDataForwardingDestination(id string) (*DataForwardingDestination, error) {
9+
10+
data, _, err := s.Get(fmt.Sprintf("v1/logsDataForwarding/destinations/%s", id))
11+
if err != nil {
12+
return nil, err
13+
}
14+
if data == nil {
15+
return nil, nil
16+
}
17+
18+
var dataForwardingDestination DataForwardingDestination
19+
err = json.Unmarshal(data, &dataForwardingDestination)
20+
21+
if err != nil {
22+
return nil, err
23+
}
24+
25+
return &dataForwardingDestination, nil
26+
27+
}
28+
func (s *Client) CreateDataForwardingDestination(dataForwardingDestination DataForwardingDestination) (*DataForwardingDestination, error) {
29+
var createdDataForwardingDestination DataForwardingDestination
30+
31+
responseBody, err := s.Post("v1/logsDataForwarding/destinations", dataForwardingDestination)
32+
33+
if err != nil {
34+
return nil, err
35+
}
36+
37+
err = json.Unmarshal(responseBody, &createdDataForwardingDestination)
38+
39+
if err != nil {
40+
return nil, err
41+
}
42+
43+
return &createdDataForwardingDestination, nil
44+
}
45+
46+
func (s *Client) UpdateDataForwardingDestination(dataForwardingDestination DataForwardingDestination) error {
47+
48+
url := fmt.Sprintf("v1/logsDataForwarding/destinations/%s", dataForwardingDestination.ID)
49+
_, err := s.Put(url, dataForwardingDestination)
50+
51+
return err
52+
}
53+
func (s *Client) DeleteDataForwardingDestination(id string) error {
54+
url := fmt.Sprintf("v1/logsDataForwarding/destinations/%s", id)
55+
56+
_, err := s.Delete(url)
57+
58+
return err
59+
}
60+
61+
type DataForwardingDestination struct {
62+
ID string `json:"id,omitempty"`
63+
DestinationName string `json:"destinationName"`
64+
Description string `json:"description,omitempty"`
65+
BucketName string `json:"bucketName"`
66+
AccessMethod string `json:"authenticationMode"`
67+
AccessKey string `json:"accessKeyId,omitempty"`
68+
SecretKey string `json:"secretAccessKey,omitempty"`
69+
RoleArn string `json:"roleArn,omitempty"`
70+
S3Region string `json:"region,omitempty"`
71+
S3ServerSideEncryption bool `json:"encrypted"`
72+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
layout: "sumologic"
3+
page_title: "SumoLogic: sumologic_data_forwarding_destination"
4+
description: |-
5+
Provides a Sumologic Data Forwarding Destination
6+
---
7+
8+
# sumologic_data_forwarding_destination
9+
Provider to manage [Sumologic Data Forwarding Destination](https://help.sumologic.com/docs/manage/data-forwarding/amazon-s3-bucket/#configure-an-s3-data-forwarding-destination)
10+
11+
## Example Usage
12+
```hcl
13+
resource "sumologic_data_forwarding_destination" "example_data_forwarding_destination" {
14+
destination_name = "df-destination"
15+
description = ""
16+
bucket_name = "df-bucket"
17+
region = "us-east-1"
18+
authentication_mode = "RoleBased"
19+
access_key_id = "accessKeyId"
20+
secret_access_key = "secretAccessKey"
21+
role_arn = "arn:aws:iam::some-valid-arn"
22+
encrypted = "false"
23+
}
24+
```
25+
## Argument reference
26+
27+
The following arguments are supported:
28+
29+
- `destination_name` - (Required) Name of the S3 data forwarding destination.
30+
- `description` - (Optional) Description of the S3 data forwarding destination.
31+
- `bucket_name` - (Required) The name of the Amazon S3 bucket.
32+
- `region` - (Optional) The region where the S3 bucket is located.
33+
- `authentication_mode` - (Required) AWS IAM authentication method used for access. Possible values are: 1. `AccessKey` 2. `RoleBased`
34+
- `access_key_id` - (Optional) The AWS Access ID to access the S3 bucket.
35+
- `secret_access_key` - (Optional) The AWS Secret Key to access the S3 bucket.
36+
- `role_arn` - (Optional) The AWS Role ARN to access the S3 bucket.
37+
- `encrypted` - (Optional) Enable S3 server-side encryption.
38+
39+
The following attributes are exported:
40+
41+
- `id` - The internal ID of the data_forwarding_destination

0 commit comments

Comments
 (0)