-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Showing
30 changed files
with
1,100 additions
and
9 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
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,151 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package subscription | ||
|
||
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/commonids" | ||
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema" | ||
"github.com/hashicorp/go-azure-helpers/resourcemanager/location" | ||
resourcesSubscription "github.com/hashicorp/go-azure-sdk/resource-manager/resources/2022-12-01/subscriptions" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" | ||
) | ||
|
||
var _ sdk.DataSource = LocationDataSource{} | ||
|
||
type LocationDataSource struct{} | ||
|
||
type LocationDataSourceModel struct { | ||
Location string `tfschema:"location"` | ||
DisplayName string `tfschema:"display_name"` | ||
ZoneMappings []LocationZoneMapping `tfschema:"zone_mappings"` | ||
} | ||
|
||
type LocationZoneMapping struct { | ||
LogicalZone string `tfschema:"logical_zone"` | ||
PhysicalZone string `tfschema:"physical_zone"` | ||
} | ||
|
||
func (r LocationDataSource) ResourceType() string { | ||
return "azurerm_location" | ||
} | ||
|
||
func (r LocationDataSource) ModelObject() interface{} { | ||
return &LocationDataSourceModel{} | ||
} | ||
|
||
func (r LocationDataSource) Arguments() map[string]*pluginsdk.Schema { | ||
return map[string]*pluginsdk.Schema{ | ||
"location": commonschema.LocationWithoutForceNew(), | ||
} | ||
} | ||
|
||
func (r LocationDataSource) Attributes() map[string]*pluginsdk.Schema { | ||
return map[string]*pluginsdk.Schema{ | ||
"display_name": { | ||
Type: pluginsdk.TypeString, | ||
Computed: true, | ||
}, | ||
"zone_mappings": { | ||
Type: pluginsdk.TypeList, | ||
Computed: true, | ||
Elem: &pluginsdk.Resource{ | ||
Schema: map[string]*pluginsdk.Schema{ | ||
"logical_zone": { | ||
Type: pluginsdk.TypeString, | ||
Computed: true, | ||
}, | ||
"physical_zone": { | ||
Type: pluginsdk.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (r LocationDataSource) Read() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
Timeout: 5 * time.Minute, | ||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
client := metadata.Client.Subscription.SubscriptionsClient | ||
subscriptionId := metadata.Client.Account.SubscriptionId | ||
|
||
id := commonids.NewSubscriptionID(subscriptionId) | ||
resp, err := client.ListLocations(ctx, id, resourcesSubscription.DefaultListLocationsOperationOptions()) | ||
if err != nil { | ||
if response.WasNotFound(resp.HttpResponse) { | ||
return fmt.Errorf("%s was not found", id) | ||
} | ||
|
||
return fmt.Errorf("retrieving %s: %+v", id, err) | ||
} | ||
|
||
if resp.Model == nil { | ||
return fmt.Errorf("retrieving %s: model was nil", id) | ||
} | ||
|
||
if resp.Model.Value == nil { | ||
return fmt.Errorf("retrieving %s: model value was nil", id) | ||
} | ||
|
||
var model LocationDataSourceModel | ||
if err := metadata.Decode(&model); err != nil { | ||
return fmt.Errorf("decoding: %+v", err) | ||
} | ||
|
||
normalizedLocation := location.Normalize(model.Location) | ||
|
||
locationValue, err := getLocation(normalizedLocation, resp.Model.Value) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var state LocationDataSourceModel | ||
state.ZoneMappings = flattenZonesMapping(locationValue) | ||
state.DisplayName = pointer.From(locationValue.DisplayName) | ||
state.Location = normalizedLocation | ||
|
||
metadata.ResourceData.SetId(fmt.Sprintf("%s/locations/%s", id.ID(), normalizedLocation)) | ||
|
||
return metadata.Encode(&state) | ||
}, | ||
} | ||
} | ||
|
||
func getLocation(location string, input *[]resourcesSubscription.Location) (*resourcesSubscription.Location, error) { | ||
for _, item := range *input { | ||
if pointer.From(item.Name) == location && pointer.From(item.Metadata.RegionType) == "Physical" { | ||
return &item, nil | ||
} | ||
} | ||
|
||
return nil, fmt.Errorf("no location was found for %q", location) | ||
} | ||
|
||
func flattenZonesMapping(location *resourcesSubscription.Location) (zoneMappings []LocationZoneMapping) { | ||
zoneMappings = make([]LocationZoneMapping, 0) | ||
|
||
if location == nil || location.AvailabilityZoneMappings == nil { | ||
return zoneMappings | ||
} | ||
|
||
for _, zoneMapping := range *location.AvailabilityZoneMappings { | ||
locationZoneMapping := LocationZoneMapping{ | ||
LogicalZone: pointer.From(zoneMapping.LogicalZone), | ||
PhysicalZone: pointer.From(zoneMapping.PhysicalZone), | ||
} | ||
|
||
zoneMappings = append(zoneMappings, locationZoneMapping) | ||
} | ||
|
||
return zoneMappings | ||
} |
57 changes: 57 additions & 0 deletions
57
internal/services/subscription/location_data_source_test.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,57 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package subscription_test | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check" | ||
) | ||
|
||
type LocationsDataSource struct{} | ||
|
||
func TestAccLocationDataSource_NonExistingRegion(t *testing.T) { | ||
data := acceptance.BuildTestData(t, "data.azurerm_location", "test") | ||
|
||
data.DataSourceTest(t, []acceptance.TestStep{ | ||
{ | ||
Config: LocationsDataSource{}.basic("not-existing-region"), | ||
ExpectError: regexp.MustCompile("\"not-existing-region\" was not found in the list of supported Azure Locations"), | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccLocationDataSource_westUS(t *testing.T) { | ||
data := acceptance.BuildTestData(t, "data.azurerm_location", "test") | ||
|
||
data.DataSourceTest(t, []acceptance.TestStep{ | ||
{ | ||
Config: LocationsDataSource{}.basic("westus"), | ||
Check: acceptance.ComposeTestCheckFunc( | ||
check.That(data.ResourceName).Key("display_name").HasValue("West US"), | ||
check.That(data.ResourceName).Key("zone_mappings.0.logical_zone").HasValue("1"), | ||
check.That(data.ResourceName).Key("zone_mappings.1.logical_zone").HasValue("2"), | ||
check.That(data.ResourceName).Key("zone_mappings.2.logical_zone").HasValue("3"), | ||
check.That(data.ResourceName).Key("zone_mappings.0.physical_zone").IsNotEmpty(), | ||
check.That(data.ResourceName).Key("zone_mappings.1.physical_zone").IsNotEmpty(), | ||
check.That(data.ResourceName).Key("zone_mappings.2.physical_zone").IsNotEmpty(), | ||
), | ||
}, | ||
}) | ||
} | ||
|
||
func (d LocationsDataSource) basic(location string) string { | ||
return fmt.Sprintf(` | ||
provider "azurerm" { | ||
features {} | ||
} | ||
data "azurerm_location" "test" { | ||
location = "%s" | ||
} | ||
`, location) | ||
} |
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
90 changes: 90 additions & 0 deletions
90
...corp/go-azure-sdk/resource-manager/resources/2022-12-01/subscriptions/README.md
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.