diff --git a/internal/provider/resources/service_group_assignment.go b/internal/provider/resources/service_group_assignment.go index 0fcd398e..cde0af43 100644 --- a/internal/provider/resources/service_group_assignment.go +++ b/internal/provider/resources/service_group_assignment.go @@ -149,29 +149,13 @@ func (r *serviceTestGroupAssignmentResource) Read( return } - 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] + // TODO: Call client getServiceTestGroupAssignment method + serviceTestGroupAssignment := GetServiceTestGroupAssignment(state.ID.ValueString()) // Update state from client response - state.ID = types.StringValue(networkGroupAssignment.Id) - state.GroupID = types.StringValue(networkGroupAssignment.Group.Id) - state.ServiceTestID = types.StringValue(networkGroupAssignment.ServiceTest.Id) + state.ID = types.StringValue(serviceTestGroupAssignment.Id) + state.GroupID = types.StringValue(serviceTestGroupAssignment.Group.Id) + state.ServiceTestID = types.StringValue(serviceTestGroupAssignment.ServiceTest.Id) // Set refreshed state diags = resp.State.Set(ctx, &state) @@ -234,3 +218,13 @@ 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 e75d9387..9db90da5 100644 --- a/test/mocked/resources/service_test_group_assignment_test.go +++ b/test/mocked/resources/service_test_group_assignment_test.go @@ -4,9 +4,11 @@ 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" @@ -36,7 +38,9 @@ func TestServiceTestGroupAssignmentResource(t *testing.T) { // required for group create util.MockPostGroup( util.GenerateGroupRequestModel("group_uid", "", ""), - util.GenerateNonRootGroupResponseModel("group_uid", "", ""), + util.StructToMap( + util.GenerateNonRootGroupResponseModel("group_uid", "", ""), + ), 1, ) util.MockGetGroup( @@ -54,21 +58,20 @@ func TestServiceTestGroupAssignmentResource(t *testing.T) { "service_test_group_assignment_uid", "", ), - util.GenerateServiceTestGroupAssignmentResponse( - "service_test_group_assignment_uid", - "", - ), - 1, - ) - util.MockGetServiceTestGroupAssignment( - "service_test_group_assignment_uid", - util.GeneratePaginatedResponse([]map[string]interface{}{ + util.StructToMap( 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 + ` @@ -110,17 +113,6 @@ 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, @@ -171,37 +163,33 @@ func TestServiceTestGroupAssignmentResource(t *testing.T) { // required for creating another group util.MockPostGroup( util.GenerateGroupRequestModel("group_uid_2", "_2", "_2"), - util.GenerateNonRootGroupResponseModel("group_uid_2", "_2", "_2"), + util.StructToMap( + util.GenerateNonRootGroupResponseModel("group_uid_2", "_2", "_2"), + ), 1, ) // required for serviceTest group assignment create - 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, - ) + 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.MockPostServiceTestGroupAssignment( util.GenerateServiceTestGroupAssignmentRequest( "service_test_group_assignment_uid_2", "_2", ), - util.GenerateServiceTestGroupAssignmentResponse( - "service_test_group_assignment_uid_2", - "_2", + util.StructToMap( + util.GenerateServiceTestGroupAssignmentResponse( + "service_test_group_assignment_uid_2", + "_2", + ), ), 1, ) @@ -337,7 +325,9 @@ func TestServiceTestGroupAssignmentResource429Handling(t *testing.T) { // required for group create util.MockPostGroup( util.GenerateGroupRequestModel("group_uid", "", ""), - util.GenerateNonRootGroupResponseModel("group_uid", "", ""), + util.StructToMap( + util.GenerateNonRootGroupResponseModel("group_uid", "", ""), + ), 1, ) util.MockGetGroup( @@ -361,21 +351,20 @@ func TestServiceTestGroupAssignmentResource429Handling(t *testing.T) { "service_test_group_assignment_uid", "", ), - util.GenerateServiceTestGroupAssignmentResponse( - "service_test_group_assignment_uid", - "", - ), - 1, - ) - util.MockGetServiceTestGroupAssignment( - "service_test_group_assignment_uid", - util.GeneratePaginatedResponse([]map[string]interface{}{ + util.StructToMap( 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 + ` @@ -421,21 +410,9 @@ 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_uid", - 1, - ) + util.MockDeleteServiceTestGroupAssignment("service_test_group_assignment", 1) }, Config: provider.ProviderConfig + ` removed { @@ -476,7 +453,9 @@ func TestServiceTestGroupAssignmentResourceHttpErrorHandling(t *testing.T) { // required for group create util.MockPostGroup( util.GenerateGroupRequestModel("group_uid", "", ""), - util.GenerateNonRootGroupResponseModel("group_uid", "", ""), + util.StructToMap( + util.GenerateNonRootGroupResponseModel("group_uid", "", ""), + ), 1, ) util.MockGetGroup( diff --git a/test/mocked/util/utils.go b/test/mocked/util/utils.go index 319ce0fe..6bcb7a2e 100644 --- a/test/mocked/util/utils.go +++ b/test/mocked/util/utils.go @@ -2,6 +2,7 @@ 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" @@ -205,12 +206,15 @@ func GenerateNetworkGroupAssignmentRequest(uid string, postfix string) map[strin } } -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", +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", } } @@ -425,16 +429,6 @@ 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{},