-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathincident_role_resource_test.go
246 lines (215 loc) · 8.81 KB
/
incident_role_resource_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package provider
import (
"context"
"fmt"
"os"
"testing"
"github.com/firehydrant/terraform-provider-firehydrant/firehydrant"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)
func TestAccIncidentRoleResource_basic(t *testing.T) {
rName := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
resource.Test(t, resource.TestCase{
PreCheck: func() { testFireHydrantIsSetup(t) },
ProviderFactories: defaultProviderFactories(),
CheckDestroy: testAccCheckIncidentRoleResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccIncidentRoleResourceConfig_basic(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckIncidentRoleResourceExistsWithAttributes_basic("firehydrant_incident_role.test_incident_role"),
resource.TestCheckResourceAttrSet("firehydrant_incident_role.test_incident_role", "id"),
resource.TestCheckResourceAttr(
"firehydrant_incident_role.test_incident_role", "name", fmt.Sprintf("test-incident-role-%s", rName)),
resource.TestCheckResourceAttr(
"firehydrant_incident_role.test_incident_role", "summary", fmt.Sprintf("test-summary-%s", rName)),
),
},
},
})
}
func TestAccIncidentRoleResource_update(t *testing.T) {
rName := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
rNameUpdated := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
resource.Test(t, resource.TestCase{
PreCheck: func() { testFireHydrantIsSetup(t) },
ProviderFactories: defaultProviderFactories(),
CheckDestroy: testAccCheckIncidentRoleResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccIncidentRoleResourceConfig_basic(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckIncidentRoleResourceExistsWithAttributes_basic("firehydrant_incident_role.test_incident_role"),
resource.TestCheckResourceAttrSet("firehydrant_incident_role.test_incident_role", "id"),
resource.TestCheckResourceAttr(
"firehydrant_incident_role.test_incident_role", "name", fmt.Sprintf("test-incident-role-%s", rName)),
resource.TestCheckResourceAttr(
"firehydrant_incident_role.test_incident_role", "summary", fmt.Sprintf("test-summary-%s", rName)),
),
},
{
Config: testAccIncidentRoleResourceConfig_update(rNameUpdated),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckIncidentRoleResourceExistsWithAttributes_update("firehydrant_incident_role.test_incident_role"),
resource.TestCheckResourceAttrSet("firehydrant_incident_role.test_incident_role", "id"),
resource.TestCheckResourceAttr(
"firehydrant_incident_role.test_incident_role", "name", fmt.Sprintf("test-incident-role-%s", rNameUpdated)),
resource.TestCheckResourceAttr(
"firehydrant_incident_role.test_incident_role", "summary", fmt.Sprintf("test-summary-%s", rNameUpdated)),
resource.TestCheckResourceAttr(
"firehydrant_incident_role.test_incident_role", "description", fmt.Sprintf("test-description-%s", rNameUpdated)),
),
},
{
Config: testAccIncidentRoleResourceConfig_basic(rNameUpdated),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckIncidentRoleResourceExistsWithAttributes_basic("firehydrant_incident_role.test_incident_role"),
resource.TestCheckResourceAttrSet("firehydrant_incident_role.test_incident_role", "id"),
resource.TestCheckResourceAttr(
"firehydrant_incident_role.test_incident_role", "name", fmt.Sprintf("test-incident-role-%s", rNameUpdated)),
resource.TestCheckResourceAttr(
"firehydrant_incident_role.test_incident_role", "summary", fmt.Sprintf("test-summary-%s", rNameUpdated)),
),
},
},
})
}
func TestAccIncidentRoleResourceImport_basic(t *testing.T) {
rName := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
resource.Test(t, resource.TestCase{
PreCheck: func() { testFireHydrantIsSetup(t) },
ProviderFactories: defaultProviderFactories(),
Steps: []resource.TestStep{
{
Config: testAccIncidentRoleResourceConfig_basic(rName),
},
{
ResourceName: "firehydrant_incident_role.test_incident_role",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
func TestAccIncidentRoleResourceImport_allAttributes(t *testing.T) {
rName := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
resource.Test(t, resource.TestCase{
PreCheck: func() { testFireHydrantIsSetup(t) },
ProviderFactories: defaultProviderFactories(),
Steps: []resource.TestStep{
{
Config: testAccIncidentRoleResourceConfig_update(rName),
},
{
ResourceName: "firehydrant_incident_role.test_incident_role",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
func testAccCheckIncidentRoleResourceExistsWithAttributes_basic(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
incidentRoleResource, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Not found: %s", resourceName)
}
if incidentRoleResource.Primary.ID == "" {
return fmt.Errorf("No ID is set")
}
client, err := firehydrant.NewRestClient(os.Getenv("FIREHYDRANT_API_KEY"))
if err != nil {
return err
}
incidentRoleResponse, err := client.IncidentRoles().Get(context.TODO(), incidentRoleResource.Primary.ID)
if err != nil {
return err
}
expected, got := incidentRoleResource.Primary.Attributes["name"], incidentRoleResponse.Name
if expected != got {
return fmt.Errorf("Unexpected name. Expected: %s, got: %s", expected, got)
}
expected, got = incidentRoleResource.Primary.Attributes["summary"], incidentRoleResponse.Summary
if expected != got {
return fmt.Errorf("Unexpected summary. Expected: %s, got: %s", expected, got)
}
if incidentRoleResponse.Description != "" {
return fmt.Errorf("Unexpected description. Expected no description, got: %s", incidentRoleResponse.Description)
}
return nil
}
}
func testAccCheckIncidentRoleResourceExistsWithAttributes_update(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
incidentRoleResource, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Not found: %s", resourceName)
}
if incidentRoleResource.Primary.ID == "" {
return fmt.Errorf("No ID is set")
}
client, err := firehydrant.NewRestClient(os.Getenv("FIREHYDRANT_API_KEY"))
if err != nil {
return err
}
incidentRoleResponse, err := client.IncidentRoles().Get(context.TODO(), incidentRoleResource.Primary.ID)
if err != nil {
return err
}
expected, got := incidentRoleResource.Primary.Attributes["name"], incidentRoleResponse.Name
if expected != got {
return fmt.Errorf("Unexpected name. Expected: %s, got: %s", expected, got)
}
expected, got = incidentRoleResource.Primary.Attributes["summary"], incidentRoleResponse.Summary
if expected != got {
return fmt.Errorf("Unexpected summary. Expected: %s, got: %s", expected, got)
}
expected, got = incidentRoleResource.Primary.Attributes["description"], incidentRoleResponse.Description
if expected != got {
return fmt.Errorf("Unexpected description. Expected: %s, got: %s", expected, got)
}
return nil
}
}
func testAccCheckIncidentRoleResourceDestroy() resource.TestCheckFunc {
return func(s *terraform.State) error {
client, err := firehydrant.NewRestClient(os.Getenv("FIREHYDRANT_API_KEY"))
if err != nil {
return err
}
for _, stateResource := range s.RootModule().Resources {
if stateResource.Type != "firehydrant_incident_role" {
continue
}
if stateResource.Primary.ID == "" {
return fmt.Errorf("No instance ID is set")
}
// Normally we'd check if err == nil here, because we'd expect a 404 if we try to get a resource
// that has been deleted. However, the incident role API will still return deleted/archived incident
// roles instead of returning 404. So, to check for incident roles that are deleted, we have to check
// for incident roles that have a DiscardedAt timestamp
incidentRoleResponse, _ := client.IncidentRoles().Get(context.TODO(), stateResource.Primary.ID)
if incidentRoleResponse.DiscardedAt.IsZero() {
return fmt.Errorf("Incident role %s still exists", stateResource.Primary.ID)
}
}
return nil
}
}
func testAccIncidentRoleResourceConfig_basic(rName string) string {
return fmt.Sprintf(`
resource "firehydrant_incident_role" "test_incident_role" {
name = "test-incident-role-%s"
summary = "test-summary-%s"
}`, rName, rName)
}
func testAccIncidentRoleResourceConfig_update(rName string) string {
return fmt.Sprintf(`
resource "firehydrant_incident_role" "test_incident_role" {
name = "test-incident-role-%s"
summary = "test-summary-%s"
description = "test-description-%s"
}`, rName, rName, rName)
}