From 864917114328364537ea775ee239cfc7ded97b5a Mon Sep 17 00:00:00 2001 From: sreallymatt <106555974+sreallymatt@users.noreply.github.com> Date: Fri, 13 Dec 2024 09:16:45 -0700 Subject: [PATCH] New Data Source - `azurerm_kubernetes_fleet_manager` (#28278) * Add kubernetes_fleet_manager datasource * fix formatting * Update internal/services/containers/kubernetes_fleet_manager_data_source_test.go Co-authored-by: stephybun --------- Co-authored-by: stephybun --- .github/labeler-issue-triage.yml | 2 +- .../kubernetes_fleet_manager_data_source.go | 88 +++++++++++++++++++ ...bernetes_fleet_manager_data_source_test.go | 40 +++++++++ internal/services/containers/registration.go | 7 +- .../d/kubernetes_fleet_manager.html.markdown | 48 ++++++++++ 5 files changed, 181 insertions(+), 4 deletions(-) create mode 100644 internal/services/containers/kubernetes_fleet_manager_data_source.go create mode 100644 internal/services/containers/kubernetes_fleet_manager_data_source_test.go create mode 100644 website/docs/d/kubernetes_fleet_manager.html.markdown diff --git a/.github/labeler-issue-triage.yml b/.github/labeler-issue-triage.yml index 7c2cb3082606..b0211680500d 100644 --- a/.github/labeler-issue-triage.yml +++ b/.github/labeler-issue-triage.yml @@ -354,4 +354,4 @@ service/vmware: - '### (|New or )Affected Resource\(s\)\/Data Source\(s\)((.|\n)*)azurerm_(vmware_cluster\W+|vmware_express_route_authorization\W+|vmware_netapp_volume_attachment\W+|vmware_private_cloud\W+|voice_services_communications_gateway\W+|voice_services_communications_gateway_test_line\W+)((.|\n)*)###' service/workloads: - - '### (|New or )Affected Resource\(s\)\/Data Source\(s\)((.|\n)*)azurerm_(chaos_studio_|container_connected_registry\W+|container_registry_cache_rule\W+|container_registry_cache_rule\W+|container_registry_task\W+|container_registry_task_schedule_run_now\W+|container_registry_token_password\W+|kubernetes_cluster_extension\W+|kubernetes_cluster_trusted_access_role_binding\W+|kubernetes_fleet_manager\W+|kubernetes_fleet_member\W+|kubernetes_fleet_update_run\W+|kubernetes_fleet_update_strategy\W+|kubernetes_flux_configuration\W+|kubernetes_node_pool_snapshot\W+|workloads_sap_)((.|\n)*)###' + - '### (|New or )Affected Resource\(s\)\/Data Source\(s\)((.|\n)*)azurerm_(chaos_studio_|container_connected_registry\W+|container_registry_cache_rule\W+|container_registry_cache_rule\W+|container_registry_task\W+|container_registry_task_schedule_run_now\W+|container_registry_token_password\W+|kubernetes_cluster_extension\W+|kubernetes_cluster_trusted_access_role_binding\W+|kubernetes_fleet_manager\W+|kubernetes_fleet_manager\W+|kubernetes_fleet_member\W+|kubernetes_fleet_update_run\W+|kubernetes_fleet_update_strategy\W+|kubernetes_flux_configuration\W+|kubernetes_node_pool_snapshot\W+|workloads_sap_)((.|\n)*)###' diff --git a/internal/services/containers/kubernetes_fleet_manager_data_source.go b/internal/services/containers/kubernetes_fleet_manager_data_source.go new file mode 100644 index 000000000000..f3fb9817b8f1 --- /dev/null +++ b/internal/services/containers/kubernetes_fleet_manager_data_source.go @@ -0,0 +1,88 @@ +package containers + +import ( + "context" + "fmt" + "time" + + "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/containerservice/2024-04-01/fleets" + "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 = KubernetesFleetManagerDataSource{} + +type KubernetesFleetManagerDataSource struct{} + +type KubernetesFleetManagerDataSourceModel struct { + Location string `tfschema:"location"` + Name string `tfschema:"name"` + ResourceGroupName string `tfschema:"resource_group_name"` + Tags map[string]interface{} `tfschema:"tags"` +} + +func (KubernetesFleetManagerDataSource) 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 (KubernetesFleetManagerDataSource) Attributes() map[string]*pluginsdk.Schema { + return map[string]*pluginsdk.Schema{ + "location": commonschema.LocationComputed(), + "tags": commonschema.TagsDataSource(), + } +} + +func (KubernetesFleetManagerDataSource) ModelObject() interface{} { + return &KubernetesFleetManagerDataSourceModel{} +} + +func (KubernetesFleetManagerDataSource) ResourceType() string { + return "azurerm_kubernetes_fleet_manager" +} + +func (KubernetesFleetManagerDataSource) Read() sdk.ResourceFunc { + return sdk.ResourceFunc{ + Timeout: 5 * time.Minute, + Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { + client := metadata.Client.ContainerService.V20231015.Fleets + subscriptionId := metadata.Client.Account.SubscriptionId + + var state KubernetesFleetManagerDataSourceModel + if err := metadata.Decode(&state); err != nil { + return fmt.Errorf("decoding: %+v", err) + } + + id := fleets.NewFleetID(subscriptionId, state.ResourceGroupName, state.Name) + + resp, err := client.Get(ctx, id) + if err != nil { + if response.WasNotFound(resp.HttpResponse) { + return fmt.Errorf("%s was not found", id) + } + + return fmt.Errorf("retrieving %s: %+v", id, err) + } + + metadata.SetID(id) + + if model := resp.Model; model != nil { + state.Location = location.Normalize(model.Location) + state.Tags = tags.Flatten(model.Tags) + } + + return metadata.Encode(&state) + }, + } +} diff --git a/internal/services/containers/kubernetes_fleet_manager_data_source_test.go b/internal/services/containers/kubernetes_fleet_manager_data_source_test.go new file mode 100644 index 000000000000..8196f94c641d --- /dev/null +++ b/internal/services/containers/kubernetes_fleet_manager_data_source_test.go @@ -0,0 +1,40 @@ +package containers_test + +import ( + "fmt" + "testing" + + "github.com/hashicorp/go-azure-helpers/resourcemanager/location" + "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" + "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check" +) + +type KubernetesFleetManagerDataSource struct{} + +func TestAccKubernetesFleetManagerDataSource_basic(t *testing.T) { + data := acceptance.BuildTestData(t, "data.azurerm_kubernetes_fleet_manager", "test") + d := KubernetesFleetManagerDataSource{} + + data.DataSourceTest(t, []acceptance.TestStep{ + { + Config: d.basic(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).Key("location").HasValue(location.Normalize(data.Locations.Primary)), + check.That(data.ResourceName).Key("tags.%").HasValue("2"), + check.That(data.ResourceName).Key("tags.environment").HasValue("terraform-acctests"), + check.That(data.ResourceName).Key("tags.some_key").HasValue("some-value"), + ), + }, + }) +} + +func (KubernetesFleetManagerDataSource) basic(data acceptance.TestData) string { + return fmt.Sprintf(` +%s + +data "azurerm_kubernetes_fleet_manager" "test" { + name = azurerm_kubernetes_fleet_manager.test.name + resource_group_name = azurerm_kubernetes_fleet_manager.test.resource_group_name +} +`, KubernetesFleetManagerTestResource{}.complete(data)) +} diff --git a/internal/services/containers/registration.go b/internal/services/containers/registration.go index 32ff3d85aa5a..2cc036b54d67 100644 --- a/internal/services/containers/registration.go +++ b/internal/services/containers/registration.go @@ -60,8 +60,9 @@ func (r Registration) SupportedResources() map[string]*pluginsdk.Resource { func (r Registration) DataSources() []sdk.DataSource { dataSources := []sdk.DataSource{ - KubernetesNodePoolSnapshotDataSource{}, ContainerRegistryCacheRuleDataSource{}, + KubernetesFleetManagerDataSource{}, + KubernetesNodePoolSnapshotDataSource{}, } dataSources = append(dataSources, r.autoRegistration.DataSources()...) return dataSources @@ -69,16 +70,16 @@ func (r Registration) DataSources() []sdk.DataSource { func (r Registration) Resources() []sdk.Resource { resources := []sdk.Resource{ + ContainerConnectedRegistryResource{}, ContainerRegistryCacheRule{}, ContainerRegistryTaskResource{}, ContainerRegistryTaskScheduleResource{}, ContainerRegistryTokenPasswordResource{}, - ContainerConnectedRegistryResource{}, KubernetesClusterExtensionResource{}, - KubernetesFluxConfigurationResource{}, KubernetesFleetManagerResource{}, KubernetesFleetUpdateRunResource{}, KubernetesFleetUpdateStrategyResource{}, + KubernetesFluxConfigurationResource{}, } resources = append(resources, r.autoRegistration.Resources()...) return resources diff --git a/website/docs/d/kubernetes_fleet_manager.html.markdown b/website/docs/d/kubernetes_fleet_manager.html.markdown new file mode 100644 index 000000000000..4196336ff0b5 --- /dev/null +++ b/website/docs/d/kubernetes_fleet_manager.html.markdown @@ -0,0 +1,48 @@ +--- +subcategory: "Container" +layout: "azurerm" +page_title: "Azure Resource Manager: Data Source: azurerm_kubernetes_fleet_manager" +description: |- + Gets information about an existing Kubernetes Fleet Manager. +--- + +# Data Source: azurerm_kubernetes_fleet_manager + +Use this data source to access information about an existing Kubernetes Fleet Manager. + +## Example Usage + +```hcl +data "azurerm_kubernetes_fleet_manager" "example" { + name = "example" + resource_group_name = "example-resource-group" +} + +output "id" { + value = data.azurerm_kubernetes_fleet_manager.example.id +} +``` + +## Arguments Reference + +The following arguments are supported: + +* `name` - (Required) The name of this Kubernetes Fleet Manager. + +* `resource_group_name` - (Required) The name of the Resource Group where the Kubernetes Fleet Manager exists. + +## Attributes Reference + +In addition to the Arguments listed above - the following Attributes are exported: + +* `id` - The ID of the Kubernetes Fleet Manager. + +* `location` - The Azure Region where the Kubernetes Fleet Manager exists. + +* `tags` - A mapping of tags assigned to the Kubernetes Fleet Manager. + +## 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 Kubernetes Fleet Manager.