Skip to content

Commit

Permalink
feat: update resources
Browse files Browse the repository at this point in the history
  • Loading branch information
1riatsila1 committed Oct 2, 2024
1 parent bfb6c6b commit 95b6431
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ 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/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
Expand Down Expand Up @@ -38,7 +40,9 @@ func NewNetworkGroupAssignmentResource() resource.Resource {
return &networkGroupAssignmentResource{}
}

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

func (r *networkGroupAssignmentResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_network_group_assignment"
Expand Down Expand Up @@ -70,6 +74,21 @@ func (r *networkGroupAssignmentResource) Schema(_ context.Context, _ resource.Sc
}

func (r *networkGroupAssignmentResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
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: Network Group Assignment. Please report this issue to the provider developers.",
)
return
}

r.client = client
}

func (r *networkGroupAssignmentResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
Expand Down Expand Up @@ -110,11 +129,23 @@ func (r *networkGroupAssignmentResource) Read(ctx context.Context, req resource.
}

// TODO: Call client getNetworkGroupAssignment method
networkGroupAssignment := GetNetworkGroupAssignment(state.ID.ValueString())
request := r.client.ConfigurationAPI.
GetUxiV1alpha1NetworkGroupAssignmentsGet(ctx).
Uid(state.ID.ValueString())

networkGroupAssignmentResponse, _, err := util.RetryFor429(request.Execute)
if err != nil || len(networkGroupAssignmentResponse.NetworkGroupAssignments) != 1 {
resp.Diagnostics.AddError(
"Error reading Network Group Assignment",
"Could not retrieve Network Group Assignment, unexpected error: "+err.Error(),
)
return
}
networkGroupAssignment := networkGroupAssignmentResponse.NetworkGroupAssignments[0]

// Update state from client response
state.GroupID = types.StringValue(networkGroupAssignment.GroupUID)
state.NetworkID = types.StringValue(networkGroupAssignment.NetworkUID)
state.GroupID = types.StringValue(networkGroupAssignment.GroupUid)
state.NetworkID = types.StringValue(networkGroupAssignment.NetworkUid)

// Set refreshed state
diags = resp.State.Set(ctx, &state)
Expand Down Expand Up @@ -151,16 +182,6 @@ func (r *networkGroupAssignmentResource) ImportState(ctx context.Context, req re
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

var GetNetworkGroupAssignment = func(uid string) NetworkGroupAssignmentResponseModel {
// TODO: Query the networkGroupAssignment using the client

return NetworkGroupAssignmentResponseModel{
UID: uid,
GroupUID: "mock_group_uid",
NetworkUID: "mock_network_uid",
}
}

var CreateNetworkGroupAssignment = func(request NetworkGroupAssignmentRequestModel) NetworkGroupAssignmentResponseModel {
// TODO: Query the networkGroupAssignment using the client

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestNetworkGroupAssignmentResource(t *testing.T) {
func TestNetworkGroupAssignmentResourceForWiredNetwork(t *testing.T) {
defer gock.Off()
mockOAuth := util.MockOAuth()

Expand Down Expand Up @@ -38,9 +38,11 @@ func TestNetworkGroupAssignmentResource(t *testing.T) {
resources.CreateNetworkGroupAssignment = func(request resources.NetworkGroupAssignmentRequestModel) resources.NetworkGroupAssignmentResponseModel {
return util.GenerateNetworkGroupAssignmentResponse("network_group_assignment_uid", "")
}
resources.GetNetworkGroupAssignment = func(uid string) resources.NetworkGroupAssignmentResponseModel {
return util.GenerateNetworkGroupAssignmentResponse("network_group_assignment_uid", "")
}
util.MockGetNetworkGroupAssignment(
"network_group_assignment_uid",
util.GenerateNetworkGroupAssignmentPaginatedResponse([]map[string]interface{}{util.GenerateNetworkGroupAssignmentGetResponse("network_group_assignment_uid", "")}),
1,
)
},

Config: provider.ProviderConfig + `
Expand Down Expand Up @@ -70,6 +72,13 @@ func TestNetworkGroupAssignmentResource(t *testing.T) {
},
// ImportState testing
{
PreConfig: func() {
util.MockGetNetworkGroupAssignment(
"network_group_assignment_uid",
util.GenerateNetworkGroupAssignmentPaginatedResponse([]map[string]interface{}{util.GenerateNetworkGroupAssignmentGetResponse("network_group_assignment_uid", "")}),
1,
)
},
ResourceName: "uxi_network_group_assignment.my_network_group_assignment",
ImportState: true,
ImportStateVerify: true,
Expand Down Expand Up @@ -105,13 +114,16 @@ func TestNetworkGroupAssignmentResource(t *testing.T) {
resources.CreateNetworkGroupAssignment = func(request resources.NetworkGroupAssignmentRequestModel) resources.NetworkGroupAssignmentResponseModel {
return util.GenerateNetworkGroupAssignmentResponse("network_group_assignment_uid_2", "_2")
}
resources.GetNetworkGroupAssignment = func(uid string) resources.NetworkGroupAssignmentResponseModel {
if uid == "network_group_assignment_uid" {
return util.GenerateNetworkGroupAssignmentResponse(uid, "")
} else {
return util.GenerateNetworkGroupAssignmentResponse(uid, "_2")
}
}
util.MockGetNetworkGroupAssignment(
"network_group_assignment_uid_2",
util.GenerateNetworkGroupAssignmentPaginatedResponse([]map[string]interface{}{util.GenerateNetworkGroupAssignmentGetResponse("network_group_assignment_uid_2", "_2")}),
2,
)
util.MockGetNetworkGroupAssignment(
"network_group_assignment_uid",
util.GenerateNetworkGroupAssignmentPaginatedResponse([]map[string]interface{}{util.GenerateNetworkGroupAssignmentGetResponse("network_group_assignment_uid", "")}),
2,
)
},
Config: provider.ProviderConfig + `
// the original resources
Expand Down Expand Up @@ -147,7 +159,7 @@ func TestNetworkGroupAssignmentResource(t *testing.T) {
// the assignment update, updated from network/group to network_2/group_2
resource "uxi_network_group_assignment" "my_network_group_assignment" {
network_id = uxi_wired_network.my_network_2.id
group_id = uxi_group.my_group_2.id
group_id = uxi_group.my_group_2.id
}`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("uxi_network_group_assignment.my_network_group_assignment", "network_id", "network_uid_2"),
Expand Down Expand Up @@ -176,6 +188,16 @@ func TestNetworkGroupAssignmentResource(t *testing.T) {
[]map[string]interface{}{util.StructToMap(util.GenerateGroupResponseGetModel("group_uid_2", "_2", "_2"))}),
1,
)
util.MockGetNetworkGroupAssignment(
"network_group_assignment_uid_2",
util.GenerateNetworkGroupAssignmentPaginatedResponse([]map[string]interface{}{util.GenerateNetworkGroupAssignmentGetResponse("network_group_assignment_uid_2", "_2")}),
1,
)
util.MockGetNetworkGroupAssignment(
"network_group_assignment_uid",
util.GenerateNetworkGroupAssignmentPaginatedResponse([]map[string]interface{}{util.GenerateNetworkGroupAssignmentGetResponse("network_group_assignment_uid", "")}),
1,
)
},
Config: provider.ProviderConfig + `
removed {
Expand All @@ -198,6 +220,13 @@ func TestNetworkGroupAssignmentResource(t *testing.T) {
},
})

mockOAuth.Mock.Disable()
}

func TestNetworkGroupAssignmentResourceForWirelessNetwork(t *testing.T) {
defer gock.Off()
mockOAuth := util.MockOAuth()

// Test Wireless Network Group Assignment
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories,
Expand All @@ -223,9 +252,11 @@ func TestNetworkGroupAssignmentResource(t *testing.T) {
resources.CreateNetworkGroupAssignment = func(request resources.NetworkGroupAssignmentRequestModel) resources.NetworkGroupAssignmentResponseModel {
return util.GenerateNetworkGroupAssignmentResponse("network_group_assignment_uid", "")
}
resources.GetNetworkGroupAssignment = func(uid string) resources.NetworkGroupAssignmentResponseModel {
return util.GenerateNetworkGroupAssignmentResponse(uid, "")
}
util.MockGetNetworkGroupAssignment(
"network_group_assignment_uid",
util.GenerateNetworkGroupAssignmentPaginatedResponse([]map[string]interface{}{util.GenerateNetworkGroupAssignmentGetResponse("network_group_assignment_uid", "")}),
1,
)
},

Config: provider.ProviderConfig + `
Expand Down Expand Up @@ -255,6 +286,13 @@ func TestNetworkGroupAssignmentResource(t *testing.T) {
},
// ImportState testing
{
PreConfig: func() {
util.MockGetNetworkGroupAssignment(
"network_group_assignment_uid",
util.GenerateNetworkGroupAssignmentPaginatedResponse([]map[string]interface{}{util.GenerateNetworkGroupAssignmentGetResponse("network_group_assignment_uid", "")}),
1,
)
},
ResourceName: "uxi_network_group_assignment.my_network_group_assignment",
ImportState: true,
ImportStateVerify: true,
Expand Down Expand Up @@ -288,13 +326,16 @@ func TestNetworkGroupAssignmentResource(t *testing.T) {
)

// required for network group assignment create
resources.GetNetworkGroupAssignment = func(uid string) resources.NetworkGroupAssignmentResponseModel {
if uid == "network_group_assignment_uid" {
return util.GenerateNetworkGroupAssignmentResponse("network_group_assignment_uid", "")
} else {
return util.GenerateNetworkGroupAssignmentResponse("network_group_assignment_uid_2", "_2")
}
}
util.MockGetNetworkGroupAssignment(
"network_group_assignment_uid_2",
util.GenerateNetworkGroupAssignmentPaginatedResponse([]map[string]interface{}{util.GenerateNetworkGroupAssignmentGetResponse("network_group_assignment_uid_2", "_2")}),
2,
)
util.MockGetNetworkGroupAssignment(
"network_group_assignment_uid",
util.GenerateNetworkGroupAssignmentPaginatedResponse([]map[string]interface{}{util.GenerateNetworkGroupAssignmentGetResponse("network_group_assignment_uid", "")}),
2,
)
resources.CreateNetworkGroupAssignment = func(request resources.NetworkGroupAssignmentRequestModel) resources.NetworkGroupAssignmentResponseModel {
return util.GenerateNetworkGroupAssignmentResponse("network_group_assignment_uid_2", "_2")
}
Expand Down Expand Up @@ -364,6 +405,16 @@ func TestNetworkGroupAssignmentResource(t *testing.T) {
util.GeneratePaginatedResponse([]map[string]interface{}{util.StructToMap(util.GenerateGroupResponseGetModel("group_uid_2", "_2", "_2"))}),
1,
)
util.MockGetNetworkGroupAssignment(
"network_group_assignment_uid_2",
util.GenerateNetworkGroupAssignmentPaginatedResponse([]map[string]interface{}{util.GenerateNetworkGroupAssignmentGetResponse("network_group_assignment_uid_2", "_2")}),
1,
)
util.MockGetNetworkGroupAssignment(
"network_group_assignment_uid",
util.GenerateNetworkGroupAssignmentPaginatedResponse([]map[string]interface{}{util.GenerateNetworkGroupAssignmentGetResponse("network_group_assignment_uid", "")}),
1,
)
},
Config: provider.ProviderConfig + `
removed {
Expand Down
24 changes: 24 additions & 0 deletions pkg/config-api-provider/test/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,21 @@ func GenerateNetworkGroupAssignmentResponse(uid string, postfix string) resource
}
}

func GenerateNetworkGroupAssignmentPaginatedResponse(networkGroupAssignments []map[string]interface{}) map[string]interface{} {
return map[string]interface{}{
"network_group_assignments": networkGroupAssignments,
"pagination": mockPaginationResponse,
}
}

func GenerateNetworkGroupAssignmentGetResponse(uid string, postfix string) map[string]interface{} {
return map[string]interface{}{
"uid": uid,
"group_uid": "group_uid" + postfix,
"network_uid": "network_uid" + postfix,
}
}

func GenerateServiceTestGroupAssignmentResponse(uid string, postfix string) resources.ServiceTestGroupAssignmentResponseModel {
return resources.ServiceTestGroupAssignmentResponseModel{
UID: uid,
Expand Down Expand Up @@ -263,5 +278,14 @@ func MockPostSensorGroupAssignment(uid string, response map[string]interface{},
Times(times).
Reply(200).
JSON(response)
}

func MockGetNetworkGroupAssignment(uid string, response map[string]interface{}, times int) {
gock.New("https://test.api.capenetworks.com").
Get("/uxi/v1alpha1/network-group-assignments").
MatchHeader("Authorization", "mock_token").
MatchParam("uid", uid).
Times(times).
Reply(200).
JSON(response)
}

0 comments on commit 95b6431

Please sign in to comment.