-
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
8 changed files
with
581 additions
and
14 deletions.
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
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,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 | ||
]) | ||
} | ||
} |
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,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 | ||
} | ||
} | ||
} |
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
Oops, something went wrong.