Skip to content

Commit

Permalink
fix: avoid override of device's values on primary ip creation and upd…
Browse files Browse the repository at this point in the history
…ate (#610)

* fix: avoid override of device's values on primary ip creation and update

* fix primary ip race condition on update

* chore: add tests on device_primary_ip behaviour upon device update
  • Loading branch information
thibaultbustarret-ovhcloud authored Jul 30, 2024
1 parent aac34b5 commit 9e7cbea
Show file tree
Hide file tree
Showing 3 changed files with 689 additions and 49 deletions.
12 changes: 0 additions & 12 deletions netbox/resource_netbox_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,18 +464,6 @@ func resourceNetboxDeviceUpdate(ctx context.Context, d *schema.ResourceData, m i
data.ConfigTemplate = &configTemplateID
}

primaryIP4Value, ok := d.GetOk("primary_ipv4")
if ok {
primaryIP4 := int64(primaryIP4Value.(int))
data.PrimaryIp4 = &primaryIP4
}

primaryIP6Value, ok := d.GetOk("primary_ipv6")
if ok {
primaryIP6 := int64(primaryIP6Value.(int))
data.PrimaryIp6 = &primaryIP6
}

data.Rack = getOptionalInt(d, "rack_id")
data.Face = getOptionalStr(d, "rack_face", false)
data.Position = getOptionalFloat(d, "rack_position")
Expand Down
40 changes: 3 additions & 37 deletions netbox/resource_netbox_device_primary_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func resourceNetboxDevicePrimaryIPUpdate(d *schema.ResourceData, m interface{})

// then update the FULL device with ALL tracked attributes
data := models.WritableDeviceWithConfigContext{}

data.Name = device.Name
data.Tags = device.Tags
// the netbox API sends the URL property as part of NestedTag, but it does not accept the URL property when we send it back
Expand All @@ -111,25 +112,14 @@ func resourceNetboxDevicePrimaryIPUpdate(d *schema.ResourceData, m interface{})
tag.Display = ""
}

data.Comments = device.Comments
data.Serial = device.Serial

if device.DeviceType != nil {
data.DeviceType = &device.DeviceType.ID
}

if device.Cluster != nil {
data.Cluster = &device.Cluster.ID
}

if device.Site != nil {
data.Site = &device.Site.ID
}

if device.Location != nil {
data.Location = &device.Location.ID
}

if device.Role != nil {
data.Role = &device.Role.ID
}
Expand All @@ -142,30 +132,6 @@ func resourceNetboxDevicePrimaryIPUpdate(d *schema.ResourceData, m interface{})
data.PrimaryIp6 = &device.PrimaryIp6.ID
}

if device.Platform != nil {
data.Platform = &device.Platform.ID
}

if device.Tenant != nil {
data.Tenant = &device.Tenant.ID
}

if device.Status != nil {
data.Status = *device.Status.Value
}

if device.Rack != nil {
data.Rack = &device.Rack.ID
}

if device.Face != nil {
data.Face = *device.Face.Value
}

if device.Position != nil {
data.Position = device.Position
}

// unset primary ip address if -1 is passed as id
if IPAddressID == -1 {
if IPAddressVersion == 4 {
Expand All @@ -181,9 +147,9 @@ func resourceNetboxDevicePrimaryIPUpdate(d *schema.ResourceData, m interface{})
}
}

updateParams := dcim.NewDcimDevicesUpdateParams().WithID(deviceID).WithData(&data)
updateParams := dcim.NewDcimDevicesPartialUpdateParams().WithID(deviceID).WithData(&data)

_, err = api.Dcim.DcimDevicesUpdate(updateParams, nil)
_, err = api.Dcim.DcimDevicesPartialUpdate(updateParams, nil)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 9e7cbea

Please sign in to comment.