forked from vmware/terraform-provider-nsxt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_nsxt_policy_gateway_qos_profile_test.go
142 lines (126 loc) · 4.28 KB
/
data_source_nsxt_policy_gateway_qos_profile_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
/* Copyright © 2019 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */
package nsxt
import (
"fmt"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
gm_infra "github.com/vmware/vsphere-automation-sdk-go/services/nsxt-gm/global_infra"
gm_model "github.com/vmware/vsphere-automation-sdk-go/services/nsxt-gm/model"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
"github.com/vmware/terraform-provider-nsxt/api/infra"
)
func TestAccDataSourceNsxtPolicyGatewayQosProfile_basic(t *testing.T) {
testAccDataSourceNsxtPolicyGatewayQosProfileBasic(t, false, func() {
testAccPreCheck(t)
testAccNSXVersion(t, "3.0.0")
})
}
func TestAccDataSourceNsxtPolicyGatewayQosProfile_multitenancy(t *testing.T) {
testAccDataSourceNsxtPolicyGatewayQosProfileBasic(t, true, func() {
testAccPreCheck(t)
testAccOnlyMultitenancy(t)
})
}
func testAccDataSourceNsxtPolicyGatewayQosProfileBasic(t *testing.T, withContext bool, preCheck func()) {
name := getAccTestDataSourceName()
testResourceName := "data.nsxt_policy_gateway_qos_profile.test"
resource.ParallelTest(t, resource.TestCase{
PreCheck: preCheck,
Providers: testAccProviders,
CheckDestroy: func(state *terraform.State) error {
return testAccDataSourceNsxtPolicyGatewayQosProfileDeleteByName(name)
},
Steps: []resource.TestStep{
{
PreConfig: func() {
if err := testAccDataSourceNsxtPolicyGatewayQosProfileCreate(name); err != nil {
t.Error(err)
}
},
Config: testAccNsxtPolicyGatewayQosProfileReadTemplate(name, withContext),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(testResourceName, "display_name", name),
resource.TestCheckResourceAttr(testResourceName, "description", name),
resource.TestCheckResourceAttrSet(testResourceName, "path"),
),
},
},
})
}
func testAccDataSourceNsxtPolicyGatewayQosProfileCreate(name string) error {
connector, err := testAccGetPolicyConnector()
if err != nil {
return fmt.Errorf("Error during test client initialization: %v", err)
}
displayName := name
description := name
obj := model.GatewayQosProfile{
Description: &description,
DisplayName: &displayName,
}
// Generate a random ID for the resource
id := newUUID()
if testAccIsGlobalManager() {
gmObj, convErr := convertModelBindingType(obj, model.GatewayQosProfileBindingType(), gm_model.GatewayQosProfileBindingType())
if convErr != nil {
return convErr
}
client := gm_infra.NewGatewayQosProfilesClient(connector)
err = client.Patch(id, gmObj.(gm_model.GatewayQosProfile), nil)
} else {
client := infra.NewGatewayQosProfilesClient(testAccGetSessionContext(), connector)
err = client.Patch(id, obj, nil)
}
if err != nil {
return handleCreateError("GatewayQosProfile", id, err)
}
return nil
}
func testAccDataSourceNsxtPolicyGatewayQosProfileDeleteByName(name string) error {
connector, err := testAccGetPolicyConnector()
if err != nil {
return fmt.Errorf("Error during test client initialization: %v", err)
}
// Find the object by name and delete it
if testAccIsGlobalManager() {
objID, err := testGetObjIDByName(name, "GatewayQosProfile")
if err == nil {
client := gm_infra.NewGatewayQosProfilesClient(connector)
err := client.Delete(objID, nil)
if err != nil {
return handleDeleteError("GatewayQosProfile", objID, err)
}
return nil
}
} else {
client := infra.NewGatewayQosProfilesClient(testAccGetSessionContext(), connector)
// Find the object by name
objList, err := client.List(nil, nil, nil, nil, nil, nil)
if err != nil {
return handleListError("GatewayQosProfile", err)
}
for _, objInList := range objList.Results {
if *objInList.DisplayName == name {
err := client.Delete(*objInList.Id, nil)
if err != nil {
return handleDeleteError("GatewayQosProfile", *objInList.Id, err)
}
return nil
}
}
}
return fmt.Errorf("Error while deleting GatewayQosProfile '%s': resource not found", name)
}
func testAccNsxtPolicyGatewayQosProfileReadTemplate(name string, withContext bool) string {
context := ""
if withContext {
context = testAccNsxtPolicyMultitenancyContext()
}
return fmt.Sprintf(`
data "nsxt_policy_gateway_qos_profile" "test" {
%s
display_name = "%s"
}`, context, name)
}