Skip to content

Commit

Permalink
feat: service test group assignment resources (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
1riatsila1 authored Aug 30, 2024
1 parent d110f69 commit 61c74f1
Show file tree
Hide file tree
Showing 6 changed files with 358 additions and 5 deletions.
1 change: 1 addition & 0 deletions pkg/config-api-provider/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@ func (p *uxiConfigurationProvider) Resources(_ context.Context) []func() resourc
resources.NewSensorGroupAssignmentResource,
resources.NewAgentGroupAssignmentResource,
resources.NewNetworkGroupAssignmentResource,
resources.NewServiceTestGroupAssignmentResource,
}
}
6 changes: 3 additions & 3 deletions pkg/config-api-provider/provider/resources/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (r *serviceTestResource) Read(ctx context.Context, req resource.ReadRequest
return
}

response := GetServiceTest()
response := GetServiceTest(state.ID.ValueString())

// Update state from client response
state.Title = types.StringValue(response.Title)
Expand Down Expand Up @@ -113,11 +113,11 @@ func (r *serviceTestResource) ImportState(ctx context.Context, req resource.Impo
}

// Get the serviceTest using the configuration-api client
var GetServiceTest = func() ServiceTestResponseModel {
var GetServiceTest = func(uid string) ServiceTestResponseModel {
// TODO: Query the serviceTest using the client

return ServiceTestResponseModel{
Uid: "uid",
Uid: uid,
Category: "category",
Title: "title",
Target: "target",
Expand Down
172 changes: 172 additions & 0 deletions pkg/config-api-provider/provider/resources/service_group_assignment.go
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",
}
}
4 changes: 2 additions & 2 deletions pkg/config-api-provider/test/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func TestServiceTestResource(t *testing.T) {
// Importing a service_test
{
PreConfig: func() {
resources.GetServiceTest = func() resources.ServiceTestResponseModel {
return GenerateServiceTestResponseModel("uid", "")
resources.GetServiceTest = func(uid string) resources.ServiceTestResponseModel {
return GenerateServiceTestResponseModel(uid, "")
}
},
Config: providerConfig + `
Expand Down
172 changes: 172 additions & 0 deletions pkg/config-api-provider/test/service_test_group_assignment_test.go
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
},
})
}
8 changes: 8 additions & 0 deletions pkg/config-api-provider/test/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,11 @@ func GenerateNetworkGroupAssignmentResponse(uid string, postfix string) resource
NetworkUID: "network_uid" + postfix,
}
}

func GenerateServiceTestGroupAssignmentResponse(uid string, postfix string) resources.ServiceTestGroupAssignmentResponseModel {
return resources.ServiceTestGroupAssignmentResponseModel{
UID: "service_test_group_assignment_uid" + postfix,
GroupUID: "group_uid" + postfix,
ServiceTestUID: "service_test_uid" + postfix,
}
}

0 comments on commit 61c74f1

Please sign in to comment.