From e0f676a914b20b42d40aca2e11cf9b430ee88182 Mon Sep 17 00:00:00 2001 From: 1riatsila1 Date: Fri, 30 Aug 2024 16:13:11 +0200 Subject: [PATCH] feat: root group data source --- .../examples/full-demo/main.tf | 11 ++- .../provider/data-sources/root_group.go | 86 +++++++++++++++++++ pkg/config-api-provider/provider/provider.go | 5 +- .../provider/resources/group.go | 16 ++-- .../test/agent_group_assignment_test.go | 12 +-- pkg/config-api-provider/test/group_test.go | 18 ++-- .../test/network_group_assignment_test.go | 24 +++--- .../test/root_group_test.go | 30 +++++++ .../test/sensor_group_assignment_test.go | 12 +-- .../service_test_group_assignment_test.go | 12 +-- pkg/config-api-provider/test/utils.go | 14 ++- 11 files changed, 189 insertions(+), 51 deletions(-) create mode 100644 pkg/config-api-provider/provider/data-sources/root_group.go create mode 100644 pkg/config-api-provider/test/root_group_test.go diff --git a/pkg/config-api-provider/examples/full-demo/main.tf b/pkg/config-api-provider/examples/full-demo/main.tf index 61ef5a59..5ae882f1 100644 --- a/pkg/config-api-provider/examples/full-demo/main.tf +++ b/pkg/config-api-provider/examples/full-demo/main.tf @@ -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 diff --git a/pkg/config-api-provider/provider/data-sources/root_group.go b/pkg/config-api-provider/provider/data-sources/root_group.go new file mode 100644 index 00000000..4eb1dc15 --- /dev/null +++ b/pkg/config-api-provider/provider/data-sources/root_group.go @@ -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 + } +} + +// 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", + } +} diff --git a/pkg/config-api-provider/provider/provider.go b/pkg/config-api-provider/provider/provider.go index 9c8b673f..5576b065 100644 --- a/pkg/config-api-provider/provider/provider.go +++ b/pkg/config-api-provider/provider/provider.go @@ -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" @@ -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. diff --git a/pkg/config-api-provider/provider/resources/group.go b/pkg/config-api-provider/provider/resources/group.go index 088c6de9..dea6166b 100644 --- a/pkg/config-api-provider/provider/resources/group.go +++ b/pkg/config-api-provider/provider/resources/group.go @@ -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 { @@ -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 @@ -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) @@ -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) @@ -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) // Set state to fully populated data diags = resp.State.Set(ctx, plan) diff --git a/pkg/config-api-provider/test/agent_group_assignment_test.go b/pkg/config-api-provider/test/agent_group_assignment_test.go index c7c0c7c9..8fc3f173 100644 --- a/pkg/config-api-provider/test/agent_group_assignment_test.go +++ b/pkg/config-api-provider/test/agent_group_assignment_test.go @@ -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" { @@ -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" { @@ -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" { diff --git a/pkg/config-api-provider/test/group_test.go b/pkg/config-api-provider/test/group_test.go index a826ba77..b8e69e9a 100644 --- a/pkg/config-api-provider/test/group_test.go +++ b/pkg/config-api-provider/test/group_test.go @@ -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"), ), }, @@ -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, @@ -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"), ), }, diff --git a/pkg/config-api-provider/test/network_group_assignment_test.go b/pkg/config-api-provider/test/network_group_assignment_test.go index 455f31b1..0f37cbdf 100644 --- a/pkg/config-api-provider/test/network_group_assignment_test.go +++ b/pkg/config-api-provider/test/network_group_assignment_test.go @@ -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" { @@ -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" { @@ -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" { @@ -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" { @@ -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" { @@ -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" { diff --git a/pkg/config-api-provider/test/root_group_test.go b/pkg/config-api-provider/test/root_group_test.go new file mode 100644 index 00000000..29f4fcbc --- /dev/null +++ b/pkg/config-api-provider/test/root_group_test.go @@ -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"), + ), + }, + }, + }) +} diff --git a/pkg/config-api-provider/test/sensor_group_assignment_test.go b/pkg/config-api-provider/test/sensor_group_assignment_test.go index a4a22e6f..06f90b5d 100644 --- a/pkg/config-api-provider/test/sensor_group_assignment_test.go +++ b/pkg/config-api-provider/test/sensor_group_assignment_test.go @@ -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" { @@ -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" { @@ -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" { diff --git a/pkg/config-api-provider/test/service_test_group_assignment_test.go b/pkg/config-api-provider/test/service_test_group_assignment_test.go index 31dce151..57c476ef 100644 --- a/pkg/config-api-provider/test/service_test_group_assignment_test.go +++ b/pkg/config-api-provider/test/service_test_group_assignment_test.go @@ -41,8 +41,8 @@ func TestServiceTestGroupAssignmentResource(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_service_test" "my_service_test" { @@ -108,8 +108,8 @@ func TestServiceTestGroupAssignmentResource(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_service_test" "my_service_test" { @@ -123,8 +123,8 @@ func TestServiceTestGroupAssignmentResource(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_service_test" "my_service_test_2" { diff --git a/pkg/config-api-provider/test/utils.go b/pkg/config-api-provider/test/utils.go index f689b36d..0da64f58 100644 --- a/pkg/config-api-provider/test/utils.go +++ b/pkg/config-api-provider/test/utils.go @@ -1,6 +1,9 @@ package test -import "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/resources" +import ( + "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" +) func GenerateSensorResponseModel(uid string, postfix string) resources.SensorResponseModel { return resources.SensorResponseModel{ @@ -41,6 +44,15 @@ func GenerateGroupResponseModel(uid string, non_replacement_field_postfix string } } +func GenerateRootGroupResponseModel(uid string) datasources.RootGroupResponseModel { + return datasources.RootGroupResponseModel{ + UID: uid, + Name: "root", + ParentUid: nil, + Path: uid, + } +} + func GenerateServiceTestResponseModel(uid string, postfix string) resources.ServiceTestResponseModel { return resources.ServiceTestResponseModel{ Uid: uid,