diff --git a/internal/provider/resources/agent.go b/internal/provider/resources/agent.go index 0a345c16..e3019848 100644 --- a/internal/provider/resources/agent.go +++ b/internal/provider/resources/agent.go @@ -166,13 +166,20 @@ func (r *agentResource) Update( return } + errorSummary := util.GenerateErrorSummary("update", "uxi_agent") patchRequest := config_api_client.NewAgentsPatchRequest() patchRequest.Name = plan.Name.ValueStringPointer() if !plan.Notes.IsUnknown() { patchRequest.Notes = plan.Notes.ValueStringPointer() } - if !plan.PCapMode.IsUnknown() { - patchRequest.PcapMode = plan.PCapMode.ValueStringPointer() + plannedPcapMode := plan.PCapMode.ValueStringPointer() + if !plan.PCapMode.IsUnknown() && plannedPcapMode != nil { + pcapMode, err := config_api_client.NewPcapModeFromValue(*plannedPcapMode) + if err != nil { + resp.Diagnostics.AddError(errorSummary, err.Error()) + return + } + patchRequest.PcapMode = pcapMode } request := r.client.ConfigurationAPI. AgentsPatch(ctx, plan.ID.ValueString()). @@ -182,7 +189,7 @@ func (r *agentResource) Update( errorPresent, errorDetail := util.RaiseForStatus(response, err) if errorPresent { - resp.Diagnostics.AddError(util.GenerateErrorSummary("update", "uxi_agent"), errorDetail) + resp.Diagnostics.AddError(errorSummary, errorDetail) return } @@ -193,7 +200,7 @@ func (r *agentResource) Update( plan.Notes = types.StringValue(*agent.Notes.Get()) } if agent.PcapMode.Get() != nil { - plan.PCapMode = types.StringValue(*agent.PcapMode.Get()) + plan.PCapMode = types.StringValue(string(*agent.PcapMode.Get())) } // Set state to fully populated data diff --git a/internal/provider/resources/sensor.go b/internal/provider/resources/sensor.go index 95077a86..40406aac 100644 --- a/internal/provider/resources/sensor.go +++ b/internal/provider/resources/sensor.go @@ -169,11 +169,20 @@ func (r *sensorResource) Update( return } + errorSummary := util.GenerateErrorSummary("update", "uxi_sensor") patchRequest := config_api_client.NewSensorsPatchRequest() patchRequest.Name = plan.Name.ValueStringPointer() patchRequest.AddressNote = plan.AddressNote.ValueStringPointer() patchRequest.Notes = plan.Notes.ValueStringPointer() - patchRequest.PcapMode = plan.PCapMode.ValueStringPointer() + plannedPcapMode := plan.PCapMode.ValueStringPointer() + if !plan.PCapMode.IsUnknown() && plannedPcapMode != nil { + pcapMode, err := config_api_client.NewPcapModeFromValue(*plannedPcapMode) + if err != nil { + resp.Diagnostics.AddError(errorSummary, err.Error()) + return + } + patchRequest.PcapMode = pcapMode + } request := r.client.ConfigurationAPI. SensorsPatch(ctx, plan.ID.ValueString()). @@ -183,7 +192,7 @@ func (r *sensorResource) Update( errorPresent, errorDetail := util.RaiseForStatus(response, err) if errorPresent { - resp.Diagnostics.AddError(util.GenerateErrorSummary("update", "uxi_sensor"), errorDetail) + resp.Diagnostics.AddError(errorSummary, errorDetail) return } @@ -191,7 +200,9 @@ func (r *sensorResource) Update( plan.Name = types.StringValue(sensor.Name) plan.AddressNote = types.StringPointerValue(sensor.AddressNote.Get()) plan.Notes = types.StringPointerValue(sensor.Notes.Get()) - plan.PCapMode = types.StringPointerValue(sensor.PcapMode.Get()) + if sensor.PcapMode.Get() != nil { + plan.PCapMode = types.StringValue(string(*sensor.PcapMode.Get())) + } diags = resp.State.Set(ctx, plan) resp.Diagnostics.Append(diags...) diff --git a/pkg/config-api-client/test/api_configuration_test.go b/pkg/config-api-client/test/api_configuration_test.go index b1acec16..2dd40ff3 100644 --- a/pkg/config-api-client/test/api_configuration_test.go +++ b/pkg/config-api-client/test/api_configuration_test.go @@ -122,7 +122,7 @@ func TestConfigurationAPI(t *testing.T) { WifiMacAddress: *config_api_client.NewNullableString(&wifiMacAddress), EthernetMacAddress: *config_api_client.NewNullableString(ðernetMacAddress), Notes: *config_api_client.NewNullableString(¬es), - PcapMode: *config_api_client.NewNullableString(&pcapMode), + PcapMode: *config_api_client.NewNullablePcapMode(&pcapMode), Type: "networking-uxi/agent", }) }) @@ -296,7 +296,7 @@ func TestConfigurationAPI(t *testing.T) { var Longitude float32 = 0.0 var Latitude float32 = 0.0 Notes := "notes" - pcapMode := config_api_client.LIGHT + pcapMode := "light" require.Nil(t, err) assert.Equal(t, http.StatusOK, httpRes.StatusCode) @@ -313,7 +313,7 @@ func TestConfigurationAPI(t *testing.T) { Longitude: *config_api_client.NewNullableFloat32(&Longitude), Latitude: *config_api_client.NewNullableFloat32(&Latitude), Notes: *config_api_client.NewNullableString(&Notes), - PcapMode: *config_api_client.NewNullablePcapMode(&pcapMode), + PcapMode: *config_api_client.NewNullableString(&pcapMode), Type: "networking-uxi/sensor", }, }, @@ -346,7 +346,7 @@ func TestConfigurationAPI(t *testing.T) { name := "new_name" addressNote := "new_address_note" notes := "new_notes" - pcapMode := "off" + pcapMode := config_api_client.OFF sensorsPatchRequest := config_api_client.SensorsPatchRequest{ Name: &name, AddressNote: &addressNote, @@ -376,7 +376,7 @@ func TestConfigurationAPI(t *testing.T) { Longitude: *config_api_client.NewNullableFloat32(&longitude), Latitude: *config_api_client.NewNullableFloat32(&latitude), Notes: *config_api_client.NewNullableString(¬es), - PcapMode: *config_api_client.NewNullableString(&pcapMode), + PcapMode: *config_api_client.NewNullablePcapMode(&pcapMode), Type: "networking-uxi/sensor", }) }) diff --git a/test/mocked/resources/agent_group_assignment_test.go b/test/mocked/resources/agent_group_assignment_test.go index 183959af..36392a37 100644 --- a/test/mocked/resources/agent_group_assignment_test.go +++ b/test/mocked/resources/agent_group_assignment_test.go @@ -245,7 +245,7 @@ func TestAgentGroupAssignmentResource(t *testing.T) { resource "uxi_agent" "my_agent_2" { name = "name_2" notes = "notes_2" - pcap_mode = "light_2" + pcap_mode = "light" } import { diff --git a/test/mocked/resources/agent_test.go b/test/mocked/resources/agent_test.go index 86a921bd..572cf153 100644 --- a/test/mocked/resources/agent_test.go +++ b/test/mocked/resources/agent_test.go @@ -109,12 +109,12 @@ func TestAgentResource(t *testing.T) { resource "uxi_agent" "my_agent" { name = "name_2" notes = "notes_2" - pcap_mode = "light_2" + pcap_mode = "light" }`, Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("uxi_agent.my_agent", "name", "name_2"), resource.TestCheckResourceAttr("uxi_agent.my_agent", "notes", "notes_2"), - resource.TestCheckResourceAttr("uxi_agent.my_agent", "pcap_mode", "light_2"), + resource.TestCheckResourceAttr("uxi_agent.my_agent", "pcap_mode", "light"), ), }, // Delete testing @@ -212,7 +212,7 @@ func TestAgentResourceTooManyRequestsHandling(t *testing.T) { resource "uxi_agent" "my_agent" { name = "name_2" notes = "notes_2" - pcap_mode = "light_2" + pcap_mode = "light" }`, Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("uxi_agent.my_agent", "name", "name_2"), @@ -366,7 +366,7 @@ func TestAgentResourceHttpErrorHandling(t *testing.T) { resource "uxi_agent" "my_agent" { name = "name_2" notes = "notes_2" - pcap_mode = "light_2" + pcap_mode = "light" }`, ExpectError: regexp.MustCompile( `(?s)Unable to update agent - pcap_mode must be one the following \['light',\s*'full', 'off'\].\s*DebugID: 12312-123123-123123-1231212`, diff --git a/test/mocked/resources/sensor_group_assignment_test.go b/test/mocked/resources/sensor_group_assignment_test.go index 7f27bcef..fcecafb3 100644 --- a/test/mocked/resources/sensor_group_assignment_test.go +++ b/test/mocked/resources/sensor_group_assignment_test.go @@ -250,7 +250,7 @@ func TestSensorGroupAssignmentResource(t *testing.T) { name = "name_2" address_note = "address_note_2" notes = "notes_2" - pcap_mode = "light_2" + pcap_mode = "light" } import { diff --git a/test/mocked/resources/sensor_test.go b/test/mocked/resources/sensor_test.go index 9dd373c1..da99ead6 100644 --- a/test/mocked/resources/sensor_test.go +++ b/test/mocked/resources/sensor_test.go @@ -110,7 +110,7 @@ func TestSensorResource(t *testing.T) { name = "name_2" address_note = "address_note_2" notes = "notes_2" - pcap_mode = "light_2" + pcap_mode = "light" }`, Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("uxi_sensor.my_sensor", "name", "name_2"), @@ -120,7 +120,7 @@ func TestSensorResource(t *testing.T) { "address_note_2", ), resource.TestCheckResourceAttr("uxi_sensor.my_sensor", "notes", "notes_2"), - resource.TestCheckResourceAttr("uxi_sensor.my_sensor", "pcap_mode", "light_2"), + resource.TestCheckResourceAttr("uxi_sensor.my_sensor", "pcap_mode", "light"), resource.TestCheckResourceAttr("uxi_sensor.my_sensor", "id", "id"), ), }, @@ -228,7 +228,7 @@ func TestSensorResourceTooManyRequestsHandling(t *testing.T) { name = "name_2" address_note = "address_note_2" notes = "notes_2" - pcap_mode = "light_2" + pcap_mode = "light" }`, Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("uxi_sensor.my_sensor", "name", "name_2"), @@ -378,7 +378,7 @@ func TestSensorResourceHttpErrorHandling(t *testing.T) { name = "name_2" address_note = "address_note_2" notes = "notes_2" - pcap_mode = "light_2" + pcap_mode = "light" }`, ExpectError: regexp.MustCompile( `(?s)Unable to update sensor - pcap_mode must be one the following \['light',\s*'full', 'off'\].\s*DebugID: 12312-123123-123123-1231212`, diff --git a/test/mocked/util/utils.go b/test/mocked/util/utils.go index 9e2b1eff..ecebb849 100644 --- a/test/mocked/util/utils.go +++ b/test/mocked/util/utils.go @@ -19,7 +19,7 @@ func GenerateSensorResponseModel(id string, postfix string) map[string]interface "longitude": 0.0, "latitude": 0.0, "notes": "notes" + postfix, - "pcapMode": "light" + postfix, + "pcapMode": "light", "type": "networking-uxi/sensor", } } @@ -29,7 +29,7 @@ func GenerateSensorRequestUpdateModel(postfix string) map[string]interface{} { "name": "name" + postfix, "addressNote": "address_note" + postfix, "notes": "notes" + postfix, - "pcapMode": "light" + postfix, + "pcapMode": "light", } } @@ -37,7 +37,7 @@ func GenerateAgentRequestUpdateModel(postfix string) map[string]interface{} { return map[string]interface{}{ "name": "name" + postfix, "notes": "notes" + postfix, - "pcapMode": "light" + postfix, + "pcapMode": "light", } } @@ -50,7 +50,7 @@ func GenerateAgentResponseModel(id string, postfix string) map[string]interface{ "wifiMacAddress": "wifi_mac_address" + postfix, "ethernetMacAddress": "ethernet_mac_address" + postfix, "notes": "notes" + postfix, - "pcapMode": "light" + postfix, + "pcapMode": "light", "type": "networking-uxi/sensor", } }