Skip to content

Commit

Permalink
and the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
1riatsila1 committed Nov 8, 2024
1 parent 203219b commit 1e4d6fc
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 75 deletions.
36 changes: 21 additions & 15 deletions internal/provider/resources/service_group_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,29 @@ func (r *serviceTestGroupAssignmentResource) Read(
return
}

// TODO: Call client getServiceTestGroupAssignment method
serviceTestGroupAssignment := GetServiceTestGroupAssignment(state.ID.ValueString())
request := r.client.ConfigurationAPI.
ServiceTestGroupAssignmentsGet(ctx).
Id(state.ID.ValueString())
networkGroupAssignmentResponse, response, err := util.RetryFor429(request.Execute)
errorPresent, errorDetail := util.RaiseForStatus(response, err)

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

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

if len(networkGroupAssignmentResponse.Items) != 1 {
resp.State.RemoveResource(ctx)
return
}
networkGroupAssignment := networkGroupAssignmentResponse.Items[0]

// Update state from client response
state.ID = types.StringValue(serviceTestGroupAssignment.Id)
state.GroupID = types.StringValue(serviceTestGroupAssignment.Group.Id)
state.ServiceTestID = types.StringValue(serviceTestGroupAssignment.ServiceTest.Id)
state.ID = types.StringValue(networkGroupAssignment.Id)
state.GroupID = types.StringValue(networkGroupAssignment.Group.Id)
state.ServiceTestID = types.StringValue(networkGroupAssignment.ServiceTest.Id)

// Set refreshed state
diags = resp.State.Set(ctx, &state)
Expand Down Expand Up @@ -218,13 +234,3 @@ func (r *serviceTestGroupAssignmentResource) ImportState(
) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

var GetServiceTestGroupAssignment = func(uid string) config_api_client.ServiceTestGroupAssignmentsPostResponse {
// TODO: Query the serviceTestGroupAssignment using the client
return config_api_client.ServiceTestGroupAssignmentsPostResponse{
Id: uid,
Group: *config_api_client.NewGroup("mock_group_uid"),
ServiceTest: *config_api_client.NewServiceTest("mock_serviceTest_uid"),
Type: "networking-uxi/service-test-group-assignment",
}
}
121 changes: 71 additions & 50 deletions test/mocked/resources/service_test_group_assignment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import (
"regexp"
"testing"

config_api_client "github.com/aruba-uxi/terraform-provider-configuration-api/pkg/config-api-client"
"github.com/aruba-uxi/terraform-provider-configuration/test/mocked/provider"
"github.com/aruba-uxi/terraform-provider-configuration/test/mocked/util"

"github.com/aruba-uxi/terraform-provider-configuration/internal/provider/resources"
"github.com/h2non/gock"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
Expand Down Expand Up @@ -38,9 +36,7 @@ func TestServiceTestGroupAssignmentResource(t *testing.T) {
// required for group create
util.MockPostGroup(
util.GenerateGroupRequestModel("group_uid", "", ""),
util.StructToMap(
util.GenerateNonRootGroupResponseModel("group_uid", "", ""),
),
util.GenerateNonRootGroupResponseModel("group_uid", "", ""),
1,
)
util.MockGetGroup(
Expand All @@ -58,20 +54,21 @@ func TestServiceTestGroupAssignmentResource(t *testing.T) {
"service_test_group_assignment_uid",
"",
),
util.StructToMap(
util.GenerateServiceTestGroupAssignmentResponse(
"service_test_group_assignment_uid",
"",
),
1,
)
util.MockGetServiceTestGroupAssignment(
"service_test_group_assignment_uid",
util.GeneratePaginatedResponse([]map[string]interface{}{
util.GenerateServiceTestGroupAssignmentResponse(
"service_test_group_assignment_uid",
"",
),
"service_test_group_assignment_uid", "",
)},
),
1,
)
resources.GetServiceTestGroupAssignment = func(uid string) config_api_client.ServiceTestGroupAssignmentsPostResponse {
return util.GenerateServiceTestGroupAssignmentResponse(
"service_test_group_assignment_uid",
"",
)
}
},

Config: provider.ProviderConfig + `
Expand Down Expand Up @@ -113,6 +110,17 @@ func TestServiceTestGroupAssignmentResource(t *testing.T) {
},
// ImportState testing
{
PreConfig: func() {
util.MockGetServiceTestGroupAssignment(
"service_test_group_assignment_uid",
util.GeneratePaginatedResponse([]map[string]interface{}{
util.GenerateServiceTestGroupAssignmentResponse(
"service_test_group_assignment_uid", "",
)},
),
1,
)
},
ResourceName: "uxi_service_test_group_assignment.my_service_test_group_assignment",
ImportState: true,
ImportStateVerify: true,
Expand Down Expand Up @@ -163,33 +171,37 @@ func TestServiceTestGroupAssignmentResource(t *testing.T) {
// required for creating another group
util.MockPostGroup(
util.GenerateGroupRequestModel("group_uid_2", "_2", "_2"),
util.StructToMap(
util.GenerateNonRootGroupResponseModel("group_uid_2", "_2", "_2"),
),
util.GenerateNonRootGroupResponseModel("group_uid_2", "_2", "_2"),
1,
)

// required for serviceTest group assignment create
resources.GetServiceTestGroupAssignment = func(uid string) config_api_client.ServiceTestGroupAssignmentsPostResponse {
if uid == "service_test_group_assignment_uid" {
return util.GenerateServiceTestGroupAssignmentResponse(
"service_test_group_assignment_uid",
"",
)
} else {
return util.GenerateServiceTestGroupAssignmentResponse("service_test_group_assignment_uid_2", "_2")
}
}
util.MockGetServiceTestGroupAssignment(
"service_test_group_assignment_uid_2",
util.GeneratePaginatedResponse([]map[string]interface{}{
util.GenerateServiceTestGroupAssignmentResponse(
"service_test_group_assignment_uid_2", "_2",
)},
),
2,
)
util.MockGetServiceTestGroupAssignment(
"service_test_group_assignment_uid",
util.GeneratePaginatedResponse([]map[string]interface{}{
util.GenerateServiceTestGroupAssignmentResponse(
"service_test_group_assignment_uid", "",
)},
),
1,
)
util.MockPostServiceTestGroupAssignment(
util.GenerateServiceTestGroupAssignmentRequest(
"service_test_group_assignment_uid_2",
"_2",
),
util.StructToMap(
util.GenerateServiceTestGroupAssignmentResponse(
"service_test_group_assignment_uid_2",
"_2",
),
util.GenerateServiceTestGroupAssignmentResponse(
"service_test_group_assignment_uid_2",
"_2",
),
1,
)
Expand Down Expand Up @@ -325,9 +337,7 @@ func TestServiceTestGroupAssignmentResource429Handling(t *testing.T) {
// required for group create
util.MockPostGroup(
util.GenerateGroupRequestModel("group_uid", "", ""),
util.StructToMap(
util.GenerateNonRootGroupResponseModel("group_uid", "", ""),
),
util.GenerateNonRootGroupResponseModel("group_uid", "", ""),
1,
)
util.MockGetGroup(
Expand All @@ -351,20 +361,21 @@ func TestServiceTestGroupAssignmentResource429Handling(t *testing.T) {
"service_test_group_assignment_uid",
"",
),
util.StructToMap(
util.GenerateServiceTestGroupAssignmentResponse(
"service_test_group_assignment_uid",
"",
),
1,
)
util.MockGetServiceTestGroupAssignment(
"service_test_group_assignment_uid",
util.GeneratePaginatedResponse([]map[string]interface{}{
util.GenerateServiceTestGroupAssignmentResponse(
"service_test_group_assignment_uid",
"",
),
"service_test_group_assignment_uid", "",
)},
),
1,
)
resources.GetServiceTestGroupAssignment = func(uid string) config_api_client.ServiceTestGroupAssignmentsPostResponse {
return util.GenerateServiceTestGroupAssignmentResponse(
"service_test_group_assignment_uid",
"",
)
}
},

Config: provider.ProviderConfig + `
Expand Down Expand Up @@ -410,9 +421,21 @@ func TestServiceTestGroupAssignmentResource429Handling(t *testing.T) {
),
1,
)
util.MockGetServiceTestGroupAssignment(
"service_test_group_assignment_uid",
util.GeneratePaginatedResponse([]map[string]interface{}{
util.GenerateServiceTestGroupAssignmentResponse(
"service_test_group_assignment_uid", "",
)},
),
1,
)

util.MockDeleteGroup("group_uid", 1)
util.MockDeleteServiceTestGroupAssignment("service_test_group_assignment", 1)
util.MockDeleteServiceTestGroupAssignment(
"service_test_group_assignment_uid",
1,
)
},
Config: provider.ProviderConfig + `
removed {
Expand Down Expand Up @@ -453,9 +476,7 @@ func TestServiceTestGroupAssignmentResourceHttpErrorHandling(t *testing.T) {
// required for group create
util.MockPostGroup(
util.GenerateGroupRequestModel("group_uid", "", ""),
util.StructToMap(
util.GenerateNonRootGroupResponseModel("group_uid", "", ""),
),
util.GenerateNonRootGroupResponseModel("group_uid", "", ""),
1,
)
util.MockGetGroup(
Expand Down
26 changes: 16 additions & 10 deletions test/mocked/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package util

import (
"encoding/json"
"github.com/aruba-uxi/terraform-provider-configuration-api/pkg/config-api-client"

"github.com/aruba-uxi/terraform-provider-configuration/internal/provider/resources"
"github.com/h2non/gock"
Expand Down Expand Up @@ -206,15 +205,12 @@ func GenerateNetworkGroupAssignmentRequest(uid string, postfix string) map[strin
}
}

func GenerateServiceTestGroupAssignmentResponse(
uid string,
postfix string,
) config_api_client.ServiceTestGroupAssignmentsPostResponse {
return config_api_client.ServiceTestGroupAssignmentsPostResponse{
Id: uid,
Group: *config_api_client.NewGroup("group_uid" + postfix),
ServiceTest: *config_api_client.NewServiceTest("service_test_uid" + postfix),
Type: "networking-uxi/service-test-group-assignment",
func GenerateServiceTestGroupAssignmentResponse(uid string, postfix string) map[string]interface{} {
return map[string]interface{}{
"id": uid,
"group": map[string]string{"id": "group_uid" + postfix},
"serviceTest": map[string]string{"id": "service_test_uid" + postfix},
"type": "networking-uxi/service-test-group-assignment",
}
}

Expand Down Expand Up @@ -429,6 +425,16 @@ func MockDeleteNetworkGroupAssignment(uid string, times int) {
Reply(204)
}

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

func MockPostServiceTestGroupAssignment(
request map[string]interface{},
response map[string]interface{},
Expand Down

0 comments on commit 1e4d6fc

Please sign in to comment.