forked from vmware/terraform-provider-nsxt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_nsxt_logical_tier1_router_test.go
104 lines (90 loc) · 3.18 KB
/
data_source_nsxt_logical_tier1_router_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
/* Copyright © 2018 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 TestAccDataSourceNsxtLogicalTier1Router_basic(t *testing.T) {
routerName := getAccTestDataSourceName()
testResourceName := "data.nsxt_logical_tier1_router.test"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccOnlyLocalManager(t); testAccTestDeprecated(t); testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: func(state *terraform.State) error {
return testAccDataSourceNsxtTier1RouterDeleteByName(routerName)
},
Steps: []resource.TestStep{
{
PreConfig: func() {
if err := testAccDataSourceNsxtTier1RouterCreate(routerName); err != nil {
t.Error(err)
}
},
Config: testAccNSXTier1RouterReadTemplate(routerName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(testResourceName, "display_name", routerName),
resource.TestCheckResourceAttr(testResourceName, "description", routerName),
),
},
},
})
}
func testAccDataSourceNsxtTier1RouterCreate(routerName string) error {
nsxClient, err := testAccGetClient()
if err != nil {
return fmt.Errorf("Error during test client initialization: %v", err)
}
displayName := routerName
description := routerName
routerType := "TIER1"
logicalRouter := manager.LogicalRouter{
Description: description,
DisplayName: displayName,
RouterType: routerType,
}
_, resp, err := nsxClient.LogicalRoutingAndServicesApi.CreateLogicalRouter(nsxClient.Context, logicalRouter)
if err != nil {
return fmt.Errorf("Error during router creation: %v", err)
}
if resp.StatusCode != http.StatusCreated {
return fmt.Errorf("Unexpected status returned during router creation: %v", resp.StatusCode)
}
return nil
}
func testAccDataSourceNsxtTier1RouterDeleteByName(routerName 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.LogicalRoutingAndServicesApi.ListLogicalRouters(nsxClient.Context, nil)
if err != nil {
return fmt.Errorf("Error while reading routers: %v", err)
}
// go over the list to find the correct one
for _, objInList := range objList.Results {
if objInList.DisplayName == routerName {
localVarOptionals := make(map[string]interface{})
responseCode, err := nsxClient.LogicalRoutingAndServicesApi.DeleteLogicalRouter(nsxClient.Context, objInList.Id, localVarOptionals)
if err != nil {
return fmt.Errorf("Error during router deletion: %v", err)
}
if responseCode.StatusCode != http.StatusOK {
return fmt.Errorf("Unexpected status returned during router deletion: %v", responseCode.StatusCode)
}
return nil
}
}
return fmt.Errorf("Error while deleting router '%s': service not found", routerName)
}
func testAccNSXTier1RouterReadTemplate(name string) string {
return fmt.Sprintf(`
data "nsxt_logical_tier1_router" "test" {
display_name = "%s"
}`, name)
}