Skip to content

Commit

Permalink
feat: network group assignment (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
1riatsila1 authored Aug 30, 2024
1 parent 4fcff03 commit d110f69
Show file tree
Hide file tree
Showing 9 changed files with 527 additions and 9 deletions.
6 changes: 6 additions & 0 deletions pkg/config-api-provider/examples/full-demo/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ resource "uxi_agent_group_assignment" "my_uxi_agent_group_assignment" {
group_id = uxi_group.my_group.id
}

// Network Group Assignment
resource "uxi_network_group_assignment" "my_uxi_network_group_assignment" {
network_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
Expand Up @@ -65,5 +65,6 @@ func (p *uxiConfigurationProvider) Resources(_ context.Context) []func() resourc
resources.NewServiceTestResource,
resources.NewSensorGroupAssignmentResource,
resources.NewAgentGroupAssignmentResource,
resources.NewNetworkGroupAssignmentResource,
}
}
172 changes: 172 additions & 0 deletions pkg/config-api-provider/provider/resources/network_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 = &networkGroupAssignmentResource{}
_ resource.ResourceWithConfigure = &networkGroupAssignmentResource{}
)

type networkGroupAssignmentResourceModel struct {
ID types.String `tfsdk:"id"`
NetworkID types.String `tfsdk:"network_id"`
GroupID types.String `tfsdk:"group_id"`
}

type NetworkGroupAssignmentResponseModel struct {
UID string // <assignment_uid>
GroupUID string // <group_uid:str>,
NetworkUID string // <network_uid:str>
}

type NetworkGroupAssignmentRequestModel struct {
GroupUID string // <group_uid:str>,
NetworkUID string // <network_uid:str>
}

func NewNetworkGroupAssignmentResource() resource.Resource {
return &networkGroupAssignmentResource{}
}

type networkGroupAssignmentResource struct{}

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

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

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

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

// TODO: Call client createNetworkGroupAssignment method
networkGroupAssignment := CreateNetworkGroupAssignment(NetworkGroupAssignmentRequestModel{
GroupUID: plan.GroupID.ValueString(),
NetworkUID: plan.NetworkID.ValueString(),
})

// Update the state to match the plan
plan.ID = types.StringValue(networkGroupAssignment.UID)
plan.GroupID = types.StringValue(networkGroupAssignment.GroupUID)
plan.NetworkID = types.StringValue(networkGroupAssignment.NetworkUID)

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

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

// TODO: Call client getNetworkGroupAssignment method
networkGroupAssignment := GetNetworkGroupAssignment(state.ID.ValueString())

// Update state from client response
state.GroupID = types.StringValue(networkGroupAssignment.GroupUID)
state.NetworkID = types.StringValue(networkGroupAssignment.NetworkUID)

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

func (r *networkGroupAssignmentResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// Retrieve values from plan
var plan networkGroupAssignmentResourceModel
diags := req.Plan.Get(ctx, &plan)
diags.AddError("operation not supported", "updating a network group assignment is not supported")
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}

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

// Delete existing networkGroupAssignment using the plan_id
}

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

var GetNetworkGroupAssignment = func(uid string) NetworkGroupAssignmentResponseModel {
// TODO: Query the networkGroupAssignment using the client

return NetworkGroupAssignmentResponseModel{
UID: uid,
GroupUID: "mock_group_uid",
NetworkUID: "mock_network_uid",
}
}

var CreateNetworkGroupAssignment = func(request NetworkGroupAssignmentRequestModel) NetworkGroupAssignmentResponseModel {
// TODO: Query the networkGroupAssignment using the client

return NetworkGroupAssignmentResponseModel{
UID: "mock_uid",
GroupUID: "mock_group_uid",
NetworkUID: "mock_network_uid",
}
}
4 changes: 2 additions & 2 deletions pkg/config-api-provider/provider/resources/wired_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (r *wiredNetworkResource) Read(ctx context.Context, req resource.ReadReques
return
}

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

// Update state from client response
state.Alias = types.StringValue(response.Alias)
Expand Down Expand Up @@ -118,7 +118,7 @@ func (r *wiredNetworkResource) ImportState(ctx context.Context, req resource.Imp
}

// Get the wiredNetwork using the configuration-api client
var GetWiredNetwork = func() WiredNetworkResponseModel {
var GetWiredNetwork = func(uid string) WiredNetworkResponseModel {
// TODO: Query the wiredNetwork using the client

return WiredNetworkResponseModel{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (r *wirelessNetworkResource) Read(ctx context.Context, req resource.ReadReq
return
}

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

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

// Get the wirelessNetwork using the configuration-api client
var GetWirelessNetwork = func() WirelessNetworkResponseModel {
var GetWirelessNetwork = func(uid string) WirelessNetworkResponseModel {
// TODO: Query the wirelessNetwork using the client

return WirelessNetworkResponseModel{
Uid: "mock_uid",
Uid: uid,
Ssid: "mock_ssid",
DatetimeCreated: "mock_datetime_created",
DatetimeUpdated: "mock_datetime_updated",
Expand Down
Loading

0 comments on commit d110f69

Please sign in to comment.