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

new datasource azurerm_custom_location #25693

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/labeler-issue-triage.yml
Expand Up @@ -141,6 +141,9 @@ service/event-grid:
service/event-hubs:
- '### (|New or )Affected Resource\(s\)\/Data Source\(s\)((.|\n)*)azurerm_eventhub((.|\n)*)###'

service/extendedlocation:
- '### (|New or )Affected Resource\(s\)\/Data Source\(s\)((.|\n)*)azurerm_custom_location((.|\n)*)###'

service/firewall:
- '### (|New or )Affected Resource\(s\)\/Data Source\(s\)((.|\n)*)azurerm_firewall((.|\n)*)###'

Expand Down
5 changes: 5 additions & 0 deletions .github/labeler-pull-request-triage.yml
Expand Up @@ -239,6 +239,11 @@ service/event-hubs:
- any-glob-to-any-file:
- internal/services/eventhub/**/*

service/extendedlocation:
- changed-files:
- any-glob-to-any-file:
- internal/services/extendedlocation/**/*

service/firewall:
- changed-files:
- any-glob-to-any-file:
Expand Down
1 change: 1 addition & 0 deletions .teamcity/components/generated/services.kt
Expand Up @@ -53,6 +53,7 @@ var services = mapOf(
"elasticsan" to "ElasticSan",
"eventgrid" to "EventGrid",
"eventhub" to "EventHub",
"extendedlocation" to "ExtendedLocation",
"firewall" to "Firewall",
"fluidrelay" to "Fluid Relay",
"frontdoor" to "FrontDoor",
Expand Down
5 changes: 5 additions & 0 deletions internal/clients/client.go
Expand Up @@ -73,6 +73,7 @@ import (
elasticsan "github.com/hashicorp/terraform-provider-azurerm/internal/services/elasticsan/client"
eventgrid "github.com/hashicorp/terraform-provider-azurerm/internal/services/eventgrid/client"
eventhub "github.com/hashicorp/terraform-provider-azurerm/internal/services/eventhub/client"
extendedlocation "github.com/hashicorp/terraform-provider-azurerm/internal/services/extendedlocation/client"
fluidrelay "github.com/hashicorp/terraform-provider-azurerm/internal/services/fluidrelay/client"
frontdoor "github.com/hashicorp/terraform-provider-azurerm/internal/services/frontdoor/client"
graph "github.com/hashicorp/terraform-provider-azurerm/internal/services/graphservices/client"
Expand Down Expand Up @@ -210,6 +211,7 @@ type Client struct {
ElasticSan *elasticsan.Client
EventGrid *eventgrid_v2022_06_15.Client
Eventhub *eventhub.Client
ExtendedLocation *extendedlocation.Client
FluidRelay *fluidrelay_2022_05_26.Client
Frontdoor *frontdoor.Client
Graph *graph.Client
Expand Down Expand Up @@ -447,6 +449,9 @@ func (client *Client) Build(ctx context.Context, o *common.ClientOptions) error
if client.Eventhub, err = eventhub.NewClient(o); err != nil {
return fmt.Errorf("building clients for Eventhub: %+v", err)
}
if client.ExtendedLocation, err = extendedlocation.NewClient(o); err != nil {
return fmt.Errorf("building clients for ExtendedLocation: %+v", err)
}
if client.FluidRelay, err = fluidrelay.NewClient(o); err != nil {
return fmt.Errorf("building clients for FluidRelay: %+v", err)
}
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/services.go
Expand Up @@ -53,6 +53,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/services/elasticsan"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/eventgrid"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/eventhub"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/extendedlocation"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/firewall"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/fluidrelay"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/frontdoor"
Expand Down Expand Up @@ -170,6 +171,7 @@ func SupportedTypedServices() []sdk.TypedServiceRegistration {
domainservices.Registration{},
elasticsan.Registration{},
eventhub.Registration{},
extendedlocation.Registration{},
fluidrelay.Registration{},
graphservices.Registration{},
storagecache.Registration{},
Expand Down
26 changes: 26 additions & 0 deletions internal/services/extendedlocation/client/client.go
@@ -0,0 +1,26 @@
package client

import (
"fmt"

extendedLocation20210815 "github.com/hashicorp/go-azure-sdk/resource-manager/extendedlocation/2021-08-15"
"github.com/hashicorp/go-azure-sdk/sdk/client/resourcemanager"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

type Client struct {
*extendedLocation20210815.Client
}

func NewClient(o *common.ClientOptions) (*Client, error) {
client, err := extendedLocation20210815.NewClientWithBaseURI(o.Environment.ResourceManager, func(c *resourcemanager.Client) {
o.Configure(c, o.Authorizers.ResourceManager)
})
if err != nil {
return nil, fmt.Errorf("building clients for Network: %+v", err)
}

return &Client{
Client: client,
}, nil
}
138 changes: 138 additions & 0 deletions internal/services/extendedlocation/custom_location_data_source.go
@@ -0,0 +1,138 @@
package extendedlocation

import (
"context"
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/identity"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/extendedlocation/2021-08-15/customlocations"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
)

type CustomLocationDataSource struct{}

var _ sdk.DataSource = CustomLocationDataSource{}

type CustomLocationDataSourceModel struct {
ClusterExtensionIds []string `tfschema:"cluster_extension_ids"`
DisplayName string `tfschema:"display_name"`
HostResourceId string `tfschema:"host_resource_id"`
Identities []identity.ModelSystemAssigned `tfschema:"identities"`
Location string `tfschema:"location"`
Name string `tfschema:"name"`
Namespace string `tfschema:"namespace"`
ResourceGroupName string `tfschema:"resource_group_name"`
Tags map[string]interface{} `tfschema:"tags"`
}

func (r CustomLocationDataSource) ResourceType() string {
return "azurerm_custom_location"
}

func (r CustomLocationDataSource) ModelObject() interface{} {
return &CustomLocationDataSourceModel{}
}

func (r CustomLocationDataSource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"resource_group_name": commonschema.ResourceGroupNameForDataSource(),
}
}

func (r CustomLocationDataSource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"cluster_extension_ids": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},

"display_name": {
Type: pluginsdk.TypeString,
Computed: true,
},

"host_resource_id": {
Type: pluginsdk.TypeString,
Computed: true,
},

"identities": commonschema.SystemAssignedIdentityComputed(),

"location": {
Type: pluginsdk.TypeString,
Computed: true,
},

"namespace": {
Type: pluginsdk.TypeString,
Computed: true,
},

"tags": commonschema.TagsDataSource(),
}
}

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

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

id := customlocations.NewCustomLocationID(subscriptionId, model.ResourceGroupName, model.Name)

existing, err := client.Get(ctx, id)
if err != nil {
if response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("%s does not exist", id)
}

return fmt.Errorf("retrieving %s: %+v", id, err)
}

state := CustomLocationDataSourceModel{
Name: model.Name,
ResourceGroupName: model.ResourceGroupName,
}

if model := existing.Model; model != nil {
state.Location = location.Normalize(model.Location)
state.Tags = tags.Flatten(model.Tags)
state.Identities = identity.FlattenSystemAssignedToModel(model.Identity)

if props := model.Properties; props != nil {
state.ClusterExtensionIds = pointer.From(props.ClusterExtensionIds)
state.DisplayName = pointer.From(props.DisplayName)
state.HostResourceId = pointer.From(props.HostResourceId)
state.Namespace = pointer.From(props.Namespace)
}
}

metadata.SetID(id)

return metadata.Encode(&state)
},
}
}
@@ -0,0 +1,52 @@
package extendedlocation_test

import (
"fmt"
"os"
"testing"

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

type CustomLocationDataSource struct{}

const (
customLocationName = "ARM_TEST_CUSTOM_LOCATION_NAME"
customLocationResourceGroupName = "ARM_TEST_CUSTOM_LOCATION_RESOURCE_GROUP_NAME"
)

func TestAccCustomLocationDataSource_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_custom_location", "test")
d := CustomLocationDataSource{}

if os.Getenv(customLocationName) == "" || os.Getenv(customLocationResourceGroupName) == "" {
t.Skipf("Skipping test due to missing environment variables: %s, %s", customLocationName, customLocationResourceGroupName)
}

data.DataSourceTestInSequence(t, []acceptance.TestStep{
{
Config: d.basic(),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("location").IsNotEmpty(),
check.That(data.ResourceName).Key("host_resource_id").IsNotEmpty(),
check.That(data.ResourceName).Key("display_name").IsNotEmpty(),
check.That(data.ResourceName).Key("namespace").IsNotEmpty(),
check.That(data.ResourceName).Key("cluster_extension_ids.#").HasValue("2"),
),
},
})
}

func (d CustomLocationDataSource) basic() string {
return fmt.Sprintf(`
provider azurerm {
features {}
}

data "azurerm_custom_location" "test" {
name = %q
resource_group_name = %q
}
`, os.Getenv(customLocationName), os.Getenv(customLocationResourceGroupName))
}
31 changes: 31 additions & 0 deletions internal/services/extendedlocation/registration.go
@@ -0,0 +1,31 @@
package extendedlocation

import "github.com/hashicorp/terraform-provider-azurerm/internal/sdk"

var _ sdk.TypedServiceRegistration = Registration{}

type Registration struct{}

func (r Registration) AssociatedGitHubLabel() string {
return "service/extendedlocation"
}

func (Registration) Name() string {
return "ExtendedLocation"
}

func (Registration) DataSources() []sdk.DataSource {
return []sdk.DataSource{
CustomLocationDataSource{},
}
}

func (Registration) Resources() []sdk.Resource {
return []sdk.Resource{}
}

func (Registration) WebsiteCategories() []string {
return []string{
"Extended Location",
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/modules.txt
Expand Up @@ -519,6 +519,7 @@ github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2021-11-01/namespace
github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2021-11-01/networkrulesets
github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2021-11-01/schemaregistry
github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2022-01-01-preview/namespaces
github.com/hashicorp/go-azure-sdk/resource-manager/extendedlocation/2021-08-15
github.com/hashicorp/go-azure-sdk/resource-manager/extendedlocation/2021-08-15/customlocations
github.com/hashicorp/go-azure-sdk/resource-manager/fluidrelay/2022-05-26
github.com/hashicorp/go-azure-sdk/resource-manager/fluidrelay/2022-05-26/fluidrelaycontainers
Expand Down
1 change: 1 addition & 0 deletions website/allowed-subcategories
Expand Up @@ -51,6 +51,7 @@ Digital Twins
Disks
Elastic
Elastic SAN
Extended Location
Fluid Relay
Graph Services
HDInsight
Expand Down