forked from vmware/terraform-provider-nsxt
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement ODS runbook invocation resource #7
Open
lizard-boy
wants to merge
2
commits into
master
Choose a base branch
from
ods-runbook-invocation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* Copyright © 2023 VMware, Inc. All Rights Reserved. | ||
SPDX-License-Identifier: MPL-2.0 */ | ||
|
||
package nsxt | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra/sha" | ||
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model" | ||
) | ||
|
||
func dataSourceNsxtPolicyODSPreDefinedRunbook() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceNsxtPolicyODSPreDefinedRunbookRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"id": getDataSourceIDSchema(), | ||
"display_name": getDataSourceDisplayNameSchema(), | ||
"description": getDataSourceDescriptionSchema(), | ||
"path": getPathSchema(), | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceNsxtPolicyODSPreDefinedRunbookRead(d *schema.ResourceData, m interface{}) error { | ||
connector := getPolicyConnector(m) | ||
client := sha.NewPreDefinedRunbooksClient(connector) | ||
|
||
objID := d.Get("id").(string) | ||
objName := d.Get("display_name").(string) | ||
var obj model.OdsPredefinedRunbook | ||
if objID != "" { | ||
// Get by id | ||
objGet, err := client.Get(objID) | ||
if err != nil { | ||
return handleDataSourceReadError(d, "OdsPredefinedRunbook", objID, err) | ||
} | ||
obj = objGet | ||
} else if objName == "" { | ||
return fmt.Errorf("error obtaining OdsPredefinedRunbook ID or name during read") | ||
} else { | ||
// Get by full name/prefix | ||
objList, err := client.List(nil, nil, nil, nil, nil, nil) | ||
if err != nil { | ||
return handleListError("OdsPredefinedRunbook", err) | ||
} | ||
// go over the list to find the correct one (prefer a perfect match. If not - prefix match) | ||
var perfectMatch []model.OdsPredefinedRunbook | ||
var prefixMatch []model.OdsPredefinedRunbook | ||
for _, objInList := range objList.Results { | ||
if strings.HasPrefix(*objInList.DisplayName, objName) { | ||
prefixMatch = append(prefixMatch, objInList) | ||
} | ||
if *objInList.DisplayName == objName { | ||
perfectMatch = append(perfectMatch, objInList) | ||
} | ||
} | ||
if len(perfectMatch) > 0 { | ||
if len(perfectMatch) > 1 { | ||
return fmt.Errorf("found multiple OdsPredefinedRunbook with name '%s'", objName) | ||
} | ||
obj = perfectMatch[0] | ||
} else if len(prefixMatch) > 0 { | ||
if len(prefixMatch) > 1 { | ||
return fmt.Errorf("found multiple OdsPredefinedRunbooks with name starting with '%s'", objName) | ||
} | ||
obj = prefixMatch[0] | ||
} else { | ||
return fmt.Errorf("OdsPredefinedRunbook with name '%s' was not found", objName) | ||
} | ||
} | ||
|
||
d.SetId(*obj.Id) | ||
d.Set("display_name", obj.DisplayName) | ||
d.Set("description", obj.Description) | ||
d.Set("path", obj.Path) | ||
|
||
return nil | ||
} |
41 changes: 41 additions & 0 deletions
41
nsxt/data_source_nsxt_policy_ods_pre_defined_runbook_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* Copyright © 2023 VMware, Inc. All Rights Reserved. | ||
SPDX-License-Identifier: MPL-2.0 */ | ||
|
||
package nsxt | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccDataSourceNsxtPolicyODSPredefinedRunbook_basic(t *testing.T) { | ||
name := "ControllerConn" | ||
testResourceName := "data.nsxt_policy_ods_pre_defined_runbook.test" | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { | ||
testAccOnlyLocalManager(t) | ||
testAccPreCheck(t) | ||
testAccNSXVersion(t, "4.1.0") | ||
}, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccNsxtPolicyODSPredefinedRunbookReadTemplate(name), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(testResourceName, "display_name", name), | ||
resource.TestCheckResourceAttrSet(testResourceName, "path"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccNsxtPolicyODSPredefinedRunbookReadTemplate(name string) string { | ||
return fmt.Sprintf(` | ||
data "nsxt_policy_ods_pre_defined_runbook" "test" { | ||
display_name = "%s" | ||
}`, name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
/* Copyright © 2023 VMware, Inc. All Rights Reserved. | ||
SPDX-License-Identifier: MPL-2.0 */ | ||
|
||
package nsxt | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/vmware/vsphere-automation-sdk-go/runtime/protocol/client" | ||
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra/sha" | ||
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model" | ||
) | ||
|
||
func resourceNsxtPolicyODSRunbookInvocation() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceNsxtPolicyODSRunbookInvocationCreate, | ||
Read: resourceNsxtPolicyODSRunbookInvocationRead, | ||
Update: resourceNsxtPolicyODSRunbookInvocationUpdate, | ||
Delete: resourceNsxtPolicyODSRunbookInvocationDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: nsxtPolicyPathResourceImporter, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"nsx_id": getNsxIDSchema(), | ||
"path": getPathSchema(), | ||
// Due to a bug, invocations with a display_name specification fail, and when there's none set, NSX assigns | ||
// the id value to the display name attribute. This should work around that bug. | ||
"display_name": { | ||
Type: schema.TypeString, | ||
Description: "Display name for this resource", | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
"revision": getRevisionSchema(), | ||
"argument": { | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
Description: "Arguments for runbook invocation", | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"key": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "Key", | ||
}, | ||
"value": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "Value", | ||
}, | ||
}, | ||
}, | ||
}, | ||
"runbook_path": getPolicyPathSchema(true, true, "Path of runbook object"), | ||
"target_node": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: "Identifier of an appliance node or transport node", | ||
ForceNew: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func getODSRunbookInvocationFromSchema(id string, d *schema.ResourceData) model.OdsRunbookInvocation { | ||
displayName := d.Get("display_name").(string) | ||
runbookPath := d.Get("runbook_path").(string) | ||
targetNode := d.Get("target_node").(string) | ||
|
||
var arguments []model.UnboundedKeyValuePair | ||
for _, arg := range d.Get("argument").(*schema.Set).List() { | ||
argMap := arg.(map[string]interface{}) | ||
key := argMap["key"].(string) | ||
value := argMap["value"].(string) | ||
item := model.UnboundedKeyValuePair{ | ||
Key: &key, | ||
Value: &value, | ||
} | ||
arguments = append(arguments, item) | ||
} | ||
|
||
obj := model.OdsRunbookInvocation{ | ||
Id: &id, | ||
RunbookPath: &runbookPath, | ||
Arguments: arguments, | ||
TargetNode: &targetNode, | ||
} | ||
if displayName != "" { | ||
obj.DisplayName = &displayName | ||
} | ||
|
||
return obj | ||
} | ||
|
||
func resourceNsxtPolicyODSRunbookInvocationCreate(d *schema.ResourceData, m interface{}) error { | ||
// Initialize resource Id and verify this ID is not yet used | ||
id, err := getOrGenerateID(d, m, resourceNsxtPolicyODSRunbookInvocationExists) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
connector := getPolicyConnector(m) | ||
client := sha.NewRunbookInvocationsClient(connector) | ||
|
||
obj := getODSRunbookInvocationFromSchema(id, d) | ||
err = client.Create(id, obj) | ||
if err != nil { | ||
return handleCreateError("OdsRunbookInvocation", id, err) | ||
} | ||
|
||
d.SetId(id) | ||
d.Set("nsx_id", id) | ||
return resourceNsxtPolicyODSRunbookInvocationRead(d, m) | ||
} | ||
|
||
func resourceNsxtPolicyODSRunbookInvocationExists(id string, connector client.Connector, isGlobalManager bool) (bool, error) { | ||
var err error | ||
client := sha.NewRunbookInvocationsClient(connector) | ||
_, err = client.Get(id) | ||
|
||
if err == nil { | ||
return true, nil | ||
} | ||
|
||
if isNotFoundError(err) { | ||
return false, nil | ||
} | ||
|
||
return false, logAPIError("Error retrieving resource", err) | ||
} | ||
|
||
func resourceNsxtPolicyODSRunbookInvocationRead(d *schema.ResourceData, m interface{}) error { | ||
connector := getPolicyConnector(m) | ||
|
||
id := d.Id() | ||
if id == "" { | ||
return fmt.Errorf("error obtaining OdsRunbookInvocation ID") | ||
} | ||
|
||
client := sha.NewRunbookInvocationsClient(connector) | ||
var err error | ||
obj, err := client.Get(id) | ||
if err != nil { | ||
return handleReadError(d, "OdsRunbookInvocation", id, err) | ||
} | ||
|
||
if obj.DisplayName != nil && *obj.DisplayName != "" { | ||
d.Set("display_name", obj.DisplayName) | ||
} | ||
d.Set("nsx_id", id) | ||
d.Set("path", obj.Path) | ||
d.Set("revision", obj.Revision) | ||
|
||
d.Set("runbook_path", obj.RunbookPath) | ||
d.Set("target_node", obj.TargetNode) | ||
|
||
var argList []map[string]interface{} | ||
for _, arg := range obj.Arguments { | ||
argData := make(map[string]interface{}) | ||
argData["key"] = arg.Key | ||
argData["value"] = arg.Value | ||
argList = append(argList, argData) | ||
} | ||
d.Set("argument", argList) | ||
|
||
return nil | ||
} | ||
|
||
func resourceNsxtPolicyODSRunbookInvocationUpdate(d *schema.ResourceData, m interface{}) error { | ||
return resourceNsxtPolicyODSRunbookInvocationRead(d, m) | ||
} | ||
Comment on lines
+171
to
+173
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: Update operation only performs a read, which may not reflect changes made to the resource |
||
|
||
func resourceNsxtPolicyODSRunbookInvocationDelete(d *schema.ResourceData, m interface{}) error { | ||
id := d.Id() | ||
if id == "" { | ||
return fmt.Errorf("error obtaining OdsRunbookInvocation ID") | ||
} | ||
|
||
connector := getPolicyConnector(m) | ||
var err error | ||
client := sha.NewRunbookInvocationsClient(connector) | ||
err = client.Delete(id) | ||
|
||
if err != nil { | ||
return handleDeleteError("OdsRunbookInvocation", id, err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Combine prefix and perfect match checks in a single loop iteration for better efficiency