@@ -2,110 +2,204 @@ package sumologic
22
33import (
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
1628func 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+
36133func 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 (`
101188resource "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}
0 commit comments