-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1275ee7
commit b8de9a7
Showing
20 changed files
with
799 additions
and
575 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...rvices/warehouse/models_data_warehouse.go → ...fabricitem/models_data_item_properties.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
34
internal/pkg/fabricitem/models_resource_item_properties.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
Oops, something went wrong.