-
Notifications
You must be signed in to change notification settings - Fork 85
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
Add discover node data source #1084
Merged
Merged
Changes from all commits
Commits
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,70 @@ | ||
/* Copyright © 2024 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/services/nsxt-mp/nsx/fabric" | ||
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx/model" | ||
) | ||
|
||
func dataSourceNsxtDiscoverNode() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceNsxtDiscoverNodeRead, | ||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Type: schema.TypeString, | ||
Description: "External id of the discovered node, ex. a mo-ref from VC", | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
"ip_address": { | ||
Type: schema.TypeString, | ||
Description: "IP Address of the the discovered node.", | ||
Optional: true, | ||
Computed: true, | ||
ValidateFunc: validateCidrOrIPOrRange(), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceNsxtDiscoverNodeRead(d *schema.ResourceData, m interface{}) error { | ||
connector := getPolicyConnector(m) | ||
discoverNodeClient := fabric.NewDiscoveredNodesClient(connector) | ||
|
||
objID := d.Get("id").(string) | ||
ipAddress := d.Get("ip_address").(string) | ||
|
||
var obj model.DiscoveredNode | ||
if objID != "" { | ||
// Get by ID | ||
objGet, err := discoverNodeClient.Get(objID) | ||
if isNotFoundError(err) { | ||
return fmt.Errorf("Discover Node %s was not found", objID) | ||
} | ||
if err != nil { | ||
return fmt.Errorf("Error while reading Discover Node %s: %v", objID, err) | ||
} | ||
obj = objGet | ||
} else if ipAddress == "" { | ||
return fmt.Errorf("Error obtaining Discover Node external ID or IP address during read") | ||
} else { | ||
// Get by IP address | ||
objList, err := discoverNodeClient.List(nil, nil, nil, nil, nil, nil, &ipAddress, nil, nil, nil, nil, nil, nil, nil) | ||
if isNotFoundError(err) { | ||
return fmt.Errorf("Discover Node with IP %s was not found", ipAddress) | ||
} | ||
if err != nil { | ||
return fmt.Errorf("Error while reading Discover Node: %v", err) | ||
} | ||
obj = objList.Results[0] | ||
} | ||
|
||
d.SetId(*obj.ExternalId) | ||
d.Set("ip_address", obj.IpAddresses[0]) | ||
return nil | ||
} |
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
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,23 @@ | ||
--- | ||
subcategory: "Fabric" | ||
layout: "nsxt" | ||
page_title: "NSXT: discover_node" | ||
description: An Discover Node data source. | ||
--- | ||
|
||
# nsxt_discover_node | ||
|
||
This data source provides information about Discover Node configured in NSX. A Discover Node can be used to create a Host Transport Node. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "nsxt_discover_node" "test" { | ||
ip_address = "10.43.251.142" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
* `id` - (Optional) External id of the discovered node, ex. a mo-ref from VC. | ||
* `ip_address` - (Optional) IP Address of the discovered node. |
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
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.
I feel pagination might be required here for big deployments?
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.
Maybe I misunderstood something. Can multiple Nodes share the same IP address? I expect only one or none(NotFound) Node will be returned.
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.
My bad, I didn't realize the List has IP filter already in the call