diff --git a/internal/provider/resources/service_group_assignment.go b/internal/provider/resources/service_group_assignment.go index cde0af43..0fcd398e 100644 --- a/internal/provider/resources/service_group_assignment.go +++ b/internal/provider/resources/service_group_assignment.go @@ -149,13 +149,29 @@ func (r *serviceTestGroupAssignmentResource) Read( return } - // TODO: Call client getServiceTestGroupAssignment method - serviceTestGroupAssignment := GetServiceTestGroupAssignment(state.ID.ValueString()) + request := r.client.ConfigurationAPI. + ServiceTestGroupAssignmentsGet(ctx). + Id(state.ID.ValueString()) + networkGroupAssignmentResponse, response, err := util.RetryFor429(request.Execute) + errorPresent, errorDetail := util.RaiseForStatus(response, err) + + errorSummary := util.GenerateErrorSummary("read", "uxi_service_test_group_assignment") + + if errorPresent { + resp.Diagnostics.AddError(errorSummary, errorDetail) + return + } + + if len(networkGroupAssignmentResponse.Items) != 1 { + resp.State.RemoveResource(ctx) + return + } + networkGroupAssignment := networkGroupAssignmentResponse.Items[0] // 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) + state.ID = types.StringValue(networkGroupAssignment.Id) + state.GroupID = types.StringValue(networkGroupAssignment.Group.Id) + state.ServiceTestID = types.StringValue(networkGroupAssignment.ServiceTest.Id) // Set refreshed state diags = resp.State.Set(ctx, &state) @@ -218,13 +234,3 @@ func (r *serviceTestGroupAssignmentResource) ImportState( ) { resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp) } - -var GetServiceTestGroupAssignment = func(uid string) config_api_client.ServiceTestGroupAssignmentsPostResponse { - // TODO: Query the serviceTestGroupAssignment using the client - return config_api_client.ServiceTestGroupAssignmentsPostResponse{ - Id: uid, - Group: *config_api_client.NewGroup("mock_group_uid"), - ServiceTest: *config_api_client.NewServiceTest("mock_serviceTest_uid"), - Type: "networking-uxi/service-test-group-assignment", - } -} diff --git a/test/mocked/resources/service_test_group_assignment_test.go b/test/mocked/resources/service_test_group_assignment_test.go index 9db90da5..e75d9387 100644 --- a/test/mocked/resources/service_test_group_assignment_test.go +++ b/test/mocked/resources/service_test_group_assignment_test.go @@ -4,11 +4,9 @@ import ( "regexp" "testing" - config_api_client "github.com/aruba-uxi/terraform-provider-configuration-api/pkg/config-api-client" "github.com/aruba-uxi/terraform-provider-configuration/test/mocked/provider" "github.com/aruba-uxi/terraform-provider-configuration/test/mocked/util" - "github.com/aruba-uxi/terraform-provider-configuration/internal/provider/resources" "github.com/h2non/gock" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/terraform" @@ -38,9 +36,7 @@ func TestServiceTestGroupAssignmentResource(t *testing.T) { // required for group create util.MockPostGroup( util.GenerateGroupRequestModel("group_uid", "", ""), - util.StructToMap( - util.GenerateNonRootGroupResponseModel("group_uid", "", ""), - ), + util.GenerateNonRootGroupResponseModel("group_uid", "", ""), 1, ) util.MockGetGroup( @@ -58,20 +54,21 @@ func TestServiceTestGroupAssignmentResource(t *testing.T) { "service_test_group_assignment_uid", "", ), - util.StructToMap( + util.GenerateServiceTestGroupAssignmentResponse( + "service_test_group_assignment_uid", + "", + ), + 1, + ) + util.MockGetServiceTestGroupAssignment( + "service_test_group_assignment_uid", + util.GeneratePaginatedResponse([]map[string]interface{}{ util.GenerateServiceTestGroupAssignmentResponse( - "service_test_group_assignment_uid", - "", - ), + "service_test_group_assignment_uid", "", + )}, ), 1, ) - resources.GetServiceTestGroupAssignment = func(uid string) config_api_client.ServiceTestGroupAssignmentsPostResponse { - return util.GenerateServiceTestGroupAssignmentResponse( - "service_test_group_assignment_uid", - "", - ) - } }, Config: provider.ProviderConfig + ` @@ -113,6 +110,17 @@ func TestServiceTestGroupAssignmentResource(t *testing.T) { }, // ImportState testing { + PreConfig: func() { + util.MockGetServiceTestGroupAssignment( + "service_test_group_assignment_uid", + util.GeneratePaginatedResponse([]map[string]interface{}{ + util.GenerateServiceTestGroupAssignmentResponse( + "service_test_group_assignment_uid", "", + )}, + ), + 1, + ) + }, ResourceName: "uxi_service_test_group_assignment.my_service_test_group_assignment", ImportState: true, ImportStateVerify: true, @@ -163,33 +171,37 @@ func TestServiceTestGroupAssignmentResource(t *testing.T) { // required for creating another group util.MockPostGroup( util.GenerateGroupRequestModel("group_uid_2", "_2", "_2"), - util.StructToMap( - util.GenerateNonRootGroupResponseModel("group_uid_2", "_2", "_2"), - ), + util.GenerateNonRootGroupResponseModel("group_uid_2", "_2", "_2"), 1, ) // required for serviceTest group assignment create - resources.GetServiceTestGroupAssignment = func(uid string) config_api_client.ServiceTestGroupAssignmentsPostResponse { - if uid == "service_test_group_assignment_uid" { - return util.GenerateServiceTestGroupAssignmentResponse( - "service_test_group_assignment_uid", - "", - ) - } else { - return util.GenerateServiceTestGroupAssignmentResponse("service_test_group_assignment_uid_2", "_2") - } - } + util.MockGetServiceTestGroupAssignment( + "service_test_group_assignment_uid_2", + util.GeneratePaginatedResponse([]map[string]interface{}{ + util.GenerateServiceTestGroupAssignmentResponse( + "service_test_group_assignment_uid_2", "_2", + )}, + ), + 2, + ) + util.MockGetServiceTestGroupAssignment( + "service_test_group_assignment_uid", + util.GeneratePaginatedResponse([]map[string]interface{}{ + util.GenerateServiceTestGroupAssignmentResponse( + "service_test_group_assignment_uid", "", + )}, + ), + 1, + ) util.MockPostServiceTestGroupAssignment( util.GenerateServiceTestGroupAssignmentRequest( "service_test_group_assignment_uid_2", "_2", ), - util.StructToMap( - util.GenerateServiceTestGroupAssignmentResponse( - "service_test_group_assignment_uid_2", - "_2", - ), + util.GenerateServiceTestGroupAssignmentResponse( + "service_test_group_assignment_uid_2", + "_2", ), 1, ) @@ -325,9 +337,7 @@ func TestServiceTestGroupAssignmentResource429Handling(t *testing.T) { // required for group create util.MockPostGroup( util.GenerateGroupRequestModel("group_uid", "", ""), - util.StructToMap( - util.GenerateNonRootGroupResponseModel("group_uid", "", ""), - ), + util.GenerateNonRootGroupResponseModel("group_uid", "", ""), 1, ) util.MockGetGroup( @@ -351,20 +361,21 @@ func TestServiceTestGroupAssignmentResource429Handling(t *testing.T) { "service_test_group_assignment_uid", "", ), - util.StructToMap( + util.GenerateServiceTestGroupAssignmentResponse( + "service_test_group_assignment_uid", + "", + ), + 1, + ) + util.MockGetServiceTestGroupAssignment( + "service_test_group_assignment_uid", + util.GeneratePaginatedResponse([]map[string]interface{}{ util.GenerateServiceTestGroupAssignmentResponse( - "service_test_group_assignment_uid", - "", - ), + "service_test_group_assignment_uid", "", + )}, ), 1, ) - resources.GetServiceTestGroupAssignment = func(uid string) config_api_client.ServiceTestGroupAssignmentsPostResponse { - return util.GenerateServiceTestGroupAssignmentResponse( - "service_test_group_assignment_uid", - "", - ) - } }, Config: provider.ProviderConfig + ` @@ -410,9 +421,21 @@ func TestServiceTestGroupAssignmentResource429Handling(t *testing.T) { ), 1, ) + util.MockGetServiceTestGroupAssignment( + "service_test_group_assignment_uid", + util.GeneratePaginatedResponse([]map[string]interface{}{ + util.GenerateServiceTestGroupAssignmentResponse( + "service_test_group_assignment_uid", "", + )}, + ), + 1, + ) util.MockDeleteGroup("group_uid", 1) - util.MockDeleteServiceTestGroupAssignment("service_test_group_assignment", 1) + util.MockDeleteServiceTestGroupAssignment( + "service_test_group_assignment_uid", + 1, + ) }, Config: provider.ProviderConfig + ` removed { @@ -453,9 +476,7 @@ func TestServiceTestGroupAssignmentResourceHttpErrorHandling(t *testing.T) { // required for group create util.MockPostGroup( util.GenerateGroupRequestModel("group_uid", "", ""), - util.StructToMap( - util.GenerateNonRootGroupResponseModel("group_uid", "", ""), - ), + util.GenerateNonRootGroupResponseModel("group_uid", "", ""), 1, ) util.MockGetGroup( diff --git a/test/mocked/util/utils.go b/test/mocked/util/utils.go index 6bcb7a2e..319ce0fe 100644 --- a/test/mocked/util/utils.go +++ b/test/mocked/util/utils.go @@ -2,7 +2,6 @@ package util import ( "encoding/json" - "github.com/aruba-uxi/terraform-provider-configuration-api/pkg/config-api-client" "github.com/aruba-uxi/terraform-provider-configuration/internal/provider/resources" "github.com/h2non/gock" @@ -206,15 +205,12 @@ func GenerateNetworkGroupAssignmentRequest(uid string, postfix string) map[strin } } -func GenerateServiceTestGroupAssignmentResponse( - uid string, - postfix string, -) config_api_client.ServiceTestGroupAssignmentsPostResponse { - return config_api_client.ServiceTestGroupAssignmentsPostResponse{ - Id: uid, - Group: *config_api_client.NewGroup("group_uid" + postfix), - ServiceTest: *config_api_client.NewServiceTest("service_test_uid" + postfix), - Type: "networking-uxi/service-test-group-assignment", +func GenerateServiceTestGroupAssignmentResponse(uid string, postfix string) map[string]interface{} { + return map[string]interface{}{ + "id": uid, + "group": map[string]string{"id": "group_uid" + postfix}, + "serviceTest": map[string]string{"id": "service_test_uid" + postfix}, + "type": "networking-uxi/service-test-group-assignment", } } @@ -429,6 +425,16 @@ func MockDeleteNetworkGroupAssignment(uid string, times int) { Reply(204) } +func MockGetServiceTestGroupAssignment(uid string, response map[string]interface{}, times int) { + gock.New("https://test.api.capenetworks.com"). + Get("/networking-uxi/v1alpha1/service-test-group-assignments"). + MatchHeader("Authorization", "mock_token"). + MatchParam("id", uid). + Times(times). + Reply(200). + JSON(response) +} + func MockPostServiceTestGroupAssignment( request map[string]interface{}, response map[string]interface{},