Skip to content

Commit

Permalink
feat: make use of post sensor group assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
1riatsila1 committed Oct 1, 2024
1 parent 7b22355 commit 255905e
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 35 deletions.
2 changes: 1 addition & 1 deletion pkg/config-api-client/test/api_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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").
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 114 in pkg/config-api-provider/provider/resources/sensor_group_assignment.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/sensor_group_assignment.go#L110-L114

Added lines #L110 - L114 were not covered by tests
}

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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
},
Expand Down Expand Up @@ -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,
)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
},
Expand Down Expand Up @@ -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,
)
},
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
)
},
Expand Down
29 changes: 24 additions & 5 deletions pkg/config-api-provider/test/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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)

}

0 comments on commit 255905e

Please sign in to comment.