Skip to content

Commit f79fcee

Browse files
committed
Implement ODS runbook invocation resource
Signed-off-by: Kobi Samoray <[email protected]>
1 parent 03aad86 commit f79fcee

4 files changed

+406
-0
lines changed

nsxt/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@ func Provider() *schema.Provider {
437437
"nsxt_policy_host_transport_node": resourceNsxtPolicyHostTransportNode(),
438438
"nsxt_edge_high_availability_profile": resourceNsxtEdgeHighAvailabilityProfile(),
439439
"nsxt_policy_host_transport_node_collection": resourceNsxtPolicyHostTransportNodeCollection(),
440+
"nsxt_policy_ods_runbook_invocation": resourceNsxtPolicyODSRunbookInvocation(),
440441
},
441442

442443
ConfigureFunc: providerConfigure,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
/* Copyright © 2023 VMware, Inc. All Rights Reserved.
2+
SPDX-License-Identifier: MPL-2.0 */
3+
4+
package nsxt
5+
6+
import (
7+
"fmt"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
"github.com/vmware/vsphere-automation-sdk-go/runtime/protocol/client"
11+
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra/sha"
12+
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
13+
)
14+
15+
func resourceNsxtPolicyODSRunbookInvocation() *schema.Resource {
16+
return &schema.Resource{
17+
Create: resourceNsxtPolicyODSRunbookInvocationCreate,
18+
Read: resourceNsxtPolicyODSRunbookInvocationRead,
19+
Update: resourceNsxtPolicyODSRunbookInvocationUpdate,
20+
Delete: resourceNsxtPolicyODSRunbookInvocationDelete,
21+
Importer: &schema.ResourceImporter{
22+
State: nsxtPolicyPathResourceImporter,
23+
},
24+
25+
Schema: map[string]*schema.Schema{
26+
"nsx_id": getNsxIDSchema(),
27+
"path": getPathSchema(),
28+
// Due to a bug, invocations with a display_name specification fail, and when there's none set, NSX assigns
29+
// the id value to the display name attribute. This should work around that bug.
30+
"display_name": {
31+
Type: schema.TypeString,
32+
Description: "Display name for this resource",
33+
Optional: true,
34+
Computed: true,
35+
},
36+
"revision": getRevisionSchema(),
37+
"argument": {
38+
Type: schema.TypeSet,
39+
Optional: true,
40+
Description: "Arguments for runbook invocation",
41+
Elem: &schema.Resource{
42+
Schema: map[string]*schema.Schema{
43+
"key": {
44+
Type: schema.TypeString,
45+
Required: true,
46+
Description: "Key",
47+
},
48+
"value": {
49+
Type: schema.TypeString,
50+
Required: true,
51+
Description: "Value",
52+
},
53+
},
54+
},
55+
},
56+
"runbook_path": getPolicyPathSchema(true, true, "Path of runbook object"),
57+
"target_node": {
58+
Type: schema.TypeString,
59+
Optional: true,
60+
Description: "Identifier of an appliance node or transport node",
61+
ForceNew: true,
62+
},
63+
},
64+
}
65+
}
66+
67+
func getODSRunbookInvocationFromSchema(id string, d *schema.ResourceData) model.OdsRunbookInvocation {
68+
displayName := d.Get("display_name").(string)
69+
runbookPath := d.Get("runbook_path").(string)
70+
targetNode := d.Get("target_node").(string)
71+
72+
var arguments []model.UnboundedKeyValuePair
73+
for _, arg := range d.Get("argument").(*schema.Set).List() {
74+
argMap := arg.(map[string]interface{})
75+
key := argMap["key"].(string)
76+
value := argMap["value"].(string)
77+
item := model.UnboundedKeyValuePair{
78+
Key: &key,
79+
Value: &value,
80+
}
81+
arguments = append(arguments, item)
82+
}
83+
84+
obj := model.OdsRunbookInvocation{
85+
Id: &id,
86+
RunbookPath: &runbookPath,
87+
Arguments: arguments,
88+
TargetNode: &targetNode,
89+
}
90+
if displayName != "" {
91+
obj.DisplayName = &displayName
92+
}
93+
94+
return obj
95+
}
96+
97+
func resourceNsxtPolicyODSRunbookInvocationCreate(d *schema.ResourceData, m interface{}) error {
98+
// Initialize resource Id and verify this ID is not yet used
99+
id, err := getOrGenerateID(d, m, resourceNsxtPolicyODSRunbookInvocationExists)
100+
if err != nil {
101+
return err
102+
}
103+
104+
connector := getPolicyConnector(m)
105+
client := sha.NewRunbookInvocationsClient(connector)
106+
107+
obj := getODSRunbookInvocationFromSchema(id, d)
108+
err = client.Create(id, obj)
109+
if err != nil {
110+
return handleCreateError("OdsRunbookInvocation", id, err)
111+
}
112+
113+
d.SetId(id)
114+
d.Set("nsx_id", id)
115+
return resourceNsxtPolicyODSRunbookInvocationRead(d, m)
116+
}
117+
118+
func resourceNsxtPolicyODSRunbookInvocationExists(id string, connector client.Connector, isGlobalManager bool) (bool, error) {
119+
var err error
120+
client := sha.NewRunbookInvocationsClient(connector)
121+
_, err = client.Get(id)
122+
123+
if err == nil {
124+
return true, nil
125+
}
126+
127+
if isNotFoundError(err) {
128+
return false, nil
129+
}
130+
131+
return false, logAPIError("Error retrieving resource", err)
132+
}
133+
134+
func resourceNsxtPolicyODSRunbookInvocationRead(d *schema.ResourceData, m interface{}) error {
135+
connector := getPolicyConnector(m)
136+
137+
id := d.Id()
138+
if id == "" {
139+
return fmt.Errorf("error obtaining OdsRunbookInvocation ID")
140+
}
141+
142+
client := sha.NewRunbookInvocationsClient(connector)
143+
var err error
144+
obj, err := client.Get(id)
145+
if err != nil {
146+
return handleReadError(d, "OdsRunbookInvocation", id, err)
147+
}
148+
149+
if obj.DisplayName != nil && *obj.DisplayName != "" {
150+
d.Set("display_name", obj.DisplayName)
151+
}
152+
d.Set("nsx_id", id)
153+
d.Set("path", obj.Path)
154+
d.Set("revision", obj.Revision)
155+
156+
d.Set("runbook_path", obj.RunbookPath)
157+
d.Set("target_node", obj.TargetNode)
158+
159+
var argList []map[string]interface{}
160+
for _, arg := range obj.Arguments {
161+
argData := make(map[string]interface{})
162+
argData["key"] = arg.Key
163+
argData["value"] = arg.Value
164+
argList = append(argList, argData)
165+
}
166+
d.Set("argument", argList)
167+
168+
return nil
169+
}
170+
171+
func resourceNsxtPolicyODSRunbookInvocationUpdate(d *schema.ResourceData, m interface{}) error {
172+
return resourceNsxtPolicyODSRunbookInvocationRead(d, m)
173+
}
174+
175+
func resourceNsxtPolicyODSRunbookInvocationDelete(d *schema.ResourceData, m interface{}) error {
176+
id := d.Id()
177+
if id == "" {
178+
return fmt.Errorf("error obtaining OdsRunbookInvocation ID")
179+
}
180+
181+
connector := getPolicyConnector(m)
182+
var err error
183+
client := sha.NewRunbookInvocationsClient(connector)
184+
err = client.Delete(id)
185+
186+
if err != nil {
187+
return handleDeleteError("OdsRunbookInvocation", id, err)
188+
}
189+
190+
return nil
191+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/* Copyright © 2023 VMware, Inc. All Rights Reserved.
2+
SPDX-License-Identifier: MPL-2.0 */
3+
4+
package nsxt
5+
6+
import (
7+
"fmt"
8+
"testing"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
12+
)
13+
14+
func TestAccResourceNsxtPolicyODSRunbookInvocation_basic(t *testing.T) {
15+
name := getAccTestResourceName()
16+
testResourceName := "nsxt_policy_ods_runbook_invocation.test"
17+
resource.Test(t, resource.TestCase{
18+
PreCheck: func() {
19+
testAccOnlyLocalManager(t)
20+
testAccPreCheck(t)
21+
testAccEnvDefined(t, "NSXT_TEST_HOST_TRANSPORT_NODE")
22+
},
23+
Providers: testAccProviders,
24+
CheckDestroy: func(state *terraform.State) error {
25+
return testAccNsxtPolicyODSRunbookInvocationCheckDestroy(state, name)
26+
},
27+
Steps: []resource.TestStep{
28+
{
29+
Config: testAccNsxtPolicyODSRunbookInvocationCreateTemplate(name, "OverlayTunnel", `
30+
argument {
31+
key = "src"
32+
value = "192.168.0.11"
33+
}
34+
argument {
35+
key = "dst"
36+
value = "192.168.0.10"
37+
}
38+
`),
39+
Check: resource.ComposeTestCheckFunc(
40+
testAccNsxtPolicyODSRunbookInvocationExists(name, testResourceName),
41+
resource.TestCheckResourceAttrSet(testResourceName, "target_node"),
42+
resource.TestCheckResourceAttrSet(testResourceName, "runbook_path"),
43+
),
44+
},
45+
},
46+
})
47+
}
48+
49+
func TestAccResourceNsxtPolicyODSRunbookInvocation_import(t *testing.T) {
50+
51+
name := getAccTestResourceName()
52+
testResourceName := "nsxt_policy_ods_runbook_invocation.test"
53+
resource.Test(t, resource.TestCase{
54+
PreCheck: func() {
55+
testAccPreCheck(t)
56+
testAccOnlyLocalManager(t)
57+
},
58+
Providers: testAccProviders,
59+
CheckDestroy: func(state *terraform.State) error {
60+
return testAccNsxtPolicyODSRunbookInvocationCheckDestroy(state, name)
61+
},
62+
Steps: []resource.TestStep{
63+
{
64+
Config: testAccNsxtPolicyODSRunbookInvocationCreateTemplate(name, "OverlayTunnel", `
65+
argument {
66+
key = "src"
67+
value = "192.168.0.11"
68+
}
69+
argument {
70+
key = "dst"
71+
value = "192.168.0.10"
72+
}
73+
`),
74+
},
75+
{
76+
ResourceName: testResourceName,
77+
ImportState: true,
78+
ImportStateVerify: true,
79+
ImportStateIdFunc: testAccResourceNsxtPolicyImportIDRetriever(testResourceName),
80+
},
81+
},
82+
})
83+
}
84+
85+
func testAccNsxtPolicyODSRunbookInvocationCheckDestroy(state *terraform.State, displayName string) error {
86+
connector := getPolicyConnector(testAccProvider.Meta().(nsxtClients))
87+
for _, rs := range state.RootModule().Resources {
88+
89+
if rs.Type != "nsxt_policy_ods_runbook_invocation" {
90+
continue
91+
}
92+
93+
resourceID := rs.Primary.Attributes["id"]
94+
exists, err := resourceNsxtPolicyODSRunbookInvocationExists(resourceID, connector, testAccIsGlobalManager())
95+
if err == nil {
96+
return err
97+
}
98+
99+
if exists {
100+
return fmt.Errorf("policy ODSRunbookInvocation %s still exists", displayName)
101+
}
102+
}
103+
return nil
104+
}
105+
106+
func testAccNsxtPolicyODSRunbookInvocationExists(displayName string, resourceName string) resource.TestCheckFunc {
107+
return func(state *terraform.State) error {
108+
109+
connector := getPolicyConnector(testAccProvider.Meta().(nsxtClients))
110+
111+
rs, ok := state.RootModule().Resources[resourceName]
112+
if !ok {
113+
return fmt.Errorf("policy ODSRunbookInvocation resource %s not found in resources", resourceName)
114+
}
115+
116+
resourceID := rs.Primary.ID
117+
if resourceID == "" {
118+
return fmt.Errorf("policy ODSRunbookInvocation resource ID not set in resources")
119+
}
120+
121+
exists, err := resourceNsxtPolicyODSRunbookInvocationExists(resourceID, connector, testAccIsGlobalManager())
122+
if err != nil {
123+
return err
124+
}
125+
if !exists {
126+
return fmt.Errorf("policy ODSRunbookInvocation %s does not exist", resourceID)
127+
}
128+
129+
return nil
130+
}
131+
}
132+
133+
func testAccNsxtPolicyODSRunbookInvocationCreateTemplate(name, runbook, arguments string) string {
134+
htnName := getHostTransportNodeName()
135+
return testAccNsxtPolicyODSPredefinedRunbookReadTemplate(runbook) + fmt.Sprintf(`
136+
data "nsxt_policy_host_transport_node" "test" {
137+
display_name = "%s"
138+
}
139+
140+
resource "nsxt_policy_ods_runbook_invocation" "test" {
141+
// Use nsx_id here to address a backend issue.
142+
nsx_id = "%s"
143+
runbook_path = data.nsxt_policy_ods_pre_defined_runbook.test.path
144+
%s
145+
target_node = data.nsxt_policy_host_transport_node.test.unique_id
146+
}
147+
`, htnName, name, arguments)
148+
}

0 commit comments

Comments
 (0)