Skip to content

Commit 38a032c

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

4 files changed

+396
-0
lines changed

Diff for: nsxt/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ func Provider() *schema.Provider {
433433
"nsxt_policy_host_transport_node": resourceNsxtPolicyHostTransportNode(),
434434
"nsxt_edge_high_availability_profile": resourceNsxtEdgeHighAvailabilityProfile(),
435435
"nsxt_policy_host_transport_node_collection": resourceNsxtPolicyHostTransportNodeCollection(),
436+
"nsxt_policy_ods_runbook_invocation": resourceNsxtPolicyODSRunbookInvocation(),
436437
},
437438

438439
ConfigureFunc: providerConfigure,

Diff for: nsxt/resource_nsxt_policy_ods_runbook_invocation.go

+191
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
/* Copyright © 2020 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+
"display_name": getDisplayNameSchema(),
29+
"description": getDescriptionSchema(),
30+
"revision": getRevisionSchema(),
31+
"tag": getTagsSchema(),
32+
"argument": {
33+
Type: schema.TypeSet,
34+
Optional: true,
35+
Description: "Arguments for runbook invocation",
36+
Elem: &schema.Resource{
37+
Schema: map[string]*schema.Schema{
38+
"key": {
39+
Type: schema.TypeString,
40+
Required: true,
41+
Description: "Key",
42+
},
43+
"value": {
44+
Type: schema.TypeString,
45+
Required: true,
46+
Description: "Value",
47+
},
48+
},
49+
},
50+
},
51+
"runbook_path": {
52+
Type: schema.TypeString,
53+
Required: true,
54+
Description: "Path of runbook object",
55+
},
56+
"target_node": {
57+
Type: schema.TypeString,
58+
Optional: true,
59+
Description: "Identifier of an appliance node or transport node",
60+
},
61+
},
62+
}
63+
}
64+
65+
func getODSRunbookInvocationFromSchema(id string, d *schema.ResourceData) model.OdsRunbookInvocation {
66+
displayName := d.Get("display_name").(string)
67+
description := d.Get("description").(string)
68+
tags := getPolicyTagsFromSchema(d)
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+
DisplayName: &displayName,
87+
Description: &description,
88+
Tags: tags,
89+
RunbookPath: &runbookPath,
90+
Arguments: arguments,
91+
TargetNode: &targetNode,
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 nil
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+
d.Set("display_name", obj.DisplayName)
150+
d.Set("description", obj.Description)
151+
setPolicyTagsInSchema(d, obj.Tags)
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 nil
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+
}
+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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),
30+
Check: resource.ComposeTestCheckFunc(
31+
testAccNsxtPolicyODSRunbookInvocationExists(name, testResourceName),
32+
resource.TestCheckResourceAttrSet(testResourceName, "target_node"),
33+
resource.TestCheckResourceAttrSet(testResourceName, "runbook_path"),
34+
),
35+
},
36+
},
37+
})
38+
}
39+
40+
func TestAccResourceNsxtPolicyODSRunbookInvocation_import(t *testing.T) {
41+
42+
name := getAccTestResourceName()
43+
testResourceName := "nsxt_policy_ods_runbook_invocation.test"
44+
resource.Test(t, resource.TestCase{
45+
PreCheck: func() {
46+
testAccPreCheck(t)
47+
testAccOnlyLocalManager(t)
48+
},
49+
Providers: testAccProviders,
50+
CheckDestroy: func(state *terraform.State) error {
51+
return testAccNsxtPolicyODSRunbookInvocationCheckDestroy(state, name)
52+
},
53+
Steps: []resource.TestStep{
54+
{
55+
Config: testAccNsxtPolicyODSRunbookInvocationCreateTemplate(name),
56+
},
57+
{
58+
ResourceName: testResourceName,
59+
ImportState: true,
60+
ImportStateVerify: true,
61+
ImportStateIdFunc: testAccResourceNsxtPolicyImportIDRetriever(testResourceName),
62+
},
63+
},
64+
})
65+
}
66+
67+
func testAccNsxtPolicyODSRunbookInvocationCheckDestroy(state *terraform.State, displayName string) error {
68+
connector := getPolicyConnector(testAccProvider.Meta().(nsxtClients))
69+
for _, rs := range state.RootModule().Resources {
70+
71+
if rs.Type != "nsxt_policy_ods_runbook_invocation" {
72+
continue
73+
}
74+
75+
resourceID := rs.Primary.Attributes["id"]
76+
exists, err := resourceNsxtPolicyODSRunbookInvocationExists(resourceID, connector, testAccIsGlobalManager())
77+
if err == nil {
78+
return err
79+
}
80+
81+
if exists {
82+
return fmt.Errorf("policy ODSRunbookInvocation %s still exists", displayName)
83+
}
84+
}
85+
return nil
86+
}
87+
88+
func testAccNsxtPolicyODSRunbookInvocationExists(displayName string, resourceName string) resource.TestCheckFunc {
89+
return func(state *terraform.State) error {
90+
91+
connector := getPolicyConnector(testAccProvider.Meta().(nsxtClients))
92+
93+
rs, ok := state.RootModule().Resources[resourceName]
94+
if !ok {
95+
return fmt.Errorf("policy ODSRunbookInvocation resource %s not found in resources", resourceName)
96+
}
97+
98+
resourceID := rs.Primary.ID
99+
if resourceID == "" {
100+
return fmt.Errorf("policy ODSRunbookInvocation resource ID not set in resources")
101+
}
102+
103+
exists, err := resourceNsxtPolicyODSRunbookInvocationExists(resourceID, connector, testAccIsGlobalManager())
104+
if err != nil {
105+
return err
106+
}
107+
if !exists {
108+
return fmt.Errorf("policy ODSRunbookInvocation %s does not exist", resourceID)
109+
}
110+
111+
return nil
112+
}
113+
}
114+
115+
func testAccNsxtPolicyODSRunbookInvocationCreateTemplate(name string) string {
116+
htnName := getHostTransportNodeName()
117+
return fmt.Sprintf(testAccNsxtPolicyODSPredefinedRunbookReadTemplate("OverlayTunnel")+`
118+
data "nsxt_policy_host_transport_node" "test" {
119+
display_name = "%s"
120+
}
121+
122+
resource "nsxt_policy_ods_runbook_invocation" "test" {
123+
display_name = "%s"
124+
runbook_path = data.nsxt_policy_ods_pre_defined_runbook.test.path
125+
argument {
126+
key = "src"
127+
value = "192.168.0.11"
128+
}
129+
argument {
130+
key = "dst"
131+
value = "192.168.0.10"
132+
}
133+
target_node = data.nsxt_policy_host_transport_node.test.unique_id
134+
}
135+
`, htnName, name)
136+
}

0 commit comments

Comments
 (0)