From 255905ee5aa74d7c130e238181d9588a69341542 Mon Sep 17 00:00:00 2001 From: 1riatsila1 Date: Tue, 1 Oct 2024 13:41:21 +0200 Subject: [PATCH] feat: make use of post sensor group assignment --- .../test/api_configuration_test.go | 2 +- .../resources/sensor_group_assignment.go | 27 +++++++++-------- .../sensor_group_assignment_test.go | 4 +-- .../resources/agent_group_assignment_test.go | 7 ++--- .../resources/sensor_group_assignment_test.go | 26 ++++++++++------- pkg/config-api-provider/test/util/utils.go | 29 +++++++++++++++---- 6 files changed, 60 insertions(+), 35 deletions(-) diff --git a/pkg/config-api-client/test/api_configuration_test.go b/pkg/config-api-client/test/api_configuration_test.go index 38294199..98227838 100644 --- a/pkg/config-api-client/test/api_configuration_test.go +++ b/pkg/config-api-client/test/api_configuration_test.go @@ -80,7 +80,7 @@ func Test_config_api_client_ConfigurationAPIService(t *testing.T) { }) }) - t.Run("Test ConfigurationAPIService PostUxiV1alpha1SensorGroupAssignmentsGet", func(t *testing.T) { + t.Run("Test ConfigurationAPIService PostUxiV1alpha1SensorGroupAssignmentsPost", func(t *testing.T) { gock.New(configuration.Scheme + "://" + configuration.Host). Post("/uxi/v1alpha1/sensor-group-assignments"). diff --git a/pkg/config-api-provider/provider/resources/sensor_group_assignment.go b/pkg/config-api-provider/provider/resources/sensor_group_assignment.go index a686773e..9e979b37 100644 --- a/pkg/config-api-provider/provider/resources/sensor_group_assignment.go +++ b/pkg/config-api-provider/provider/resources/sensor_group_assignment.go @@ -103,18 +103,21 @@ func (r *sensorGroupAssignmentResource) Create(ctx context.Context, req resource return } - // TODO: Call client createSensorGroupAssignment method - sensorGroupAssignment := CreateSensorGroupAssignment(SensorGroupAssignmentRequestModel{ - GroupUID: plan.GroupID.ValueString(), - SensorUID: plan.SensorID.ValueString(), - }) - - // Update the state to match the plan - plan.ID = types.StringValue(sensorGroupAssignment.UID) - plan.GroupID = types.StringValue(sensorGroupAssignment.GroupUID) - plan.SensorID = types.StringValue(sensorGroupAssignment.SensorUID) - - // Set state to fully populated data + postRequest := config_api_client.NewSensorGroupAssignmentsPostRequest(plan.GroupID.ValueString(), plan.SensorID.ValueString()) + request := r.client.ConfigurationAPI.PostUxiV1alpha1SensorGroupAssignmentsPost(context.Background()).SensorGroupAssignmentsPostRequest(*postRequest) + sensorGroupAssignment, _, err := util.RetryFor429(request.Execute) + if err != nil { + resp.Diagnostics.AddError( + "Error creating sensor group assignment", + "Could not create sensor group assignment, unexpected error: "+err.Error(), + ) + return + } + + plan.ID = types.StringValue(sensorGroupAssignment.Id) + plan.GroupID = types.StringValue(sensorGroupAssignment.Group.Id) + plan.SensorID = types.StringValue(sensorGroupAssignment.Sensor.Id) + diags = resp.State.Set(ctx, plan) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { diff --git a/pkg/config-api-provider/test/data-sources/sensor_group_assignment_test.go b/pkg/config-api-provider/test/data-sources/sensor_group_assignment_test.go index 0a30630a..99651426 100644 --- a/pkg/config-api-provider/test/data-sources/sensor_group_assignment_test.go +++ b/pkg/config-api-provider/test/data-sources/sensor_group_assignment_test.go @@ -23,7 +23,7 @@ func TestSensorGroupAssignmentDataSource(t *testing.T) { PreConfig: func() { util.MockGetSensorGroupAssignment( "uid", - util.GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{util.StructToMap(util.GenerateSensorGroupAssignmentResponse("uid", ""))}), + util.GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{util.GenerateSensorGroupAssignmentGetResponse("uid", "")}), 3, ) }, @@ -67,7 +67,7 @@ func TestSensorGroupAssignmentSource429Handling(t *testing.T) { }) util.MockGetSensorGroupAssignment( "uid", - util.GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{util.StructToMap(util.GenerateSensorGroupAssignmentResponse("uid", ""))}), + util.GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{util.GenerateSensorGroupAssignmentGetResponse("uid", "")}), 3, ) }, diff --git a/pkg/config-api-provider/test/resources/agent_group_assignment_test.go b/pkg/config-api-provider/test/resources/agent_group_assignment_test.go index 0779fbb0..2d24c01d 100644 --- a/pkg/config-api-provider/test/resources/agent_group_assignment_test.go +++ b/pkg/config-api-provider/test/resources/agent_group_assignment_test.go @@ -27,10 +27,9 @@ func TestAgentGroupAssignmentResource(t *testing.T) { // required for group create util.MockPostGroup(util.StructToMap(util.GenerateGroupResponsePostModel("group_uid", "", "")), 1) - util.MockGetGroup("group_uid", util.GeneratePaginatedResponse( - []map[string]interface{}{ - util.StructToMap(util.GenerateGroupResponseGetModel("group_uid", "", "")), - }), + util.MockGetGroup( + "group_uid", + util.GeneratePaginatedResponse([]map[string]interface{}{util.StructToMap(util.GenerateGroupResponseGetModel("group_uid", "", ""))}), 1, ) diff --git a/pkg/config-api-provider/test/resources/sensor_group_assignment_test.go b/pkg/config-api-provider/test/resources/sensor_group_assignment_test.go index 9682cab0..e44629ff 100644 --- a/pkg/config-api-provider/test/resources/sensor_group_assignment_test.go +++ b/pkg/config-api-provider/test/resources/sensor_group_assignment_test.go @@ -34,12 +34,14 @@ func TestSensorGroupAssignmentResource(t *testing.T) { ) // required for sensor group assignment create - resources.CreateSensorGroupAssignment = func(request resources.SensorGroupAssignmentRequestModel) resources.SensorGroupAssignmentResponseModel { - return util.GenerateSensorGroupAssignmentResponse("sensor_group_assignment_uid", "") - } + util.MockPostSensorGroupAssignment( + "sensor_group_assignment_uid", + util.GenerateSensorGroupAssignmentPostResponse("sensor_group_assignment_uid", ""), + 1, + ) util.MockGetSensorGroupAssignment( "sensor_group_assignment_uid", - util.GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{util.StructToMap(util.GenerateSensorGroupAssignmentResponse("sensor_group_assignment_uid", ""))}), + util.GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{util.GenerateSensorGroupAssignmentGetResponse("sensor_group_assignment_uid", "")}), 1, ) }, @@ -77,7 +79,7 @@ func TestSensorGroupAssignmentResource(t *testing.T) { PreConfig: func() { util.MockGetSensorGroupAssignment( "sensor_group_assignment_uid", - util.GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{util.StructToMap(util.GenerateSensorGroupAssignmentResponse("sensor_group_assignment_uid", ""))}), + util.GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{util.GenerateSensorGroupAssignmentGetResponse("sensor_group_assignment_uid", "")}), 1, ) }, @@ -112,18 +114,20 @@ func TestSensorGroupAssignmentResource(t *testing.T) { // required for sensor group assignment create util.MockGetSensorGroupAssignment( "sensor_group_assignment_uid", - util.GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{util.StructToMap(util.GenerateSensorGroupAssignmentResponse("sensor_group_assignment_uid", ""))}), + util.GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{util.GenerateSensorGroupAssignmentGetResponse("sensor_group_assignment_uid", "")}), 1, ) util.MockGetSensorGroupAssignment( "sensor_group_assignment_uid_2", - util.GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{util.StructToMap(util.GenerateSensorGroupAssignmentResponse("sensor_group_assignment_uid_2", "_2"))}), + util.GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{util.GenerateSensorGroupAssignmentGetResponse("sensor_group_assignment_uid_2", "_2")}), 1, ) - resources.CreateSensorGroupAssignment = func(request resources.SensorGroupAssignmentRequestModel) resources.SensorGroupAssignmentResponseModel { - return util.GenerateSensorGroupAssignmentResponse("sensor_group_assignment_uid_2", "_2") - } + util.MockPostSensorGroupAssignment( + "sensor_group_assignment_uid_2", + util.GenerateSensorGroupAssignmentPostResponse("sensor_group_assignment_uid_2", "_2"), + 1, + ) }, Config: provider.ProviderConfig + ` // the original resources @@ -188,7 +192,7 @@ func TestSensorGroupAssignmentResource(t *testing.T) { ) util.MockGetSensorGroupAssignment( "sensor_group_assignment_uid", - util.GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{util.StructToMap(util.GenerateSensorGroupAssignmentResponse("sensor_group_assignment_uid", ""))}), + util.GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{util.GenerateSensorGroupAssignmentGetResponse("sensor_group_assignment_uid", "")}), 1, ) }, diff --git a/pkg/config-api-provider/test/util/utils.go b/pkg/config-api-provider/test/util/utils.go index 2d79978c..84b64335 100644 --- a/pkg/config-api-provider/test/util/utils.go +++ b/pkg/config-api-provider/test/util/utils.go @@ -134,11 +134,19 @@ func GenerateWirelessNetworkPaginatedResponse(wirelessNetworks []map[string]inte } } -func GenerateSensorGroupAssignmentResponse(uid string, postfix string) resources.SensorGroupAssignmentResponseModel { - return resources.SensorGroupAssignmentResponseModel{ - UID: uid, - GroupUID: "group_uid" + postfix, - SensorUID: "sensor_uid" + postfix, +func GenerateSensorGroupAssignmentPostResponse(uid string, postfix string) map[string]interface{} { + return map[string]interface{}{ + "id": uid, + "group": map[string]string{"id": "group_uid" + postfix}, + "sensor": map[string]string{"id": "sensor_uid" + postfix}, + } +} + +func GenerateSensorGroupAssignmentGetResponse(uid string, postfix string) map[string]interface{} { + return map[string]interface{}{ + "uid": uid, + "group_uid": "group_uid" + postfix, + "sensor_uid": "sensor_uid" + postfix, } } @@ -246,3 +254,14 @@ func MockGetSensorGroupAssignment(uid string, response map[string]interface{}, t Reply(200). JSON(response) } + +func MockPostSensorGroupAssignment(uid string, response map[string]interface{}, times int) { + gock.New("https://test.api.capenetworks.com"). + Post("/uxi/v1alpha1/sensor-group-assignments"). + MatchHeader("Content-Type", "application/json"). + MatchHeader("Authorization", "mock_token"). + Times(times). + Reply(200). + JSON(response) + +}