Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: resources | integrate agent assignment GET #69

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
21 changes: 20 additions & 1 deletion test/mocked/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,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 @@ -317,6 +326,16 @@ func MockGetWirelessNetwork(uid string, response map[string]interface{}, times i
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).
Reply(200).
JSON(response)
}

func MockGetSensorGroupAssignment(uid string, response map[string]interface{}, times int) {
gock.New("https://test.api.capenetworks.com").
Get("/networking-uxi/v1alpha1/sensor-group-assignments").
Expand Down
Loading