Skip to content

Commit

Permalink
fix: update resource ID on read and update
Browse files Browse the repository at this point in the history
  • Loading branch information
1riatsila1 committed Nov 7, 2024
1 parent 25f92ea commit a93add3
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 8 deletions.
2 changes: 2 additions & 0 deletions internal/provider/resources/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ func (r *agentResource) Read(
}
sensor := sensorResponse.Items[0]

state.ID = types.StringValue(sensor.Id)
state.Name = types.StringValue(sensor.Name)
state.Notes = types.StringPointerValue(sensor.Notes.Get())
state.PCapMode = types.StringPointerValue(sensor.PcapMode.Get())
Expand Down Expand Up @@ -190,6 +191,7 @@ func (r *agentResource) Update(
})

// Update resource state with updated items
plan.ID = types.StringValue(response.UID)
plan.Name = types.StringValue(response.Name)
plan.Notes = types.StringValue(response.Notes)
plan.PCapMode = types.StringValue(response.PCapMode)
Expand Down
1 change: 1 addition & 0 deletions internal/provider/resources/agent_group_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ func (r *agentGroupAssignmentResource) Read(
agentGroupAssignment := agentGroupAssignmentResponse.Items[0]

// Update state from client response
state.ID = types.StringValue(agentGroupAssignment.Id)
state.GroupID = types.StringValue(agentGroupAssignment.Group.Id)
state.AgentID = types.StringValue(agentGroupAssignment.Agent.Id)

Expand Down
2 changes: 2 additions & 0 deletions internal/provider/resources/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func (r *groupResource) Read(
}

// Update state from client response
state.ID = types.StringValue(group.Id)
state.Name = types.StringValue(group.Name)
// only update parent if not attached to root node (else leave it as null)
parentGroup, _ := r.getGroup(ctx, group.Parent.Get().Id)
Expand Down Expand Up @@ -216,6 +217,7 @@ func (r *groupResource) Update(
}

// Update the state to match the plan (replace with response from client)
plan.ID = types.StringValue(group.Id)
plan.Name = types.StringValue(group.Name)
plan.ParentGroupId = types.StringValue(group.Parent.Id)

Expand Down
1 change: 1 addition & 0 deletions internal/provider/resources/network_group_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func (r *networkGroupAssignmentResource) Read(
networkGroupAssignment := networkGroupAssignmentResponse.Items[0]

// Update state from client response
state.ID = types.StringValue(networkGroupAssignment.Id)
state.GroupID = types.StringValue(networkGroupAssignment.Group.Id)
state.NetworkID = types.StringValue(networkGroupAssignment.Network.Id)

Expand Down
10 changes: 6 additions & 4 deletions internal/provider/resources/sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ type SensorResponseModel struct {
WifiMacAddress string
EthernetMacAddress string
AddressNote string
Longitude string
Latitude string
Longitude float32
Latitude float32
Notes string
PCapMode string
}
Expand Down Expand Up @@ -167,6 +167,7 @@ func (r *sensorResource) Read(
sensor := sensorResponse.Items[0]

// Update state from client response
state.ID = types.StringValue(sensor.Id)
state.Name = types.StringValue(sensor.Name)
state.AddressNote = types.StringPointerValue(sensor.AddressNote.Get())
state.Notes = types.StringPointerValue(sensor.Notes.Get())
Expand Down Expand Up @@ -202,6 +203,7 @@ func (r *sensorResource) Update(
})

// Update resource state with updated items
plan.ID = types.StringValue(response.UID)
plan.Name = types.StringValue(response.Name)
plan.AddressNote = types.StringValue(response.AddressNote)
plan.Notes = types.StringValue(response.Notes)
Expand Down Expand Up @@ -250,8 +252,8 @@ var UpdateSensor = func(request SensorUpdateRequestModel) SensorResponseModel {
WifiMacAddress: "mock_wifi_mac_address",
EthernetMacAddress: "mock_ethernet_mac_address",
AddressNote: request.AddressNote,
Longitude: "mock_longitude",
Latitude: "mock_latitude",
Longitude: 0.0,
Latitude: 0.0,
Notes: request.Notes,
PCapMode: request.PCapMode,
}
Expand Down
1 change: 1 addition & 0 deletions internal/provider/resources/sensor_group_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ func (r *sensorGroupAssignmentResource) Read(
sensorGroupAssignment := sensorGroupAssignmentResponse.Items[0]

// Update state from client response
state.ID = types.StringValue(sensorGroupAssignment.Id)
state.GroupID = types.StringValue(sensorGroupAssignment.Group.Id)
state.SensorID = types.StringValue(sensorGroupAssignment.Sensor.Id)

Expand Down
1 change: 1 addition & 0 deletions internal/provider/resources/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func (r *serviceTestResource) Read(
serviceTest := sensorResponse.Items[0]

// Update state from client response
state.ID = types.StringValue(serviceTest.Id)
state.Name = types.StringValue(serviceTest.Name)

// Set refreshed state
Expand Down
1 change: 1 addition & 0 deletions internal/provider/resources/service_group_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ func (r *serviceTestGroupAssignmentResource) Read(
serviceTestGroupAssignment := GetServiceTestGroupAssignment(state.ID.ValueString())

// Update state from client response
state.ID = types.StringValue(serviceTestGroupAssignment.Id)
state.GroupID = types.StringValue(serviceTestGroupAssignment.Group.Id)
state.ServiceTestID = types.StringValue(serviceTestGroupAssignment.ServiceTest.Id)

Expand Down
1 change: 1 addition & 0 deletions internal/provider/resources/wired_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func (r *wiredNetworkResource) Read(
network := networkResponse.Items[0]

// Update state from client response
state.ID = types.StringValue(network.Id)
state.Name = types.StringValue(network.Name)

// Set refreshed state
Expand Down
1 change: 1 addition & 0 deletions internal/provider/resources/wireless_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func (r *wirelessNetworkResource) Read(
network := networkResponse.Items[0]

// Update state from client response
state.ID = types.StringValue(network.Id)
state.Name = types.StringValue(network.Name)

// Set refreshed state
Expand Down
6 changes: 4 additions & 2 deletions test/mocked/resources/agent_group_assignment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ func TestAgentGroupAssignmentResource(t *testing.T) {
PreConfig: func() {
// required for agent import
util.MockGetAgent(
"uid",
"agent_uid",
util.GeneratePaginatedResponse(
[]map[string]interface{}{util.GenerateAgentResponseModel("uid", "")},
[]map[string]interface{}{
util.GenerateAgentResponseModel("agent_uid", ""),
},
),
2,
)
Expand Down
9 changes: 7 additions & 2 deletions test/mocked/resources/agent_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package resource_test

import (
"regexp"
"testing"

"github.com/aruba-uxi/terraform-provider-configuration/internal/provider/resources"
"github.com/aruba-uxi/terraform-provider-configuration/test/mocked/provider"
"github.com/aruba-uxi/terraform-provider-configuration/test/mocked/util"
"github.com/h2non/gock"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
"github.com/nbio/st"
"regexp"
"testing"
)

func TestAgentResource(t *testing.T) {
Expand Down Expand Up @@ -88,6 +90,9 @@ func TestAgentResource(t *testing.T) {
),
1,
)
resources.UpdateAgent = func(request resources.AgentUpdateRequestModel) resources.AgentResponseModel {
return util.GenerateMockedAgentResponseModel("uid", "_2")
}
// updated
util.MockGetAgent(
"uid",
Expand Down
4 changes: 4 additions & 0 deletions test/mocked/resources/sensor_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package resource_test

import (
"github.com/aruba-uxi/terraform-provider-configuration/internal/provider/resources"
"github.com/aruba-uxi/terraform-provider-configuration/test/mocked/provider"
"github.com/aruba-uxi/terraform-provider-configuration/test/mocked/util"
"regexp"
Expand Down Expand Up @@ -91,6 +92,9 @@ func TestSensorResource(t *testing.T) {
[]map[string]interface{}{util.GenerateSensorResponseModel("uid", "")}),
1,
)
resources.UpdateSensor = func(request resources.SensorUpdateRequestModel) resources.SensorResponseModel {
return util.GenerateMockedSensorResponseModel("uid", "_2")
}
// updated sensor
util.MockGetSensor("uid", util.GeneratePaginatedResponse(
[]map[string]interface{}{util.GenerateSensorResponseModel("uid", "_2")}),
Expand Down
16 changes: 16 additions & 0 deletions test/mocked/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ func GenerateSensorResponseModel(uid string, postfix string) map[string]interfac
}
}

func GenerateMockedSensorResponseModel(uid string, postfix string) resources.SensorResponseModel {
return resources.SensorResponseModel{
UID: uid,
Serial: "serial" + postfix,
Name: "name" + postfix,
ModelNumber: "model_number" + postfix,
WifiMacAddress: "wifi_mac_address" + postfix,
EthernetMacAddress: "ethernet_mac_address" + postfix,
AddressNote: "address_note" + postfix,
Longitude: 0.0,
Latitude: 0.0,
Notes: "notes" + postfix,
PCapMode: "light" + postfix,
}
}

func GenerateAgentResponseModel(uid string, postfix string) map[string]interface{} {
return map[string]interface{}{
"id": uid,
Expand Down

0 comments on commit a93add3

Please sign in to comment.