Skip to content

Commit

Permalink
created import function for netbox_available_prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
wrighbr committed Jul 17, 2020
1 parent 101f7d7 commit 3cadf66
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 15 deletions.
2 changes: 1 addition & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (c *Client) SendRequest(method string, path string, payload interface{}, st
if statusCode != 0 {
if resp.StatusCode != statusCode {

return "", fmt.Errorf("[ERROR] unexpected status code got: %v expected: %v \n %v", resp.StatusCode, statusCode, strbody)
return "", fmt.Errorf("[ERROR] unexpected status code got: %v expected: %v \n %v \n %v", resp.StatusCode, statusCode, strbody, url)
}
}

Expand Down
2 changes: 1 addition & 1 deletion provider/data_prefixes.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func dataPrefixRead(d *schema.ResourceData, m interface{}) error {
if newCidr == "" {
return fmt.Errorf("[ERROR] 'cidr_notation' is empty")
}
prefixPath := "api/ipam/prefixes/?prefix=" + newCidr
prefixPath := "/ipam/prefixes/?prefix=" + newCidr

resp, err := apiClient.SendRequest("GET", prefixPath, nil, 200)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion provider/data_sites.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func dataSites() *schema.Resource {
func dataSitesRead(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*client.Client)
siteName := d.Get("name").(string)
sitesPath := "api/dcim/sites/?name=" + siteName
sitesPath := "/dcim/sites/?name=" + siteName

resp, err := apiClient.SendRequest("GET", sitesPath, nil, 200)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func Provider() terraform.ResourceProvider {
}

func providerConfigure(d *schema.ResourceData) (interface{}, error) {
endpoint := d.Get("endpoint").(string)
endpoint := d.Get("endpoint").(string) + "api"
apiToken := d.Get("api_token").(string)

return client.NewClient(endpoint, apiToken), nil
Expand Down
52 changes: 41 additions & 11 deletions provider/resource_availableprefixes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package provider

import (
"encoding/json"
"regexp"
"strconv"
"time"

"github.com/BESTSELLER/terraform-provider-netbox/client"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

var pathAvailablePrefixes = "api/ipam/prefixes/"
var pathAvailablePrefixes = "/ipam/prefixes/"

type AvailablePrefixes struct {
PrefixLenght int `json:"prefix_length"`
Expand All @@ -20,6 +21,13 @@ type AvailablePrefixes struct {
Description string `json:"description,omitempty"`
}

type responeListOfPrefixes struct {
Count int `json:"count"`
Next interface{} `json:"next"`
Previous interface{} `json:"previous"`
Results []reponseAvailablePrefixes `json:"results"`
}

type reponseAvailablePrefixes struct {
ID int `json:"id"`
Family struct {
Expand Down Expand Up @@ -92,6 +100,9 @@ func resourceAvailablePrefixes() *schema.Resource {
Read: resourceAvailablePrefixRead,
Update: resourceAvailablePrefixUpdate,
Delete: resourceAvailablePrefixDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
}
}

Expand All @@ -108,22 +119,42 @@ func resourceAvailablePrefixCreate(d *schema.ResourceData, m interface{}) error
var jsonData reponseAvailablePrefixes
json.Unmarshal([]byte(resp), &jsonData)

d.Set("prefix_id", jsonData.ID)
d.Set("cidr_notation", jsonData.Prefix)
d.SetId(randomString(15))
d.SetId(pathAvailablePrefixes + strconv.Itoa(jsonData.ID) + "/")
return resourceAvailablePrefixRead(d, m)
}

func resourceAvailablePrefixRead(d *schema.ResourceData, m interface{}) error {
// apiClient := m.(*client.Client)
apiClient := m.(*client.Client)
resp, err := apiClient.SendRequest("GET", d.Id(), nil, 200)
if err != nil {
return err
}
var jsonData reponseAvailablePrefixes
json.Unmarshal([]byte(resp), &jsonData)

re := regexp.MustCompile(`(?m)(?:[0-9]{1,3}\.){3}[0-9]{1,3}/`)
prefixLenght, _ := strconv.Atoi(re.ReplaceAllString(jsonData.Prefix, ""))

resp2, err := apiClient.SendRequest("GET", pathAvailablePrefixes+"?q="+jsonData.Prefix, nil, 200)
if err != nil {
return err
}
var jsonData2 responeListOfPrefixes
json.Unmarshal([]byte(resp2), &jsonData2)

d.Set("cidr_notation", jsonData.Prefix)
d.Set("description", jsonData.Description)
d.Set("prefix_length", prefixLenght)
d.Set("prefix_id", jsonData.ID)
d.Set("parent_prefix_id", jsonData2.Results[0].ID)
d.SetId(pathAvailablePrefixes + strconv.Itoa(jsonData.ID) + "/")

return nil
}

func resourceAvailablePrefixUpdate(d *schema.ResourceData, m interface{}) error {
apiClient, body := availablePrefixBody(d, m)
id := strconv.Itoa(d.Get("prefix_id").(int))

path := pathAvailablePrefixes + id + "/"
path := d.Id()

apiClient.SendRequest("PATCH", path, body, 200)

Expand All @@ -132,9 +163,8 @@ func resourceAvailablePrefixUpdate(d *schema.ResourceData, m interface{}) error

func resourceAvailablePrefixDelete(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*client.Client)
id := strconv.Itoa(d.Get("prefix_id").(int))
prefixPath := pathAvailablePrefixes + id + "/"
apiClient.SendRequest("DELETE", prefixPath, nil, 204)

apiClient.SendRequest("DELETE", d.Id(), nil, 204)
return nil
}

Expand Down

0 comments on commit 3cadf66

Please sign in to comment.