Skip to content

Commit

Permalink
add the agent code too
Browse files Browse the repository at this point in the history
  • Loading branch information
1riatsila1 committed Aug 22, 2024
1 parent abe0902 commit 02676fd
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 27 deletions.
79 changes: 52 additions & 27 deletions pkg/config-api-provider/provider/resources/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package resources
import (
"context"

"time"

// "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/config-api-client"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
Expand All @@ -20,15 +19,15 @@ var (
)

type agentResourceModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Notes types.String `tfsdk:"notes"`
PCapMode types.String `tfsdk:"pcap_mode"`
LastUpdated types.String `tfsdk:"last_updated"`
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Notes types.String `tfsdk:"notes"`
PCapMode types.String `tfsdk:"pcap_mode"`
}

type agentResponseModel struct {
Uid string
// TODO: Switch this to use the Client Model when that becomes available
type AgentResponseModel struct {
UID string
Serial string
Name string
ModelNumber string
Expand All @@ -38,6 +37,13 @@ type agentResponseModel struct {
PCapMode string
}

// TODO: Switch this to use the Client Model when that becomes available
type AgentUpdateRequestModel struct {
Name string
Notes string
PCapMode string
}

func NewAgentResource() resource.Resource {
return &agentResource{}
}
Expand All @@ -57,9 +63,6 @@ func (r *agentResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
stringplanmodifier.UseStateForUnknown(),
},
},
"last_updated": schema.StringAttribute{
Computed: true,
},
"name": schema.StringAttribute{
Required: true,
},
Expand All @@ -74,6 +77,7 @@ func (r *agentResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
}

func (r *agentResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {

}

func (r *agentResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
Expand All @@ -93,15 +97,12 @@ func (r *agentResource) Read(ctx context.Context, req resource.ReadRequest, resp
return
}

// TODO: Call client create-agent method
// We are mocking the response of the client for this early stage of development
response := GetAgent()

// Update state from client response
state.Name = types.StringValue(response.Name)
state.Notes = types.StringValue(response.Notes)
state.PCapMode = types.StringValue(response.PCapMode)
state.LastUpdated = types.StringValue(time.Now().Format(time.RFC850))

// Set refreshed state
diags = resp.State.Set(ctx, &state)
Expand All @@ -120,10 +121,17 @@ func (r *agentResource) Update(ctx context.Context, req resource.UpdateRequest,
return
}

// TODO: Call client update-agent method
// Update existing order
response := UpdateAgent(AgentUpdateRequestModel{
Name: plan.Name.ValueString(),
Notes: plan.Notes.ValueString(),
PCapMode: plan.PCapMode.ValueString(),
})

// Update the state to match the plan (replace with response from client)
plan.LastUpdated = types.StringValue(time.Now().Format(time.RFC850))
// Update resource state with updated items
plan.Name = types.StringValue(response.Name)
plan.Notes = types.StringValue(response.Notes)
plan.PCapMode = types.StringValue(response.PCapMode)

// Set state to fully populated data
diags = resp.State.Set(ctx, plan)
Expand All @@ -146,16 +154,33 @@ func (r *agentResource) ImportState(ctx context.Context, req resource.ImportStat
}

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

return AgentResponseModel{
UID: "mock_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

return agentResponseModel{
Serial: "temporary_serial",
Name: "temporary_name",
ModelNumber: "temporary_model_number",
WifiMacAddress: "temporary_wifi_mac_address",
EthernetMacAddress: "temporary_ethernet_mac_address",
Notes: "temporary_notes",
PCapMode: "temporary_pcap_mode",
return AgentResponseModel{
UID: "mock_uid",
Serial: "mock_serial",
Name: request.Name,
ModelNumber: "mock_model_number",
WifiMacAddress: "mock_wifi_mac_address",
EthernetMacAddress: "mock_ethernet_mac_address",
Notes: request.Notes,
PCapMode: request.PCapMode,
}
}
121 changes: 121 additions & 0 deletions pkg/config-api-provider/test/agent_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package test

import (
"regexp"
"testing"

"github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/resources"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
)

func TestAgentResource(t *testing.T) {

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
// we required terraform 1.7.0 and above for the `removed` block
tfversion.RequireAbove(tfversion.Version1_7_0),
},
Steps: []resource.TestStep{
// Creating an agent is not allowed
{
Config: providerConfig + `
resource "uxi_agent" "my_agent" {
name = "name"
notes = "note"
pcap_mode = "light"
}`,

ExpectError: regexp.MustCompile(`creating an agent is not supported; agents can only be imported`),
},
// Importing an agent
{
PreConfig: func() {
resources.GetAgent = func() resources.AgentResponseModel {
return resources.AgentResponseModel{
UID: "uid",
Serial: "serial",
Name: "imported_name",
ModelNumber: "model_number",
WifiMacAddress: "wifi_mac_address",
EthernetMacAddress: "ethernet_mac_address",
Notes: "imported_notes",
PCapMode: "light",
}
}
},
Config: providerConfig + `
resource "uxi_agent" "my_agent" {
name = "imported_name"
notes = "imported_notes"
pcap_mode = "light"
}
import {
to = uxi_agent.my_agent
id = "test_uid"
}`,

Check: resource.ComposeAggregateTestCheckFunc(
// Verify first order item updated
resource.TestCheckResourceAttr("uxi_agent.my_agent", "name", "imported_name"),
resource.TestCheckResourceAttr("uxi_agent.my_agent", "notes", "imported_notes"),
resource.TestCheckResourceAttr("uxi_agent.my_agent", "pcap_mode", "light"),
// Verify first coffee item has Computed attributes updated.
resource.TestCheckResourceAttr("uxi_agent.my_agent", "id", "test_uid"),
),
},
// ImportState testing
{
ResourceName: "uxi_agent.my_agent",
ImportState: true,
ImportStateVerify: true,
},
// Update and Read testing
{
PreConfig: func() {
resources.GetAgent = func() resources.AgentResponseModel {
return resources.AgentResponseModel{
UID: "uid",
Serial: "serial",
Name: "updated_name",
ModelNumber: "model_number",
WifiMacAddress: "wifi_mac_address",
EthernetMacAddress: "ethernet_mac_address",
Notes: "updated_notes",
PCapMode: "not_light",
}
}
},
Config: providerConfig + `
resource "uxi_agent" "my_agent" {
name = "updated_name"
notes = "updated_notes"
pcap_mode = "not_light"
}`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("uxi_agent.my_agent", "name", "updated_name"),
resource.TestCheckResourceAttr("uxi_agent.my_agent", "notes", "updated_notes"),
resource.TestCheckResourceAttr("uxi_agent.my_agent", "pcap_mode", "not_light"),
),
},
// Deleting an agent is not allowed
{
Config: providerConfig + ``,
ExpectError: regexp.MustCompile(`deleting an agent is not supported; agents can only removed from state`),
},
// Remove agent from state
{
Config: providerConfig + `
removed {
from = uxi_agent.my_agent
lifecycle {
destroy = false
}
}`,
},
},
})
}

0 comments on commit 02676fd

Please sign in to comment.