-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
59 additions
and
1 deletion.
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
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,53 @@ | ||
package netbox | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strconv" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/validation" | ||
netboxclient "github.com/netbox-community/go-netbox/netbox/client" | ||
"github.com/netbox-community/go-netbox/netbox/client/dcim" | ||
) | ||
|
||
func dataNetboxDcimPlatform() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataNetboxDcimPlatformRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"slug": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringMatch( | ||
regexp.MustCompile("^[-a-zA-Z0-9_]{1,50}$"), | ||
"Must be like ^[-a-zA-Z0-9_]{1,50}$"), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataNetboxDcimPlatformRead(d *schema.ResourceData, m interface{}) error { | ||
client := m.(*netboxclient.NetBoxAPI) | ||
|
||
slug := d.Get("slug").(string) | ||
|
||
resource := dcim.NewDcimPlatformsListParams().WithSlug(&slug) | ||
|
||
list, err := client.Dcim.DcimPlatformsList(resource, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if *list.Payload.Count < 1 { | ||
return fmt.Errorf("Your query returned no results. " + | ||
"Please change your search criteria and try again.") | ||
} else if *list.Payload.Count > 1 { | ||
return fmt.Errorf("Your query returned more than one result. " + | ||
"Please try a more specific search criteria.") | ||
} | ||
|
||
d.SetId(strconv.FormatInt(list.Payload.Results[0].ID, 10)) | ||
|
||
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