Skip to content

Commit

Permalink
Merge branch 'main' into ay/feat/acceptance-tests/data-source/sensor-…
Browse files Browse the repository at this point in the history
…group-assignment
  • Loading branch information
1riatsila1 authored Nov 13, 2024
2 parents 8668c06 + 53fe26a commit dbd099f
Show file tree
Hide file tree
Showing 64 changed files with 730 additions and 110 deletions.
43 changes: 14 additions & 29 deletions internal/provider/resources/sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types"
)

// Ensure the implementation satisfies the expected interfaces.
var (
_ resource.Resource = &sensorResource{}
_ resource.ResourceWithConfigure = &sensorResource{}
Expand Down Expand Up @@ -61,12 +60,18 @@ func (r *sensorResource) Schema(
},
"address_note": schema.StringAttribute{
Optional: true,
// computed because goes from nil -> "" when sensor becomes configured
Computed: true,
},
"notes": schema.StringAttribute{
Optional: true,
// computed because goes from from nil -> "" when sensor becomes configured
Computed: true,
},
"pcap_mode": schema.StringAttribute{
Optional: true,
// computed because goes from from nil -> "light" when sensor becomes configured
Computed: true,
},
},
}
Expand All @@ -77,8 +82,6 @@ func (r *sensorResource) Configure(
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
}
Expand All @@ -101,7 +104,6 @@ func (r *sensorResource) Create(
req resource.CreateRequest,
resp *resource.CreateResponse,
) {
// Retrieve values from plan
var plan sensorResourceModel
diags := req.Plan.Get(ctx, &plan)
diags.AddError(
Expand All @@ -116,7 +118,6 @@ func (r *sensorResource) Read(
req resource.ReadRequest,
resp *resource.ReadResponse,
) {
// Get current state
var state sensorResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
Expand All @@ -143,14 +144,12 @@ func (r *sensorResource) Read(
}
sensor := sensorResponse.Items[0]

// Update state from client response
state.ID = types.StringValue(sensor.Id)
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)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand All @@ -163,7 +162,6 @@ func (r *sensorResource) Update(
req resource.UpdateRequest,
resp *resource.UpdateResponse,
) {
// Retrieve values from plan
var plan sensorResourceModel
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
Expand All @@ -172,15 +170,11 @@ func (r *sensorResource) Update(
}

patchRequest := config_api_client.NewSensorsPatchRequest()
if !plan.AddressNote.IsUnknown() {
patchRequest.AddressNote = plan.AddressNote.ValueStringPointer()
}
if !plan.Notes.IsUnknown() {
patchRequest.Notes = plan.Notes.ValueStringPointer()
}
if !plan.PCapMode.IsUnknown() {
patchRequest.PcapMode = plan.PCapMode.ValueStringPointer()
}
patchRequest.Name = plan.Name.ValueStringPointer()
patchRequest.AddressNote = plan.AddressNote.ValueStringPointer()
patchRequest.Notes = plan.Notes.ValueStringPointer()
patchRequest.PcapMode = plan.PCapMode.ValueStringPointer()

request := r.client.ConfigurationAPI.
SensorsPatch(ctx, plan.ID.ValueString()).
SensorsPatchRequest(*patchRequest)
Expand All @@ -193,20 +187,12 @@ func (r *sensorResource) Update(
return
}

// Update the state to match the plan (replace with response from client)
plan.ID = types.StringValue(sensor.Id)
plan.Name = types.StringValue(sensor.Name)
if sensor.AddressNote.Get() != nil {
plan.AddressNote = types.StringValue(*sensor.AddressNote.Get())
}
if sensor.Notes.Get() != nil {
plan.Notes = types.StringValue(*sensor.Notes.Get())
}
if sensor.PcapMode.Get() != nil {
plan.PCapMode = types.StringValue(*sensor.PcapMode.Get())
}
plan.AddressNote = types.StringPointerValue(sensor.AddressNote.Get())
plan.Notes = types.StringPointerValue(sensor.Notes.Get())
plan.PCapMode = types.StringPointerValue(sensor.PcapMode.Get())

// Set state to fully populated data
diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand All @@ -219,7 +205,6 @@ func (r *sensorResource) Delete(
req resource.DeleteRequest,
resp *resource.DeleteResponse,
) {
// Retrieve values from state
var state sensorResourceModel
diags := req.State.Get(ctx, &state)
diags.AddError(
Expand Down
3 changes: 2 additions & 1 deletion pkg/config-api-client/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 41 additions & 1 deletion pkg/config-api-client/api/.openapi.source.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 40 additions & 1 deletion pkg/config-api-client/api/openapi.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit dbd099f

Please sign in to comment.