forked from nutanix/terraform-provider-nutanix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_nutanix_category_key_test.go
132 lines (118 loc) · 3.39 KB
/
resource_nutanix_category_key_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
package nutanix
import (
"fmt"
"strings"
"testing"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)
func TestAccNutanixCategoryKey_basic(t *testing.T) {
resourceName := "nutanix_category_key.test"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckNutanixCategoryKeyDestroy,
Steps: []resource.TestStep{
{
Config: testAccNutanixCategoryKeyConfig(acctest.RandIntRange(0, 500)),
Check: resource.ComposeTestCheckFunc(
testAccCheckNutanixCategoryKeyExists(resourceName),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
func TestAccNutanixCategoryKey_update(t *testing.T) {
resourceName := "nutanix_category_key.test_update"
rInt := acctest.RandIntRange(0, 500)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckNutanixCategoryKeyDestroy,
Steps: []resource.TestStep{
{
Config: testAccNutanixCategoryKeyConfigToUpdate(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckNutanixCategoryKeyExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "name", fmt.Sprintf("app-support-%d", rInt)),
resource.TestCheckResourceAttr(resourceName, "description", "App Support CategoryKey"),
),
},
{
Config: testAccNutanixCategoryKeyConfigUpdated(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckNutanixCategoryKeyExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "name", fmt.Sprintf("app-support-%d-updated", rInt)),
resource.TestCheckResourceAttr(resourceName, "description", "App Support CategoryKey Updated"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
func testAccCheckNutanixCategoryKeyExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("no ID is set")
}
return nil
}
}
func testAccCheckNutanixCategoryKeyDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*Client)
for _, rs := range s.RootModule().Resources {
if rs.Type != "nutanix_category_key" {
continue
}
for {
_, err := conn.API.V3.GetCategoryKey(rs.Primary.ID)
if err != nil {
if strings.Contains(fmt.Sprint(err), "ENTITY_NOT_FOUND") {
return nil
}
return err
}
time.Sleep(3000 * time.Millisecond)
}
}
return nil
}
func testAccNutanixCategoryKeyConfig(r int) string {
return fmt.Sprintf(`
resource "nutanix_category_key" "test"{
name = "app-support-%d"
description = "App Support CategoryKey"
}
`, r)
}
func testAccNutanixCategoryKeyConfigToUpdate(r int) string {
return fmt.Sprintf(`
resource "nutanix_category_key" "test_update"{
name = "app-support-%d"
description = "App Support CategoryKey"
}
`, r)
}
func testAccNutanixCategoryKeyConfigUpdated(r int) string {
return fmt.Sprintf(`
resource "nutanix_category_key" "test_update"{
name = "app-support-%d-updated"
description = "App Support CategoryKey Updated"
}
`, r)
}