Skip to content

Commit

Permalink
feat: refactor warehouse
Browse files Browse the repository at this point in the history
  • Loading branch information
DariuszPorowski committed Dec 20, 2024
1 parent 1275ee7 commit 7b3e885
Show file tree
Hide file tree
Showing 21 changed files with 802 additions and 575 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
170 changes: 170 additions & 0 deletions internal/pkg/fabricitem/data_item_properties.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
// Copyright (c) Microsoft Corporation
// SPDX-License-Identifier: MPL-2.0

package fabricitem

import (
"context"
"fmt"
"net/http"

"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"

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

// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSourceWithConfigValidators = (*DataSourceFabricItemProperties[struct{}, struct{}])(nil)
_ datasource.DataSourceWithConfigure = (*DataSourceFabricItemProperties[struct{}, struct{}])(nil)
)

type DataSourceFabricItemProperties[Ttfprop, Titemprop any] struct {
DataSourceFabricItem
PropertiesSchema schema.SingleNestedAttribute
PropertiesSetter func(ctx context.Context, from *Titemprop, to *DataSourceFabricItemPropertiesModel[Ttfprop, Titemprop]) diag.Diagnostics
ItemGetter func(ctx context.Context, fabricClient fabric.Client, model DataSourceFabricItemPropertiesModel[Ttfprop, Titemprop], fabricItem *FabricItemProperties[Titemprop]) error
ItemListGetter func(ctx context.Context, fabricClient fabric.Client, model DataSourceFabricItemPropertiesModel[Ttfprop, Titemprop], errNotFound fabcore.ResponseError, fabricItem *FabricItemProperties[Titemprop]) error
}

func NewDataSourceFabricItemProperties[Ttfprop, Titemprop any](config DataSourceFabricItemProperties[Ttfprop, Titemprop]) datasource.DataSource {
return &config
}

func (d *DataSourceFabricItemProperties[Ttfprop, Titemprop]) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { //revive:disable-line:confusing-naming
resp.TypeName = req.ProviderTypeName + "_" + d.TFName
}

func (d *DataSourceFabricItemProperties[Ttfprop, Titemprop]) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { //revive:disable-line:confusing-naming
resp.Schema = GetDataSourceFabricItemPropertiesSchema1(ctx, *d)
}

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

return []datasource.ConfigValidator{}
}

func (d *DataSourceFabricItemProperties[Ttfprop, Titemprop]) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { //revive:disable-line:confusing-naming
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 = fabcore.NewClientFactoryWithClient(*pConfigData.FabricClient).NewItemsClient()
}

func (d *DataSourceFabricItemProperties[Ttfprop, Titemprop]) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { //revive:disable-line:confusing-naming
tflog.Debug(ctx, "READ", map[string]any{
"action": "start",
})
tflog.Trace(ctx, "READ", map[string]any{
"config": req.Config,
})

var data DataSourceFabricItemPropertiesModel[Ttfprop, Titemprop]

if resp.Diagnostics.Append(req.Config.Get(ctx, &data)...); resp.Diagnostics.HasError() {
return
}

timeout, diags := data.Timeouts.Read(ctx, d.pConfigData.Timeout)
if resp.Diagnostics.Append(diags...); resp.Diagnostics.HasError() {
return
}

ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

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

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

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

tflog.Debug(ctx, "READ", map[string]any{
"action": "end",
})

if resp.Diagnostics.HasError() {
return
}
}

func (d *DataSourceFabricItemProperties[Ttfprop, Titemprop]) getByID(ctx context.Context, model *DataSourceFabricItemPropertiesModel[Ttfprop, Titemprop]) diag.Diagnostics {
tflog.Trace(ctx, fmt.Sprintf("getting %s by ID: %s", d.Name, model.ID.ValueString()))

var fabricItem FabricItemProperties[Titemprop]

err := d.ItemGetter(ctx, *d.pConfigData.FabricClient, *model, &fabricItem)
if diags := utils.GetDiagsFromError(ctx, err, utils.OperationRead, nil); diags.HasError() {
return diags
}

model.set(fabricItem)

return d.PropertiesSetter(ctx, fabricItem.Properties, model)
}

func (d *DataSourceFabricItemProperties[Ttfprop, Titemprop]) getByDisplayName(ctx context.Context, model *DataSourceFabricItemPropertiesModel[Ttfprop, Titemprop]) diag.Diagnostics {
tflog.Trace(ctx, fmt.Sprintf("getting %s by Display Name: %s", d.Name, model.DisplayName.ValueString()))

errNotFoundCode := fabcore.ErrCommon.EntityNotFound.Error()
errNotFoundMsg := fmt.Sprintf("Unable to find %s with 'display_name': %s in the Workspace ID: %s", d.Name, model.DisplayName.ValueString(), model.WorkspaceID.ValueString())

errNotFound := fabcore.ResponseError{
ErrorCode: errNotFoundCode,
StatusCode: http.StatusNotFound,
ErrorResponse: &fabcore.ErrorResponse{
ErrorCode: &errNotFoundCode,
Message: &errNotFoundMsg,
},
}

var fabricItem FabricItemProperties[Titemprop]

err := d.ItemListGetter(ctx, *d.pConfigData.FabricClient, *model, errNotFound, &fabricItem)
if diags := utils.GetDiagsFromError(ctx, err, utils.OperationRead, nil); diags.HasError() {
return diags
}

model.set(fabricItem)

return d.PropertiesSetter(ctx, fabricItem.Properties, model)
}
10 changes: 10 additions & 0 deletions internal/pkg/fabricitem/data_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ func GetDataSourceFabricItemPropertiesSchema(ctx context.Context, d DataSourceFa
}
}

func GetDataSourceFabricItemPropertiesSchema1[Ttfprop, Titemprop any](ctx context.Context, d DataSourceFabricItemProperties[Ttfprop, Titemprop]) schema.Schema {
attributes := getDataSourceFabricItemBaseAttributes(ctx, d.Name, d.IsDisplayNameUnique)
attributes["properties"] = d.PropertiesSchema

return schema.Schema{
MarkdownDescription: d.MarkdownDescription,
Attributes: attributes,
}
}

func GetDataSourceFabricItemDefinitionPropertiesSchema(ctx context.Context, d DataSourceFabricItemDefinition, properties schema.SingleNestedAttribute) schema.Schema {
attributes := getDataSourceFabricItemBaseAttributes(ctx, d.Name, d.IsDisplayNameUnique)
attributes["properties"] = properties
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Copyright (c) Microsoft Corporation
// SPDX-License-Identifier: MPL-2.0

package warehouse
package fabricitem

import (
"github.com/hashicorp/terraform-plugin-framework-timeouts/datasource/timeouts"
)

type dataSourceWarehouseModel struct {
baseWarehouseModel
type DataSourceFabricItemPropertiesModel[Ttfprop, Titemprop any] struct {
FabricItemPropertiesModel[Ttfprop, Titemprop]
Timeouts timeouts.Value `tfsdk:"timeouts"`
}
34 changes: 34 additions & 0 deletions internal/pkg/fabricitem/models_resource_item_properties.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) Microsoft Corporation
// SPDX-License-Identifier: MPL-2.0

package fabricitem

import (
azto "github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
fabcore "github.com/microsoft/fabric-sdk-go/fabric/core"
)

type ResourceFabricItemPropertiesModel[Ttfprop, Titemprop any] struct {
FabricItemPropertiesModel[Ttfprop, Titemprop]
Timeouts timeouts.Value `tfsdk:"timeouts"`
}

type requestCreateFabricItemProperties[Ttfprop, Titemprop any] struct {
fabcore.CreateItemRequest
}

func (to *requestCreateFabricItemProperties[Ttfprop, Titemprop]) set(from ResourceFabricItemPropertiesModel[Ttfprop, Titemprop], itemType fabcore.ItemType) { //revive:disable-line:confusing-naming
to.DisplayName = from.DisplayName.ValueStringPointer()
to.Description = from.Description.ValueStringPointer()
to.Type = azto.Ptr(itemType)
}

type requestUpdateFabricItemProperties[Ttfprop, Titemprop any] struct {
fabcore.UpdateItemRequest
}

func (to *requestUpdateFabricItemProperties[Ttfprop, Titemprop]) set(from ResourceFabricItemPropertiesModel[Ttfprop, Titemprop]) { //revive:disable-line:confusing-naming
to.DisplayName = from.DisplayName.ValueStringPointer()
to.Description = from.Description.ValueStringPointer()
}
Loading

0 comments on commit 7b3e885

Please sign in to comment.