Skip to content

Commit

Permalink
Merge branch 'main' into ay/feat/resources/integrate-service-test
Browse files Browse the repository at this point in the history
  • Loading branch information
1riatsila1 authored Nov 7, 2024
2 parents f738349 + 8685e41 commit 9b2d545
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 28 deletions.
55 changes: 40 additions & 15 deletions internal/provider/resources/agent_group_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
64 changes: 52 additions & 12 deletions test/mocked/resources/agent_group_assignment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 + `
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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",
)
Expand Down
20 changes: 19 additions & 1 deletion test/mocked/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {

Check failure on line 339 in test/mocked/util/utils.go

View workflow job for this annotation

GitHub Actions / Lint & Test Code

expected '(', found MockGetAgentGroupAssignment
gock.New("https://test.api.capenetworks.com").
Get("/networking-uxi/v1alpha1/agent-group-assignments").
MatchHeader("Authorization", "mock_token").
MatchParam("id", uid).
Times(times).
Expand Down

0 comments on commit 9b2d545

Please sign in to comment.