Skip to content

Commit

Permalink
feat: provider | integrate agent GET (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
1riatsila1 authored Oct 31, 2024
1 parent 9818428 commit 48fedc4
Show file tree
Hide file tree
Showing 7 changed files with 540 additions and 43 deletions.
160 changes: 160 additions & 0 deletions pkg/config-api-provider/provider/data-sources/agent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package datasources

import (
"context"

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/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)

var (
_ datasource.DataSource = &agentDataSource{}
_ datasource.DataSourceWithConfigure = &agentDataSource{}
)

func NewAgentDataSource() datasource.DataSource {
return &agentDataSource{}
}

type agentDataSource struct {
client *config_api_client.APIClient
}

type agentDataSourceModel struct {
Id types.String `tfsdk:"id"`
Serial types.String `tfsdk:"serial"`
Name types.String `tfsdk:"name"`
ModelNumber types.String `tfsdk:"model_number"`
WifiMacAddress types.String `tfsdk:"wifi_mac_address"`
EthernetMacAddress types.String `tfsdk:"ethernet_mac_address"`
AddressNote types.String `tfsdk:"address_note"`
Longitude types.Float32 `tfsdk:"longitude"`
Latitude types.Float32 `tfsdk:"latitude"`
Notes types.String `tfsdk:"notes"`
PcapMode types.String `tfsdk:"pcap_mode"`
Filter struct {
AgentID types.String `tfsdk:"agent_id"`
} `tfsdk:"filter"`
}

func (d *agentDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_agent"
}

func (d *agentDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
},
"serial": schema.StringAttribute{
Computed: true,
},
"name": schema.StringAttribute{
Computed: true,
},
"model_number": schema.StringAttribute{
Computed: true,
},
"wifi_mac_address": schema.StringAttribute{
Computed: true,
},
"ethernet_mac_address": schema.StringAttribute{
Computed: true,
},
"address_note": schema.StringAttribute{
Computed: true,
},
"longitude": schema.Float32Attribute{
Computed: true,
},
"latitude": schema.Float32Attribute{
Computed: true,
},
"notes": schema.StringAttribute{
Computed: true,
},
"pcap_mode": schema.StringAttribute{
Computed: true,
},
"filter": schema.SingleNestedAttribute{
Required: true,
Attributes: map[string]schema.Attribute{
"agent_id": schema.StringAttribute{
Required: true,
},
},
},
},
}
}

func (d *agentDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state agentDataSourceModel

// Read configuration from request
diags := req.Config.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

request := d.client.ConfigurationAPI.
AgentsGet(ctx).
Id(state.Filter.AgentID.ValueString())

agentResponse, response, err := util.RetryFor429(request.Execute)
errorPresent, errorDetail := util.RaiseForStatus(response, err)

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

if errorPresent {
resp.Diagnostics.AddError(errorSummary, errorDetail)
return
}

if len(agentResponse.Items) != 1 {
resp.Diagnostics.AddError(errorSummary, "Could not find specified data source")
return
}

agent := agentResponse.Items[0]

state.Id = types.StringValue(agent.Id)
state.Name = types.StringValue(agent.Name)
state.ModelNumber = types.StringPointerValue(agent.ModelNumber.Get())
state.WifiMacAddress = types.StringPointerValue(agent.WifiMacAddress.Get())
state.EthernetMacAddress = types.StringPointerValue(agent.EthernetMacAddress.Get())
state.Notes = types.StringPointerValue(agent.Notes.Get())
state.PcapMode = types.StringPointerValue(agent.PcapMode.Get())

// Set state
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}

func (d *agentDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.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",
"Data Source type: Agent. Please report this issue to the provider developers.",
)
return
}

d.client = client
}
1 change: 1 addition & 0 deletions pkg/config-api-provider/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ func (p *uxiConfigurationProvider) Configure(ctx context.Context, req provider.C
// DataSources defines the data sources implemented in the provider.
func (p *uxiConfigurationProvider) DataSources(_ context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
datasources.NewAgentDataSource,
datasources.NewGroupDataSource,
datasources.NewSensorDataSource,
datasources.NewWiredNetworkDataSource,
Expand Down
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
}

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
}

if len(sensorResponse.Items) != 1 {
resp.State.RemoveResource(ctx)
return
}
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
Loading

0 comments on commit 48fedc4

Please sign in to comment.