Skip to content

Commit

Permalink
Feat: Adding data source for device type + Fixing typo in model names (
Browse files Browse the repository at this point in the history
…#36)

* Adding data device type and fixing spelling mistake

* Adding data device type

* This should do the trick

* The arrow faces the wrong way

* Fixing deprecation warning

* Removed uHeight and improved custom_fields schema
  • Loading branch information
OrKarstoft authored Jul 18, 2023
1 parent 14ebd57 commit 1c5e4e6
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ override.tf.json

# End of https://www.toptal.com/developers/gitignore/api/terraform

dist
dist
dist/
24 changes: 20 additions & 4 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"regexp"
Expand Down Expand Up @@ -41,7 +41,7 @@ func AvailablePrefixBody(d *schema.ResourceData) models.AvailablePrefixes {
}

// GetPrefix will return a prefix
func (client *Client) GetPrefix(newCidr string) (*models.ReponsePrefix, error) {
func (client *Client) GetPrefix(newCidr string) (*models.ResponsePrefix, error) {
if newCidr == "" {
return nil, fmt.Errorf("[ERROR] 'cidr_notation' is empty")
}
Expand All @@ -52,7 +52,7 @@ func (client *Client) GetPrefix(newCidr string) (*models.ReponsePrefix, error) {
return nil, err
}

var jsonData models.ReponsePrefix
var jsonData models.ResponsePrefix
err = json.Unmarshal([]byte(resp), &jsonData)
if err != nil {
return nil, err
Expand Down Expand Up @@ -185,7 +185,7 @@ func (client *Client) SendRequest(method string, path string, payload interface{
return "", err
}

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
Expand All @@ -201,3 +201,19 @@ func (client *Client) SendRequest(method string, path string, payload interface{

return strbody, nil
}

func (client *Client) GetDeviceType(id int) (*models.ResponseDeviceTypes, error) {
path := fmt.Sprintf("%s%d/", models.PathDeviceTypes, id)
resp, err := client.SendRequest("GET", path, nil, 200)
if err != nil {
return nil, err
}

var jsonData models.ResponseDeviceTypes
err = json.Unmarshal([]byte(resp), &jsonData)
if err != nil {
return nil, err
}

return &jsonData, nil
}
19 changes: 18 additions & 1 deletion models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package models
import "time"

var PathAvailablePrefixes = "/ipam/prefixes/"
var PathDeviceTypes = "/dcim/device-types/"

type ReponseAvailablePrefixes struct {
ID int `json:"id"`
Expand Down Expand Up @@ -98,7 +99,7 @@ type ResponseSites struct {
} `json:"results"`
}

type ReponsePrefix struct {
type ResponsePrefix struct {
Count int `json:"count"`
Next interface{} `json:"next"`
Previous interface{} `json:"previous"`
Expand Down Expand Up @@ -143,3 +144,19 @@ type ReponsePrefix struct {
LastUpdated time.Time `json:"last_updated"`
} `json:"results"`
}

type ResponseDeviceTypes struct {
ID int `json:"id"`
DisplayName string `json:"display"`
Manufacturer struct {
ID int `json:"id"`
DisplayName string `json:"display"`
Name string `json:"name"`
Slug string `json:"slug"`
} `json:"manufacturer"`
Model string `json:"model"`
Slug string `json:"slug"`
PartNumber string `json:"part_number"`
Descrption string `json:"description"`
CustomFields interface{} `json:"custom_fields"`
}
122 changes: 122 additions & 0 deletions provider/data_device_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package provider

import (
"fmt"
"strconv"

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

func dataDeviceType() *schema.Resource {
return &schema.Resource{
Read: dataDeviceTypeRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Required: true,
Description: "A unique integer value identifying this device type.",
},
"displayname": {
Type: schema.TypeString,
Computed: true,
Description: "The display name of the device type.",
},
"manufacturer": {
Type: schema.TypeMap,
Computed: true,
Description: "The manufacturer object of the device type.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Computed: true,
Description: "A unique integer value identifying this manufacturer.",
},
"displayname": {
Type: schema.TypeString,
Computed: true,
Description: "The display name of the manufacturer.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The name of the manufacturer name.",
},
"slug": {
Type: schema.TypeString,
Computed: true,
Description: "The slug of the manufacturer.",
},
},
},
},
"model": {
Type: schema.TypeString,
Computed: true,
Description: "The model of the device type.",
},
"slug": {
Type: schema.TypeString,
Computed: true,
Description: "The slug of the device type.",
},
"part_number": {
Type: schema.TypeString,
Computed: true,
Description: "The part number of the device type.",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "The description of the device type.",
},
"custom_fields": {
Type: schema.TypeMap,
Computed: true,
Description: "The custom fields of the device type.",
Default: nil,
Elem: &schema.Schema{
Type: schema.TypeString,
Default: nil,
},
},
},
}
}

func dataDeviceTypeRead(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*client.Client)

id := d.Get("id").(int)
if id < 0 {
return fmt.Errorf("[ERROR] 'id' cannot be less than 0")
}

resp, err := apiClient.GetDeviceType(id)
if err != nil {
return err
}

cf := getCustomFields(resp.CustomFields)
if cf != nil {
d.Set("custom_fields", cf)
}

d.Set("displayname", resp.DisplayName)
d.Set("manufacturer", resp.Manufacturer)
d.Set("model", resp.Model)
d.Set("slug", resp.Slug)
d.Set("part_number", resp.PartNumber)
d.Set("description", resp.Descrption)
d.SetId(strconv.Itoa(resp.ID))
return nil
}

func getCustomFields(cf interface{}) map[string]interface{} {
cfm, ok := cf.(map[string]interface{})
if !ok || len(cfm) == 0 {
return nil
}
return cfm
}
5 changes: 3 additions & 2 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ func Provider() terraform.ResourceProvider {
"netbox_available_prefix": resourceAvailablePrefixes(),
},
DataSourcesMap: map[string]*schema.Resource{
"netbox_prefix": dataPrefix(),
"netbox_sites": dataSites(),
"netbox_prefix": dataPrefix(),
"netbox_sites": dataSites(),
"netbox_device_type": dataDeviceType(),
},

ConfigureFunc: providerConfigure,
Expand Down
2 changes: 1 addition & 1 deletion provider/resource_availableprefixes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func testAccCheckRegistryDestroy(s *terraform.State) error {

func testAccCheckAvailablePrefix() string {

return fmt.Sprintf(`
return (`
data "netbox_prefix" "prefix" {
cidr_notation = "172.24.0.0/13"
Expand Down

0 comments on commit 1c5e4e6

Please sign in to comment.