Skip to content

Commit

Permalink
feat: Add ipam rir resource
Browse files Browse the repository at this point in the history
  • Loading branch information
amhn committed Nov 18, 2022
1 parent 02ee807 commit 2aa2edd
Show file tree
Hide file tree
Showing 8 changed files with 581 additions and 14 deletions.
13 changes: 8 additions & 5 deletions examples/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,13 @@ resource "netbox_virtualization_vm" "vm_test" {
}
}

resource "netbox_ipam_rir" "rir_test" {
name = "Test RIR"
slug = "test-rir"

description = "Test RIR"
}

resource "netbox_ipam_service" "service_test" {
name = "SMTP"
virtualmachine_id = netbox_virtualization_vm.vm_test.id
Expand Down Expand Up @@ -721,13 +728,9 @@ resource "netbox_virtualization_interface" "interface_test" {
description = "Interface de test"
}

data "netbox_json_ipam_rirs_list" "json_rir" {
limit = 1
}

resource "netbox_ipam_aggregate" "aggregate_test" {
prefix = "192.167.0.0/24"
rir_id = jsondecode(data.netbox_json_ipam_rirs_list.json_rir.json)[0].id
rir_id = netbox_ipam_rir.rir_test.id
date_added = "2020-12-21"
description = "Aggregate created by terraform"

Expand Down
2 changes: 1 addition & 1 deletion examples/resources/netbox_ipam_aggregate/resource.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
resource "netbox_ipam_aggregate" "aggregate_test" {
prefix = "192.168.56.0/24"
rir_id = 1
rir_id = netbox_ipam_rir.rir_test.id
date_created = "2020-12-21"

tag {
Expand Down
83 changes: 83 additions & 0 deletions examples/resources/netbox_ipam_rir/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
resource "netbox_ipam_rir" "rir_test" {
name = "Test RIR"
slug = "test-rir"

description = "Test RIR"

tag {
name = "tag1"
slug = "tag1"
}

custom_field {
name = "cf_boolean"
type = "boolean"
value = "true"
}

custom_field {
name = "cf_date"
type = "date"
value = "2020-12-25"
}

custom_field {
name = "cf_text"
type = "text"
value = "some text"
}

custom_field {
name = "cf_integer"
type = "integer"
value = "10"
}

custom_field {
name = "cf_selection"
type = "select"
value = "1"
}

custom_field {
name = "cf_url"
type = "url"
value = "https://github.com"
}

custom_field {
name = "cf_multi_selection"
type = "multiselect"
value = jsonencode([
"0",
"1"
])
}

custom_field {
name = "cf_json"
type = "json"
value = jsonencode({
stringvalue = "string"
boolvalue = false
dictionary = {
numbervalue = 5
}
})
}

custom_field {
name = "cf_object"
type = "object"
value = 1
}

custom_field {
name = "cf_multi_object"
type = "multiobject"
value = jsonencode([
1,
2
])
}
}
80 changes: 80 additions & 0 deletions netbox/internal/requestmodifier/netboxrequestmodifier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package requestmodifier

import (
"encoding/json"
"fmt"

"github.com/go-openapi/runtime"
"github.com/go-openapi/strfmt"
)

type netboxRequestModifier struct {
origwriter runtime.ClientRequestWriter
modifiedFields map[string]interface{}
requiredFields []string
}

func (o netboxRequestModifier) WriteToRequest(r runtime.ClientRequest, reg strfmt.Registry) error {
err := o.origwriter.WriteToRequest(r, reg)
if err != nil {
return err
}

jsonString, err := json.Marshal(r.GetBodyParam())
if err != nil {
return err
}

var objmap map[string]interface{}
err = json.Unmarshal(jsonString, &objmap)
if err != nil {
return err
}

for _, k := range o.requiredFields {
if val, ok := objmap[k]; ok && (val == nil || val == "0001-01-01T00:00:00.000Z") {
delete(objmap, k)
}
}

for k, v := range o.modifiedFields {
switch v.(type) {
case int64:
if v == int64(0) {
objmap[k] = nil
}
case float64:
if v == float64(0) {
objmap[k] = nil
}
case bool:
if v == false {
objmap[k] = false
}
case string:
if v == "" {
objmap[k] = ""
}
case nil:
objmap[k] = nil
default:
return fmt.Errorf("unknown type for '%s'", k)
}
}

err = r.SetBodyParam(objmap)
return err
}

func NewNetboxRequestModifier(modifiedFields map[string]interface{}, requiredFields []string) func(*runtime.ClientOperation) {
return func(op *runtime.ClientOperation) {
if len(modifiedFields) > 0 || len(requiredFields) > 0 {
tmp := netboxRequestModifier{
origwriter: op.Params,
modifiedFields: modifiedFields,
requiredFields: requiredFields,
}
op.Params = tmp
}
}
}
12 changes: 4 additions & 8 deletions netbox/ipam/resource_netbox_ipam_aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,9 @@ func TestAccNetboxIpamAggregateMininmalFullMinimal(t *testing.T) {

func testAccCheckNetboxIPAMAggregateConfig(nameSuffix string, resourceFull, extraResources bool, prefix string) string {
template := `
#resource "netbox_ipam_rir" "test" {
# name = "test-{{ .namesuffix }}"
# slug = "test-{{ .namesuffix }}"
#}
data "netbox_json_ipam_rirs_list" "json_rir" {
limit = 1
resource "netbox_ipam_rir" "test" {
name = "test-{{ .namesuffix }}"
slug = "test-{{ .namesuffix }}"
}
{{ if eq .extraresources "true" }}
Expand All @@ -117,8 +114,7 @@ func testAccCheckNetboxIPAMAggregateConfig(nameSuffix string, resourceFull, extr
resource "netbox_ipam_aggregate" "test" {
prefix = "{{ .prefix }}"
rir_id = jsondecode(data.netbox_json_ipam_rirs_list.json_rir.json)[0].id
#rir_id = netbox_ipam_rir.test.id
rir_id = netbox_ipam_rir.test.id
{{ if eq .resourcefull "true" }}
tenant_id = netbox_tenancy_tenant.test.id
Expand Down
Loading

0 comments on commit 2aa2edd

Please sign in to comment.