-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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_extended_location_custom_location
#28066
base: main
Are you sure you want to change the base?
Changes from 3 commits
b60acba
a044c45
cc52604
fd264c7
432099f
efd0a00
a674616
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,158 @@ | ||||||||||||||||||||||||||||||||
package extendedlocation | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
import ( | ||||||||||||||||||||||||||||||||
"context" | ||||||||||||||||||||||||||||||||
"fmt" | ||||||||||||||||||||||||||||||||
"regexp" | ||||||||||||||||||||||||||||||||
"time" | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
"github.com/hashicorp/go-azure-helpers/lang/pointer" | ||||||||||||||||||||||||||||||||
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema" | ||||||||||||||||||||||||||||||||
"github.com/hashicorp/go-azure-sdk/resource-manager/extendedlocation/2021-08-15/customlocations" | ||||||||||||||||||||||||||||||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||||||||||||||||||||||||||||||||
"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" | ||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
var _ sdk.DataSource = CustomLocationDataSource{} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
type CustomLocationDataSource struct{} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
type CustomLocationDataSourceModel struct { | ||||||||||||||||||||||||||||||||
Name string `tfschema:"name"` | ||||||||||||||||||||||||||||||||
ResourceGroupName string `tfschema:"resource_group_name"` | ||||||||||||||||||||||||||||||||
Location string `tfschema:"location"` | ||||||||||||||||||||||||||||||||
Authentication []AuthModel `tfschema:"authentication"` | ||||||||||||||||||||||||||||||||
ClusterExtensionIds []string `tfschema:"cluster_extension_ids"` | ||||||||||||||||||||||||||||||||
DisplayName string `tfschema:"display_name"` | ||||||||||||||||||||||||||||||||
HostResourceId string `tfschema:"host_resource_id"` | ||||||||||||||||||||||||||||||||
HostType string `tfschema:"host_type"` | ||||||||||||||||||||||||||||||||
Namespace string `tfschema:"namespace"` | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
func (r CustomLocationDataSource) Arguments() map[string]*pluginsdk.Schema { | ||||||||||||||||||||||||||||||||
return map[string]*schema.Schema{ | ||||||||||||||||||||||||||||||||
"name": { | ||||||||||||||||||||||||||||||||
Type: pluginsdk.TypeString, | ||||||||||||||||||||||||||||||||
Required: true, | ||||||||||||||||||||||||||||||||
ValidateFunc: validation.StringMatch( | ||||||||||||||||||||||||||||||||
regexp.MustCompile(`^[A-Za-z\d.\-_]*[A-Za-z\d]$`), | ||||||||||||||||||||||||||||||||
"supported alphanumeric characters and periods, underscores, hyphens. Name should end with an alphanumeric character.", | ||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
"resource_group_name": commonschema.ResourceGroupNameForDataSource(), | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
func (r CustomLocationDataSource) Attributes() map[string]*pluginsdk.Schema { | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could these be ordered alphabetically? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||||||||||||||||||||||||||||||||
return map[string]*schema.Schema{ | ||||||||||||||||||||||||||||||||
"location": commonschema.LocationComputed(), | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
"namespace": { | ||||||||||||||||||||||||||||||||
Type: pluginsdk.TypeString, | ||||||||||||||||||||||||||||||||
Computed: true, | ||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
"host_resource_id": { | ||||||||||||||||||||||||||||||||
Type: pluginsdk.TypeString, | ||||||||||||||||||||||||||||||||
Computed: true, | ||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
"cluster_extension_ids": { | ||||||||||||||||||||||||||||||||
Type: pluginsdk.TypeList, | ||||||||||||||||||||||||||||||||
Computed: true, | ||||||||||||||||||||||||||||||||
Elem: &schema.Schema{ | ||||||||||||||||||||||||||||||||
Type: schema.TypeString, | ||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
"host_type": { | ||||||||||||||||||||||||||||||||
Type: pluginsdk.TypeString, | ||||||||||||||||||||||||||||||||
Computed: true, | ||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
"display_name": { | ||||||||||||||||||||||||||||||||
Type: pluginsdk.TypeString, | ||||||||||||||||||||||||||||||||
Computed: true, | ||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
"authentication": { | ||||||||||||||||||||||||||||||||
Type: pluginsdk.TypeList, | ||||||||||||||||||||||||||||||||
Computed: true, | ||||||||||||||||||||||||||||||||
Elem: &pluginsdk.Resource{ | ||||||||||||||||||||||||||||||||
Schema: map[string]*pluginsdk.Schema{ | ||||||||||||||||||||||||||||||||
"type": { | ||||||||||||||||||||||||||||||||
Type: pluginsdk.TypeString, | ||||||||||||||||||||||||||||||||
Computed: true, | ||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
"value": { | ||||||||||||||||||||||||||||||||
Type: pluginsdk.TypeString, | ||||||||||||||||||||||||||||||||
Computed: true, | ||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
func (r CustomLocationDataSource) ModelObject() interface{} { | ||||||||||||||||||||||||||||||||
return &CustomLocationDataSourceModel{} | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
func (r CustomLocationDataSource) ResourceType() string { | ||||||||||||||||||||||||||||||||
return "azurerm_extended_location_custom_location" | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
func (r CustomLocationDataSource) Read() sdk.ResourceFunc { | ||||||||||||||||||||||||||||||||
return sdk.ResourceFunc{ | ||||||||||||||||||||||||||||||||
Timeout: 5 * time.Minute, | ||||||||||||||||||||||||||||||||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||||||||||||||||||||||||||||||||
var model CustomLocationResourceModel | ||||||||||||||||||||||||||||||||
if err := metadata.Decode(&model); err != nil { | ||||||||||||||||||||||||||||||||
return err | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
subscriptionId := metadata.Client.Account.SubscriptionId | ||||||||||||||||||||||||||||||||
client := metadata.Client.ExtendedLocation.CustomLocationsClient | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
id := customlocations.NewCustomLocationID(subscriptionId, model.ResourceGroupName, model.Name) | ||||||||||||||||||||||||||||||||
resp, err := client.Get(ctx, id) | ||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||
return fmt.Errorf("reading %s: %+v", id, err) | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
state := CustomLocationDataSourceModel{ | ||||||||||||||||||||||||||||||||
Name: id.CustomLocationName, | ||||||||||||||||||||||||||||||||
ResourceGroupName: id.ResourceGroupName, | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
if model := resp.Model; model != nil { | ||||||||||||||||||||||||||||||||
state.Location = model.Location | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
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.HostType = string(pointer.From(props.HostType)) | ||||||||||||||||||||||||||||||||
state.Namespace = pointer.From(props.Namespace) | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
// API always returns an empty `authentication` block even it's not specified. Tracing the bug: https://github.com/Azure/azure-rest-api-specs/issues/30101 | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can remove this given that this is the datasource There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||||||||||||||||||||||||||||||||
if props.Authentication != nil && props.Authentication.Type != nil && props.Authentication.Value != nil { | ||||||||||||||||||||||||||||||||
state.Authentication = []AuthModel{ | ||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||
Type: pointer.From(props.Authentication.Type), | ||||||||||||||||||||||||||||||||
Value: pointer.From(props.Authentication.Value), | ||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
metadata.SetID(id) | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
return metadata.Encode(&state) | ||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package extendedlocation_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check" | ||
) | ||
|
||
type CustomLocationDataSource struct{} | ||
|
||
func TestAccCustomLocationDataSource_basic(t *testing.T) { | ||
data := acceptance.BuildTestData(t, "data.azurerm_extended_location_custom_location", "test") | ||
d := CustomLocationDataSource{} | ||
|
||
credential, privateKey, publicKey := CustomLocationResource{}.getCredentials(t) | ||
|
||
data.DataSourceTestInSequence(t, []acceptance.TestStep{ | ||
{ | ||
Config: d.basic(data, credential, privateKey, publicKey), | ||
Check: acceptance.ComposeTestCheckFunc( | ||
check.That(data.ResourceName).Key("location").IsNotEmpty(), | ||
check.That(data.ResourceName).Key("host_type").HasValue("Kubernetes"), | ||
check.That(data.ResourceName).Key("cluster_extension_ids.#").HasValue("1"), | ||
check.That(data.ResourceName).Key("display_name").IsNotEmpty(), | ||
check.That(data.ResourceName).Key("namespace").IsNotEmpty(), | ||
check.That(data.ResourceName).Key("host_resource_id").IsNotEmpty(), | ||
), | ||
}, | ||
}) | ||
} | ||
|
||
func (d CustomLocationDataSource) basic(data acceptance.TestData, credential string, privateKey string, publicKey string) string { | ||
return fmt.Sprintf(` | ||
%s | ||
|
||
data "azurerm_extended_location_custom_location" "test" { | ||
name = azurerm_extended_location_custom_location.test.name | ||
resource_group_name = azurerm_extended_location_custom_location.test.resource_group_name | ||
} | ||
`, CustomLocationResource{}.complete(data, credential, privateKey, publicKey)) | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could we retain the original tests in addition to new ones so we can ensure the original resource is still working as expected? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why
extended_location_custom_location\W+
twice?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The file is generated by the
go generate ./internal/provider
command. The duplicates should be handled within theinternal/tools/generator-services/main.go
file now.