Skip to content

Commit

Permalink
feat: provider | integrate agent GET
Browse files Browse the repository at this point in the history
  • Loading branch information
1riatsila1 committed Oct 30, 2024
1 parent 9818428 commit 7a2d1eb
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 43 deletions.
68 changes: 43 additions & 25 deletions pkg/config-api-provider/provider/resources/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package resources
import (
"context"

// "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/config-api-client"
"github.com/aruba-uxi/configuration-api-terraform-provider/pkg/config-api-client"
"github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/util"

"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
Expand Down Expand Up @@ -48,7 +50,9 @@ func NewAgentResource() resource.Resource {
return &agentResource{}
}

type agentResource struct{}
type agentResource struct {
client *config_api_client.APIClient
}

func (r *agentResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_agent"
Expand Down Expand Up @@ -77,7 +81,23 @@ func (r *agentResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
}

func (r *agentResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
// Add a nil check when handling ProviderData because Terraform
// sets that data after it calls the ConfigureProvider RPC.
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: Group. Please report this issue to the provider developers.",
)
return
}

Check warning on line 98 in pkg/config-api-provider/provider/resources/agent.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/agent.go#L93-L98

Added lines #L93 - L98 were not covered by tests

r.client = client
}

func (r *agentResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
Expand All @@ -89,22 +109,36 @@ func (r *agentResource) Create(ctx context.Context, req resource.CreateRequest,
}

func (r *agentResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
// Get current state
var state agentResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

response := GetAgent(state.ID.ValueString())
request := r.client.ConfigurationAPI.
AgentsGet(ctx).
Id(state.ID.ValueString())
sensorResponse, response, err := util.RetryFor429(request.Execute)
errorPresent, errorDetail := util.RaiseForStatus(response, err)

errorSummary := util.GenerateErrorSummary("read", "uxi_agent")

// Update state from client response
state.Name = types.StringValue(response.Name)
state.Notes = types.StringValue(response.Notes)
state.PCapMode = types.StringValue(response.PCapMode)
if errorPresent {
resp.Diagnostics.AddError(errorSummary, errorDetail)
return
}

Check warning on line 130 in pkg/config-api-provider/provider/resources/agent.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/agent.go#L128-L130

Added lines #L128 - L130 were not covered by tests

if len(sensorResponse.Items) != 1 {
resp.State.RemoveResource(ctx)
return
}

Check warning on line 135 in pkg/config-api-provider/provider/resources/agent.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/agent.go#L133-L135

Added lines #L133 - L135 were not covered by tests
sensor := sensorResponse.Items[0]

state.Name = types.StringValue(sensor.Name)
state.Notes = types.StringPointerValue(sensor.Notes.Get())
state.PCapMode = types.StringPointerValue(sensor.PcapMode.Get())

// Set refreshed state
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand Down Expand Up @@ -158,22 +192,6 @@ func (r *agentResource) ImportState(ctx context.Context, req resource.ImportStat
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

// Get the agent using the configuration-api client
var GetAgent = func(uid string) AgentResponseModel {
// TODO: Query the agent using the client

return AgentResponseModel{
UID: uid,
Serial: "mock_serial",
Name: "mock_name",
ModelNumber: "mock_model_number",
WifiMacAddress: "mock_wifi_mac_address",
EthernetMacAddress: "mock_ethernet_mac_address",
Notes: "mock_notes",
PCapMode: "mock_pcap_mode",
}
}

// Update the agent using the configuration-api client
var UpdateAgent = func(request AgentUpdateRequestModel) AgentResponseModel {
// TODO: Query the agent using the client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ func TestAgentGroupAssignmentResource(t *testing.T) {
{
PreConfig: func() {
// required for agent import
resources.GetAgent = func(uid string) resources.AgentResponseModel {
return util.GenerateAgentResponseModel(uid, "")
}
util.MockGetAgent(
"uid",
util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateAgentResponseModel("uid", "")}),
2,
)

// required for group create
util.MockPostGroup(
Expand Down Expand Up @@ -83,13 +85,16 @@ func TestAgentGroupAssignmentResource(t *testing.T) {
// Update and Read testing
{
PreConfig: func() {
resources.GetAgent = func(uid string) resources.AgentResponseModel {
if uid == "agent_uid" {
return util.GenerateAgentResponseModel(uid, "")
} else {
return util.GenerateAgentResponseModel(uid, "_2")
}
}
util.MockGetAgent(
"agent_uid_2",
util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateAgentResponseModel("agent_uid_2", "_2")}),
2,
)
util.MockGetAgent(
"agent_uid",
util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateAgentResponseModel("agent_uid", "")}),
2,
)
util.MockGetGroup(
"group_uid_2",
util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateGroupResponseModel("group_uid_2", "_2", "_2")}),
Expand Down Expand Up @@ -169,6 +174,16 @@ func TestAgentGroupAssignmentResource(t *testing.T) {
// Delete testing automatically occurs in TestCase
{
PreConfig: func() {
util.MockGetAgent(
"agent_uid",
util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateAgentResponseModel("agent_uid", "")}),
1,
)
util.MockGetAgent(
"agent_uid_2",
util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateAgentResponseModel("agent_uid_2", "_2")}),
1,
)
util.MockGetGroup(
"group_uid",
util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateGroupResponseModel("group_uid", "", "")}),
Expand Down
31 changes: 24 additions & 7 deletions pkg/config-api-provider/test/resources/agent_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package resource_test

import (
"github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/resources"
"github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider"
"github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/util"
"github.com/h2non/gock"
Expand Down Expand Up @@ -31,9 +30,11 @@ func TestAgentResource(t *testing.T) {
// Importing an agent
{
PreConfig: func() {
resources.GetAgent = func(uid string) resources.AgentResponseModel {
return util.GenerateAgentResponseModel(uid, "")
}
util.MockGetAgent(
"uid",
util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateAgentResponseModel("uid", "")}),
2,
)
},
Config: provider.ProviderConfig + `
resource "uxi_agent" "my_agent" {
Expand All @@ -56,16 +57,32 @@ func TestAgentResource(t *testing.T) {
},
// ImportState testing
{
PreConfig: func() {
util.MockGetAgent(
"uid",
util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateAgentResponseModel("uid", "")}),
1,
)
},
ResourceName: "uxi_agent.my_agent",
ImportState: true,
ImportStateVerify: true,
},
// Update and Read testing
{
PreConfig: func() {
resources.GetAgent = func(uid string) resources.AgentResponseModel {
return util.GenerateAgentResponseModel(uid, "_2")
}
// original
util.MockGetAgent(
"uid",
util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateAgentResponseModel("uid", "")}),
1,
)
// updated
util.MockGetAgent(
"uid",
util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateAgentResponseModel("uid", "_2")}),
1,
)
},
Config: provider.ProviderConfig + `
resource "uxi_agent" "my_agent" {
Expand Down
26 changes: 25 additions & 1 deletion pkg/config-api-provider/test/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,21 @@ func GenerateSensorResponseModel(uid string, postfix string) map[string]interfac
}
}

func GenerateAgentResponseModel(uid string, postfix string) resources.AgentResponseModel {
func GenerateAgentResponseModel(uid string, postfix string) map[string]interface{} {
return map[string]interface{}{
"id": uid,
"serial": "serial" + postfix,
"name": "name" + postfix,
"modelNumber": "model_number" + postfix,
"wifiMacAddress": "wifi_mac_address" + postfix,
"ethernetMacAddress": "ethernet_mac_address" + postfix,
"notes": "notes" + postfix,
"pcapMode": "light" + postfix,
"type": "networking-uxi/sensor",
}
}

func GenerateMockedAgentResponseModel(uid string, postfix string) resources.AgentResponseModel {

Check warning on line 42 in pkg/config-api-provider/test/util/utils.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/test/util/utils.go#L42

Added line #L42 was not covered by tests
return resources.AgentResponseModel{
UID: uid,
Serial: "serial" + postfix,
Expand Down Expand Up @@ -192,6 +206,16 @@ func MockOAuth() *gock.Response {

}

func MockGetAgent(uid string, response map[string]interface{}, times int) {
gock.New("https://test.api.capenetworks.com").
Get("/networking-uxi/v1alpha1/agents").
MatchHeader("Authorization", "mock_token").
MatchParam("id", uid).
Times(times).
Reply(200).
JSON(response)
}

func MockPostGroup(request map[string]interface{}, response map[string]interface{}, times int) {
gock.New("https://test.api.capenetworks.com").
Post("/networking-uxi/v1alpha1/groups").
Expand Down

0 comments on commit 7a2d1eb

Please sign in to comment.