Skip to content

Commit

Permalink
New resource and data source: azurerm_aadb2c_directory
Browse files Browse the repository at this point in the history
  • Loading branch information
manicminer committed Dec 20, 2021
1 parent 4d6aa89 commit f6e6a3d
Show file tree
Hide file tree
Showing 40 changed files with 2,865 additions and 0 deletions.
1 change: 1 addition & 0 deletions .teamcity/components/generated/services.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// NOTE: this is Generated from the Service Definitions - manual changes will be lost
// to re-generate this file, run 'make generate' in the root of the repository
var services = mapOf(
"aadb2c" to "AAD B2C",
"apimanagement" to "API Management",
"advisor" to "Advisor",
"analysisservices" to "Analysis Services",
Expand Down
3 changes: 3 additions & 0 deletions internal/clients/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/Azure/go-autorest/autorest/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
"github.com/hashicorp/terraform-provider-azurerm/internal/features"
aadb2c "github.com/hashicorp/terraform-provider-azurerm/internal/services/aadb2c/client"
advisor "github.com/hashicorp/terraform-provider-azurerm/internal/services/advisor/client"
analysisServices "github.com/hashicorp/terraform-provider-azurerm/internal/services/analysisservices/client"
apiManagement "github.com/hashicorp/terraform-provider-azurerm/internal/services/apimanagement/client"
Expand Down Expand Up @@ -113,6 +114,7 @@ type Client struct {
Account *ResourceManagerAccount
Features features.UserFeatures

AadB2c *aadb2c.Client
Advisor *advisor.Client
AnalysisServices *analysisServices.Client
ApiManagement *apiManagement.Client
Expand Down Expand Up @@ -222,6 +224,7 @@ func (client *Client) Build(ctx context.Context, o *common.ClientOptions) error
client.Features = o.Features
client.StopContext = ctx

client.AadB2c = aadb2c.NewClient(o)
client.Advisor = advisor.NewClient(o)
client.AnalysisServices = analysisServices.NewClient(o)
client.ApiManagement = apiManagement.NewClient(o)
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/aadb2c"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/advisor"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/analysisservices"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/apimanagement"
Expand Down Expand Up @@ -106,6 +107,7 @@ import (

func SupportedTypedServices() []sdk.TypedServiceRegistration {
return []sdk.TypedServiceRegistration{
aadb2c.Registration{},
apimanagement.Registration{},
appconfiguration.Registration{},
appservice.Registration{},
Expand Down
158 changes: 158 additions & 0 deletions internal/services/aadb2c/aadb2c_directory_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package aadb2c

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

"github.com/hashicorp/terraform-provider-azurerm/internal/services/aadb2c/sdk/2021-04-01-preview/tenants"

"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/aadb2c/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
)

type AadB2cDirectoryDataSourceModel struct {
BillingType string `tfschema:"billing_type"`
DataResidencyLocation string `tfschema:"data_residency_location"`
DomainName string `tfschema:"domain_name"`
EffectiveStartDate string `tfschema:"effective_start_date"`
ResourceGroup string `tfschema:"resource_group_name"`
Sku string `tfschema:"sku_name"`
Tags map[string]string `tfschema:"tags"`
TenantId string `tfschema:"tenant_id"`
}

type AadB2cDirectoryDataSource struct{}

var _ sdk.DataSource = AadB2cDirectoryDataSource{}

func (r AadB2cDirectoryDataSource) ResourceType() string {
return "azurerm_aadb2c_directory"
}

func (r AadB2cDirectoryDataSource) ModelObject() interface{} {
return &AadB2cDirectoryModel{}
}

func (r AadB2cDirectoryDataSource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return validate.B2CDirectoryID
}

func (r AadB2cDirectoryDataSource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"domain_name": {
Description: "Domain name of the B2C tenant, including onmicrosoft.com suffix.",
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),
}
}

func (r AadB2cDirectoryDataSource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"billing_type": {
Description: "The type of billing for the B2C tenant. Possible values include: `MAU` or `Auths`.",
Type: pluginsdk.TypeString,
Computed: true,
},

"data_residency_location": {
Description: "Location in which the B2C tenant is hosted and data resides.",
Type: pluginsdk.TypeString,
Computed: true,
},

"effective_start_date": {
Description: "The date from which the billing type took effect. May not be populated until after the first billing cycle.",
Type: pluginsdk.TypeString,
Computed: true,
},

"tenant_id": {
Description: "The Tenant ID for the B2C tenant.",
Type: pluginsdk.TypeString,
Computed: true,
},

"sku_name": {
Description: "Billing SKU for the B2C tenant.",
Type: pluginsdk.TypeString,
Computed: true,
},

"tags": tags.SchemaDataSource(),
}
}

func (r AadB2cDirectoryDataSource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.AadB2c.TenantsClient
subscriptionId := metadata.Client.Account.SubscriptionId

var state AadB2cDirectoryDataSourceModel
if err := metadata.Decode(&state); err != nil {
return fmt.Errorf("decoding: %+v", err)
}

id := tenants.NewB2CDirectoryID(subscriptionId, state.ResourceGroup, state.DomainName)

metadata.Logger.Infof("Reading %s", id)
resp, err := client.Get(ctx, id)
if err != nil {
if resp.HttpResponse.StatusCode == http.StatusNotFound {
return metadata.MarkAsGone(id)
}
return fmt.Errorf("retrieving %s: %v", id, err)
}

model := resp.Model
if model == nil {
return fmt.Errorf("retrieving %s: model was nil", id)
}

state.DomainName = id.DirectoryName
state.ResourceGroup = id.ResourceGroup

if model.Location != nil {
state.DataResidencyLocation = string(*model.Location)
}

if model.Sku != nil {
state.Sku = string(model.Sku.Name)
}

if model.Tags != nil {
state.Tags = *model.Tags
}

if properties := model.Properties; properties != nil {
if billingConfig := properties.BillingConfig; billingConfig != nil {
if billingConfig.BillingType != nil {
state.BillingType = string(*billingConfig.BillingType)
}
if billingConfig.EffectiveStartDateUtc != nil {
state.EffectiveStartDate = *billingConfig.EffectiveStartDateUtc
}
}

if properties.TenantId != nil {
state.TenantId = *properties.TenantId
}
}

metadata.SetID(id)

return metadata.Encode(&state)
},
}
}
37 changes: 37 additions & 0 deletions internal/services/aadb2c/aadb2c_directory_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package aadb2c_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
)

type AadB2cDirectoryDataSource struct{}

func TestAccAadB2cDirectoryDataSource_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_aadb2c_directory", "test")
d := AadB2cDirectoryDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: d.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("data_residency_location").HasValue("United States"),
check.That(data.ResourceName).Key("sku_name").HasValue("PremiumP1"),
),
},
})
}

func (d AadB2cDirectoryDataSource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%[1]s
data "azurerm_aadb2c_directory" "test" {
domain_name = azurerm_aadb2c_directory.test.domain_name
resource_group_name = azurerm_aadb2c_directory.test.resource_group_name
}
`, AadB2cDirectoryResource{}.basic(data))
}
Loading

0 comments on commit f6e6a3d

Please sign in to comment.