-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: service test group assignment resources (#15)
- Loading branch information
1 parent
d110f69
commit 61c74f1
Showing
6 changed files
with
358 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
172 changes: 172 additions & 0 deletions
172
pkg/config-api-provider/provider/resources/service_group_assignment.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
package resources | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
// Ensure the implementation satisfies the expected interfaces. | ||
var ( | ||
_ resource.Resource = &serviceTestGroupAssignmentResource{} | ||
_ resource.ResourceWithConfigure = &serviceTestGroupAssignmentResource{} | ||
) | ||
|
||
type serviceTestGroupAssignmentResourceModel struct { | ||
ID types.String `tfsdk:"id"` | ||
ServiceTestID types.String `tfsdk:"service_test_id"` | ||
GroupID types.String `tfsdk:"group_id"` | ||
} | ||
|
||
type ServiceTestGroupAssignmentResponseModel struct { | ||
UID string // <assignment_uid> | ||
GroupUID string // <group_uid:str>, | ||
ServiceTestUID string // <service_test_uid:str> | ||
} | ||
|
||
type ServiceTestGroupAssignmentRequestModel struct { | ||
GroupUID string // <group_uid:str>, | ||
ServiceTestUID string // <service_test_uid:str> | ||
} | ||
|
||
func NewServiceTestGroupAssignmentResource() resource.Resource { | ||
return &serviceTestGroupAssignmentResource{} | ||
} | ||
|
||
type serviceTestGroupAssignmentResource struct{} | ||
|
||
func (r *serviceTestGroupAssignmentResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_service_test_group_assignment" | ||
} | ||
|
||
func (r *serviceTestGroupAssignmentResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Attributes: map[string]schema.Attribute{ | ||
"id": schema.StringAttribute{ | ||
Computed: true, | ||
PlanModifiers: []planmodifier.String{ | ||
stringplanmodifier.UseStateForUnknown(), | ||
}, | ||
}, | ||
"service_test_id": schema.StringAttribute{ | ||
Required: true, | ||
PlanModifiers: []planmodifier.String{ | ||
stringplanmodifier.RequiresReplace(), | ||
}, | ||
}, | ||
"group_id": schema.StringAttribute{ | ||
Required: true, | ||
PlanModifiers: []planmodifier.String{ | ||
stringplanmodifier.RequiresReplace(), | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (r *serviceTestGroupAssignmentResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { | ||
} | ||
|
||
func (r *serviceTestGroupAssignmentResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { | ||
// Retrieve values from plan | ||
var plan serviceTestGroupAssignmentResourceModel | ||
diags := req.Plan.Get(ctx, &plan) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
// TODO: Call client createServiceTestGroupAssignment method | ||
serviceTestGroupAssignment := CreateServiceTestGroupAssignment(ServiceTestGroupAssignmentRequestModel{ | ||
GroupUID: plan.GroupID.ValueString(), | ||
ServiceTestUID: plan.ServiceTestID.ValueString(), | ||
}) | ||
|
||
// Update the state to match the plan | ||
plan.ID = types.StringValue(serviceTestGroupAssignment.UID) | ||
plan.GroupID = types.StringValue(serviceTestGroupAssignment.GroupUID) | ||
plan.ServiceTestID = types.StringValue(serviceTestGroupAssignment.ServiceTestUID) | ||
|
||
// Set state to fully populated data | ||
diags = resp.State.Set(ctx, plan) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
} | ||
|
||
func (r *serviceTestGroupAssignmentResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { | ||
// Get current state | ||
var state serviceTestGroupAssignmentResourceModel | ||
diags := req.State.Get(ctx, &state) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
// TODO: Call client getServiceTestGroupAssignment method | ||
serviceTestGroupAssignment := GetServiceTestGroupAssignment(state.ID.ValueString()) | ||
|
||
// Update state from client response | ||
state.GroupID = types.StringValue(serviceTestGroupAssignment.GroupUID) | ||
state.ServiceTestID = types.StringValue(serviceTestGroupAssignment.ServiceTestUID) | ||
|
||
// Set refreshed state | ||
diags = resp.State.Set(ctx, &state) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
} | ||
|
||
func (r *serviceTestGroupAssignmentResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { | ||
// Retrieve values from plan | ||
var plan serviceTestGroupAssignmentResourceModel | ||
diags := req.Plan.Get(ctx, &plan) | ||
diags.AddError("operation not supported", "updating a service_test group assignment is not supported") | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
} | ||
|
||
func (r *serviceTestGroupAssignmentResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { | ||
// Retrieve values from state | ||
var state serviceTestGroupAssignmentResourceModel | ||
diags := req.State.Get(ctx, &state) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
// Delete existing serviceTestGroupAssignment using the plan_id | ||
} | ||
|
||
func (r *serviceTestGroupAssignmentResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { | ||
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp) | ||
} | ||
|
||
var GetServiceTestGroupAssignment = func(uid string) ServiceTestGroupAssignmentResponseModel { | ||
// TODO: Query the serviceTestGroupAssignment using the client | ||
|
||
return ServiceTestGroupAssignmentResponseModel{ | ||
UID: uid, | ||
GroupUID: "mock_group_uid", | ||
ServiceTestUID: "mock_serviceTest_uid", | ||
} | ||
} | ||
|
||
var CreateServiceTestGroupAssignment = func(request ServiceTestGroupAssignmentRequestModel) ServiceTestGroupAssignmentResponseModel { | ||
// TODO: Query the serviceTestGroupAssignment using the client | ||
|
||
return ServiceTestGroupAssignmentResponseModel{ | ||
UID: "mock_uid", | ||
GroupUID: "mock_group_uid", | ||
ServiceTestUID: "mock_serviceTest_uid", | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
172 changes: 172 additions & 0 deletions
172
pkg/config-api-provider/test/service_test_group_assignment_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
package test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/resources" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
) | ||
|
||
func TestServiceTestGroupAssignmentResource(t *testing.T) { | ||
|
||
resource.Test(t, resource.TestCase{ | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
// Creating a serviceTest group assignment | ||
{ | ||
PreConfig: func() { | ||
// required for serviceTest import | ||
resources.GetServiceTest = func(uid string) resources.ServiceTestResponseModel { | ||
return GenerateServiceTestResponseModel(uid, "") | ||
} | ||
|
||
// required for group create | ||
groupResponse := GenerateGroupResponseModel("group_uid", "", "") | ||
resources.CreateGroup = func(request resources.GroupCreateRequestModel) resources.GroupResponseModel { | ||
return groupResponse | ||
} | ||
resources.GetGroup = func(uid string) resources.GroupResponseModel { | ||
return groupResponse | ||
} | ||
|
||
// required for serviceTest group assignment create | ||
serviceTestGroupAssignmentResponse := GenerateServiceTestGroupAssignmentResponse("service_test_group_assignment_uid", "") | ||
resources.CreateServiceTestGroupAssignment = func(request resources.ServiceTestGroupAssignmentRequestModel) resources.ServiceTestGroupAssignmentResponseModel { | ||
return serviceTestGroupAssignmentResponse | ||
} | ||
resources.GetServiceTestGroupAssignment = func(uid string) resources.ServiceTestGroupAssignmentResponseModel { | ||
return serviceTestGroupAssignmentResponse | ||
} | ||
}, | ||
|
||
Config: providerConfig + ` | ||
resource "uxi_group" "my_group" { | ||
name = "name" | ||
parent_uid = "parent_uid" | ||
} | ||
resource "uxi_service_test" "my_service_test" { | ||
title = "title" | ||
} | ||
import { | ||
to = uxi_service_test.my_service_test | ||
id = "service_test_uid" | ||
} | ||
resource "uxi_service_test_group_assignment" "my_service_test_group_assignment" { | ||
service_test_id = uxi_service_test.my_service_test.id | ||
group_id = uxi_group.my_group.id | ||
}`, | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr("uxi_service_test_group_assignment.my_service_test_group_assignment", "service_test_id", "service_test_uid"), | ||
resource.TestCheckResourceAttr("uxi_service_test_group_assignment.my_service_test_group_assignment", "group_id", "group_uid"), | ||
resource.TestCheckResourceAttr("uxi_service_test_group_assignment.my_service_test_group_assignment", "id", "service_test_group_assignment_uid"), | ||
), | ||
}, | ||
// ImportState testing | ||
{ | ||
ResourceName: "uxi_service_test_group_assignment.my_service_test_group_assignment", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
// Update and Read testing | ||
{ | ||
PreConfig: func() { | ||
resources.GetServiceTest = func(uid string) resources.ServiceTestResponseModel { | ||
if uid == "service_test_uid" { | ||
return GenerateServiceTestResponseModel("service_test_uid", "") | ||
} else { | ||
return GenerateServiceTestResponseModel("service_test_uid", "_2") | ||
} | ||
} | ||
|
||
// required for creating another group | ||
resources.CreateGroup = func(request resources.GroupCreateRequestModel) resources.GroupResponseModel { | ||
return GenerateGroupResponseModel("group_uid_2", "_2", "_2") | ||
} | ||
resources.GetGroup = func(uid string) resources.GroupResponseModel { | ||
if uid == "group_uid" { | ||
return GenerateGroupResponseModel(uid, "", "") | ||
} else { | ||
return GenerateGroupResponseModel("group_uid_2", "_2", "_2") | ||
} | ||
} | ||
|
||
// required for serviceTest group assignment create | ||
resources.GetServiceTestGroupAssignment = func(uid string) resources.ServiceTestGroupAssignmentResponseModel { | ||
if uid == "service_test_group_assignment_uid" { | ||
return GenerateServiceTestGroupAssignmentResponse("service_test_group_assignment_uid", "") | ||
} else { | ||
return GenerateServiceTestGroupAssignmentResponse("service_test_group_assignment_uid_2", "_2") | ||
} | ||
} | ||
resources.CreateServiceTestGroupAssignment = func(request resources.ServiceTestGroupAssignmentRequestModel) resources.ServiceTestGroupAssignmentResponseModel { | ||
return GenerateServiceTestGroupAssignmentResponse("service_test_group_assignment_uid_2", "_2") | ||
} | ||
}, | ||
Config: providerConfig + ` | ||
// the original resources | ||
resource "uxi_group" "my_group" { | ||
name = "name" | ||
parent_uid = "parent_uid" | ||
} | ||
resource "uxi_service_test" "my_service_test" { | ||
title = "title" | ||
} | ||
import { | ||
to = uxi_service_test.my_service_test | ||
id = "service_test_uid" | ||
} | ||
// the new resources we wanna update the assignment to | ||
resource "uxi_group" "my_group_2" { | ||
name = "name_2" | ||
parent_uid = "parent_uid_2" | ||
} | ||
resource "uxi_service_test" "my_service_test_2" { | ||
title = "title_2" | ||
} | ||
import { | ||
to = uxi_service_test.my_service_test_2 | ||
id = "service_test_uid_2" | ||
} | ||
// the assignment update, updated from service_test/group to service_test_2/group_2 | ||
resource "uxi_service_test_group_assignment" "my_service_test_group_assignment" { | ||
service_test_id = uxi_service_test.my_service_test_2.id | ||
group_id = uxi_group.my_group_2.id | ||
}`, | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr("uxi_service_test_group_assignment.my_service_test_group_assignment", "service_test_id", "service_test_uid_2"), | ||
resource.TestCheckResourceAttr("uxi_service_test_group_assignment.my_service_test_group_assignment", "group_id", "group_uid_2"), | ||
resource.TestCheckResourceAttr("uxi_service_test_group_assignment.my_service_test_group_assignment", "id", "service_test_group_assignment_uid_2"), | ||
), | ||
}, | ||
// Remove serviceTests from state | ||
{ | ||
Config: providerConfig + ` | ||
removed { | ||
from = uxi_service_test.my_service_test | ||
lifecycle { | ||
destroy = false | ||
} | ||
} | ||
removed { | ||
from = uxi_service_test.my_service_test_2 | ||
lifecycle { | ||
destroy = false | ||
} | ||
}`, | ||
}, | ||
// Delete testing automatically occurs in TestCase | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters