From fbc27390675bb854d81043adb09936fa7c31ddf0 Mon Sep 17 00:00:00 2001 From: sreallymatt <106555974+sreallymatt@users.noreply.github.com> Date: Fri, 13 Dec 2024 06:51:49 -0700 Subject: [PATCH 1/3] Add kubernetes_fleet_manager datasource --- .github/labeler-issue-triage.yml | 2 +- .../kubernetes_fleet_manager_data_source.go | 88 +++++++++++++++++++ ...bernetes_fleet_manager_data_source_test.go | 57 ++++++++++++ internal/services/containers/registration.go | 7 +- .../d/kubernetes_fleet_manager.html.markdown | 48 ++++++++++ 5 files changed, 198 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..3d7941d72d55 --- /dev/null +++ b/internal/services/containers/kubernetes_fleet_manager_data_source_test.go @@ -0,0 +1,57 @@ +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(` +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "test" { + location = %q + name = "acctestrg-%d" +} + +resource "azurerm_kubernetes_fleet_manager" "test" { + location = azurerm_resource_group.test.location + name = "acctestkfm-%s" + resource_group_name = azurerm_resource_group.test.name + tags = { + environment = "terraform-acctests" + some_key = "some-value" + } +} + +data "azurerm_kubernetes_fleet_manager" "test" { + name = azurerm_kubernetes_fleet_manager.test.name + resource_group_name = azurerm_kubernetes_fleet_manager.test.resource_group_name +} +`, data.Locations.Primary, data.RandomInteger, data.RandomString) +} 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..8481817bc21e --- /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. \ No newline at end of file From d1fd1abcbefbfa0c5f0819bf8fcc117ad6b499f4 Mon Sep 17 00:00:00 2001 From: sreallymatt <106555974+sreallymatt@users.noreply.github.com> Date: Fri, 13 Dec 2024 08:21:38 -0700 Subject: [PATCH 2/3] fix formatting --- .../kubernetes_fleet_manager_data_source_test.go | 12 ++++++------ .../docs/d/kubernetes_fleet_manager.html.markdown | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/internal/services/containers/kubernetes_fleet_manager_data_source_test.go b/internal/services/containers/kubernetes_fleet_manager_data_source_test.go index 3d7941d72d55..ad5162fd84b2 100644 --- a/internal/services/containers/kubernetes_fleet_manager_data_source_test.go +++ b/internal/services/containers/kubernetes_fleet_manager_data_source_test.go @@ -35,22 +35,22 @@ provider "azurerm" { } resource "azurerm_resource_group" "test" { - location = %q - name = "acctestrg-%d" + location = "%s" + name = "acctestrg-%d" } resource "azurerm_kubernetes_fleet_manager" "test" { - location = azurerm_resource_group.test.location - name = "acctestkfm-%s" + location = azurerm_resource_group.test.location + name = "acctestkfm-%s" resource_group_name = azurerm_resource_group.test.name tags = { environment = "terraform-acctests" - some_key = "some-value" + some_key = "some-value" } } data "azurerm_kubernetes_fleet_manager" "test" { - name = azurerm_kubernetes_fleet_manager.test.name + name = azurerm_kubernetes_fleet_manager.test.name resource_group_name = azurerm_kubernetes_fleet_manager.test.resource_group_name } `, data.Locations.Primary, data.RandomInteger, data.RandomString) diff --git a/website/docs/d/kubernetes_fleet_manager.html.markdown b/website/docs/d/kubernetes_fleet_manager.html.markdown index 8481817bc21e..4196336ff0b5 100644 --- a/website/docs/d/kubernetes_fleet_manager.html.markdown +++ b/website/docs/d/kubernetes_fleet_manager.html.markdown @@ -14,7 +14,7 @@ Use this data source to access information about an existing Kubernetes Fleet Ma ```hcl data "azurerm_kubernetes_fleet_manager" "example" { - name = "example" + name = "example" resource_group_name = "example-resource-group" } @@ -45,4 +45,4 @@ In addition to the Arguments listed above - the following Attributes are exporte 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. \ No newline at end of file +* `read` - (Defaults to 5 minutes) Used when retrieving the Kubernetes Fleet Manager. From c708abd8d2921c45c0eef259b50f955de4fb1910 Mon Sep 17 00:00:00 2001 From: sreallymatt <106555974+sreallymatt@users.noreply.github.com> Date: Fri, 13 Dec 2024 08:59:23 -0700 Subject: [PATCH 3/3] Update internal/services/containers/kubernetes_fleet_manager_data_source_test.go Co-authored-by: stephybun --- ...bernetes_fleet_manager_data_source_test.go | 21 ++----------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/internal/services/containers/kubernetes_fleet_manager_data_source_test.go b/internal/services/containers/kubernetes_fleet_manager_data_source_test.go index ad5162fd84b2..8196f94c641d 100644 --- a/internal/services/containers/kubernetes_fleet_manager_data_source_test.go +++ b/internal/services/containers/kubernetes_fleet_manager_data_source_test.go @@ -30,28 +30,11 @@ func TestAccKubernetesFleetManagerDataSource_basic(t *testing.T) { func (KubernetesFleetManagerDataSource) basic(data acceptance.TestData) string { return fmt.Sprintf(` -provider "azurerm" { - features {} -} - -resource "azurerm_resource_group" "test" { - location = "%s" - name = "acctestrg-%d" -} - -resource "azurerm_kubernetes_fleet_manager" "test" { - location = azurerm_resource_group.test.location - name = "acctestkfm-%s" - resource_group_name = azurerm_resource_group.test.name - tags = { - environment = "terraform-acctests" - some_key = "some-value" - } -} +%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 } -`, data.Locations.Primary, data.RandomInteger, data.RandomString) +`, KubernetesFleetManagerTestResource{}.complete(data)) }