Skip to content

Commit

Permalink
refactor(warehouse): move to generic impl
Browse files Browse the repository at this point in the history
  • Loading branch information
DariuszPorowski committed Dec 20, 2024
1 parent f607dbd commit 39599dd
Show file tree
Hide file tree
Showing 16 changed files with 253 additions and 585 deletions.
6 changes: 3 additions & 3 deletions docs/data-sources/warehouse.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ Optional:

Read-Only:

- `connection_string` (String) Connection String
- `created_date` (String) Created Date
- `last_updated_time` (String) Last Updated Time
- `connection_string` (String) The SQL connection string connected to the workspace containing this warehouse.
- `created_date` (String) The date and time the warehouse was created.
- `last_updated_time` (String) The date and time the warehouse was last updated.
11 changes: 11 additions & 0 deletions docs/data-sources/warehouses.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,15 @@ Read-Only:
- `description` (String) The Warehouse description.
- `display_name` (String) The Warehouse display name.
- `id` (String) The Warehouse ID.
- `properties` (Attributes) The Warehouse properties. (see [below for nested schema](#nestedatt--values--properties))
- `workspace_id` (String) The Workspace ID.

<a id="nestedatt--values--properties"></a>

### Nested Schema for `values.properties`

Read-Only:

- `connection_string` (String) The SQL connection string connected to the workspace containing this warehouse.
- `created_date` (String) The date and time the warehouse was created.
- `last_updated_time` (String) The date and time the warehouse was last updated.
14 changes: 7 additions & 7 deletions docs/resources/warehouse.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
page_title: "fabric_warehouse Resource - terraform-provider-fabric"
subcategory: ""
description: |-
This resource manages a Fabric Warehouse.
See Warehouse https://learn.microsoft.com/fabric/data-warehouse/data-warehousing for more information.
Manage a Fabric Warehouse.
Use this resource to manage a Warehouse https://learn.microsoft.com/fabric/data-warehouse/data-warehousing.
-> This item does not support Service Principal. Please use a User context authentication.
---

# fabric_warehouse (Resource)

This resource manages a Fabric Warehouse.
Manage a Fabric Warehouse.

See [Warehouse](https://learn.microsoft.com/fabric/data-warehouse/data-warehousing) for more information.
Use this resource to manage a [Warehouse](https://learn.microsoft.com/fabric/data-warehouse/data-warehousing).

-> This item does not support Service Principal. Please use a User context authentication.

Expand Down Expand Up @@ -60,9 +60,9 @@ Optional:

Read-Only:

- `connection_string` (String) Connection String
- `created_date` (String) Created Date
- `last_updated_time` (String) Last Updated Time
- `connection_string` (String) The SQL connection string connected to the workspace containing this warehouse.
- `created_date` (String) The date and time the warehouse was created.
- `last_updated_time` (String) The date and time the warehouse was last updated.

## Import

Expand Down
6 changes: 3 additions & 3 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ func (p *FabricProvider) Resources(ctx context.Context) []func() resource.Resour
spark.NewResourceSparkEnvironmentSettings,
spark.NewResourceSparkWorkspaceSettings,
func() resource.Resource { return sparkjobdefinition.NewResourceSparkJobDefinition(ctx) },
warehouse.NewResourceWarehouse,
func() resource.Resource { return warehouse.NewResourceWarehouse(ctx) },
workspace.NewResourceWorkspace,
workspace.NewResourceWorkspaceRoleAssignment,
workspace.NewResourceWorkspaceGit,
Expand Down Expand Up @@ -422,8 +422,8 @@ func (p *FabricProvider) DataSources(ctx context.Context) []func() datasource.Da
func() datasource.DataSource { return sparkjobdefinition.NewDataSourceSparkJobDefinition(ctx) },
func() datasource.DataSource { return sparkjobdefinition.NewDataSourceSparkJobDefinitions(ctx) },
sqlendpoint.NewDataSourceSQLEndpoints,
warehouse.NewDataSourceWarehouse,
warehouse.NewDataSourceWarehouses,
func() datasource.DataSource { return warehouse.NewDataSourceWarehouse(ctx) },
func() datasource.DataSource { return warehouse.NewDataSourceWarehouses(ctx) },
workspace.NewDataSourceWorkspace,
workspace.NewDataSourceWorkspaces,
workspace.NewDataSourceWorkspaceRoleAssignments,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ func NewDataSourceSparkJobDefinition(ctx context.Context) datasource.DataSource
propertiesModel := &sparkJobDefinitionPropertiesModel{}
propertiesModel.set(from)

diags := properties.Set(ctx, propertiesModel)
if diags.HasError() {
if diags := properties.Set(ctx, propertiesModel); diags.HasError() {
return diags
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ func NewDataSourceSparkJobDefinitions(ctx context.Context) datasource.DataSource
propertiesModel := &sparkJobDefinitionPropertiesModel{}
propertiesModel.set(from)

diags := properties.Set(ctx, propertiesModel)
if diags.HasError() {
if diags := properties.Set(ctx, propertiesModel); diags.HasError() {
return diags
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ func TestUnit_SparkJobDefinitionResource_CRUD(t *testing.T) {
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrPtr(testResourceItemFQN, "display_name", entityAfter.DisplayName),
resource.TestCheckResourceAttr(testResourceItemFQN, "definition_update_enabled", "true"),
// resource.TestCheckResourceAttrSet(testResourceItemFQN, "properties.onelake_root_path"),
),
},
// Delete testing automatically occurs in TestCase
Expand Down Expand Up @@ -273,6 +274,7 @@ func TestAcc_SparkJobDefinitionResource_CRUD(t *testing.T) {
resource.TestCheckResourceAttr(testResourceItemFQN, "display_name", entityCreateDisplayName),
resource.TestCheckResourceAttr(testResourceItemFQN, "description", ""),
resource.TestCheckResourceAttr(testResourceItemFQN, "definition_update_enabled", "true"),
resource.TestCheckResourceAttrSet(testResourceItemFQN, "properties.onelake_root_path"),
),
},
// Update and Read
Expand All @@ -291,6 +293,7 @@ func TestAcc_SparkJobDefinitionResource_CRUD(t *testing.T) {
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(testResourceItemFQN, "display_name", entityUpdateDisplayName),
resource.TestCheckResourceAttr(testResourceItemFQN, "definition_update_enabled", "true"),
resource.TestCheckResourceAttrSet(testResourceItemFQN, "properties.onelake_root_path"),
),
},
},
Expand Down
229 changes: 66 additions & 163 deletions internal/services/warehouse/data_warehouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,206 +5,109 @@ package warehouse

import (
"context"
"fmt"

supertypes "github.com/FrangipaneTeam/terraform-plugin-framework-supertypes"
"github.com/hashicorp/terraform-plugin-framework-timeouts/datasource/timeouts"
"github.com/hashicorp/terraform-plugin-framework-timetypes/timetypes"
"github.com/hashicorp/terraform-plugin-framework-validators/datasourcevalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/microsoft/fabric-sdk-go/fabric"
fabcore "github.com/microsoft/fabric-sdk-go/fabric/core"
fabwarehouse "github.com/microsoft/fabric-sdk-go/fabric/warehouse"

"github.com/microsoft/terraform-provider-fabric/internal/common"
"github.com/microsoft/terraform-provider-fabric/internal/framework/customtypes"
"github.com/microsoft/terraform-provider-fabric/internal/pkg/utils"
pconfig "github.com/microsoft/terraform-provider-fabric/internal/provider/config"
"github.com/microsoft/terraform-provider-fabric/internal/pkg/fabricitem"
)

// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSourceWithConfigValidators = (*dataSourceWarehouse)(nil)
_ datasource.DataSourceWithConfigure = (*dataSourceWarehouse)(nil)
)

type dataSourceWarehouse struct {
pConfigData *pconfig.ProviderData
client *fabwarehouse.ItemsClient
}

func NewDataSourceWarehouse() datasource.DataSource {
return &dataSourceWarehouse{}
}

func (d *dataSourceWarehouse) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_" + ItemTFName
}

func (d *dataSourceWarehouse) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Get a Fabric Warehouse.\n\n" +
"Use this data source to fetch a [Warehouse](https://learn.microsoft.com/fabric/data-warehouse/data-warehousing).\n\n" +
ItemDocsSPNSupport,
func NewDataSourceWarehouse(ctx context.Context) datasource.DataSource {
propertiesSchema := schema.SingleNestedAttribute{
MarkdownDescription: "The " + ItemName + " properties.",
Computed: true,
CustomType: supertypes.NewSingleNestedObjectTypeOf[warehousePropertiesModel](ctx),
Attributes: map[string]schema.Attribute{
"workspace_id": schema.StringAttribute{
MarkdownDescription: "The Workspace ID.",
Required: true,
CustomType: customtypes.UUIDType{},
},
"id": schema.StringAttribute{
MarkdownDescription: "The Warehouse ID.",
Optional: true,
Computed: true,
CustomType: customtypes.UUIDType{},
},
"display_name": schema.StringAttribute{
MarkdownDescription: "The Warehouse display name.",
Optional: true,
"connection_string": schema.StringAttribute{
MarkdownDescription: "The SQL connection string connected to the workspace containing this warehouse.",
Computed: true,
},
"description": schema.StringAttribute{
MarkdownDescription: "The Warehouse description.",
"created_date": schema.StringAttribute{
MarkdownDescription: "The date and time the warehouse was created.",
Computed: true,
CustomType: timetypes.RFC3339Type{},
},
"properties": schema.SingleNestedAttribute{
"last_updated_time": schema.StringAttribute{
MarkdownDescription: "The date and time the warehouse was last updated.",
Computed: true,
MarkdownDescription: "The Warehouse properties.",
CustomType: supertypes.NewSingleNestedObjectTypeOf[warehousePropertiesModel](ctx),
Attributes: map[string]schema.Attribute{
"connection_string": schema.StringAttribute{
MarkdownDescription: "Connection String",
Computed: true,
},
"created_date": schema.StringAttribute{
MarkdownDescription: "Created Date",
Computed: true,
CustomType: timetypes.RFC3339Type{},
},
"last_updated_time": schema.StringAttribute{
MarkdownDescription: "Last Updated Time",
Computed: true,
CustomType: timetypes.RFC3339Type{},
},
},
CustomType: timetypes.RFC3339Type{},
},
"timeouts": timeouts.Attributes(ctx),
},
}
}

func (d *dataSourceWarehouse) ConfigValidators(_ context.Context) []datasource.ConfigValidator {
return []datasource.ConfigValidator{
datasourcevalidator.Conflicting(
path.MatchRoot("id"),
path.MatchRoot("display_name"),
),
datasourcevalidator.ExactlyOneOf(
path.MatchRoot("id"),
path.MatchRoot("display_name"),
),
}
}

func (d *dataSourceWarehouse) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}

pConfigData, ok := req.ProviderData.(*pconfig.ProviderData)
if !ok {
resp.Diagnostics.AddError(
common.ErrorDataSourceConfigType,
fmt.Sprintf(common.ErrorFabricClientType, req.ProviderData),
)

return
}

d.pConfigData = pConfigData
d.client = fabwarehouse.NewClientFactoryWithClient(*pConfigData.FabricClient).NewItemsClient()
}

// Read refreshes the Terraform state with the latest data.
func (d *dataSourceWarehouse) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
tflog.Debug(ctx, "READ", map[string]any{
"action": "start",
})
tflog.Trace(ctx, "READ", map[string]any{
"config": req.Config,
})

var data dataSourceWarehouseModel

if resp.Diagnostics.Append(req.Config.Get(ctx, &data)...); resp.Diagnostics.HasError() {
return
}
propertiesSetter := func(ctx context.Context, from *fabwarehouse.Properties, to *fabricitem.DataSourceFabricItemPropertiesModel[warehousePropertiesModel, fabwarehouse.Properties]) diag.Diagnostics {
properties := supertypes.NewSingleNestedObjectValueOfNull[warehousePropertiesModel](ctx)

timeout, diags := data.Timeouts.Read(ctx, d.pConfigData.Timeout)
if resp.Diagnostics.Append(diags...); resp.Diagnostics.HasError() {
return
}
if from != nil {
propertiesModel := &warehousePropertiesModel{}
propertiesModel.set(from)

ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
if diags := properties.Set(ctx, propertiesModel); diags.HasError() {
return diags
}
}

if data.ID.ValueString() != "" {
diags = d.getByID(ctx, &data)
} else {
diags = d.getByDisplayName(ctx, &data)
}
to.Properties = properties

if resp.Diagnostics.Append(diags...); resp.Diagnostics.HasError() {
return
return nil
}

resp.Diagnostics.Append(resp.State.Set(ctx, data)...)

tflog.Debug(ctx, "READ", map[string]any{
"action": "end",
})
itemGetter := func(ctx context.Context, fabricClient fabric.Client, model fabricitem.DataSourceFabricItemPropertiesModel[warehousePropertiesModel, fabwarehouse.Properties], fabricItem *fabricitem.FabricItemProperties[fabwarehouse.Properties]) error {
client := fabwarehouse.NewClientFactoryWithClient(fabricClient).NewItemsClient()

if resp.Diagnostics.HasError() {
return
}
}
respGet, err := client.GetWarehouse(ctx, model.WorkspaceID.ValueString(), model.ID.ValueString(), nil)
if err != nil {
return err
}

func (d *dataSourceWarehouse) getByID(ctx context.Context, model *dataSourceWarehouseModel) diag.Diagnostics {
tflog.Trace(ctx, "getting Warehouse by 'id'")
fabricItem.Set(respGet.Warehouse)

respGet, err := d.client.GetWarehouse(ctx, model.WorkspaceID.ValueString(), model.ID.ValueString(), nil)
if diags := utils.GetDiagsFromError(ctx, err, utils.OperationRead, nil); diags.HasError() {
return diags
return nil
}

return model.set(ctx, respGet.Warehouse)
}

func (d *dataSourceWarehouse) getByDisplayName(ctx context.Context, model *dataSourceWarehouseModel) diag.Diagnostics {
tflog.Trace(ctx, "getting Warehouse by 'display_name'")
itemListGetter := func(ctx context.Context, fabricClient fabric.Client, model fabricitem.DataSourceFabricItemPropertiesModel[warehousePropertiesModel, fabwarehouse.Properties], errNotFound fabcore.ResponseError, fabricItem *fabricitem.FabricItemProperties[fabwarehouse.Properties]) error {
client := fabwarehouse.NewClientFactoryWithClient(fabricClient).NewItemsClient()

var diags diag.Diagnostics
pager := client.NewListWarehousesPager(model.WorkspaceID.ValueString(), nil)
for pager.More() {
page, err := pager.NextPage(ctx)
if err != nil {
return err
}

pager := d.client.NewListWarehousesPager(model.WorkspaceID.ValueString(), nil)
for pager.More() {
page, err := pager.NextPage(ctx)
if diags := utils.GetDiagsFromError(ctx, err, utils.OperationList, nil); diags.HasError() {
return diags
}
for _, entity := range page.Value {
if *entity.DisplayName == model.DisplayName.ValueString() {
fabricItem.Set(entity)

for _, entity := range page.Value {
if *entity.DisplayName == model.DisplayName.ValueString() {
return model.set(ctx, entity)
return nil
}
}
}

return &errNotFound
}

diags.AddError(
common.ErrorReadHeader,
fmt.Sprintf("Unable to find Warehouse with 'display_name': %s in the Workspace ID: %s ", model.DisplayName.ValueString(), model.WorkspaceID.ValueString()),
)
config := fabricitem.DataSourceFabricItemProperties[warehousePropertiesModel, fabwarehouse.Properties]{
DataSourceFabricItem: fabricitem.DataSourceFabricItem{
Type: ItemType,
Name: ItemName,
TFName: ItemTFName,
MarkdownDescription: "Get a Fabric " + ItemName + ".\n\n" +
"Use this data source to fetch a [" + ItemName + "](" + ItemDocsURL + ").\n\n" +
ItemDocsSPNSupport,
IsDisplayNameUnique: true,
},
PropertiesSchema: propertiesSchema,
PropertiesSetter: propertiesSetter,
ItemGetter: itemGetter,
ItemListGetter: itemListGetter,
}

return diags
return fabricitem.NewDataSourceFabricItemProperties(config)
}
Loading

0 comments on commit 39599dd

Please sign in to comment.