Skip to content

Commit

Permalink
fix: Store empty static String field as nil in TF state
Browse files Browse the repository at this point in the history
The 'static' String fields (description/dns_name) when empty are
returned via the Netbox API as an empty String `""` but they cannot
be cleared by POST/PATCH-ing an emptry String or null value. They are
cleared by setting a single whitespace character `" "`, before this
change these empty fields were always stored in the TF state as an existent
resource property even though they were unset. This also resulted in
these whitespace values being sent to Netbox every time a resource was
created/updated.

This change stores unset fields as `nil` and sets the default values of
the description and dns_name property to `nil`. Empty String fields are
stored as nil when reading from Netbox and nil fields are POST/PATCH-ed
to Netbox as the single whitespace String `" "`. When unset the
properties will not be sent in the request body to Netbox and will not
show in the TF show command.
  • Loading branch information
bmhughes committed Dec 23, 2021
1 parent d56574b commit 00dd09e
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 53 deletions.
13 changes: 8 additions & 5 deletions netbox/resource_netbox_ipam_aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func resourceNetboxIpamAggregate() *schema.Resource {
"description": {
Type: schema.TypeString,
Optional: true,
Default: " ",
Default: nil,
ValidateFunc: validation.StringLenBetween(1, 200),
},
"prefix": {
Expand Down Expand Up @@ -171,9 +171,9 @@ func resourceNetboxIpamAggregateRead(d *schema.ResourceData,
return err
}

var description string
var description interface{}
if resource.Description == "" {
description = " "
description = nil
} else {
description = resource.Description
}
Expand Down Expand Up @@ -241,8 +241,11 @@ func resourceNetboxIpamAggregateUpdate(d *schema.ResourceData,
}

if d.HasChange("description") {
description := d.Get("description").(string)
params.Description = description
if description, exist := d.GetOk("description"); exist {
params.Description = description.(string)
} else {
params.Description = " "
}
}

tags := d.Get("tag").(*schema.Set).List()
Expand Down
18 changes: 11 additions & 7 deletions netbox/resource_netbox_ipam_ip_addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ func resourceNetboxIpamIPAddresses() *schema.Resource {
"description": {
Type: schema.TypeString,
Optional: true,
Default: " ",
Default: nil,
ValidateFunc: validation.StringLenBetween(1, 100),
},
"dns_name": {
Type: schema.TypeString,
Optional: true,
Default: " ",
Default: nil,
ValidateFunc: validation.StringMatch(
regexp.MustCompile("^[-a-zA-Z0-9_.]{1,255}$"),
"Must be like ^[-a-zA-Z0-9_.]{1,255}$"),
Expand Down Expand Up @@ -228,9 +228,9 @@ func resourceNetboxIpamIPAddressesRead(d *schema.ResourceData,
return err
}

var description string
var description interface{}
if resource.Description == "" {
description = " "
description = nil
} else {
description = resource.Description
}
Expand All @@ -239,9 +239,9 @@ func resourceNetboxIpamIPAddressesRead(d *schema.ResourceData,
return err
}

var dnsName string
var dnsName interface{}
if resource.DNSName == "" {
dnsName = " "
dnsName = nil
} else {
dnsName = resource.DNSName
}
Expand Down Expand Up @@ -386,7 +386,11 @@ func resourceNetboxIpamIPAddressesUpdate(d *schema.ResourceData,
}

if d.HasChange("dns_name") {
params.DNSName = d.Get("dns_name").(string)
if dnsName, exist := d.GetOk("dns_name"); exist {
params.DNSName = dnsName.(string)
} else {
params.DNSName = " "
}
}

if d.HasChange("nat_inside_id") {
Expand Down
14 changes: 8 additions & 6 deletions netbox/resource_netbox_ipam_prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func resourceNetboxIpamPrefix() *schema.Resource {
"description": {
Type: schema.TypeString,
Optional: true,
Default: " ",
Default: nil,
ValidateFunc: validation.StringLenBetween(1, 100),
},
"is_pool": {
Expand Down Expand Up @@ -186,10 +186,9 @@ func resourceNetboxIpamPrefixRead(d *schema.ResourceData,
return err
}

var description string

var description interface{}
if resource.Description == "" {
description = " "
description = nil
} else {
description = resource.Description
}
Expand Down Expand Up @@ -294,8 +293,11 @@ func resourceNetboxIpamPrefixUpdate(d *schema.ResourceData,
}

if d.HasChange("description") {
description := d.Get("description").(string)
params.Description = description
if description, exist := d.GetOk("description"); exist {
params.Description = description.(string)
} else {
params.Description = " "
}
}

params.IsPool = d.Get("is_pool").(bool)
Expand Down
14 changes: 8 additions & 6 deletions netbox/resource_netbox_ipam_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func resourceNetboxIpamService() *schema.Resource {
"description": {
Type: schema.TypeString,
Optional: true,
Default: " ",
Default: nil,
ValidateFunc: validation.StringLenBetween(1, 200),
},
"device_id": {
Expand Down Expand Up @@ -178,10 +178,9 @@ func resourceNetboxIpamServiceRead(d *schema.ResourceData,
return err
}

var description string

var description interface{}
if resource.Description == "" {
description = " "
description = nil
} else {
description = resource.Description
}
Expand Down Expand Up @@ -289,8 +288,11 @@ func resourceNetboxIpamServiceUpdate(d *schema.ResourceData,
}

if d.HasChange("description") {
description := d.Get("description").(string)
params.Description = description
if description, exist := d.GetOk("description"); exist {
params.Description = description.(string)
} else {
params.Description = " "
}
}

tags := d.Get("tag").(*schema.Set).List()
Expand Down
14 changes: 8 additions & 6 deletions netbox/resource_netbox_ipam_vlan.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func resourceNetboxIpamVlan() *schema.Resource {
"description": {
Type: schema.TypeString,
Optional: true,
Default: " ",
Default: nil,
ValidateFunc: validation.StringLenBetween(1, 100),
},
"vlan_group_id": {
Expand Down Expand Up @@ -175,10 +175,9 @@ func resourceNetboxIpamVlanRead(d *schema.ResourceData,
return err
}

var description string

var description interface{}
if resource.Description == "" {
description = " "
description = nil
} else {
description = resource.Description
}
Expand Down Expand Up @@ -270,8 +269,11 @@ func resourceNetboxIpamVlanUpdate(d *schema.ResourceData,
params.Vid = &vid

if d.HasChange("description") {
description := d.Get("description").(string)
params.Description = description
if description, exist := d.GetOk("description"); exist {
params.Description = description.(string)
} else {
params.Description = " "
}
}

if d.HasChange("custom_field") {
Expand Down
27 changes: 16 additions & 11 deletions netbox/resource_netbox_tenancy_tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func resourceNetboxTenancyTenant() *schema.Resource {
"comments": {
Type: schema.TypeString,
Optional: true,
Default: " ",
Default: nil,
},
"custom_field": {
Type: schema.TypeSet,
Expand All @@ -54,7 +54,7 @@ func resourceNetboxTenancyTenant() *schema.Resource {
"description": {
Type: schema.TypeString,
Optional: true,
Default: " ",
Default: nil,
ValidateFunc: validation.StringLenBetween(1, 100),
},
"tenant_group_id": {
Expand Down Expand Up @@ -145,10 +145,10 @@ func resourceNetboxTenancyTenantRead(d *schema.ResourceData,

for _, resource := range resources.Payload.Results {
if strconv.FormatInt(resource.ID, 10) == d.Id() {
var comments string

var comments interface{}
if resource.Comments == "" {
comments = " "
comments = nil
} else {
comments = resource.Comments
}
Expand All @@ -164,10 +164,9 @@ func resourceNetboxTenancyTenantRead(d *schema.ResourceData,
return err
}

var description string

var description interface{}
if resource.Description == "" {
description = " "
description = nil
} else {
description = resource.Description
}
Expand Down Expand Up @@ -219,8 +218,11 @@ func resourceNetboxTenancyTenantUpdate(d *schema.ResourceData,
params.Slug = &slug

if d.HasChange("comments") {
comments := d.Get("comments").(string)
params.Comments = comments
if comments, exist := d.GetOk("comments"); exist {
params.Comments = comments.(string)
} else {
params.Comments = " "
}
}

if d.HasChange("custom_field") {
Expand All @@ -230,8 +232,11 @@ func resourceNetboxTenancyTenantUpdate(d *schema.ResourceData,
}

if d.HasChange("description") {
description := d.Get("description").(string)
params.Description = description
if description, exist := d.GetOk("description"); exist {
params.Description = description.(string)
} else {
params.Description = " "
}
}

if d.HasChange("tenant_group_id") {
Expand Down
14 changes: 8 additions & 6 deletions netbox/resource_netbox_virtualization_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func resourceNetboxVirtualizationInterface() *schema.Resource {
"description": {
Type: schema.TypeString,
Optional: true,
Default: " ",
Default: nil,
ValidateFunc: validation.StringLenBetween(1, 200),
},
"enabled": {
Expand Down Expand Up @@ -199,10 +199,9 @@ func resourceNetboxVirtualizationInterfaceRead(d *schema.ResourceData,
return err
}

var description string

var description interface{}
if resource.Description == "" {
description = " "
description = nil
} else {
description = resource.Description
}
Expand Down Expand Up @@ -293,8 +292,11 @@ func resourceNetboxVirtualizationInterfaceUpdate(d *schema.ResourceData,
}

if d.HasChange("description") {
description := d.Get("description").(string)
params.Description = description
if description, exist := d.GetOk("description"); exist {
params.Description = description.(string)
} else {
params.Description = " "
}
}

if d.HasChange("enabled") {
Expand Down
14 changes: 8 additions & 6 deletions netbox/resource_netbox_virtualization_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func resourceNetboxVirtualizationVM() *schema.Resource {
"comments": {
Type: schema.TypeString,
Optional: true,
Default: " ",
Default: nil,
},
"custom_field": {
Type: schema.TypeSet,
Expand Down Expand Up @@ -218,10 +218,9 @@ func resourceNetboxVirtualizationVMRead(d *schema.ResourceData,
return err
}

var comments string

var comments interface{}
if resource.Comments == "" {
comments = " "
comments = nil
} else {
comments = resource.Comments
}
Expand Down Expand Up @@ -316,8 +315,11 @@ func resourceNetboxVirtualizationVMUpdate(d *schema.ResourceData,
params.Cluster = &clusterID

if d.HasChange("comments") {
comments := d.Get("comments").(string)
params.Comments = comments
if comments, exist := d.GetOk("comments"); exist {
params.Comments = comments.(string)
} else {
params.Comments = " "
}
}

if d.HasChange("custom_field") {
Expand Down

0 comments on commit 00dd09e

Please sign in to comment.