diff --git a/pkg/config-api-provider/provider/resources/network_group_assignment.go b/pkg/config-api-provider/provider/resources/network_group_assignment.go index c9d5da90..d7f56acc 100644 --- a/pkg/config-api-provider/provider/resources/network_group_assignment.go +++ b/pkg/config-api-provider/provider/resources/network_group_assignment.go @@ -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" @@ -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" @@ -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) { @@ -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) @@ -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 diff --git a/pkg/config-api-provider/test/resources/network_group_assignment_test.go b/pkg/config-api-provider/test/resources/network_group_assignment_test.go index 07cad397..5cf7ff2e 100644 --- a/pkg/config-api-provider/test/resources/network_group_assignment_test.go +++ b/pkg/config-api-provider/test/resources/network_group_assignment_test.go @@ -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() @@ -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 + ` @@ -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, @@ -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 @@ -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"), @@ -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 { @@ -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, @@ -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 + ` @@ -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, @@ -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") } @@ -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 { diff --git a/pkg/config-api-provider/test/util/utils.go b/pkg/config-api-provider/test/util/utils.go index 84b64335..bc8f6cf6 100644 --- a/pkg/config-api-provider/test/util/utils.go +++ b/pkg/config-api-provider/test/util/utils.go @@ -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, @@ -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) }