forked from vmware/terraform-provider-nsxt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_source_nsxt_policy_ipv6_dad_profile_test.go
117 lines (102 loc) · 3.36 KB
/
data_source_nsxt_policy_ipv6_dad_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
/* 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"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
"github.com/vmware/terraform-provider-nsxt/api/infra"
)
func TestAccDataSourceNsxtPolicyIpv6DadProfile_basic(t *testing.T) {
testAccDataSourceNsxtPolicyIpv6DadProfileBasic(t, false, func() {
testAccPreCheck(t)
})
}
func TestAccDataSourceNsxtPolicyIpv6DadProfile_multitenancy(t *testing.T) {
testAccDataSourceNsxtPolicyIpv6DadProfileBasic(t, true, func() {
testAccPreCheck(t)
testAccOnlyMultitenancy(t)
})
}
func testAccDataSourceNsxtPolicyIpv6DadProfileBasic(t *testing.T, withContext bool, preCheck func()) {
name := getAccTestDataSourceName()
testResourceName := "data.nsxt_policy_ipv6_dad_profile.test"
resource.ParallelTest(t, resource.TestCase{
PreCheck: preCheck,
Providers: testAccProviders,
CheckDestroy: func(state *terraform.State) error {
return testAccDataSourceNsxtPolicyIpv6DadProfileDeleteByName(name)
},
Steps: []resource.TestStep{
{
PreConfig: func() {
if err := testAccDataSourceNsxtPolicyIpv6DadProfileCreate(name); err != nil {
t.Error(err)
}
},
Config: testAccNsxtPolicyIpv6DadProfileReadTemplate(name, withContext),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(testResourceName, "display_name", name),
resource.TestCheckResourceAttr(testResourceName, "description", name),
resource.TestCheckResourceAttrSet(testResourceName, "path"),
),
},
},
})
}
func testAccDataSourceNsxtPolicyIpv6DadProfileCreate(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.Ipv6DadProfile{
Description: &description,
DisplayName: &displayName,
}
// Generate a random ID for the resource
id := newUUID()
client := infra.NewIpv6DadProfilesClient(testAccGetSessionContext(), connector)
err = client.Patch(id, obj, nil)
if err != nil {
return handleCreateError("Ipv6DadProfile", id, err)
}
return nil
}
func testAccDataSourceNsxtPolicyIpv6DadProfileDeleteByName(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
client := infra.NewIpv6DadProfilesClient(testAccGetSessionContext(), connector)
// Find the object by name
objList, err := client.List(nil, nil, nil, nil, nil, nil)
if err != nil {
return handleListError("Ipv6DadProfile", err)
}
for _, objInList := range objList.Results {
if *objInList.DisplayName == name {
err := client.Delete(*objInList.Id, nil)
if err != nil {
return handleDeleteError("Ipv6DadProfile", *objInList.Id, err)
}
return nil
}
}
return fmt.Errorf("Error while deleting Ipv6DadProfile '%s': resource not found", name)
}
func testAccNsxtPolicyIpv6DadProfileReadTemplate(name string, withContext bool) string {
context := ""
if withContext {
context = testAccNsxtPolicyMultitenancyContext()
}
return fmt.Sprintf(`
data "nsxt_policy_ipv6_dad_profile" "test" {
%s
display_name = "%s"
}`, context, name)
}