diff --git a/internal/provider/resources/agent_group_assignment.go b/internal/provider/resources/agent_group_assignment.go index 7af729fd..1b31cad8 100644 --- a/internal/provider/resources/agent_group_assignment.go +++ b/internal/provider/resources/agent_group_assignment.go @@ -2,6 +2,8 @@ package resources import ( "context" + "github.com/aruba-uxi/terraform-provider-configuration-api/pkg/config-api-client" + "github.com/aruba-uxi/terraform-provider-configuration/internal/provider/util" "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" @@ -38,7 +40,9 @@ func NewAgentGroupAssignmentResource() resource.Resource { return &agentGroupAssignmentResource{} } -type agentGroupAssignmentResource struct{} +type agentGroupAssignmentResource struct { + client *config_api_client.APIClient +} func (r *agentGroupAssignmentResource) Metadata( ctx context.Context, @@ -82,6 +86,21 @@ func (r *agentGroupAssignmentResource) Configure( req resource.ConfigureRequest, resp *resource.ConfigureResponse, ) { + if req.ProviderData == nil { + return + } + + client, ok := req.ProviderData.(*config_api_client.APIClient) + + if !ok { + resp.Diagnostics.AddError( + "Unexpected Data Source Configure Type", + "Resource type: Network Group Assignment. Please report this issue to the provider developers.", + ) + return + } + + r.client = client } func (r *agentGroupAssignmentResource) Create( @@ -129,12 +148,28 @@ func (r *agentGroupAssignmentResource) Read( return } - // TODO: Call client getAgentGroupAssignment method - agentGroupAssignment := GetAgentGroupAssignment(state.ID.ValueString()) + request := r.client.ConfigurationAPI. + AgentGroupAssignmentsGet(ctx). + Id(state.ID.ValueString()) + agentGroupAssignmentResponse, response, err := util.RetryFor429(request.Execute) + errorPresent, errorDetail := util.RaiseForStatus(response, err) + + errorSummary := util.GenerateErrorSummary("read", "uxi_agent_group_assignment") + + if errorPresent { + resp.Diagnostics.AddError(errorSummary, errorDetail) + return + } + + if len(agentGroupAssignmentResponse.Items) != 1 { + resp.State.RemoveResource(ctx) + return + } + agentGroupAssignment := agentGroupAssignmentResponse.Items[0] // Update state from client response - state.GroupID = types.StringValue(agentGroupAssignment.GroupUID) - state.AgentID = types.StringValue(agentGroupAssignment.AgentUID) + state.GroupID = types.StringValue(agentGroupAssignment.Group.Id) + state.AgentID = types.StringValue(agentGroupAssignment.Agent.Id) // Set refreshed state diags = resp.State.Set(ctx, &state) @@ -183,16 +218,6 @@ func (r *agentGroupAssignmentResource) ImportState( resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp) } -var GetAgentGroupAssignment = func(uid string) AgentGroupAssignmentResponseModel { - // TODO: Query the agentGroupAssignment using the client - - return AgentGroupAssignmentResponseModel{ - UID: uid, - GroupUID: "mock_group_uid", - AgentUID: "mock_agent_uid", - } -} - var CreateAgentGroupAssignment = func(request AgentGroupAssignmentRequestModel) AgentGroupAssignmentResponseModel { // TODO: Query the agentGroupAssignment using the client diff --git a/test/mocked/resources/agent_group_assignment_test.go b/test/mocked/resources/agent_group_assignment_test.go index fe5247c6..001b7b69 100644 --- a/test/mocked/resources/agent_group_assignment_test.go +++ b/test/mocked/resources/agent_group_assignment_test.go @@ -48,16 +48,25 @@ func TestAgentGroupAssignmentResource(t *testing.T) { ) // required for agent group assignment create - agentGroupAssignmentResponse := util.GenerateAgentGroupAssignmentResponse( + agentGroupAssignmentResponse := util.GenerateAgentGroupAssignmentResponseMockedModel( "agent_group_assignment_uid", "", ) resources.CreateAgentGroupAssignment = func(request resources.AgentGroupAssignmentRequestModel) resources.AgentGroupAssignmentResponseModel { return agentGroupAssignmentResponse } - resources.GetAgentGroupAssignment = func(uid string) resources.AgentGroupAssignmentResponseModel { - return agentGroupAssignmentResponse - } + util.MockGetAgentGroupAssignment( + "agent_group_assignment_uid", + util.GeneratePaginatedResponse( + []map[string]interface{}{ + util.GenerateAgentGroupAssignmentResponse( + "agent_group_assignment_uid", + "", + ), + }, + ), + 1, + ) }, Config: provider.ProviderConfig + ` @@ -101,6 +110,20 @@ func TestAgentGroupAssignmentResource(t *testing.T) { }, // ImportState testing { + PreConfig: func() { + util.MockGetAgentGroupAssignment( + "agent_group_assignment_uid", + util.GeneratePaginatedResponse( + []map[string]interface{}{ + util.GenerateAgentGroupAssignmentResponse( + "agent_group_assignment_uid", + "", + ), + }, + ), + 1, + ) + }, ResourceName: "uxi_agent_group_assignment.my_agent_group_assignment", ImportState: true, ImportStateVerify: true, @@ -155,15 +178,32 @@ func TestAgentGroupAssignmentResource(t *testing.T) { ) // required for agent group assignment create - resources.GetAgentGroupAssignment = func(uid string) resources.AgentGroupAssignmentResponseModel { - if uid == "agent_group_assignment_uid" { - return util.GenerateAgentGroupAssignmentResponse(uid, "") - } else { - return util.GenerateAgentGroupAssignmentResponse(uid, "_2") - } - } + util.MockGetAgentGroupAssignment( + "agent_group_assignment_uid_2", + util.GeneratePaginatedResponse( + []map[string]interface{}{ + util.GenerateAgentGroupAssignmentResponse( + "agent_group_assignment_uid_2", + "_2", + ), + }, + ), + 2, + ) + util.MockGetAgentGroupAssignment( + "agent_group_assignment_uid", + util.GeneratePaginatedResponse( + []map[string]interface{}{ + util.GenerateAgentGroupAssignmentResponse( + "agent_group_assignment_uid", + "", + ), + }, + ), + 1, + ) resources.CreateAgentGroupAssignment = func(request resources.AgentGroupAssignmentRequestModel) resources.AgentGroupAssignmentResponseModel { - return util.GenerateAgentGroupAssignmentResponse( + return util.GenerateAgentGroupAssignmentResponseMockedModel( "agent_group_assignment_uid_2", "_2", ) diff --git a/test/mocked/util/utils.go b/test/mocked/util/utils.go index f4f5863d..d2e07b50 100644 --- a/test/mocked/util/utils.go +++ b/test/mocked/util/utils.go @@ -154,7 +154,16 @@ func GenerateSensorGroupAssignmentRequest(uid string, postfix string) map[string } } -func GenerateAgentGroupAssignmentResponse( +func GenerateAgentGroupAssignmentResponse(uid string, postfix string) map[string]interface{} { + return map[string]interface{}{ + "id": uid, + "group": map[string]string{"id": "group_uid" + postfix}, + "agent": map[string]string{"id": "agent_uid" + postfix}, + "type": "networking-uxi/agent-group-assignment", + } +} + +func GenerateAgentGroupAssignmentResponseMockedModel( uid string, postfix string, ) resources.AgentGroupAssignmentResponseModel { @@ -321,6 +330,15 @@ func MockGetWirelessNetwork(uid string, response map[string]interface{}, times i func MockGetServiceTest(uid string, response map[string]interface{}, times int) { gock.New("https://test.api.capenetworks.com"). Get("/networking-uxi/v1alpha1/service-tests"). + MatchHeader("Authorization", "mock_token"). + MatchParam("id", uid). + Times(times). + Reply(200). + JSON(response) + +func MockGetAgentGroupAssignment(uid string, response map[string]interface{}, times int) { + gock.New("https://test.api.capenetworks.com"). + Get("/networking-uxi/v1alpha1/agent-group-assignments"). MatchHeader("Authorization", "mock_token"). MatchParam("id", uid). Times(times).