Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gateway): onboard new resource/data-source #233

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
"github.com/microsoft/terraform-provider-fabric/internal/services/environment"
"github.com/microsoft/terraform-provider-fabric/internal/services/eventhouse"
"github.com/microsoft/terraform-provider-fabric/internal/services/eventstream"
"github.com/microsoft/terraform-provider-fabric/internal/services/gateway"
"github.com/microsoft/terraform-provider-fabric/internal/services/kqldashboard"
"github.com/microsoft/terraform-provider-fabric/internal/services/kqldatabase"
"github.com/microsoft/terraform-provider-fabric/internal/services/kqlqueryset"
Expand Down Expand Up @@ -387,6 +388,8 @@ func (p *FabricProvider) Resources(ctx context.Context) []func() resource.Resour
func() resource.Resource { return environment.NewResourceEnvironment(ctx) },
func() resource.Resource { return eventhouse.NewResourceEventhouse(ctx) },
eventstream.NewResourceEventstream,
gateway.NewResourceGatewayRoleAssignment,
gateway.NewResourceVirtualNetworkGateway,
kqldashboard.NewResourceKQLDashboard,
kqldatabase.NewResourceKQLDatabase,
kqlqueryset.NewResourceKQLQueryset,
Expand Down Expand Up @@ -424,6 +427,13 @@ func (p *FabricProvider) DataSources(ctx context.Context) []func() datasource.Da
func() datasource.DataSource { return eventhouse.NewDataSourceEventhouses(ctx) },
eventstream.NewDataSourceEventstream,
eventstream.NewDataSourceEventstreams,
gateway.NewDataSourceGatewayRoleAssignments,
gateway.NewDataSourceOnPremisesGateway,
gateway.NewDataSourceOnPremisesGateways,
gateway.NewDataSourceOnPremisesGatewayPersonal,
gateway.NewDataSourceOnPremisesGatewayPersonals,
gateway.NewDataSourceVirtualNetworkGateway,
gateway.NewDataSourceVirtualNetworkGateways,
kqldashboard.NewDataSourceKQLDashboard,
kqldashboard.NewDataSourceKQLDashboards,
kqldatabase.NewDataSourceKQLDatabase,
Expand Down
30 changes: 30 additions & 0 deletions internal/services/gateway/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) Microsoft Corporation
// SPDX-License-Identifier: MPL-2.0

package gateway

import (
"github.com/microsoft/terraform-provider-fabric/internal/common"
)

const (
OnPremisesItemTFType = "on_premises_gateway"
OnPremisesItemsTFType = "on_premises_gateways"
OnPremisesPersonalItemType = "on_premises_personal_gateway"
OnPremisesPersonalItemsType = "on_premises_personal_gateways"
VirtualNetworkItemTFType = "virtual_network_gateway"
VirtualNetworkItemsTFType = "virtual_network_gateways"
ItemName = "Gateway"
ItemsName = "Gateways"
ItemsTFName = "gateways"
ItemDocsSPNSupport = common.DocsSPNSupported
ItemDocsURL = "https://learn.microsoft.com/en-us/fabric/data-factory/how-to-access-on-premises-data"
)

var (
PossibleInactivityMinutesBeforeSleepValues = []int32{30, 60, 90, 120, 150, 240, 360, 480, 720, 1440}

Check failure on line 25 in internal/services/gateway/base.go

View workflow job for this annotation

GitHub Actions / 🏗️ Check Build

PossibleInactivityMinutesBeforeSleepValues is a global variable (gochecknoglobals)

MinNumberOfMemberGatewaysValues = int32(1)

Check failure on line 27 in internal/services/gateway/base.go

View workflow job for this annotation

GitHub Actions / 🏗️ Check Build

MinNumberOfMemberGatewaysValues is a global variable (gochecknoglobals)

MaxNumberOfMemberGatewaysValues = int32(7)

Check failure on line 29 in internal/services/gateway/base.go

View workflow job for this annotation

GitHub Actions / 🏗️ Check Build

MaxNumberOfMemberGatewaysValues is a global variable (gochecknoglobals)
)
19 changes: 19 additions & 0 deletions internal/services/gateway/base_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Microsoft Corporation
// SPDX-License-Identifier: MPL-2.0

package gateway_test

import (
"github.com/microsoft/terraform-provider-fabric/internal/services/gateway"
)

const (
VirtualNetworkItemTFName = gateway.VirtualNetworkItemTFType
VirtualNetworkItemsTFName = gateway.VirtualNetworkItemsTFType
OnPremisesItemTFName = gateway.OnPremisesItemTFType
OnPremisesItemsTFName = gateway.OnPremisesItemsTFType
OnPremisesPersonalItemTFName = gateway.OnPremisesPersonalItemType
OnPremisesPersonalItemsTFName = gateway.OnPremisesPersonalItemsType
itemsTFName = gateway.ItemsTFName
itemType = gateway.ItemName
)
136 changes: 136 additions & 0 deletions internal/services/gateway/data_gateway_role_assignments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// Copyright (c) Microsoft Corporation
// SPDX-License-Identifier: MPL-2.0

package gateway

import (
"context"
"fmt"

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

"github.com/hashicorp/terraform-plugin-framework-timeouts/datasource/timeouts"
"github.com/hashicorp/terraform-plugin-log/tflog"

fabcore "github.com/microsoft/fabric-sdk-go/fabric/core"
supertypes "github.com/orange-cloudavenue/terraform-plugin-framework-supertypes"

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

var _ datasource.DataSourceWithConfigure = (*dataSourceGatewayRoleAssignments)(nil)

type dataSourceGatewayRoleAssignments struct {
pConfigData *pconfig.ProviderData
client *fabcore.GatewaysClient
}

func NewDataSourceGatewayRoleAssignments() datasource.DataSource {
return &dataSourceGatewayRoleAssignments{}
}

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

func (d *dataSourceGatewayRoleAssignments) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "List the Fabric gateway role assignments.\n\n" +
"Use this data source to list the role assignments for a gateway.\n\n" +
ItemDocsSPNSupport,
Attributes: map[string]schema.Attribute{
"gateway_id": schema.StringAttribute{
MarkdownDescription: "The Gateway ID.",
Required: true,
CustomType: customtypes.UUIDType{},
},
"values": schema.ListNestedAttribute{
MarkdownDescription: "A list of gateway role assignments.",
Computed: true,
CustomType: supertypes.NewListNestedObjectTypeOf[gatewayRoleAssignmentModel](ctx),
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "The role assignment ID.",
Computed: true,
CustomType: customtypes.UUIDType{},
},
"role": schema.StringAttribute{
MarkdownDescription: "The gateway role of the principal.",
Computed: true,
},
"display_name": schema.StringAttribute{
MarkdownDescription: "The principal's display name.",
Computed: true,
},
"type": schema.StringAttribute{
MarkdownDescription: "The type of the principal.",
Computed: true,
},
},
},
},
"timeouts": timeouts.Attributes(ctx),
},
}
}

func (d *dataSourceGatewayRoleAssignments) 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
// Create a gateways client via the provider's FabricClient.
d.client = (*fabcore.GatewaysClient)(fabcore.NewClientFactoryWithClient(*pConfigData.FabricClient).NewGatewaysClient())
}

func (d *dataSourceGatewayRoleAssignments) 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 dataSourceGatewayRoleAssignmentsModel
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()

respList, err := d.client.ListGatewayRoleAssignments(ctx, data.GatewayID.ValueString(), nil)
if diags := utils.GetDiagsFromError(ctx, err, utils.OperationList, nil); diags.HasError() {
resp.Diagnostics.Append(diags...)
return
}

if diags := data.setValues(ctx, respList); diags.HasError() {
resp.Diagnostics.Append(diags...)
return
}

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

tflog.Debug(ctx, "READ", map[string]any{
"action": "end",
})
}
80 changes: 80 additions & 0 deletions internal/services/gateway/data_gateway_role_assignments_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (c) Microsoft Corporation
// SPDX-License-Identifier: MPL-2.0

package gateway_test

import (
"regexp"
"testing"

at "github.com/dcarbone/terraform-plugin-framework-utils/v3/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"

"github.com/microsoft/terraform-provider-fabric/internal/testhelp"
"github.com/microsoft/terraform-provider-fabric/internal/testhelp/fakes"
)

var (
testDataSourceGatewayRoleAssignments = testhelp.DataSourceFQN("fabric", "gateway_role_assignments", "test")
testDataSourceGatewayRoleAssignmentsHeader = at.DataSourceHeader(testhelp.TypeName("fabric", "gateway_role_assignments"), "test")
)

func TestUnit_GatewayRoleAssignmentsDataSource(t *testing.T) {
gatewayID := testhelp.RandomUUID()
gatewayRoleAssignments := NewRandomGatewayRoleAssignments()
fakes.FakeServer.ServerFactory.Core.GatewaysServer.NewListGatewayRoleAssignmentsPager = fakeGatewayRoleAssignments(gatewayRoleAssignments)

entity := gatewayRoleAssignments.Value[1]

resource.ParallelTest(t, testhelp.NewTestUnitCase(t, nil, fakes.FakeServer.ServerFactory, nil, []resource.TestStep{
// Step 1: Error on unexpected attribute.
{
Config: at.CompileConfig(
testDataSourceGatewayRoleAssignmentsHeader,
map[string]any{
"gateway_id": gatewayID,
"unexpected_attr": "test",
},
),
ExpectError: regexp.MustCompile(`An argument named "unexpected_attr" is not expected here`),
},
// Step 2: Read the role assignments.
{
Config: at.CompileConfig(
testDataSourceGatewayRoleAssignmentsHeader,
map[string]any{
"gateway_id": gatewayID,
},
),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(testDataSourceGatewayRoleAssignments, "gateway_id", gatewayID),
resource.TestCheckResourceAttrPtr(testDataSourceGatewayRoleAssignments, "values.1.id", entity.ID),
resource.TestCheckResourceAttrPtr(testDataSourceGatewayRoleAssignments, "values.1.role", (*string)(entity.Role)),
resource.TestCheckResourceAttrPtr(testDataSourceGatewayRoleAssignments, "values.1.display_name", entity.Principal.DisplayName),
resource.TestCheckResourceAttrPtr(testDataSourceGatewayRoleAssignments, "values.1.type", (*string)(entity.Principal.Type)),
),
},
}))
}

func TestAcc_GatewayRoleAssignmentsDataSource(t *testing.T) {
// For acceptance testing, assume a well-known gateway is provided.
gateway := testhelp.WellKnown()["GatewayVirtualNetwork"].(map[string]any)
gatewayID := gateway["id"].(string)

resource.ParallelTest(t, testhelp.NewTestAccCase(t, nil, nil, []resource.TestStep{
// Step: Read the gateway role assignments.
{
Config: at.CompileConfig(
testDataSourceGatewayRoleAssignmentsHeader,
map[string]any{
"gateway_id": gatewayID,
},
),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(testDataSourceGatewayRoleAssignments, "gateway_id", gatewayID),
resource.TestCheckResourceAttrSet(testDataSourceGatewayRoleAssignments, "values.0.id"),
),
},
}))
}
Loading
Loading