Skip to content

Commit

Permalink
feat: delete group and sensors get integration
Browse files Browse the repository at this point in the history
  • Loading branch information
1riatsila1 committed Oct 22, 2024
1 parent 617b14e commit b93040a
Show file tree
Hide file tree
Showing 9 changed files with 370 additions and 75 deletions.
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 @@ 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/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 @@ func NewSensorResource() resource.Resource {
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) Schema(_ context.Context, _ resource.SchemaRequest, res
}

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 @@ func (r *sensorResource) Read(ctx context.Context, req resource.ReadRequest, res
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 @@ func (r *sensorResource) ImportState(ctx context.Context, req resource.ImportSta
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
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,23 @@ func TestAgentGroupAssignmentResource(t *testing.T) {
),
},
// Delete testing automatically occurs in TestCase
{
PreConfig: func() {
util.MockGetGroup(
"group_uid",
util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateGroupResponseModel("group_uid", "", "")}),
2,
)
util.MockGetGroup(
"group_uid_2",
util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateGroupResponseModel("group_uid_2", "_2", "_2")}),
1,
)
util.MockDeleteGroup("group_uid", 1)
util.MockDeleteGroup("group_uid_2", 1)
},
Config: provider.ProviderConfig,
},
},
})

Expand Down
62 changes: 59 additions & 3 deletions pkg/config-api-provider/test/resources/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ func TestGroupResource(t *testing.T) {
[]map[string]interface{}{util.GenerateGroupResponseModel("new_uid", "", "_2")}),
1,
)
// delete old group (being replaced)
util.MockDeleteGroup("uid", 1)
},
Config: provider.ProviderConfig + `
resource "uxi_group" "my_group" {
Expand All @@ -110,7 +112,17 @@ func TestGroupResource(t *testing.T) {
resource.TestCheckResourceAttr("uxi_group.my_group", "id", "new_uid"),
),
},
// Delete testing automatically occurs in TestCase
// Delete testing
{
PreConfig: func() {
util.MockGetGroup("new_uid", util.GeneratePaginatedResponse(
[]map[string]interface{}{util.GenerateGroupResponseModel("new_uid", "", "_2")}),
1,
)
util.MockDeleteGroup("new_uid", 1)
},
Config: provider.ProviderConfig,
},
},
})

Expand Down Expand Up @@ -227,7 +239,17 @@ func TestGroupResource429Handling(t *testing.T) {
},
),
},
// TODO: Test Deleting 429s
// Delete testing
{
PreConfig: func() {
util.MockGetGroup("uid", util.GeneratePaginatedResponse(
[]map[string]interface{}{util.GenerateGroupResponseModel("uid", "", "")}),
1,
)
util.MockDeleteGroup("uid", 1)
},
Config: provider.ProviderConfig,
},
},
})

Expand Down Expand Up @@ -355,7 +377,41 @@ func TestGroupResourceHttpErrorHandling(t *testing.T) {
}`,
ExpectError: regexp.MustCompile(`(?s)Unable to create group - a sibling group already has the specified name\s*DebugID: 12312-123123-123123-1231212`),
},
// TODO: Test Deleting Error Handling
// Delete 4xx
{
PreConfig: func() {
// existing group
util.MockGetGroup("uid", util.GeneratePaginatedResponse(
[]map[string]interface{}{util.GenerateGroupResponseModel("uid", "", "")}),
1,
)
// delete group - with error
gock.New("https://test.api.capenetworks.com").
Delete("/uxi/v1alpha1/groups/uid").
Reply(422).
JSON(map[string]interface{}{
"httpStatusCode": 422,
"errorCode": "HPE_GL_UXI_GROUP_CANNOT_BE_DELETED",
"message": "Unable to delete group",
"debugId": "12312-123123-123123-1231212",
})
},
Config: provider.ProviderConfig,
ExpectError: regexp.MustCompile(`(?s)Unable to delete group\s*DebugID: 12312-123123-123123-1231212`),
},
// Actually delete group for cleanup reasons
{
PreConfig: func() {
// existing group
util.MockGetGroup("uid", util.GeneratePaginatedResponse(
[]map[string]interface{}{util.GenerateGroupResponseModel("uid", "", "")}),
1,
)
// delete group
util.MockDeleteGroup("uid", 1)
},
Config: provider.ProviderConfig,
},
},
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ func TestNetworkGroupAssignmentResourceForWiredNetwork(t *testing.T) {
1,
)

util.MockDeleteGroup("group_uid", 1)
util.MockDeleteGroup("group_uid_2", 1)
util.MockDeleteNetworkGroupAssignment("network_group_assignment_uid", 1)
util.MockDeleteNetworkGroupAssignment("network_group_assignment_uid_2", 1)
},
Expand Down Expand Up @@ -430,6 +432,8 @@ func TestNetworkGroupAssignmentResourceForWirelessNetwork(t *testing.T) {
1,
)

util.MockDeleteGroup("group_uid", 1)
util.MockDeleteGroup("group_uid_2", 1)
util.MockDeleteNetworkGroupAssignment("network_group_assignment_uid", 1)
util.MockDeleteNetworkGroupAssignment("network_group_assignment_uid_2", 1)
},
Expand Down Expand Up @@ -542,6 +546,7 @@ func TestNetworkGroupAssignmentResource429Handling(t *testing.T) {
1,
)

util.MockDeleteGroup("group_uid", 1)
mock429 = gock.New("https://test.api.capenetworks.com").
Delete("/uxi/v1alpha1/network-group-assignments/network_group_assignment_uid").
Reply(429).
Expand Down Expand Up @@ -839,6 +844,7 @@ func TestNetworkGroupAssignmentResourceHttpErrorHandling(t *testing.T) {
util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateNetworkGroupAssignmentResponse("network_group_assignment_uid", "")}),
1,
)
util.MockDeleteGroup("group_uid", 1)
util.MockDeleteNetworkGroupAssignment("network_group_assignment_uid", 1)
},
Config: provider.ProviderConfig + `
Expand Down
Loading

0 comments on commit b93040a

Please sign in to comment.