Skip to content

Commit

Permalink
acc tests & set well known
Browse files Browse the repository at this point in the history
  • Loading branch information
OrBaubergMicrosoft committed Feb 23, 2025
1 parent e2b5558 commit cf606ee
Show file tree
Hide file tree
Showing 17 changed files with 467 additions and 246 deletions.
25 changes: 0 additions & 25 deletions internal/services/gateway/data_gateway_role_assignments.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,31 +71,6 @@ func (d *dataSourceGatewayRoleAssignments) Schema(ctx context.Context, _ datasou
MarkdownDescription: "The type of the principal.",
Computed: true,
},
"details": schema.SingleNestedAttribute{
MarkdownDescription: "The principal details.",
Computed: true,
CustomType: supertypes.NewSingleNestedObjectTypeOf[principalDetailsModel](ctx),
Attributes: map[string]schema.Attribute{
"user_principal_name": schema.StringAttribute{
MarkdownDescription: "The user principal name.",
Computed: true,
},
"group_type": schema.StringAttribute{
MarkdownDescription: "The group type.",
Computed: true,
},
"app_id": schema.StringAttribute{
MarkdownDescription: "The Service Principal's Microsoft Entra App ID.",
Computed: true,
CustomType: customtypes.UUIDType{},
},
"parent_principal_id": schema.StringAttribute{
MarkdownDescription: "The parent principal ID of Service Principal Profile.",
Computed: true,
CustomType: customtypes.UUIDType{},
},
},
},
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,14 @@ func TestUnit_GatewayRoleAssignmentsDataSource(t *testing.T) {
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)),
// Additional nested details checks can be added here.
),
},
}))
}

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

resource.ParallelTest(t, testhelp.NewTestAccCase(t, nil, nil, []resource.TestStep{
Expand Down
13 changes: 5 additions & 8 deletions internal/services/gateway/data_on_premises_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,9 @@ func (d *dataSourceOnPremisesGateway) Configure(_ context.Context, req datasourc
}

d.pConfigData = pConfigData
d.client = (*fabcore.GatewaysClient)(fabcore.NewClientFactoryWithClient(*pConfigData.FabricClient).NewGatewaysClient())
d.client = fabcore.NewClientFactoryWithClient(*pConfigData.FabricClient).NewGatewaysClient()
}

// Read refreshes the Terraform state with the latest data.
func (d *dataSourceOnPremisesGateway) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
tflog.Debug(ctx, "READ", map[string]any{
"action": "start",
Expand Down Expand Up @@ -209,15 +208,13 @@ func (d *dataSourceOnPremisesGateway) getByDisplayName(ctx context.Context, mode
}

for _, gw := range gateways {
if OnPremisesGateway, ok := gw.(*fabcore.OnPremisesGateway); ok {
if *OnPremisesGateway.DisplayName == model.DisplayName.ValueString() {
model.set(ctx, *OnPremisesGateway)
if onPremisesGateway, ok := gw.(*fabcore.OnPremisesGateway); ok {
if *onPremisesGateway.DisplayName == model.DisplayName.ValueString() {
model.set(ctx, *onPremisesGateway)
return nil
}
}
}

var diags diag.Diagnostics
diags.AddError(common.ErrorReadHeader, "no on-premises gateway with display name found")
return diags
return diag.Diagnostics{diag.NewErrorDiagnostic(common.ErrorReadHeader, "on-premises gateway not found")}
}
40 changes: 24 additions & 16 deletions internal/services/gateway/data_on_premises_gateway_personal.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework-timeouts/datasource/timeouts"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-log/tflog"

fabcore "github.com/microsoft/fabric-sdk-go/fabric/core"

Expand Down Expand Up @@ -38,9 +39,8 @@ func (d *dataSourceOnPremisesGatewayPersonal) Schema(ctx context.Context, _ data
MarkdownDescription: "Retrieve an on-premises gateway in its 'personal' form (ID, public key, type, version).",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Required: true,
MarkdownDescription: "The gateway ID.",
Optional: true,
Computed: true,
CustomType: customtypes.UUIDType{},
},
"version": schema.StringAttribute{
Expand Down Expand Up @@ -73,7 +73,6 @@ func (d *dataSourceOnPremisesGatewayPersonal) Configure(ctx context.Context, req
}

pConfigData, ok := req.ProviderData.(*pconfig.ProviderData)

if !ok {
resp.Diagnostics.AddError(
common.ErrorDataSourceConfigType,
Expand All @@ -82,27 +81,33 @@ func (d *dataSourceOnPremisesGatewayPersonal) Configure(ctx context.Context, req
return
}
d.pConfigData = pConfigData
d.client = (*fabcore.GatewaysClient)(fabcore.NewClientFactoryWithClient(*pConfigData.FabricClient).NewGatewaysClient())
d.client = fabcore.NewClientFactoryWithClient(*pConfigData.FabricClient).NewGatewaysClient()
}

func (d *dataSourceOnPremisesGatewayPersonal) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data datasourceOnPremisesGatewayPersonalModel
tflog.Debug(ctx, "READ", map[string]any{
"action": "start",
})
tflog.Trace(ctx, "READ", map[string]any{
"config": req.Config,
})

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

if data.ID.ValueString() == "" {
resp.Diagnostics.AddError(
"Missing ID",
"An ID is required to look up a personal on-premises gateway.",
)
timeout, diags := data.Timeouts.Read(ctx, d.pConfigData.Timeout)
if resp.Diagnostics.Append(diags...); resp.Diagnostics.HasError() {
return
}

gatewayResp, errResp := d.client.GetGateway(ctx, data.ID.ValueString(), nil)
if errResp != nil {
resp.Diagnostics.AddError("GetGateway failed", errResp.Error())
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

gatewayResp, err := d.client.GetGateway(ctx, data.ID.ValueString(), nil)
if err != nil {
resp.Diagnostics.AddError("GetGateway failed", err.Error())
return
}

Expand All @@ -112,14 +117,17 @@ func (d *dataSourceOnPremisesGatewayPersonal) Read(ctx context.Context, req data
return
}

gateway := datasourceOnPremisesGatewayPersonalModel{}
diags := gateway.set(ctx, *realGw)
data.set(ctx, *realGw)
if diags.HasError() {
resp.Diagnostics.Append(diags...)
return
}

if diags := resp.State.Set(ctx, gateway); diags.HasError() {
tflog.Debug(ctx, "READ", map[string]any{
"action": "end",
})

if diags := resp.State.Set(ctx, data); diags.HasError() {
resp.Diagnostics.Append(diags...)
return
}
Expand Down
48 changes: 13 additions & 35 deletions internal/services/gateway/data_on_premises_gateway_personal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,19 @@ func TestUnit_OnPremisesGatewayPersonalDataSource(t *testing.T) {
testDataSourceOnPremisesPersonalHeader,
map[string]any{},
),
// "Missing ID" error is raised in Read when data.ID is empty.
ExpectError: regexp.MustCompile(`Missing ID`),
ExpectError: regexp.MustCompile(`The argument "id" is required`),
},
// Step 3: Invalid UUID string should trigger an error.
// Step 3: Read by id - not found
{
Config: at.CompileConfig(
testDataSourceOnPremisesItemHeader,
map[string]any{
"id": testhelp.RandomUUID(),
},
),
ExpectError: regexp.MustCompile(common.ErrorReadHeader),
},
// Step 4: Invalid UUID string should trigger an error.
{
Config: at.CompileConfig(
testDataSourceOnPremisesPersonalHeader,
Expand All @@ -62,7 +71,7 @@ func TestUnit_OnPremisesGatewayPersonalDataSource(t *testing.T) {
),
ExpectError: regexp.MustCompile(`invalid uuid`),
},
// Step 4: Valid read test using the entity's ID.
// Step 5: Valid read test using the entity's ID.
{
Config: at.CompileConfig(
testDataSourceOnPremisesPersonalHeader,
Expand All @@ -80,34 +89,3 @@ func TestUnit_OnPremisesGatewayPersonalDataSource(t *testing.T) {
},
))
}

func TestAcc_OnPremisesGatewayPersonalDataSource(t *testing.T) {
entity := testhelp.WellKnown()["OnPremisesGatewayPersonal"].(map[string]any)
entityID := entity["id"].(string)
entityDescription := entity["description"].(string)

resource.ParallelTest(t, testhelp.NewTestAccCase(t, nil, nil, []resource.TestStep{
{
Config: at.CompileConfig(
testDataSourceOnPremisesPersonalHeader,
map[string]any{
"id": entityID,
},
),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(testDataSourceOnPremisesPersonalFQN, "id", entityID),
resource.TestCheckResourceAttr(testDataSourceOnPremisesPersonalFQN, "description", entityDescription),
),
},
// read by id - not found
{
Config: at.CompileConfig(
testDataSourceOnPremisesPersonalHeader,
map[string]any{
"id": testhelp.RandomUUID(),
},
),
ExpectError: regexp.MustCompile(common.ErrorReadHeader),
},
}))
}
33 changes: 0 additions & 33 deletions internal/services/gateway/data_on_premises_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,36 +131,3 @@ func TestUnit_OnPremisesGatewayDataSource(t *testing.T) {
},
}))
}

func TestAcc_OnPremisesGatewayDataSource(t *testing.T) {
entity := testhelp.WellKnown()["OnPremisesGateway"].(map[string]any)
entityID := entity["id"].(string)
entityDisplayName := entity["displayName"].(string)
entityDescription := entity["description"].(string)

resource.ParallelTest(t, testhelp.NewTestAccCase(t, nil, nil, []resource.TestStep{
{
Config: at.CompileConfig(
testDataSourceOnPremisesItemHeader,
map[string]any{
"id": entityID,
},
),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(testDataSourceOnPremisesItemFabricFQN, "id", entityID),
resource.TestCheckResourceAttr(testDataSourceOnPremisesItemFabricFQN, "display_name", entityDisplayName),
resource.TestCheckResourceAttr(testDataSourceOnPremisesItemFabricFQN, "description", entityDescription),
),
},
// read by id - not found
{
Config: at.CompileConfig(
testDataSourceOnPremisesItemHeader,
map[string]any{
"id": testhelp.RandomUUID(),
},
),
ExpectError: regexp.MustCompile(common.ErrorReadHeader),
},
}))
}
20 changes: 0 additions & 20 deletions internal/services/gateway/data_on_premises_gateways_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,3 @@ func TestUnit_OnPremisesGatewaysDataSource(t *testing.T) {
},
))
}

func TestAcc_OnPremisesGatewaysDataSource(t *testing.T) {
resource.ParallelTest(t, testhelp.NewTestAccCase(
t,
nil,
nil,
[]resource.TestStep{
// read
{
Config: at.CompileConfig(
testDataSourceOnPremisesGatewaysHeader,
map[string]any{},
),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet(testDataSourceOnPremisesGatewaysFQN, "values.0.id"),
),
},
},
))
}
17 changes: 9 additions & 8 deletions internal/services/gateway/data_virtual_network_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (d *dataSourceVirtualNetworkGateway) Configure(_ context.Context, req datas
}

d.pConfigData = pConfigData
d.client = (*fabcore.GatewaysClient)(fabcore.NewClientFactoryWithClient(*pConfigData.FabricClient).NewGatewaysClient())
d.client = fabcore.NewClientFactoryWithClient(*pConfigData.FabricClient).NewGatewaysClient()
}

// Read refreshes the Terraform state with the latest data.
Expand Down Expand Up @@ -190,10 +190,13 @@ func (d *dataSourceVirtualNetworkGateway) getByID(ctx context.Context, model *da
if gw, ok := respGet.GatewayClassification.(*fabcore.VirtualNetworkGateway); ok {
model.set(ctx, *gw)
return nil
} else {
var diags diag.Diagnostics
diags.AddError(common.ErrorReadHeader, "expected gateway to be an on-premises gateway")
return diags
}

return diag.Diagnostics{
diag.NewErrorDiagnostic(
common.ErrorReadHeader,
"expected gateway to be a virtual network gateway",
),
}
}

Expand All @@ -215,7 +218,5 @@ func (d *dataSourceVirtualNetworkGateway) getByDisplayName(ctx context.Context,
}
}

var diags diag.Diagnostics
diags.AddError(common.ErrorReadHeader, "expected gateway to be an on-premises gateway")
return diags
return diag.Diagnostics{diag.NewErrorDiagnostic(common.ErrorReadHeader, "virtual network gateway not found")}
}
11 changes: 8 additions & 3 deletions internal/services/gateway/data_virtual_network_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,9 @@ func TestUnit_VirtualNetworkGatewayDataSource(t *testing.T) {
}

func TestAcc_VirtualNetworkGatewayDataSource(t *testing.T) {
entity := testhelp.WellKnown()["VirtualNetworkGateway"].(map[string]any)
entity := testhelp.WellKnown()["GatewayVirtualNetwork"].(map[string]any)
entityID := entity["id"].(string)
entityDisplayName := entity["displayName"].(string)
entityDescription := entity["description"].(string)

resource.ParallelTest(t, testhelp.NewTestAccCase(t, nil, nil, []resource.TestStep{
{
Expand All @@ -149,7 +148,13 @@ func TestAcc_VirtualNetworkGatewayDataSource(t *testing.T) {
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(testDataSourceVirtualNetworkFQN, "id", entityID),
resource.TestCheckResourceAttr(testDataSourceVirtualNetworkFQN, "display_name", entityDisplayName),
resource.TestCheckResourceAttr(testDataSourceVirtualNetworkFQN, "description", entityDescription),
resource.TestCheckResourceAttrSet(testDataSourceVirtualNetworkFQN, "inactivity_minutes_before_sleep"),
resource.TestCheckResourceAttrSet(testDataSourceVirtualNetworkFQN, "capacity_id"),
resource.TestCheckResourceAttrSet(testDataSourceVirtualNetworkFQN, "number_of_member_gateways"),
resource.TestCheckResourceAttrSet(testDataSourceVirtualNetworkFQN, "virtual_network_azure_resource.subscription_id"),
resource.TestCheckResourceAttrSet(testDataSourceVirtualNetworkFQN, "virtual_network_azure_resource.resource_group_name"),
resource.TestCheckResourceAttrSet(testDataSourceVirtualNetworkFQN, "virtual_network_azure_resource.virtual_network_name"),
resource.TestCheckResourceAttrSet(testDataSourceVirtualNetworkFQN, "virtual_network_azure_resource.subnet_name"),
),
},
// read by id - not found
Expand Down
2 changes: 1 addition & 1 deletion internal/services/gateway/fake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func NewRandomGatewayRoleAssignments() fabcore.GatewayRoleAssignments {
},
{
ID: azto.Ptr(assignmentID1),
Role: azto.Ptr(fabcore.GatewayRoleAdmin),
Role: azto.Ptr(fabcore.GatewayRoleConnectionCreator),
Principal: &fabcore.Principal{
ID: azto.Ptr(principalID1),
Type: azto.Ptr(fabcore.PrincipalTypeUser),
Expand Down
Loading

0 comments on commit cf606ee

Please sign in to comment.