Skip to content

Commit 8248d74

Browse files
authored
Merge pull request #114 from SumoLogic/emichaeli-add-subdomain-resource
Add Subdomain resource
2 parents e09570f + b48281d commit 8248d74

File tree

6 files changed

+375
-0
lines changed

6 files changed

+375
-0
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 2.6.0 (November 23, 2020)
2+
3+
FEATURES:
4+
5+
* **New Resource:** sumologic_subdomain (GH-114)
6+
17
## 2.5.0 (November 13, 2020)
28

39
FEATURES:

sumologic/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ func Provider() terraform.ResourceProvider {
6767
"sumologic_ingest_budget_v2": resourceSumologicIngestBudgetV2(),
6868
"sumologic_field": resourceSumologicField(),
6969
"sumologic_lookup_table": resourceSumologicLookupTable(),
70+
"sumologic_subdomain": resourceSumologicSubdomain(),
7071
},
7172
DataSourcesMap: map[string]*schema.Resource{
7273
"sumologic_caller_identity": dataSourceSumologicCallerIdentity(),
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// ----------------------------------------------------------------------------
2+
//
3+
// *** AUTO GENERATED CODE *** AUTO GENERATED CODE ***
4+
//
5+
// ----------------------------------------------------------------------------
6+
//
7+
// This file is automatically generated by Sumo Logic and manual
8+
// changes will be clobbered when the file is regenerated. Do not submit
9+
// changes to this file.
10+
//
11+
// ----------------------------------------------------------------------------
12+
package sumologic
13+
14+
import (
15+
"log"
16+
17+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
18+
)
19+
20+
func resourceSumologicSubdomain() *schema.Resource {
21+
return &schema.Resource{
22+
Create: resourceSumologicSubdomainCreate,
23+
Read: resourceSumologicSubdomainRead,
24+
Update: resourceSumologicSubdomainUpdate,
25+
Delete: resourceSumologicSubdomainDelete,
26+
Importer: &schema.ResourceImporter{
27+
State: schema.ImportStatePassthrough,
28+
},
29+
30+
Schema: map[string]*schema.Schema{
31+
"subdomain": {
32+
Type: schema.TypeString,
33+
Required: true,
34+
ForceNew: false,
35+
},
36+
},
37+
}
38+
}
39+
40+
func resourceSumologicSubdomainRead(d *schema.ResourceData, meta interface{}) error {
41+
c := meta.(*Client)
42+
43+
id := d.Id()
44+
subdomain, err := c.GetSubdomain()
45+
46+
if err != nil {
47+
return err
48+
}
49+
50+
if subdomain == nil {
51+
log.Printf("[WARN] Subdomain not found, removing from state: %v - %v", id, err)
52+
d.SetId("")
53+
return nil
54+
}
55+
56+
d.Set("subdomain", subdomain.Subdomain)
57+
58+
return nil
59+
}
60+
61+
func resourceSumologicSubdomainCreate(d *schema.ResourceData, meta interface{}) error {
62+
c := meta.(*Client)
63+
64+
if d.Id() == "" {
65+
subdomain := resourceToSubdomain(d)
66+
id, err := c.CreateSubdomain(subdomain)
67+
68+
if err != nil {
69+
return err
70+
}
71+
72+
d.SetId(id)
73+
}
74+
75+
return resourceSumologicSubdomainRead(d, meta)
76+
}
77+
78+
func resourceSumologicSubdomainDelete(d *schema.ResourceData, meta interface{}) error {
79+
c := meta.(*Client)
80+
81+
return c.DeleteSubdomain()
82+
}
83+
84+
func resourceSumologicSubdomainUpdate(d *schema.ResourceData, meta interface{}) error {
85+
c := meta.(*Client)
86+
87+
subdomain := resourceToSubdomain(d)
88+
89+
err := c.UpdateSubdomain(subdomain)
90+
if err != nil {
91+
return err
92+
}
93+
94+
return resourceSumologicSubdomainRead(d, meta)
95+
}
96+
97+
func resourceToSubdomain(d *schema.ResourceData) Subdomain {
98+
return Subdomain{
99+
Subdomain: d.Get("subdomain").(string),
100+
}
101+
}
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
// ----------------------------------------------------------------------------
2+
//
3+
// *** AUTO GENERATED CODE *** AUTO GENERATED CODE ***
4+
//
5+
// ----------------------------------------------------------------------------
6+
//
7+
// This file is automatically generated by Sumo Logic and manual
8+
// changes will be clobbered when the file is regenerated. Do not submit
9+
// changes to this file.
10+
//
11+
// ----------------------------------------------------------------------------
12+
package sumologic
13+
14+
import (
15+
"fmt"
16+
"strconv"
17+
"strings"
18+
"testing"
19+
20+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
21+
"github.com/hashicorp/terraform-plugin-sdk/terraform"
22+
)
23+
24+
func TestAccSumologicSubdomain_basic(t *testing.T) {
25+
var subdomain Subdomain
26+
testSubdomain := "my-company"
27+
28+
resource.Test(t, resource.TestCase{
29+
PreCheck: func() { testAccPreCheck(t) },
30+
Providers: testAccProviders,
31+
CheckDestroy: testAccCheckSubdomainDestroy(subdomain),
32+
Steps: []resource.TestStep{
33+
{
34+
Config: testAccCheckSumologicSubdomainConfigImported(testSubdomain),
35+
},
36+
{
37+
ResourceName: "sumologic_subdomain.foo",
38+
ImportState: true,
39+
ImportStateVerify: true,
40+
},
41+
},
42+
})
43+
}
44+
45+
func TestAccSubdomain_create(t *testing.T) {
46+
var subdomain Subdomain
47+
testSubdomain := "my-company"
48+
resource.Test(t, resource.TestCase{
49+
PreCheck: func() { testAccPreCheck(t) },
50+
Providers: testAccProviders,
51+
CheckDestroy: testAccCheckSubdomainDestroy(subdomain),
52+
Steps: []resource.TestStep{
53+
{
54+
Config: testAccSumologicSubdomain(testSubdomain),
55+
Check: resource.ComposeTestCheckFunc(
56+
testAccCheckSubdomainExists("sumologic_subdomain.test", &subdomain, t),
57+
testAccCheckSubdomainAttributes("sumologic_subdomain.test"),
58+
resource.TestCheckResourceAttr("sumologic_subdomain.test", "subdomain", testSubdomain),
59+
),
60+
},
61+
},
62+
})
63+
}
64+
65+
func TestAccSubdomain_update(t *testing.T) {
66+
var subdomain Subdomain
67+
testSubdomain := "my-company"
68+
69+
testUpdatedSubdomain := "my-new-company"
70+
71+
resource.Test(t, resource.TestCase{
72+
PreCheck: func() { testAccPreCheck(t) },
73+
Providers: testAccProviders,
74+
CheckDestroy: testAccCheckSubdomainDestroy(subdomain),
75+
Steps: []resource.TestStep{
76+
{
77+
Config: testAccSumologicSubdomain(testSubdomain),
78+
Check: resource.ComposeTestCheckFunc(
79+
testAccCheckSubdomainExists("sumologic_subdomain.test", &subdomain, t),
80+
testAccCheckSubdomainAttributes("sumologic_subdomain.test"),
81+
resource.TestCheckResourceAttr("sumologic_subdomain.test", "subdomain", testSubdomain),
82+
),
83+
},
84+
{
85+
Config: testAccSumologicSubdomainUpdate(testUpdatedSubdomain),
86+
Check: resource.ComposeTestCheckFunc(
87+
resource.TestCheckResourceAttr("sumologic_subdomain.test", "subdomain", testUpdatedSubdomain),
88+
),
89+
},
90+
},
91+
})
92+
}
93+
94+
func testAccCheckSubdomainDestroy(subdomain Subdomain) resource.TestCheckFunc {
95+
return func(s *terraform.State) error {
96+
client := testAccProvider.Meta().(*Client)
97+
for _, r := range s.RootModule().Resources {
98+
id := r.Primary.ID
99+
u, err := client.GetSubdomain()
100+
if err != nil {
101+
return fmt.Errorf("Encountered an error: " + err.Error())
102+
}
103+
if u != nil {
104+
return fmt.Errorf("Subdomain %s still exists", id)
105+
}
106+
}
107+
return nil
108+
}
109+
}
110+
111+
func testAccCheckSubdomainExists(name string, subdomain *Subdomain, t *testing.T) resource.TestCheckFunc {
112+
return func(s *terraform.State) error {
113+
rs, ok := s.RootModule().Resources[name]
114+
if !ok {
115+
//need this so that we don't get an unused import error for strconv in some cases
116+
return fmt.Errorf("Error = %s. Subdomain not found: %s", strconv.FormatBool(ok), name)
117+
}
118+
119+
//need this so that we don't get an unused import error for strings in some cases
120+
if strings.EqualFold(rs.Primary.ID, "") {
121+
return fmt.Errorf("Subdomain ID is not set")
122+
}
123+
124+
id := rs.Primary.ID
125+
client := testAccProvider.Meta().(*Client)
126+
newSubdomain, err := client.GetSubdomain()
127+
if err != nil {
128+
return fmt.Errorf("Subdomain %s not found", id)
129+
}
130+
subdomain = newSubdomain
131+
return nil
132+
}
133+
}
134+
135+
func testAccCheckSumologicSubdomainConfigImported(subdomain string) string {
136+
return fmt.Sprintf(`
137+
resource "sumologic_subdomain" "foo" {
138+
subdomain = "%s"
139+
}
140+
`, subdomain)
141+
}
142+
143+
func testAccSumologicSubdomain(subdomain string) string {
144+
return fmt.Sprintf(`
145+
resource "sumologic_subdomain" "test" {
146+
subdomain = "%s"
147+
}
148+
`, subdomain)
149+
}
150+
151+
func testAccSumologicSubdomainUpdate(subdomain string) string {
152+
return fmt.Sprintf(`
153+
resource "sumologic_subdomain" "test" {
154+
subdomain = "%s"
155+
}
156+
`, subdomain)
157+
}
158+
159+
func testAccCheckSubdomainAttributes(name string) resource.TestCheckFunc {
160+
return func(s *terraform.State) error {
161+
f := resource.ComposeTestCheckFunc(
162+
resource.TestCheckResourceAttrSet(name, "subdomain"),
163+
)
164+
return f(s)
165+
}
166+
}

sumologic/sumologic_subdomain.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// ----------------------------------------------------------------------------
2+
//
3+
// *** AUTO GENERATED CODE *** AUTO GENERATED CODE ***
4+
//
5+
// ----------------------------------------------------------------------------
6+
//
7+
// This file is automatically generated by Sumo Logic and manual
8+
// changes will be clobbered when the file is regenerated. Do not submit
9+
// changes to this file.
10+
//
11+
// ----------------------------------------------------------------------------
12+
package sumologic
13+
14+
import (
15+
"encoding/json"
16+
)
17+
18+
func (s *Client) GetSubdomain() (*Subdomain, error) {
19+
urlWithoutParams := "v1/account/subdomain"
20+
21+
data, _, err := s.Get(urlWithoutParams)
22+
if err != nil {
23+
return nil, err
24+
}
25+
if data == nil {
26+
return nil, nil
27+
}
28+
29+
var subdomain Subdomain
30+
err = json.Unmarshal(data, &subdomain)
31+
if err != nil {
32+
return nil, err
33+
}
34+
35+
return &subdomain, nil
36+
}
37+
38+
func (s *Client) CreateSubdomain(subdomain Subdomain) (string, error) {
39+
urlWithoutParams := "v1/account/subdomain"
40+
41+
data, err := s.Post(urlWithoutParams, subdomain)
42+
if err != nil {
43+
return "", err
44+
}
45+
46+
var createdSubdomain Subdomain
47+
err = json.Unmarshal(data, &createdSubdomain)
48+
if err != nil {
49+
return "", err
50+
}
51+
52+
return createdSubdomain.Subdomain, nil
53+
}
54+
55+
func (s *Client) DeleteSubdomain() error {
56+
urlWithoutParams := "v1/account/subdomain"
57+
58+
_, err := s.Delete(urlWithoutParams)
59+
return err
60+
}
61+
62+
func (s *Client) UpdateSubdomain(subdomain Subdomain) error {
63+
urlWithoutParams := "v1/account/subdomain"
64+
65+
_, err := s.Put(urlWithoutParams, subdomain)
66+
return err
67+
}
68+
69+
type Subdomain struct {
70+
Subdomain string `json:"subdomain"`
71+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
layout: "sumologic"
3+
page_title: "SumoLogic: sumologic_subdomain"
4+
description: |-
5+
Provides a Sumologic Subdomain
6+
---
7+
8+
# sumologic_lookup_table
9+
Provides a [Sumologic Subdomain][1].
10+
11+
## Example Usage
12+
```hcl
13+
resource "sumologic_subdomain" "exampleSubdomain" {
14+
subdomain = "my-company"
15+
}
16+
```
17+
18+
## Argument reference
19+
20+
The following arguments are supported:
21+
22+
- `subdomain` - (Required) The subdomain.
23+
24+
## Attributes reference
25+
26+
The following attributes are exported:
27+
28+
- `id` - Unique identifier for the subdomain.
29+
30+
[1]: https://help.sumologic.com/Manage/01Account_Usage/05Manage_Organization#change-account-subdomain

0 commit comments

Comments
 (0)