Skip to content

Commit

Permalink
feat: root group data source
Browse files Browse the repository at this point in the history
  • Loading branch information
1riatsila1 committed Aug 30, 2024
1 parent 61c74f1 commit e0f676a
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 51 deletions.
11 changes: 9 additions & 2 deletions pkg/config-api-provider/examples/full-demo/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@ terraform {

provider "uxi" {}

data "uxi_root_group" "my_root_group" {}

resource "uxi_group" "my_group" {
name = "name"
parent_uid = "parent_uid"
name = "parent"
parent_group_id = data.uxi_root_group.my_root_group.id
}

resource "uxi_group" "my_group_2" {
name = "child"
parent_group_id = uxi_group.my_group.id
}

// Sensor Resource
Expand Down
86 changes: 86 additions & 0 deletions pkg/config-api-provider/provider/data-sources/root_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package datasources

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)

// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &rootGroupDataSource{}
_ datasource.DataSourceWithConfigure = &rootGroupDataSource{}
)

// NewRootGroupDataSource is a helper function to simplify the provider implementation.
func NewRootGroupDataSource() datasource.DataSource {
return &rootGroupDataSource{}
}

// rootGroupDataSource is the data source implementation.
type rootGroupDataSource struct{}

// rootGroupDataSourceModel maps the data source schema data.
type rootGroupDataSourceModel struct {
ID types.String `tfsdk:"id"`
}

// Metadata returns the data source type name.
func (d *rootGroupDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_root_group"
}

// Schema defines the schema for the data source.
func (d *rootGroupDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
},
},
}
}

// Read refreshes the Terraform state with the latest data.
func (d *rootGroupDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state rootGroupDataSourceModel

rootGroup := GetRootGroup()

state.ID = types.StringValue(rootGroup.UID)

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

Check warning on line 58 in pkg/config-api-provider/provider/data-sources/root_group.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/data-sources/root_group.go#L58

Added line #L58 was not covered by tests
}
}

// Configure adds the provider configured client to the data source.
func (d *rootGroupDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Add a nil check when handling ProviderData because Terraform
// sets that data after it calls the ConfigureProvider RPC.
if req.ProviderData == nil {
return
}
}

// TODO: Switch this to use the Client Model when that becomes available
type RootGroupResponseModel struct {
UID string
Name string
ParentUid *string
Path string
}

var GetRootGroup = func() RootGroupResponseModel {
return RootGroupResponseModel{
UID: "mock_uid",
Name: "root",
ParentUid: nil,
Path: "mock_uid",

Check warning on line 84 in pkg/config-api-provider/provider/data-sources/root_group.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/data-sources/root_group.go#L79-L84

Added lines #L79 - L84 were not covered by tests
}
}
5 changes: 4 additions & 1 deletion pkg/config-api-provider/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"context"

datasources "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/data-sources"
"github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/resources"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/provider"
Expand Down Expand Up @@ -51,7 +52,9 @@ func (p *uxiConfigurationProvider) Configure(ctx context.Context, req provider.C

// DataSources defines the data sources implemented in the provider.
func (p *uxiConfigurationProvider) DataSources(_ context.Context) []func() datasource.DataSource {
return nil
return []func() datasource.DataSource{
datasources.NewRootGroupDataSource,
}
}

// Resources defines the resources implemented in the provider.
Expand Down
16 changes: 8 additions & 8 deletions pkg/config-api-provider/provider/resources/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ var (
)

type groupResourceModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
ParentUid types.String `tfsdk:"parent_uid"`
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
ParentGroupId types.String `tfsdk:"parent_group_id"`
}

type GroupResponseModel struct {
Expand Down Expand Up @@ -61,7 +61,7 @@ func (r *groupResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
"name": schema.StringAttribute{
Required: true,
},
"parent_uid": schema.StringAttribute{
"parent_group_id": schema.StringAttribute{
Required: true,
PlanModifiers: []planmodifier.String{
// UXI business logic does not permit moving of groups
Expand All @@ -88,13 +88,13 @@ func (r *groupResource) Create(ctx context.Context, req resource.CreateRequest,
// We are mocking the response of the client for this early stage of development
group := CreateGroup(GroupCreateRequestModel{
Name: plan.Name.ValueString(),
ParentUid: plan.ParentUid.ValueString(),
ParentUid: plan.ParentGroupId.ValueString(),
})

// Update the state to match the plan (replace with response from client)
plan.ID = types.StringValue(group.UID)
plan.Name = types.StringValue(group.Name)
plan.ParentUid = types.StringValue(group.ParentUid)
plan.ParentGroupId = types.StringValue(group.ParentUid)

// Set state to fully populated data
diags = resp.State.Set(ctx, plan)
Expand All @@ -119,7 +119,7 @@ func (r *groupResource) Read(ctx context.Context, req resource.ReadRequest, resp

// Update state from client response
state.Name = types.StringValue(response.Name)
state.ParentUid = types.StringValue(response.ParentUid)
state.ParentGroupId = types.StringValue(response.ParentUid)

// Set refreshed state
diags = resp.State.Set(ctx, &state)
Expand All @@ -145,7 +145,7 @@ func (r *groupResource) Update(ctx context.Context, req resource.UpdateRequest,

// Update the state to match the plan (replace with response from client)
plan.Name = types.StringValue(group.Name)
plan.ParentUid = types.StringValue(group.ParentUid)
plan.ParentGroupId = types.StringValue(group.ParentUid)

Check warning on line 148 in pkg/config-api-provider/provider/resources/group.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/group.go#L148

Added line #L148 was not covered by tests

// Set state to fully populated data
diags = resp.State.Set(ctx, plan)
Expand Down
12 changes: 6 additions & 6 deletions pkg/config-api-provider/test/agent_group_assignment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func TestAgentGroupAssignmentResource(t *testing.T) {

Config: providerConfig + `
resource "uxi_group" "my_group" {
name = "name"
parent_uid = "parent_uid"
name = "name"
parent_group_id = "parent_uid"
}
resource "uxi_agent" "my_agent" {
Expand Down Expand Up @@ -109,8 +109,8 @@ func TestAgentGroupAssignmentResource(t *testing.T) {
Config: providerConfig + `
// the original resources
resource "uxi_group" "my_group" {
name = "name"
parent_uid = "parent_uid"
name = "name"
parent_group_id = "parent_uid"
}
resource "uxi_agent" "my_agent" {
Expand All @@ -126,8 +126,8 @@ func TestAgentGroupAssignmentResource(t *testing.T) {
// the new resources we wanna update the assignment to
resource "uxi_group" "my_group_2" {
name = "name_2"
parent_uid = "parent_uid_2"
name = "name_2"
parent_group_id = "parent_uid_2"
}
resource "uxi_agent" "my_agent_2" {
Expand Down
18 changes: 9 additions & 9 deletions pkg/config-api-provider/test/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ func TestGroupResource(t *testing.T) {
},
Config: providerConfig + `
resource "uxi_group" "my_group" {
name = "name"
parent_uid = "parent_uid"
name = "name"
parent_group_id = "parent_uid"
}`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("uxi_group.my_group", "name", "name"),
resource.TestCheckResourceAttr("uxi_group.my_group", "parent_uid", "parent_uid"),
resource.TestCheckResourceAttr("uxi_group.my_group", "parent_group_id", "parent_uid"),
resource.TestCheckResourceAttr("uxi_group.my_group", "id", "uid"),
),
},
Expand All @@ -55,12 +55,12 @@ func TestGroupResource(t *testing.T) {
},
Config: providerConfig + `
resource "uxi_group" "my_group" {
name = "name_2"
parent_uid = "parent_uid"
name = "name_2"
parent_group_id = "parent_uid"
}`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("uxi_group.my_group", "name", "name_2"),
resource.TestCheckResourceAttr("uxi_group.my_group", "parent_uid", "parent_uid"),
resource.TestCheckResourceAttr("uxi_group.my_group", "parent_group_id", "parent_uid"),
resource.TestCheckResourceAttr("uxi_group.my_group", "id", "uid"),
),
Destroy: false,
Expand All @@ -81,12 +81,12 @@ func TestGroupResource(t *testing.T) {
},
Config: providerConfig + `
resource "uxi_group" "my_group" {
name = "name"
parent_uid = "parent_uid_2"
name = "name"
parent_group_id = "parent_uid_2"
}`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("uxi_group.my_group", "name", "name"),
resource.TestCheckResourceAttr("uxi_group.my_group", "parent_uid", "parent_uid_2"),
resource.TestCheckResourceAttr("uxi_group.my_group", "parent_group_id", "parent_uid_2"),
resource.TestCheckResourceAttr("uxi_group.my_group", "id", "new_uid"),
),
},
Expand Down
24 changes: 12 additions & 12 deletions pkg/config-api-provider/test/network_group_assignment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func TestNetworkGroupAssignmentResource(t *testing.T) {

Config: providerConfig + `
resource "uxi_group" "my_group" {
name = "name"
parent_uid = "parent_uid"
name = "name"
parent_group_id = "parent_uid"
}
resource "uxi_wired_network" "my_network" {
Expand Down Expand Up @@ -107,8 +107,8 @@ func TestNetworkGroupAssignmentResource(t *testing.T) {
Config: providerConfig + `
// the original resources
resource "uxi_group" "my_group" {
name = "name"
parent_uid = "parent_uid"
name = "name"
parent_group_id = "parent_uid"
}
resource "uxi_wired_network" "my_network" {
Expand All @@ -122,8 +122,8 @@ func TestNetworkGroupAssignmentResource(t *testing.T) {
// the new resources we wanna update the assignment to
resource "uxi_group" "my_group_2" {
name = "name_2"
parent_uid = "parent_uid_2"
name = "name_2"
parent_group_id = "parent_uid_2"
}
resource "uxi_wired_network" "my_network_2" {
Expand Down Expand Up @@ -200,8 +200,8 @@ func TestNetworkGroupAssignmentResource(t *testing.T) {

Config: providerConfig + `
resource "uxi_group" "my_group" {
name = "name"
parent_uid = "parent_uid"
name = "name"
parent_group_id = "parent_uid"
}
resource "uxi_wireless_network" "my_network" {
Expand Down Expand Up @@ -267,8 +267,8 @@ func TestNetworkGroupAssignmentResource(t *testing.T) {
Config: providerConfig + `
// the original resources
resource "uxi_group" "my_group" {
name = "name"
parent_uid = "parent_uid"
name = "name"
parent_group_id = "parent_uid"
}
resource "uxi_wireless_network" "my_network" {
Expand All @@ -282,8 +282,8 @@ func TestNetworkGroupAssignmentResource(t *testing.T) {
// the new resources we wanna update the assignment to
resource "uxi_group" "my_group_2" {
name = "name_2"
parent_uid = "parent_uid_2"
name = "name_2"
parent_group_id = "parent_uid_2"
}
resource "uxi_wireless_network" "my_network_2" {
Expand Down
30 changes: 30 additions & 0 deletions pkg/config-api-provider/test/root_group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package test

import (
"testing"

datasources "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/data-sources"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestRootGroupDataSource(t *testing.T) {
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Read testing
{
PreConfig: func() {
datasources.GetRootGroup = func() datasources.RootGroupResponseModel {
return GenerateRootGroupResponseModel("mock_uid")
}
},
Config: providerConfig + `
data "uxi_root_group" "my_root_group" {}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.uxi_root_group.my_root_group", "id", "mock_uid"),
),
},
},
})
}
12 changes: 6 additions & 6 deletions pkg/config-api-provider/test/sensor_group_assignment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func TestSensorGroupAssignmentResource(t *testing.T) {

Config: providerConfig + `
resource "uxi_group" "my_group" {
name = "name"
parent_uid = "parent_uid"
name = "name"
parent_group_id = "parent_uid"
}
resource "uxi_sensor" "my_sensor" {
Expand Down Expand Up @@ -113,8 +113,8 @@ func TestSensorGroupAssignmentResource(t *testing.T) {
Config: providerConfig + `
// the original resources
resource "uxi_group" "my_group" {
name = "name"
parent_uid = "parent_uid"
name = "name"
parent_group_id = "parent_uid"
}
resource "uxi_sensor" "my_sensor" {
Expand All @@ -131,8 +131,8 @@ func TestSensorGroupAssignmentResource(t *testing.T) {
// the new resources we wanna update the assignment to
resource "uxi_group" "my_group_2" {
name = "name_2"
parent_uid = "parent_uid_2"
name = "name_2"
parent_group_id = "parent_uid_2"
}
resource "uxi_sensor" "my_sensor_2" {
Expand Down
Loading

0 comments on commit e0f676a

Please sign in to comment.