Skip to content

Commit 4524f9b

Browse files
committed
CSRE-183: Added data forwarding resource
1 parent 7cb0dbd commit 4524f9b

File tree

3 files changed

+173
-90
lines changed

3 files changed

+173
-90
lines changed

sumologic/resource_sumologic_data_forwarding.go

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ func resourceSumologicDataForwarding() *schema.Resource {
1212
Read: resourceSumologicDataForwardingRead,
1313
Update: resourceSumologicDataForwardingUpdate,
1414
Delete: resourceSumologicDataForwardingDelete,
15+
Importer: &schema.ResourceImporter{
16+
State: schema.ImportStatePassthrough,
17+
},
1518

1619
Schema: map[string]*schema.Schema{
1720

@@ -24,19 +27,11 @@ func resourceSumologicDataForwarding() *schema.Resource {
2427
Type: schema.TypeString,
2528
Optional: true,
2629
},
27-
"destination_type": {
28-
Type: schema.TypeString,
29-
Optional: true,
30-
},
3130
"bucket_name": {
3231
Type: schema.TypeString,
3332
Required: true,
3433
ForceNew: true,
3534
},
36-
"status": {
37-
Type: schema.TypeString,
38-
Optional: true,
39-
},
4035
"authentication": {
4136
Type: schema.TypeList,
4237
Required: true,
@@ -67,7 +62,7 @@ func resourceSumologicDataForwarding() *schema.Resource {
6762
Optional: true,
6863
},
6964
"s3_server_side_encryption": {
70-
Type: schema.TypeBool, //bool
65+
Type: schema.TypeBool,
7166
Optional: true,
7267
},
7368
},
@@ -126,9 +121,7 @@ func resourceSumologicDataForwardingRead(d *schema.ResourceData, meta interface{
126121

127122
d.Set("destination_name", dataForwarding.DestinationName)
128123
d.Set("description", dataForwarding.Description)
129-
d.Set("destination_type", dataForwarding.DestinationType)
130124
d.Set("bucket_name", dataForwarding.BucketName)
131-
d.Set("status", dataForwarding.Status)
132125
d.Set("S3_region", dataForwarding.S3Region)
133126
d.Set("S3_server_side_encryption", dataForwarding.S3ServerSideEncryption)
134127

@@ -150,9 +143,7 @@ func resourceToDataForwarding(d *schema.ResourceData) DataForwarding {
150143
ID: d.Id(),
151144
DestinationName: d.Get("destination_name").(string),
152145
Description: d.Get("description").(string),
153-
DestinationType: d.Get("destination_type").(string),
154146
BucketName: d.Get("bucket_name").(string),
155-
Status: d.Get("status").(string),
156147
AccessMethod: authentication["type"].(string),
157148
AccessKey: authentication["access_key"].(string),
158149
SecretKey: authentication["secret_key"].(string),
@@ -169,9 +160,9 @@ func extractAuthenticationDetails(authenticationList []interface{}) map[string]i
169160
}
170161
// Default values if authentication block is not provided
171162
return map[string]interface{}{
172-
"type": "",
173-
"role_arn": "",
174-
"access_key": "",
175-
"secret_key": "",
163+
"type": nil,
164+
"role_arn": nil,
165+
"access_key": nil,
166+
"secret_key": nil,
176167
}
177168
}

sumologic/resource_sumologic_data_forwarding_test.go

Lines changed: 160 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,110 +2,204 @@ package sumologic
22

33
import (
44
"fmt"
5+
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
56
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
67
"github.com/hashicorp/terraform-plugin-sdk/terraform"
8+
"os"
9+
"strings"
710
"testing"
811
)
912

10-
//func getRandomizedParams() (string, string, string) {
11-
// name := acctest.RandomWithPrefix("tf-acc-test")
12-
// description := acctest.RandomWithPrefix("tf-acc-test")
13-
// category := acctest.RandomWithPrefix("tf-acc-test")
14-
// return name, description, category }
13+
func getTestParams() (string, string, string, string, string, string) {
14+
dataForwardingResourceName := "sumologic_data_forwarding.test"
15+
destinationName, description, region := getRandomizedDataForwardingParams()
16+
testAwsRoleArn := os.Getenv("SUMOLOGIC_TEST_ROLE_ARN")
17+
testAwsBucket := os.Getenv("SUMOLOGIC_TEST_BUCKET_NAME")
18+
return dataForwardingResourceName, destinationName, description, region, testAwsRoleArn, testAwsBucket
19+
}
20+
21+
func getRandomizedDataForwardingParams() (string, string, string) {
22+
destinationName := acctest.RandomWithPrefix("tf-acc-test")
23+
description := acctest.RandomWithPrefix("tf-acc-test")
24+
region := acctest.RandomWithPrefix("tf-acc-test")
25+
return destinationName, description, region
26+
}
1527

1628
func TestAccSumologicDataForwarding_create(t *testing.T) {
17-
//var dataForwarding DataForwarding
18-
testAwsAccessKey := ""
19-
testAwsSecretKey := ""
29+
dataForwardingResourceName, destinationName, description, region, testAwsRoleArn, testAwsBucket := getTestParams()
30+
resource.Test(t, resource.TestCase{
31+
IsUnitTest: true,
32+
PreCheck: func() { testAccPreCheckWithAWS(t) },
33+
Providers: testAccProviders,
34+
CheckDestroy: testAccCheckDataForwardingDestroy(),
35+
Steps: []resource.TestStep{
36+
{
37+
Config: testAccSumologicDataForwardingCreateConfig(destinationName, description, testAwsBucket, testAwsRoleArn, region),
38+
Check: resource.ComposeTestCheckFunc(
39+
testAccCheckDataForwardingExists(),
40+
resource.TestCheckResourceAttr(dataForwardingResourceName, "destination_name", destinationName),
41+
resource.TestCheckResourceAttr(dataForwardingResourceName, "description", description),
42+
resource.TestCheckResourceAttr(dataForwardingResourceName, "s3_region", region),
43+
resource.TestCheckResourceAttr(dataForwardingResourceName, "s3_server_side_encryption", "false"),
44+
),
45+
},
46+
},
47+
})
48+
}
49+
50+
func TestAccSumologicDataForwarding_read(t *testing.T) {
51+
dataForwardingResourceName, destinationName, description, region, testAwsRoleArn, testAwsBucket := getTestParams()
52+
resource.Test(t, resource.TestCase{
53+
IsUnitTest: true,
54+
PreCheck: func() { testAccPreCheckWithAWS(t) },
55+
Providers: testAccProviders,
56+
CheckDestroy: testAccCheckDataForwardingDestroy(),
57+
Steps: []resource.TestStep{
58+
{
59+
Config: testAccSumologicDataForwardingCreateConfig(destinationName, description, testAwsBucket, testAwsRoleArn, region),
60+
Check: resource.ComposeTestCheckFunc(
61+
testAccCheckDataForwardingExists(),
62+
resource.TestCheckResourceAttr(dataForwardingResourceName, "destination_name", destinationName),
63+
resource.TestCheckResourceAttr(dataForwardingResourceName, "description", description),
64+
resource.TestCheckResourceAttr(dataForwardingResourceName, "bucket_name", testAwsBucket),
65+
resource.TestCheckResourceAttr(dataForwardingResourceName, "s3_region", region),
66+
resource.TestCheckResourceAttr(dataForwardingResourceName, "s3_server_side_encryption", "false"),
67+
),
68+
},
69+
{
70+
ResourceName: dataForwardingResourceName,
71+
ImportState: true,
72+
},
73+
},
74+
})
75+
}
2076

77+
func TestAccSumologicDataForwarding_update(t *testing.T) {
78+
dataForwardingResourceName, destinationName, description, region, testAwsRoleArn, testAwsBucket := getTestParams()
2179
resource.Test(t, resource.TestCase{
22-
PreCheck: func() { testAccPreCheck(t) },
23-
Providers: testAccProviders,
80+
IsUnitTest: true,
81+
PreCheck: func() { testAccPreCheckWithAWS(t) },
82+
Providers: testAccProviders,
83+
CheckDestroy: testAccCheckDataForwardingDestroy(),
2484
Steps: []resource.TestStep{
2585
{
26-
Config: testAccSumologicDataForwardingConfig(testAwsAccessKey, testAwsSecretKey),
86+
Config: testAccSumologicDataForwardingCreateConfig(destinationName, description, testAwsBucket, testAwsRoleArn, region),
87+
Check: resource.ComposeTestCheckFunc(
88+
testAccCheckDataForwardingExists(),
89+
resource.TestCheckResourceAttr(dataForwardingResourceName, "destination_name", destinationName),
90+
resource.TestCheckResourceAttr(dataForwardingResourceName, "description", description),
91+
resource.TestCheckResourceAttr(dataForwardingResourceName, "s3_region", region),
92+
resource.TestCheckResourceAttr(dataForwardingResourceName, "s3_server_side_encryption", "false"),
93+
),
94+
}, {
95+
Config: testAccSumologicDataForwardingUpdateConfig(destinationName, description, testAwsBucket, testAwsRoleArn, region),
2796
Check: resource.ComposeTestCheckFunc(
2897
testAccCheckDataForwardingExists(),
29-
resource.TestCheckResourceAttr("sumologic_data_forwarding.test", "destination_name", "abc")),
98+
resource.TestCheckResourceAttr(dataForwardingResourceName, "destination_name", destinationName),
99+
resource.TestCheckResourceAttr(dataForwardingResourceName, "description", description),
100+
resource.TestCheckResourceAttr(dataForwardingResourceName, "s3_region", region),
101+
resource.TestCheckResourceAttr(dataForwardingResourceName, "s3_server_side_encryption", "true"),
102+
),
30103
},
31104
},
32105
})
33106

34107
}
35108

109+
func TestAccSumologicDataForwarding_delete(t *testing.T) {
110+
dataForwardingResourceName, destinationName, description, region, testAwsRoleArn, testAwsBucket := getTestParams()
111+
resource.Test(t, resource.TestCase{
112+
IsUnitTest: true,
113+
PreCheck: func() { testAccPreCheckWithAWS(t) },
114+
Providers: testAccProviders,
115+
Steps: []resource.TestStep{
116+
{
117+
Config: testAccSumologicDataForwardingCreateConfig(destinationName, description, testAwsBucket, testAwsRoleArn, region),
118+
Check: resource.ComposeTestCheckFunc(
119+
testAccCheckDataForwardingExists(),
120+
resource.TestCheckResourceAttr(dataForwardingResourceName, "destination_name", destinationName),
121+
resource.TestCheckResourceAttr(dataForwardingResourceName, "description", description),
122+
resource.TestCheckResourceAttr(dataForwardingResourceName, "s3_region", region),
123+
resource.TestCheckResourceAttr(dataForwardingResourceName, "s3_server_side_encryption", "false"),
124+
),
125+
}, {
126+
Config: testAccSumologicDataForwardingDeleteConfig(),
127+
Check: resource.ComposeTestCheckFunc(testAccCheckDataForwardingDestroy()),
128+
},
129+
},
130+
})
131+
}
132+
36133
func testAccCheckDataForwardingExists() resource.TestCheckFunc {
37134
return func(s *terraform.State) error {
38135
client := testAccProvider.Meta().(*Client)
39136
for _, r := range s.RootModule().Resources {
40137
id := r.Primary.ID
41-
if _, err := client.GetPartition(id); err != nil {
138+
if _, err := client.getDataForwarding(id); err != nil {
42139
return fmt.Errorf("Received an error retrieving data forwarding %s", err)
43140
}
44141
}
45142
return nil
46143
}
47144
}
48145

49-
func testAccSumologicDataForwardingConfig(testAwsAccessKey string, testAwsSecretKey string) string {
50-
return fmt.Sprintf(`
51-
resource "aws_s3_bucket" "test_bucket" {
52-
bucket = "S3-tests-data-forwarding"
53-
}
146+
func testAccCheckDataForwardingDestroy() resource.TestCheckFunc {
147+
return func(s *terraform.State) error {
148+
client := testAccProvider.Meta().(*Client)
149+
for _, r := range s.RootModule().Resources {
150+
id := r.Primary.ID
151+
p, err := client.getDataForwarding(id)
54152

55-
resource "aws_iam_role" "sumo_role" {
56-
name = "SumoRole"
57-
assume_role_policy = jsonencode({
58-
"Version": "2012-10-17",
59-
"Statement": [
60-
{
61-
"Effect": "Allow",
62-
"Principal": {
63-
"AWS": "arn:aws:iam::246946804217:root"
64-
},
65-
"Action": "sts:AssumeRole",
66-
"Condition": {
67-
"StringEquals": {
68-
"sts:ExternalId": "long:0000000000000005"
69-
}
70-
}
71-
}
72-
]
73-
})
74-
path = "/"
75-
}
153+
if err != nil {
154+
if strings.Contains(err.Error(), "Data forwarding Destination doesn't exists") {
155+
continue
156+
}
76157

77-
resource "aws_iam_policy" "sumo_policy" {
78-
name = "SumoPolicy"
79-
policy = jsonencode({
80-
"Version": "2012-10-17",
81-
"Statement": [
82-
{
83-
"Effect": "Allow",
84-
"Action": ["s3:PutObject"],
85-
"Resource": ["arn:aws:s3:::${aws_s3_bucket.test_bucket.bucket}/*"]
86-
}
87-
]
88-
})
89-
}
158+
return fmt.Errorf("Encountered an error: " + err.Error())
159+
}
90160

91-
resource "aws_iam_role_policy_attachment" "sumo_policy_attach" {
92-
role = aws_iam_role.sumo_role.name
93-
policy_arn = aws_iam_policy.sumo_policy.arn
161+
if p != nil {
162+
return fmt.Errorf("Data Forwarding still exists")
163+
}
164+
}
165+
return nil
166+
}
94167
}
95168

96-
output "sumo_role_arn" {
97-
description = "ARN of the created role. Copy this ARN back to Sumo to complete the data forwarding destination creation process."
98-
value = aws_iam_role.sumo_role.arn
169+
func testAccSumologicDataForwardingCreateConfig(destinationName string, description string, testAwsBucket string, testAwsRoleArn string, region string) string {
170+
return fmt.Sprintf(`
171+
resource "sumologic_data_forwarding" "test" {
172+
destination_name = "%s"
173+
description = "%s"
174+
bucket_name = "%s"
175+
authentication {
176+
type = "RoleBased"
177+
role_arn = "%s"
178+
}
179+
s3_region = "%s"
180+
s3_server_side_encryption = false
181+
182+
}
183+
`, destinationName, description, testAwsBucket, testAwsRoleArn, region)
99184
}
100185

186+
func testAccSumologicDataForwardingUpdateConfig(destinationName string, description string, testAwsBucket string, testAwsRoleArn string, region string) string {
187+
return fmt.Sprintf(`
101188
resource "sumologic_data_forwarding" "test" {
102-
destination_name = "abc"
103-
bucket_name = "${aws_s3_bucket.test_bucket.bucket}"
104-
authentication {
105-
type = "RoleBased"
106-
role_arn = "${aws_iam_role_policy_attachment.sumo_policy_attach.policy_arn}"
107-
}
108-
destination_type = "temp"
189+
destination_name = "%s"
190+
description = "%s"
191+
bucket_name = "%s"
192+
authentication {
193+
type = "RoleBased"
194+
role_arn = "%s"
195+
}
196+
s3_region = "%s"
197+
s3_server_side_encryption = true
198+
199+
}
200+
`, destinationName, description, testAwsBucket, testAwsRoleArn, region)
109201
}
110-
`)
202+
203+
func testAccSumologicDataForwardingDeleteConfig() string {
204+
return fmt.Sprintf(` `)
111205
}

sumologic/sumologic_data_forwarding.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,12 @@ func (s *Client) DeleteDataForwarding(id string) error {
6161
type DataForwarding struct {
6262
ID string `json:"id,omitempty"`
6363
DestinationName string `json:"destinationName"`
64-
Description string `json:"description"`
65-
DestinationType string `json:"destinationType"`
64+
Description string `json:"description,omitempty"`
6665
BucketName string `json:"bucketName"`
67-
Status string `json:"status"`
6866
AccessMethod string `json:"authenticationMode"`
69-
AccessKey string `json:"accessKeyId"`
70-
SecretKey string `json:"secretAccessKey"`
71-
RoleArn string `json:"roleArn"`
72-
S3Region string `json:"region"`
67+
AccessKey string `json:"accessKeyId,omitempty"`
68+
SecretKey string `json:"secretAccessKey,omitempty"`
69+
RoleArn string `json:"roleArn,omitempty"`
70+
S3Region string `json:"region,omitempty"`
7371
S3ServerSideEncryption bool `json:"encrypted"`
7472
}

0 commit comments

Comments
 (0)