forked from vmware/terraform-provider-nsxt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_nsxt_firewall_section_test.go
99 lines (86 loc) · 3 KB
/
data_source_nsxt_firewall_section_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
/* 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 TestAccDataSourceNsxtFirewallSection_basic(t *testing.T) {
name := getAccTestDataSourceName()
testResourceName := "data.nsxt_firewall_section.test"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccOnlyLocalManager(t); testAccTestDeprecated(t); testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: func(state *terraform.State) error {
return testAccDataSourceNsxtFirewallSectionDeleteByName(name)
},
Steps: []resource.TestStep{
{
PreConfig: func() {
if err := testAccDataSourceNsxtFirewallSectionCreate(name); err != nil {
t.Error(err)
}
},
Config: testAccNSXFirewallSectionReadTemplate(name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(testResourceName, "display_name", name),
resource.TestCheckResourceAttr(testResourceName, "description", name),
),
},
},
})
}
func testAccDataSourceNsxtFirewallSectionCreate(name string) error {
nsxClient, err := testAccGetClient()
if err != nil {
return fmt.Errorf("Error during test client initialization: %v", err)
}
section := manager.FirewallSection{
Description: name,
DisplayName: name,
SectionType: "LAYER3",
}
_, responseCode, err := nsxClient.ServicesApi.AddSection(nsxClient.Context, section, nil)
if err != nil {
return fmt.Errorf("Error during firewall section creation: %v", err)
}
if responseCode.StatusCode != http.StatusCreated {
return fmt.Errorf("Unexpected status returned during firewall section creation: %v", responseCode.StatusCode)
}
return nil
}
func testAccDataSourceNsxtFirewallSectionDeleteByName(name 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.ServicesApi.ListSections(nsxClient.Context, nil)
if err != nil {
return fmt.Errorf("Error while reading Firewall sections: %v", err)
}
// go over the list to find the correct one
for _, objInList := range objList.Results {
if objInList.DisplayName == name {
responseCode, err := nsxClient.ServicesApi.DeleteSection(nsxClient.Context, objInList.Id, nil)
if err != nil {
return fmt.Errorf("Error during firewall section deletion: %v", err)
}
if responseCode.StatusCode != http.StatusOK {
return fmt.Errorf("Unexpected status returned during firewall section deletion: %v", responseCode.StatusCode)
}
return nil
}
}
return fmt.Errorf("Error while deleting Firewall section '%s': firewall section not found", name)
}
func testAccNSXFirewallSectionReadTemplate(name string) string {
return fmt.Sprintf(`
data "nsxt_firewall_section" "test" {
display_name = "%s"
}`, name)
}