-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_cf_isolation_segment_test.go
188 lines (173 loc) · 5.64 KB
/
resource_cf_isolation_segment_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
package cloudfoundry
import (
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
"fmt"
"github.com/terraform-providers/terraform-provider-cloudfoundry/cloudfoundry/managers"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)
const segmentResource = `
resource "cloudfoundry_org" "iso-org1" {
name = "iso-organization-one"
}
resource "cloudfoundry_org" "iso-org2" {
name = "iso-organization-two"
}
resource "cloudfoundry_isolation_segment" "segment1" {
name = "segment-one"
}
resource "cloudfoundry_isolation_segment_entitlement" "segment1_orgs" {
segment = "${cloudfoundry_isolation_segment.segment1.id}"
orgs = [
"${cloudfoundry_org.iso-org1.id}",
"${cloudfoundry_org.iso-org2.id}"
]
}
`
const segmentResourceUpdateName = `
resource "cloudfoundry_org" "iso-org1" {
name = "iso-organization-one"
}
resource "cloudfoundry_org" "iso-org2" {
name = "iso-organization-two"
}
resource "cloudfoundry_isolation_segment" "segment1" {
name = "segment-one-name"
}
resource "cloudfoundry_isolation_segment_entitlement" "segment1_orgs" {
segment = "${cloudfoundry_isolation_segment.segment1.id}"
orgs = [
"${cloudfoundry_org.iso-org1.id}",
"${cloudfoundry_org.iso-org2.id}"
]
}
`
const segmentResourceUpdateOrgs = `
resource "cloudfoundry_org" "iso-org1" {
name = "iso-organization-one"
}
resource "cloudfoundry_isolation_segment" "segment1" {
name = "segment-one-name"
}
resource "cloudfoundry_isolation_segment_entitlement" "segment1_orgs" {
segment = "${cloudfoundry_isolation_segment.segment1.id}"
orgs = [
"${cloudfoundry_org.iso-org1.id}"
]
default = true
}
`
var defaultLenIsolationSegments int
func TestAccResSegment_normal(t *testing.T) {
segRef := "cloudfoundry_isolation_segment.segment1"
entitleRef := "cloudfoundry_isolation_segment_entitlement.segment1_orgs"
segments, _, err := testSession().ClientV3.GetIsolationSegments()
if err != nil {
panic(err)
}
defaultLenIsolationSegments = len(segments)
resource.ParallelTest(t,
resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProvidersFactories,
CheckDestroy: testAccCheckSegmentDestroyed("segment-one-name"),
Steps: []resource.TestStep{
resource.TestStep{
Config: segmentResource,
Check: resource.ComposeTestCheckFunc(
testAccCheckSegmentExists(segRef, entitleRef),
resource.TestCheckResourceAttr(segRef, "name", "segment-one"),
resource.TestCheckResourceAttr(entitleRef, "orgs.#", "2"),
),
},
resource.TestStep{
Config: segmentResourceUpdateName,
Check: resource.ComposeTestCheckFunc(
testAccCheckSegmentExists(segRef, entitleRef),
resource.TestCheckResourceAttr(segRef, "name", "segment-one-name"),
resource.TestCheckResourceAttr(entitleRef, "orgs.#", "2"),
),
},
resource.TestStep{
Config: segmentResourceUpdateOrgs,
Check: resource.ComposeTestCheckFunc(
testAccCheckSegmentExists(segRef, entitleRef),
resource.TestCheckResourceAttr(segRef, "name", "segment-one-name"),
resource.TestCheckResourceAttr(entitleRef, "orgs.#", "1"),
),
},
},
})
}
func testAccCheckSegmentExists(segName string, entitleName string) resource.TestCheckFunc {
return func(s *terraform.State) (err error) {
session := testAccProvider.Meta().(*managers.Session)
sm := session.ClientV3
seg, ok := s.RootModule().Resources[segName]
if !ok {
return fmt.Errorf("segment '%s' not found in terraform state", segName)
}
entitle, ok := s.RootModule().Resources[entitleName]
if !ok {
return fmt.Errorf("segment '%s' not found in terraform state", entitleName)
}
segID := seg.Primary.ID
segAttributes := seg.Primary.Attributes
segment, _, err := sm.GetIsolationSegment(segID)
if err != nil {
return fmt.Errorf("segment '%s' not found", segName)
}
if err = assertEquals(segAttributes, "name", segment.Name); err != nil {
return err
}
entitleID := entitle.Primary.ID
entitleAttributes := entitle.Primary.Attributes
if entitleAttributes["segment"] != segID {
return fmt.Errorf("entitlement resource id '%s does not match segment id '%s'", entitleID, segID)
}
if err = assertEquals(entitleAttributes, "segment", segID); err != nil {
return err
}
orgs, _, err := sm.GetIsolationSegmentOrganizations(entitleAttributes["segment"])
if err != nil {
return fmt.Errorf("could fetch orgs from segment '%s'", segID)
}
tfOrgs := make([]interface{}, len(orgs))
for i, org := range orgs {
tfOrgs[i] = org.GUID
}
if err = assertSetEquals(entitleAttributes, "orgs", tfOrgs); err != nil {
return err
}
for _, org := range orgs {
rl, _, err := sm.GetOrganizationDefaultIsolationSegment(org.GUID)
if err != nil {
return err
}
if entitleAttributes["default"] == "1" && rl.GUID != segID {
return fmt.Errorf("entitlement resource default isolation segment for org %s mismatch, this should be using current segment", org.Name)
}
if entitleAttributes["default"] == "0" && rl.GUID == segID {
return fmt.Errorf("entitlement resource default isolation segment for org %s mismatch, this should be not using current segment", org.Name)
}
}
return
}
}
func testAccCheckSegmentDestroyed(segName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
session := testAccProvider.Meta().(*managers.Session)
orgs, _, err := session.ClientV3.GetIsolationSegments(ccv3.Query{
Key: ccv3.NameFilter,
Values: []string{segName},
})
if err != nil {
return err
}
if len(orgs) > 0 {
return fmt.Errorf("segment with name '%s' still exists in cloud foundry", segName)
}
return nil
}
}