Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: agent group assignment #13

Merged
merged 5 commits into from
Aug 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pkg/config-api-provider/examples/full-demo/main.tf
Original file line number Diff line number Diff line change
@@ -120,11 +120,18 @@ import {
id = "uid"
}

// Sensor Group Assignment
resource "uxi_sensor_group_assignment" "my_uxi_sensor_group_assignment" {
sensor_id = uxi_sensor.my_sensor.id
group_id = uxi_group.my_group.id
}

// Agent Group Assignment
resource "uxi_agent_group_assignment" "my_uxi_agent_group_assignment" {
agent_id = uxi_agent.my_agent.id
group_id = uxi_group.my_group.id
}

# output "group" {
# value = uxi_group.group
# }
1 change: 1 addition & 0 deletions pkg/config-api-provider/provider/provider.go
Original file line number Diff line number Diff line change
@@ -64,5 +64,6 @@ func (p *uxiConfigurationProvider) Resources(_ context.Context) []func() resourc
resources.NewWirelessNetworkResource,
resources.NewServiceTestResource,
resources.NewSensorGroupAssignmentResource,
resources.NewAgentGroupAssignmentResource,
}
}
6 changes: 3 additions & 3 deletions pkg/config-api-provider/provider/resources/agent.go
Original file line number Diff line number Diff line change
@@ -97,7 +97,7 @@
return
}

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

// Update state from client response
state.Name = types.StringValue(response.Name)
@@ -159,11 +159,11 @@
}

// Get the agent using the configuration-api client
var GetAgent = func() AgentResponseModel {
var GetAgent = func(uid string) AgentResponseModel {

Check warning on line 162 in pkg/config-api-provider/provider/resources/agent.go

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/agent.go#L162

Added line #L162 was not covered by tests
// TODO: Query the agent using the client

return AgentResponseModel{
UID: "mock_uid",
UID: uid,

Check warning on line 166 in pkg/config-api-provider/provider/resources/agent.go

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/agent.go#L166

Added line #L166 was not covered by tests
Serial: "mock_serial",
Name: "mock_name",
ModelNumber: "mock_model_number",
172 changes: 172 additions & 0 deletions pkg/config-api-provider/provider/resources/agent_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 = &agentGroupAssignmentResource{}
_ resource.ResourceWithConfigure = &agentGroupAssignmentResource{}
)

type agentGroupAssignmentResourceModel struct {
ID types.String `tfsdk:"id"`
AgentID types.String `tfsdk:"agent_id"`
GroupID types.String `tfsdk:"group_id"`
}

type AgentGroupAssignmentResponseModel struct {
UID string // <assignment_uid>
GroupUID string // <group_uid:str>,
AgentUID string // <agent_uid:str>
}

type AgentGroupAssignmentRequestModel struct {
GroupUID string // <group_uid:str>,
AgentUID string // <agent_uid:str>
}

func NewAgentGroupAssignmentResource() resource.Resource {
return &agentGroupAssignmentResource{}
}

type agentGroupAssignmentResource struct{}

func (r *agentGroupAssignmentResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_agent_group_assignment"
}

func (r *agentGroupAssignmentResource) 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(),
},
},
"agent_id": schema.StringAttribute{
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"group_id": schema.StringAttribute{
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
},
}
}

func (r *agentGroupAssignmentResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
}

func (r *agentGroupAssignmentResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
// Retrieve values from plan
var plan agentGroupAssignmentResourceModel
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return

Check warning on line 81 in pkg/config-api-provider/provider/resources/agent_group_assignment.go

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/agent_group_assignment.go#L81

Added line #L81 was not covered by tests
}

// TODO: Call client createAgentGroupAssignment method
agentGroupAssignment := CreateAgentGroupAssignment(AgentGroupAssignmentRequestModel{
GroupUID: plan.GroupID.ValueString(),
AgentUID: plan.AgentID.ValueString(),
})

// Update the state to match the plan
plan.ID = types.StringValue(agentGroupAssignment.UID)
plan.GroupID = types.StringValue(agentGroupAssignment.GroupUID)
plan.AgentID = types.StringValue(agentGroupAssignment.AgentUID)

// Set state to fully populated data
diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return

Check warning on line 99 in pkg/config-api-provider/provider/resources/agent_group_assignment.go

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/agent_group_assignment.go#L99

Added line #L99 was not covered by tests
}
}

func (r *agentGroupAssignmentResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
// Get current state
var state agentGroupAssignmentResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return

Check warning on line 109 in pkg/config-api-provider/provider/resources/agent_group_assignment.go

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/agent_group_assignment.go#L109

Added line #L109 was not covered by tests
}

// TODO: Call client getAgentGroupAssignment method
agentGroupAssignment := GetAgentGroupAssignment(state.ID.ValueString())

// Update state from client response
state.GroupID = types.StringValue(agentGroupAssignment.GroupUID)
state.AgentID = types.StringValue(agentGroupAssignment.AgentUID)

// Set refreshed state
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return

Check warning on line 123 in pkg/config-api-provider/provider/resources/agent_group_assignment.go

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/agent_group_assignment.go#L123

Added line #L123 was not covered by tests
}
}

func (r *agentGroupAssignmentResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {

Check warning on line 127 in pkg/config-api-provider/provider/resources/agent_group_assignment.go

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/agent_group_assignment.go#L127

Added line #L127 was not covered by tests
// Retrieve values from plan
var plan agentGroupAssignmentResourceModel
diags := req.Plan.Get(ctx, &plan)
diags.AddError("operation not supported", "updating an agent group assignment is not supported")
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return

Check warning on line 134 in pkg/config-api-provider/provider/resources/agent_group_assignment.go

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/agent_group_assignment.go#L129-L134

Added lines #L129 - L134 were not covered by tests
}
}
1riatsila1 marked this conversation as resolved.
Show resolved Hide resolved

func (r *agentGroupAssignmentResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
// Retrieve values from state
var state agentGroupAssignmentResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return

Check warning on line 144 in pkg/config-api-provider/provider/resources/agent_group_assignment.go

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/agent_group_assignment.go#L144

Added line #L144 was not covered by tests
}

// Delete existing agentGroupAssignment using the plan_id
}

func (r *agentGroupAssignmentResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

var GetAgentGroupAssignment = func(uid string) AgentGroupAssignmentResponseModel {

Check warning on line 154 in pkg/config-api-provider/provider/resources/agent_group_assignment.go

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/agent_group_assignment.go#L154

Added line #L154 was not covered by tests
// TODO: Query the agentGroupAssignment using the client

return AgentGroupAssignmentResponseModel{
UID: uid,
GroupUID: "mock_group_uid",
AgentUID: "mock_agent_uid",

Check warning on line 160 in pkg/config-api-provider/provider/resources/agent_group_assignment.go

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/agent_group_assignment.go#L157-L160

Added lines #L157 - L160 were not covered by tests
}
}

var CreateAgentGroupAssignment = func(request AgentGroupAssignmentRequestModel) AgentGroupAssignmentResponseModel {

Check warning on line 164 in pkg/config-api-provider/provider/resources/agent_group_assignment.go

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/agent_group_assignment.go#L164

Added line #L164 was not covered by tests
// TODO: Query the agentGroupAssignment using the client

return AgentGroupAssignmentResponseModel{
UID: "mock_uid",
GroupUID: "mock_group_uid",
AgentUID: "mock_agent_uid",

Check warning on line 170 in pkg/config-api-provider/provider/resources/agent_group_assignment.go

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/agent_group_assignment.go#L167-L170

Added lines #L167 - L170 were not covered by tests
}
}
158 changes: 158 additions & 0 deletions pkg/config-api-provider/test/agent_group_assignment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
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 TestAgentGroupAssignmentResource(t *testing.T) {

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Creating a agent group assignment
{
PreConfig: func() {
// required for agent import
resources.GetAgent = func(uid string) resources.AgentResponseModel {
return GenerateAgentResponseModel(uid, "")
}

// required for group create
resources.CreateGroup = func(request resources.GroupCreateRequestModel) resources.GroupResponseModel {
return GenerateGroupResponseModel("group_uid", "", "")
}
resources.GetGroup = func(uid string) resources.GroupResponseModel {
return GenerateGroupResponseModel("group_uid", "", "")
}

// required for agent group assignment create
agentGroupAssignmentResponse := GenerateAgentGroupAssignmentResponse("agent_group_assignment_uid", "")
resources.CreateAgentGroupAssignment = func(request resources.AgentGroupAssignmentRequestModel) resources.AgentGroupAssignmentResponseModel {
return agentGroupAssignmentResponse
}
resources.GetAgentGroupAssignment = func(uid string) resources.AgentGroupAssignmentResponseModel {
return agentGroupAssignmentResponse
}
},

Config: providerConfig + `
resource "uxi_group" "my_group" {
name = "name"
parent_uid = "parent_uid"
}

resource "uxi_agent" "my_agent" {
name = "name"
notes = "notes"
pcap_mode = "light"
}

import {
to = uxi_agent.my_agent
id = "agent_uid"
}

resource "uxi_agent_group_assignment" "my_agent_group_assignment" {
agent_id = uxi_agent.my_agent.id
group_id = uxi_group.my_group.id
}`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("uxi_agent_group_assignment.my_agent_group_assignment", "agent_id", "agent_uid"),
resource.TestCheckResourceAttr("uxi_agent_group_assignment.my_agent_group_assignment", "group_id", "group_uid"),
resource.TestCheckResourceAttr("uxi_agent_group_assignment.my_agent_group_assignment", "id", "agent_group_assignment_uid"),
),
},
// ImportState testing
{
ResourceName: "uxi_agent_group_assignment.my_agent_group_assignment",
ImportState: true,
ImportStateVerify: true,
},
// Update and Read testing
{
PreConfig: func() {
resources.GetAgent = func(uid string) resources.AgentResponseModel {
if uid == "agent_uid" {
return GenerateAgentResponseModel(uid, "")
} else {
return GenerateAgentResponseModel(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(uid, "_2", "_2")
}
}

// required for agent group assignment create
resources.GetAgentGroupAssignment = func(uid string) resources.AgentGroupAssignmentResponseModel {
if uid == "agent_group_assignment_uid" {
return GenerateAgentGroupAssignmentResponse(uid, "")
} else {
return GenerateAgentGroupAssignmentResponse(uid, "_2")
}
}
resources.CreateAgentGroupAssignment = func(request resources.AgentGroupAssignmentRequestModel) resources.AgentGroupAssignmentResponseModel {
return GenerateAgentGroupAssignmentResponse("agent_group_assignment_uid_2", "_2")
}
},
Config: providerConfig + `
// the original resources
resource "uxi_group" "my_group" {
name = "name"
parent_uid = "parent_uid"
}

resource "uxi_agent" "my_agent" {
name = "name"
notes = "notes"
pcap_mode = "light"
}

import {
to = uxi_agent.my_agent
id = "agent_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_agent" "my_agent_2" {
name = "name_2"
notes = "notes_2"
pcap_mode = "light_2"
}

import {
to = uxi_agent.my_agent_2
id = "agent_uid_2"
}

// the assignment update, updated from agent/group to agent_2/group_2
resource "uxi_agent_group_assignment" "my_agent_group_assignment" {
agent_id = uxi_agent.my_agent_2.id
group_id = uxi_group.my_group_2.id
}`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("uxi_agent_group_assignment.my_agent_group_assignment", "agent_id", "agent_uid_2"),
resource.TestCheckResourceAttr("uxi_agent_group_assignment.my_agent_group_assignment", "group_id", "group_uid_2"),
resource.TestCheckResourceAttr("uxi_agent_group_assignment.my_agent_group_assignment", "id", "agent_group_assignment_uid_2"),
),
},
// Delete testing automatically occurs in TestCase
},
})
}
Loading
Loading