forked from vmware/terraform-provider-nsxt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_nsxt_ns_service_test.go
123 lines (109 loc) · 3.85 KB
/
data_source_nsxt_ns_service_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
/* Copyright © 2017 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */
package nsxt
import (
"fmt"
"net/http"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/vmware/go-vmware-nsxt/manager"
)
func TestAccDataSourceNsxtNsService_basic(t *testing.T) {
serviceName := getAccTestDataSourceName()
testResourceName := "data.nsxt_ns_service.test"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccOnlyLocalManager(t); testAccTestDeprecated(t); testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: func(state *terraform.State) error {
return testAccDataSourceNsxtNsServiceDeleteByName(serviceName)
},
Steps: []resource.TestStep{
{
PreConfig: func() {
if err := testAccDataSourceNsxtNsServiceCreate(serviceName); err != nil {
t.Error(err)
}
},
Config: testAccNSXNsServiceReadTemplate(serviceName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(testResourceName, "display_name", serviceName),
resource.TestCheckResourceAttr(testResourceName, "description", serviceName),
),
},
},
})
}
func TestAccDataSourceNsxtNsService_systemOwned(t *testing.T) {
serviceName := "WINS"
testResourceName := "data.nsxt_ns_service.test"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccOnlyLocalManager(t); testAccTestDeprecated(t); testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccNSXNsServiceReadTemplate(serviceName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(testResourceName, "display_name", serviceName),
resource.TestCheckResourceAttr(testResourceName, "description", serviceName),
),
},
},
})
}
func testAccDataSourceNsxtNsServiceCreate(serviceName string) error {
nsxClient, err := testAccGetClient()
if err != nil {
return fmt.Errorf("Error during test client initialization: %v", err)
}
nsService := manager.IgmpTypeNsService{
NsService: manager.NsService{
DisplayName: serviceName,
Description: serviceName,
DefaultService: false,
},
NsserviceElement: manager.IgmpTypeNsServiceEntry{
ResourceType: "IGMPTypeNSService",
},
}
_, responseCode, err := nsxClient.GroupingObjectsApi.CreateIgmpTypeNSService(nsxClient.Context, nsService)
if err != nil {
return fmt.Errorf("Error during nsService creation: %v", err)
}
if responseCode.StatusCode != http.StatusCreated {
return fmt.Errorf("Unexpected status returned during nsService creation: %v", responseCode.StatusCode)
}
return nil
}
func testAccDataSourceNsxtNsServiceDeleteByName(serviceName string) error {
nsxClient, err := testAccGetClient()
if err != nil {
return fmt.Errorf("Error during test client initialization: %v", err)
}
// Find the object by name
objList, _, err := nsxClient.GroupingObjectsApi.ListNSServices(nsxClient.Context, nil)
if err != nil {
return fmt.Errorf("Error while reading NS services: %v", err)
}
// go over the list to find the correct one
for _, objInList := range objList.Results {
if objInList.DisplayName == serviceName {
localVarOptionals := make(map[string]interface{})
responseCode, err := nsxClient.GroupingObjectsApi.DeleteNSService(nsxClient.Context, objInList.Id, localVarOptionals)
if err != nil {
return fmt.Errorf("Error during nsService deletion: %v", err)
}
if responseCode.StatusCode != http.StatusOK {
return fmt.Errorf("Unexpected status returned during nsService deletion: %v", responseCode.StatusCode)
}
return nil
}
}
return fmt.Errorf("Error while deleting NS service '%s': service not found", serviceName)
}
func testAccNSXNsServiceReadTemplate(serviceName string) string {
return fmt.Sprintf(`
data "nsxt_ns_service" "test" {
display_name = "%s"
}`, serviceName)
}