Skip to content

Commit 6210c98

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

4 files changed

+394
-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

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

0 commit comments

Comments
 (0)