Skip to content

Commit

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

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]
// TODO: Call client getServiceTestGroupAssignment method
serviceTestGroupAssignment := GetServiceTestGroupAssignment(state.ID.ValueString())

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

// Set refreshed state
diags = resp.State.Set(ctx, &state)
Expand Down Expand Up @@ -234,3 +218,13 @@ 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: 50 additions & 71 deletions test/mocked/resources/service_test_group_assignment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ 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 @@ -36,7 +38,9 @@ func TestServiceTestGroupAssignmentResource(t *testing.T) {
// required for group create
util.MockPostGroup(
util.GenerateGroupRequestModel("group_uid", "", ""),
util.GenerateNonRootGroupResponseModel("group_uid", "", ""),
util.StructToMap(
util.GenerateNonRootGroupResponseModel("group_uid", "", ""),
),
1,
)
util.MockGetGroup(
Expand All @@ -54,21 +58,20 @@ func TestServiceTestGroupAssignmentResource(t *testing.T) {
"service_test_group_assignment_uid",
"",
),
util.GenerateServiceTestGroupAssignmentResponse(
"service_test_group_assignment_uid",
"",
),
1,
)
util.MockGetServiceTestGroupAssignment(
"service_test_group_assignment_uid",
util.GeneratePaginatedResponse([]map[string]interface{}{
util.StructToMap(
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 @@ -110,17 +113,6 @@ 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 @@ -171,37 +163,33 @@ func TestServiceTestGroupAssignmentResource(t *testing.T) {
// required for creating another group
util.MockPostGroup(
util.GenerateGroupRequestModel("group_uid_2", "_2", "_2"),
util.GenerateNonRootGroupResponseModel("group_uid_2", "_2", "_2"),
util.StructToMap(
util.GenerateNonRootGroupResponseModel("group_uid_2", "_2", "_2"),
),
1,
)

// required for serviceTest group assignment create
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,
)
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.MockPostServiceTestGroupAssignment(
util.GenerateServiceTestGroupAssignmentRequest(
"service_test_group_assignment_uid_2",
"_2",
),
util.GenerateServiceTestGroupAssignmentResponse(
"service_test_group_assignment_uid_2",
"_2",
util.StructToMap(
util.GenerateServiceTestGroupAssignmentResponse(
"service_test_group_assignment_uid_2",
"_2",
),
),
1,
)
Expand Down Expand Up @@ -337,7 +325,9 @@ func TestServiceTestGroupAssignmentResource429Handling(t *testing.T) {
// required for group create
util.MockPostGroup(
util.GenerateGroupRequestModel("group_uid", "", ""),
util.GenerateNonRootGroupResponseModel("group_uid", "", ""),
util.StructToMap(
util.GenerateNonRootGroupResponseModel("group_uid", "", ""),
),
1,
)
util.MockGetGroup(
Expand All @@ -361,21 +351,20 @@ func TestServiceTestGroupAssignmentResource429Handling(t *testing.T) {
"service_test_group_assignment_uid",
"",
),
util.GenerateServiceTestGroupAssignmentResponse(
"service_test_group_assignment_uid",
"",
),
1,
)
util.MockGetServiceTestGroupAssignment(
"service_test_group_assignment_uid",
util.GeneratePaginatedResponse([]map[string]interface{}{
util.StructToMap(
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 @@ -421,21 +410,9 @@ 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_uid",
1,
)
util.MockDeleteServiceTestGroupAssignment("service_test_group_assignment", 1)
},
Config: provider.ProviderConfig + `
removed {
Expand Down Expand Up @@ -476,7 +453,9 @@ func TestServiceTestGroupAssignmentResourceHttpErrorHandling(t *testing.T) {
// required for group create
util.MockPostGroup(
util.GenerateGroupRequestModel("group_uid", "", ""),
util.GenerateNonRootGroupResponseModel("group_uid", "", ""),
util.StructToMap(
util.GenerateNonRootGroupResponseModel("group_uid", "", ""),
),
1,
)
util.MockGetGroup(
Expand Down
26 changes: 10 additions & 16 deletions test/mocked/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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 @@ -205,12 +206,15 @@ func GenerateNetworkGroupAssignmentRequest(uid string, postfix string) map[strin
}
}

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",
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",
}
}

Expand Down Expand Up @@ -425,16 +429,6 @@ 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 c891900

Please sign in to comment.