-
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_stack_hci_cluster
#24032
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
132 changes: 132 additions & 0 deletions
132
internal/services/azurestackhci/stack_hci_cluster_data_source.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,132 @@ | ||||||
package azurestackhci | ||||||
|
||||||
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/location" | ||||||
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags" | ||||||
"github.com/hashicorp/go-azure-sdk/resource-manager/azurestackhci/2023-08-01/clusters" | ||||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||||||
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk" | ||||||
autoParse "github.com/hashicorp/terraform-provider-azurerm/internal/services/automanage/parse" | ||||||
"github.com/hashicorp/terraform-provider-azurerm/internal/services/azurestackhci/validate" | ||||||
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" | ||||||
"github.com/hashicorp/terraform-provider-azurerm/utils" | ||||||
) | ||||||
|
||||||
var _ sdk.DataSource = StackHCIClusterDataSource{} | ||||||
|
||||||
type StackHCIClusterDataSource struct{} | ||||||
|
||||||
func (r StackHCIClusterDataSource) ResourceType() string { | ||||||
return "azurerm_stack_hci_cluster" | ||||||
} | ||||||
|
||||||
func (r StackHCIClusterDataSource) ModelObject() interface{} { | ||||||
return &StackHCIClusterDataSourceModel{} | ||||||
} | ||||||
|
||||||
type StackHCIClusterDataSourceModel struct { | ||||||
Name string `tfschema:"name"` | ||||||
ResourceGroupName string `tfschema:"resource_group_name"` | ||||||
Location string `tfschema:"location"` | ||||||
ClientId string `tfschema:"client_id"` | ||||||
TenantId string `tfschema:"tenant_id"` | ||||||
AutomanageConfigurationId string `tfschema:"automanage_configuration_id"` | ||||||
Tags map[string]interface{} `tfschema:"tags"` | ||||||
} | ||||||
|
||||||
func (r StackHCIClusterDataSource) Arguments() map[string]*schema.Schema { | ||||||
return map[string]*schema.Schema{ | ||||||
"name": { | ||||||
Type: pluginsdk.TypeString, | ||||||
Required: true, | ||||||
ForceNew: true, | ||||||
ValidateFunc: validate.ClusterName, | ||||||
}, | ||||||
|
||||||
"resource_group_name": commonschema.ResourceGroupName(), | ||||||
} | ||||||
} | ||||||
|
||||||
func (r StackHCIClusterDataSource) Attributes() map[string]*schema.Schema { | ||||||
return map[string]*schema.Schema{ | ||||||
"location": commonschema.LocationComputed(), | ||||||
|
||||||
"client_id": { | ||||||
Type: pluginsdk.TypeString, | ||||||
Computed: true, | ||||||
}, | ||||||
|
||||||
"tenant_id": { | ||||||
Type: pluginsdk.TypeString, | ||||||
Computed: true, | ||||||
}, | ||||||
|
||||||
"automanage_configuration_id": { | ||||||
Type: pluginsdk.TypeString, | ||||||
Computed: true, | ||||||
}, | ||||||
|
||||||
"tags": commonschema.TagsDataSource(), | ||||||
} | ||||||
} | ||||||
|
||||||
func (r StackHCIClusterDataSource) Read() sdk.ResourceFunc { | ||||||
return sdk.ResourceFunc{ | ||||||
Timeout: 5 * time.Minute, | ||||||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||||||
client := metadata.Client.AzureStackHCI.Clusters | ||||||
subscriptionId := metadata.Client.Account.SubscriptionId | ||||||
hciAssignmentClient := metadata.Client.Automanage.HCIAssignmentClient | ||||||
|
||||||
var cluster StackHCIClusterDataSourceModel | ||||||
if err := metadata.Decode(&cluster); err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
id := clusters.NewClusterID(subscriptionId, cluster.ResourceGroupName, cluster.Name) | ||||||
|
||||||
existing, err := client.Get(ctx, id) | ||||||
if err != nil { | ||||||
if response.WasNotFound(existing.HttpResponse) { | ||||||
return fmt.Errorf("%s was not found", id) | ||||||
} | ||||||
return fmt.Errorf("reading %s: %+v", id, err) | ||||||
} | ||||||
|
||||||
if model := existing.Model; model != nil { | ||||||
cluster.Location = location.Normalize(model.Location) | ||||||
cluster.Tags = tags.Flatten(model.Tags) | ||||||
|
||||||
if props := model.Properties; props != nil { | ||||||
cluster.ClientId = pointer.From(props.AadClientId) | ||||||
cluster.TenantId = pointer.From(props.AadTenantId) | ||||||
|
||||||
assignmentResp, err := hciAssignmentClient.Get(ctx, id.ResourceGroupName, id.ClusterName, "default") | ||||||
if err != nil && !utils.ResponseWasNotFound(assignmentResp.Response) { | ||||||
return err | ||||||
} | ||||||
configId := "" | ||||||
if !utils.ResponseWasNotFound(assignmentResp.Response) && assignmentResp.Properties != nil && assignmentResp.Properties.ConfigurationProfile != nil { | ||||||
automanageConfigId, err := autoParse.AutomanageConfigurationID(*assignmentResp.Properties.ConfigurationProfile) | ||||||
if err != nil { | ||||||
return 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
|
||||||
} | ||||||
configId = automanageConfigId.ID() | ||||||
} | ||||||
cluster.AutomanageConfigurationId = configId | ||||||
} | ||||||
} | ||||||
|
||||||
metadata.SetID(id) | ||||||
|
||||||
return metadata.Encode(&cluster) | ||||||
}, | ||||||
} | ||||||
} |
38 changes: 38 additions & 0 deletions
38
internal/services/azurestackhci/stack_hci_cluster_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,38 @@ | ||
package azurestackhci_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check" | ||
) | ||
|
||
type StackHCIClusterDataSource struct{} | ||
|
||
func TestAccStackHCIClusterDataSource_basic(t *testing.T) { | ||
data := acceptance.BuildTestData(t, "data.azurerm_stack_hci_cluster", "test") | ||
r := StackHCIClusterDataSource{} | ||
|
||
data.DataSourceTest(t, []acceptance.TestStep{ | ||
{ | ||
Config: r.basic(data), | ||
Check: acceptance.ComposeTestCheckFunc( | ||
check.That(data.ResourceName).Key("location").IsNotEmpty(), | ||
check.That(data.ResourceName).Key("client_id").IsNotEmpty(), | ||
check.That(data.ResourceName).Key("tenant_id").IsNotEmpty(), | ||
), | ||
}, | ||
}) | ||
} | ||
|
||
func (d StackHCIClusterDataSource) basic(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
%s | ||
|
||
data "azurerm_stack_hci_cluster" "test" { | ||
name = azurerm_stack_hci_cluster.test.name | ||
resource_group_name = azurerm_stack_hci_cluster.test.resource_group_name | ||
} | ||
`, StackHCIClusterResource{}.basic(data)) | ||
} |
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,64 @@ | ||||||||||||||
--- | ||||||||||||||
subcategory: "Azure Stack HCI" | ||||||||||||||
layout: "azurerm" | ||||||||||||||
page_title: "Azure Resource Manager: azurerm_stack_hci_cluster" | ||||||||||||||
description: |- | ||||||||||||||
Gets information about an existing Azure Stack HCI Cluster. | ||||||||||||||
--- | ||||||||||||||
|
||||||||||||||
# Data Source: azurerm_stack_hci_cluster | ||||||||||||||
|
||||||||||||||
Use this data source to access information about an existing Azure Stack HCI Cluster instance. | ||||||||||||||
|
||||||||||||||
## Example Usage | ||||||||||||||
|
||||||||||||||
```hcl | ||||||||||||||
data "azurerm_stack_hci_cluster" "example" { | ||||||||||||||
name = "existing" | ||||||||||||||
resource_group_name = "existing" | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
output "id" { | ||||||||||||||
value = data.azurerm_stack_hci_cluster.example.id | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
output "location" { | ||||||||||||||
value = data.azurerm_stack_hci_cluster.example.location | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
output "client_id" { | ||||||||||||||
value = data.azurerm_stack_hci_cluster.example.client_id | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
## Arguments Reference | ||||||||||||||
|
||||||||||||||
The following arguments are supported: | ||||||||||||||
|
||||||||||||||
* `name` - (Required) The name which should be used for this Azure Stack HCI Cluster. Changing this forces a new resource to be created. | ||||||||||||||
|
||||||||||||||
* `resource_group_name` - (Required) The name of the Resource Group where the Azure Stack HCI Cluster should exist. Changing this forces a new resource to be created. | ||||||||||||||
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
|
||||||||||||||
|
||||||||||||||
## Attributes Reference | ||||||||||||||
|
||||||||||||||
In addition to the Arguments listed above - the following Attributes are exported: | ||||||||||||||
|
||||||||||||||
* `id` - The ID of the Azure Stack HCI Cluster. | ||||||||||||||
|
||||||||||||||
* `location` - The Azure Region where the Azure Stack HCI Cluster exists. | ||||||||||||||
|
||||||||||||||
* `client_id` - The Client ID of the Azure Active Directory used by the Azure Stack HCI Cluster. | ||||||||||||||
|
||||||||||||||
* `tenant_id` - The Tenant ID of the Azure Active Directory used by the Azure Stack HCI Cluster. | ||||||||||||||
|
||||||||||||||
* `tags` - A mapping of tags assigned to the Azure Stack HCI Cluster. | ||||||||||||||
|
||||||||||||||
* `automanage_configuration_id` - The ID of the Automanage Configuration assigned to the Azure Stack HCI Cluster. | ||||||||||||||
|
||||||||||||||
## Timeouts | ||||||||||||||
|
||||||||||||||
The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/language/resources/syntax#operation-timeouts) for certain actions: | ||||||||||||||
|
||||||||||||||
* `read` - (Defaults to 5 minutes) Used when retrieving the Azure Stack HCI Cluster. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
we can remove
ForceNew
here since this is a data source