Skip to content

Commit d509020

Browse files
authored
Merge pull request #203 from SumoLogic/yuting-add-token-terraform
Add Terraform support for token
2 parents 8dab0ed + ab7df64 commit d509020

File tree

6 files changed

+448
-0
lines changed

6 files changed

+448
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## 2.9.4 (Unreleased)
22

3+
FEATURES:
4+
5+
* **New Resource:** sumologic_token (GH-203)
6+
37
## 2.9.3 (April 26, 2021)
48

59
FEATURES:

sumologic/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ func Provider() terraform.ResourceProvider {
7474
"sumologic_password_policy": resourceSumologicPasswordPolicy(),
7575
"sumologic_saml_configuration": resourceSumologicSamlConfiguration(),
7676
"sumologic_kinesis_metrics_source": resourceSumologicKinesisMetricsSource(),
77+
"sumologic_token": resourceSumologicToken(),
7778
},
7879
DataSourcesMap: map[string]*schema.Resource{
7980
"sumologic_caller_identity": dataSourceSumologicCallerIdentity(),
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package sumologic
2+
3+
import (
4+
"log"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
8+
)
9+
10+
func resourceSumologicToken() *schema.Resource {
11+
return &schema.Resource{
12+
Create: resourceSumologicTokenCreate,
13+
Read: resourceSumologicTokenRead,
14+
Update: resourceSumologicTokenUpdate,
15+
Delete: resourceSumologicTokenDelete,
16+
Importer: &schema.ResourceImporter{
17+
State: schema.ImportStatePassthrough,
18+
},
19+
20+
Schema: map[string]*schema.Schema{
21+
"version": {
22+
Type: schema.TypeInt,
23+
Optional: true,
24+
Computed: true,
25+
},
26+
27+
"status": {
28+
Type: schema.TypeString,
29+
Required: true,
30+
},
31+
32+
"name": {
33+
Type: schema.TypeString,
34+
Required: true,
35+
},
36+
37+
"description": {
38+
Type: schema.TypeString,
39+
Optional: true,
40+
},
41+
42+
"type": {
43+
Type: schema.TypeString,
44+
Required: true,
45+
ValidateFunc: validation.StringInSlice([]string{"CollectorRegistration", "CollectorRegistrationTokenResponse"}, false),
46+
},
47+
},
48+
}
49+
}
50+
51+
func resourceSumologicTokenCreate(d *schema.ResourceData, meta interface{}) error {
52+
c := meta.(*Client)
53+
54+
if d.Id() == "" {
55+
token := resourceToToken(d)
56+
id, err := c.CreateToken(token)
57+
if err != nil {
58+
return err
59+
}
60+
61+
d.SetId(id)
62+
}
63+
64+
return resourceSumologicTokenRead(d, meta)
65+
}
66+
67+
func resourceSumologicTokenRead(d *schema.ResourceData, meta interface{}) error {
68+
c := meta.(*Client)
69+
70+
id := d.Id()
71+
token, err := c.GetToken(id)
72+
if err != nil {
73+
return err
74+
}
75+
76+
if token == nil {
77+
log.Printf("[WARN] Token not found, removing from state: %v - %v", id, err)
78+
d.SetId("")
79+
return nil
80+
}
81+
82+
d.Set("name", token.Name)
83+
d.Set("status", token.Status)
84+
d.Set("description", token.Description)
85+
d.Set("version", token.Version)
86+
87+
return nil
88+
}
89+
90+
func resourceSumologicTokenDelete(d *schema.ResourceData, meta interface{}) error {
91+
c := meta.(*Client)
92+
93+
return c.DeleteToken(d.Id())
94+
}
95+
96+
func resourceSumologicTokenUpdate(d *schema.ResourceData, meta interface{}) error {
97+
c := meta.(*Client)
98+
99+
token := resourceToToken(d)
100+
err := c.UpdateToken(token)
101+
if err != nil {
102+
return err
103+
}
104+
105+
return resourceSumologicTokenRead(d, meta)
106+
}
107+
108+
func resourceToToken(d *schema.ResourceData) Token {
109+
110+
return Token{
111+
Name: d.Get("name").(string),
112+
ID: d.Id(),
113+
Description: d.Get("description").(string),
114+
Version: d.Get("version").(int),
115+
Type: d.Get("type").(string),
116+
Status: d.Get("status").(string),
117+
}
118+
}
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
package sumologic
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
"strings"
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
10+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
11+
"github.com/hashicorp/terraform-plugin-sdk/terraform"
12+
)
13+
14+
func getRandomizedParamsForToken() (string, string, string, string) {
15+
name := acctest.RandomWithPrefix("tf-acc-test")
16+
description := acctest.RandomWithPrefix("tf-acc-test")
17+
status := "Active"
18+
tokenType := "CollectorRegistration"
19+
return name, description, status, tokenType
20+
}
21+
22+
func TestAccSumologicToken_basic(t *testing.T) {
23+
var token Token
24+
25+
testName, testDescription, testStatus, _ := getRandomizedParamsForToken()
26+
27+
resource.Test(t, resource.TestCase{
28+
PreCheck: func() { testAccPreCheck(t) },
29+
Providers: testAccProviders,
30+
CheckDestroy: testAccCheckTokenDestroy(token),
31+
Steps: []resource.TestStep{
32+
{
33+
Config: testAccCheckSumologicTokenConfigImported(testName, testDescription, testStatus),
34+
},
35+
{
36+
ResourceName: "sumologic_token.foo",
37+
ImportState: true,
38+
ImportStateVerify: false,
39+
},
40+
},
41+
})
42+
}
43+
44+
func TestAccSumologicToken_create(t *testing.T) {
45+
var token Token
46+
testName, testDescription, testStatus, testType := getRandomizedParamsForToken()
47+
48+
resource.Test(t, resource.TestCase{
49+
PreCheck: func() { testAccPreCheck(t) },
50+
Providers: testAccProviders,
51+
CheckDestroy: testAccCheckTokenDestroy(token),
52+
Steps: []resource.TestStep{
53+
{
54+
Config: testAccSumologicToken(testName, testDescription, testStatus),
55+
Check: resource.ComposeTestCheckFunc(
56+
testAccCheckTokenExists("sumologic_token.test", &token, t),
57+
testAccCheckTokenAttributes("sumologic_token.test"),
58+
resource.TestCheckResourceAttr("sumologic_token.test", "name", testName),
59+
resource.TestCheckResourceAttr("sumologic_token.test", "description", testDescription),
60+
resource.TestCheckResourceAttr("sumologic_token.test", "status", testStatus),
61+
resource.TestCheckResourceAttr("sumologic_token.test", "type", testType),
62+
),
63+
},
64+
},
65+
})
66+
}
67+
68+
func testAccCheckTokenDestroy(token Token) resource.TestCheckFunc {
69+
return func(s *terraform.State) error {
70+
client := testAccProvider.Meta().(*Client)
71+
for _, r := range s.RootModule().Resources {
72+
id := r.Primary.ID
73+
u, err := client.GetToken(id)
74+
if err != nil {
75+
return fmt.Errorf("Encountered an error: " + err.Error())
76+
}
77+
if u != nil {
78+
return fmt.Errorf("Token %s still exists", id)
79+
}
80+
}
81+
return nil
82+
}
83+
}
84+
85+
func testAccCheckTokenExists(name string, token *Token, t *testing.T) resource.TestCheckFunc {
86+
return func(s *terraform.State) error {
87+
rs, ok := s.RootModule().Resources[name]
88+
if !ok {
89+
//need this so that we don't get an unused import error for strconv in some cases
90+
return fmt.Errorf("Error = %s. Token not found: %s", strconv.FormatBool(ok), name)
91+
}
92+
93+
//need this so that we don't get an unused import error for strings in some cases
94+
if strings.EqualFold(rs.Primary.ID, "") {
95+
return fmt.Errorf("Token ID is not set")
96+
}
97+
98+
id := rs.Primary.ID
99+
c := testAccProvider.Meta().(*Client)
100+
newToken, err := c.GetToken(id)
101+
if err != nil {
102+
return fmt.Errorf("Token %s not found", id)
103+
}
104+
token = newToken
105+
return nil
106+
}
107+
}
108+
109+
func TestAccSumologicToken_update(t *testing.T) {
110+
var token Token
111+
testName, testDescription, testStatus, testType := getRandomizedParamsForToken()
112+
113+
testUpdatedName, testUpdatedDescription, _, _ := getRandomizedParamsForToken()
114+
testUpdatedStatus := "Inactive"
115+
116+
resource.Test(t, resource.TestCase{
117+
PreCheck: func() { testAccPreCheck(t) },
118+
Providers: testAccProviders,
119+
CheckDestroy: testAccCheckTokenDestroy(token),
120+
Steps: []resource.TestStep{
121+
{
122+
Config: testAccSumologicToken(testName, testDescription, testStatus),
123+
Check: resource.ComposeTestCheckFunc(
124+
testAccCheckTokenExists("sumologic_token.test", &token, t),
125+
testAccCheckTokenAttributes("sumologic_token.test"),
126+
resource.TestCheckResourceAttr("sumologic_token.test", "name", testName),
127+
resource.TestCheckResourceAttr("sumologic_token.test", "description", testDescription),
128+
resource.TestCheckResourceAttr("sumologic_token.test", "status", testStatus),
129+
resource.TestCheckResourceAttr("sumologic_token.test", "type", testType),
130+
),
131+
},
132+
{
133+
Config: testAccSumologicTokenUpdate(testUpdatedName, testUpdatedDescription, testUpdatedStatus),
134+
Check: resource.ComposeTestCheckFunc(
135+
testAccCheckTokenExists("sumologic_token.test", &token, t),
136+
testAccCheckTokenAttributes("sumologic_token.test"),
137+
resource.TestCheckResourceAttr("sumologic_token.test", "name", testUpdatedName),
138+
resource.TestCheckResourceAttr("sumologic_token.test", "description", testUpdatedDescription),
139+
resource.TestCheckResourceAttr("sumologic_token.test", "status", testUpdatedStatus),
140+
resource.TestCheckResourceAttr("sumologic_token.test", "type", testType),
141+
),
142+
},
143+
},
144+
})
145+
}
146+
147+
func testAccCheckSumologicTokenConfigImported(name string, description string, status string) string {
148+
return fmt.Sprintf(`
149+
resource "sumologic_token" "foo" {
150+
name = "%s"
151+
description = "%s"
152+
status = "%s"
153+
type = "CollectorRegistration"
154+
}
155+
`, name, description, status)
156+
}
157+
158+
func testAccSumologicToken(name string, description string, status string) string {
159+
return fmt.Sprintf(`
160+
resource "sumologic_token" "test" {
161+
name = "%s"
162+
description = "%s"
163+
status = "%s"
164+
type = "CollectorRegistration"
165+
}
166+
`, name, description, status)
167+
}
168+
169+
func testAccSumologicTokenUpdate(name string, description string, status string) string {
170+
return fmt.Sprintf(`
171+
resource "sumologic_token" "test" {
172+
name = "%s"
173+
description = "%s"
174+
status = "%s"
175+
type = "CollectorRegistration"
176+
}
177+
`, name, description, status)
178+
}
179+
180+
func testAccCheckTokenAttributes(name string) resource.TestCheckFunc {
181+
return func(s *terraform.State) error {
182+
f := resource.ComposeTestCheckFunc(
183+
resource.TestCheckResourceAttrSet(name, "name"),
184+
resource.TestCheckResourceAttrSet(name, "description"),
185+
resource.TestCheckResourceAttrSet(name, "status"),
186+
resource.TestCheckResourceAttrSet(name, "type"),
187+
resource.TestCheckResourceAttrSet(name, "version"),
188+
)
189+
return f(s)
190+
}
191+
}

0 commit comments

Comments
 (0)