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: delete group and sensors get integration #45

Merged
merged 2 commits into from
Oct 22, 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
163 changes: 163 additions & 0 deletions pkg/config-api-provider/provider/data-sources/sensor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
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 = &sensorDataSource{}
_ datasource.DataSourceWithConfigure = &sensorDataSource{}
)

func NewSensorDataSource() datasource.DataSource {
return &sensorDataSource{}
}

type sensorDataSource struct {
client *config_api_client.APIClient
}

type sensorDataSourceModel 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 {
SensorID types.String `tfsdk:"sensor_id"`
} `tfsdk:"filter"`
}

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

func (d *sensorDataSource) 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{
"sensor_id": schema.StringAttribute{
Required: true,
},
},
},
},
}
}

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

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

Check warning on line 103 in pkg/config-api-provider/provider/data-sources/sensor.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/data-sources/sensor.go#L102-L103

Added lines #L102 - L103 were not covered by tests

request := d.client.ConfigurationAPI.
GetUxiV1alpha1SensorsGet(ctx).
Id(state.Filter.SensorID.ValueString())

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

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

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

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

sensor := sensorResponse.Items[0]

state.Id = types.StringValue(sensor.Id)
state.Name = types.StringValue(sensor.Name)
state.ModelNumber = types.StringPointerValue(sensor.ModelNumber.Get())
state.WifiMacAddress = types.StringPointerValue(sensor.WifiMacAddress.Get())
state.EthernetMacAddress = types.StringPointerValue(sensor.EthernetMacAddress.Get())
state.AddressNote = types.StringPointerValue(sensor.AddressNote.Get())
state.Longitude = types.Float32PointerValue(sensor.Longitude.Get())
state.Latitude = types.Float32PointerValue(sensor.Latitude.Get())
state.Notes = types.StringPointerValue(sensor.Notes.Get())
state.PcapMode = types.StringPointerValue(sensor.PcapMode.Get())

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

Check warning on line 142 in pkg/config-api-provider/provider/data-sources/sensor.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/data-sources/sensor.go#L141-L142

Added lines #L141 - L142 were not covered by tests
}

func (d *sensorDataSource) 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: Sensor. Please report this issue to the provider developers.",
)
return
}

Check warning on line 160 in pkg/config-api-provider/provider/data-sources/sensor.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/data-sources/sensor.go#L155-L160

Added lines #L155 - L160 were not covered by tests

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 @@ -156,6 +156,7 @@ func (p *uxiConfigurationProvider) Configure(ctx context.Context, req provider.C
func (p *uxiConfigurationProvider) DataSources(_ context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
datasources.NewGroupDataSource,
datasources.NewSensorDataSource,
1riatsila1 marked this conversation as resolved.
Show resolved Hide resolved
datasources.NewWiredNetworkDataSource,
datasources.NewWirelessNetworkDataSource,
datasources.NewSensorGroupAssignmentDataSource,
Expand Down
10 changes: 10 additions & 0 deletions pkg/config-api-provider/provider/resources/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,16 @@ func (r *groupResource) Delete(ctx context.Context, req resource.DeleteRequest,
}

// Delete existing group using the plan_id
request := r.client.ConfigurationAPI.
GroupsDeleteUxiV1alpha1GroupsGroupUidDelete(ctx, state.ID.ValueString())

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

if errorPresent {
resp.Diagnostics.AddError(util.GenerateErrorSummary("delete", "uxi_group"), errorDetail)
return
}
}

func (r *groupResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
Expand Down
69 changes: 43 additions & 26 deletions pkg/config-api-provider/provider/resources/sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import (
"context"
"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/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 Down Expand Up @@ -53,7 +54,9 @@
return &sensorResource{}
}

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

func (r *sensorResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_sensor"
Expand Down Expand Up @@ -85,7 +88,23 @@
}

func (r *sensorResource) 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 105 in pkg/config-api-provider/provider/resources/sensor.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/sensor.go#L100-L105

Added lines #L100 - L105 were not covered by tests

r.client = client
}

func (r *sensorResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
Expand All @@ -105,13 +124,30 @@
return
}

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

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

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

if len(sensorResponse.Items) != 1 {
resp.Diagnostics.AddError(errorSummary, "Could not find specified resource")
return
}
sensor := sensorResponse.Items[0]

// Update state from client response
state.Name = types.StringValue(response.Name)
state.AddressNote = types.StringValue(response.AddressNote)
state.Notes = types.StringValue(response.Notes)
state.PCapMode = types.StringValue(response.PCapMode)
state.Name = types.StringValue(sensor.Name)
state.AddressNote = types.StringPointerValue(sensor.AddressNote.Get())
state.Notes = types.StringPointerValue(sensor.Notes.Get())
state.PCapMode = types.StringPointerValue(sensor.PcapMode.Get())

// Set refreshed state
diags = resp.State.Set(ctx, &state)
Expand Down Expand Up @@ -164,25 +200,6 @@
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

// Get the sensor using the configuration-api client
var GetSensor = func(uid string) SensorResponseModel {
// TODO: Query the sensor using the client

return SensorResponseModel{
UID: uid,
Serial: "mock_serial",
Name: "mock_name",
ModelNumber: "mock_model_number",
WifiMacAddress: "mock_wifi_mac_address",
EthernetMacAddress: "mock_ethernet_mac_address",
AddressNote: "mock_address_note",
Longitude: "mock_longitude",
Latitude: "mock_latitude",
Notes: "mock_notes",
PCapMode: "mock_pcap_mode",
}
}

// Update the sensor using the configuration-api client
var UpdateSensor = func(request SensorUpdateRequestModel) SensorResponseModel {
// TODO: Query the sensor using the client
Expand Down
Loading
Loading