diff --git a/docs/data-sources/cloud_provider_azure_credential.md b/docs/data-sources/cloud_provider_azure_credential.md index 31957a481..4fc9f88ac 100644 --- a/docs/data-sources/cloud_provider_azure_credential.md +++ b/docs/data-sources/cloud_provider_azure_credential.md @@ -34,16 +34,6 @@ resource "grafana_cloud_provider_azure_credential" "test" { data "grafana_cloud_provider_azure_credential" "test" { stack_id = grafana_cloud_provider_azure_credential.test.stack_id resource_id = grafana_cloud_provider_azure_credential.test.resource_id - - resource_discovery_tag_filter { - key = grafana_cloud_provider_azure_credential.test.resource_discovery_tag_filter[0].key - value = grafana_cloud_provider_azure_credential.test.resource_discovery_tag_filter[0].value - } - - resource_discovery_tag_filter { - key = grafana_cloud_provider_azure_credential.test.resource_discovery_tag_filter[1].key - value = grafana_cloud_provider_azure_credential.test.resource_discovery_tag_filter[1].value - } } ``` @@ -55,22 +45,19 @@ data "grafana_cloud_provider_azure_credential" "test" { - `resource_id` (String) The ID given by the Grafana Cloud Provider API to this Azure Credential resource. - `stack_id` (String) The StackID of the Grafana Cloud instance. Part of the Terraform Resource ID. -### Optional - -- `resource_discovery_tag_filter` (Block List) The list of tag filters to apply to resources. (see [below for nested schema](#nestedblock--resource_discovery_tag_filter)) - ### Read-Only - `client_id` (String) The client ID of the Azure Credential. - `client_secret` (String, Sensitive) The client secret of the Azure Credential. - `id` (String) The Terraform Resource ID. This has the format "{{ stack_id }}:{{ resource_id }}". - `name` (String) The name of the Azure Credential. +- `resource_discovery_tag_filter` (Block List) The list of tag filters to apply to resources. (see [below for nested schema](#nestedblock--resource_discovery_tag_filter)) - `tenant_id` (String) The tenant ID of the Azure Credential. ### Nested Schema for `resource_discovery_tag_filter` -Required: +Read-Only: - `key` (String) The key of the tag filter. - `value` (String) The value of the tag filter. diff --git a/examples/data-sources/grafana_cloud_provider_azure_credential/data-source.tf b/examples/data-sources/grafana_cloud_provider_azure_credential/data-source.tf index ca38f0a7d..6bbaabb7f 100644 --- a/examples/data-sources/grafana_cloud_provider_azure_credential/data-source.tf +++ b/examples/data-sources/grafana_cloud_provider_azure_credential/data-source.tf @@ -19,14 +19,4 @@ resource "grafana_cloud_provider_azure_credential" "test" { data "grafana_cloud_provider_azure_credential" "test" { stack_id = grafana_cloud_provider_azure_credential.test.stack_id resource_id = grafana_cloud_provider_azure_credential.test.resource_id - - resource_discovery_tag_filter { - key = grafana_cloud_provider_azure_credential.test.resource_discovery_tag_filter[0].key - value = grafana_cloud_provider_azure_credential.test.resource_discovery_tag_filter[0].value - } - - resource_discovery_tag_filter { - key = grafana_cloud_provider_azure_credential.test.resource_discovery_tag_filter[1].key - value = grafana_cloud_provider_azure_credential.test.resource_discovery_tag_filter[1].value - } } \ No newline at end of file diff --git a/internal/resources/cloudprovider/data_source_azure_credential.go b/internal/resources/cloudprovider/data_source_azure_credential.go index c8c3ac16a..97fd6d170 100644 --- a/internal/resources/cloudprovider/data_source_azure_credential.go +++ b/internal/resources/cloudprovider/data_source_azure_credential.go @@ -3,6 +3,8 @@ package cloudprovider import ( "context" + "github.com/hashicorp/terraform-plugin-framework/diag" + "github.com/grafana/terraform-provider-grafana/v3/internal/common" "github.com/grafana/terraform-provider-grafana/v3/internal/common/cloudproviderapi" "github.com/hashicorp/terraform-plugin-framework/datasource" @@ -81,11 +83,11 @@ func (r *datasourceAzureCredential) Schema(ctx context.Context, req datasource.S Attributes: map[string]schema.Attribute{ "key": schema.StringAttribute{ Description: "The key of the tag filter.", - Required: true, + Computed: true, }, "value": schema.StringAttribute{ Description: "The value of the tag filter.", - Required: true, + Computed: true, }, }, }, @@ -141,4 +143,35 @@ func (r *datasourceAzureCredential) Read(ctx context.Context, req datasource.Rea if resp.Diagnostics.HasError() { return } + + convertedTagFilters, diags := r.convertTagFilters(ctx, credential.ResourceTagFilters) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + diags = resp.State.SetAttribute(ctx, path.Root("resource_discovery_tag_filter"), convertedTagFilters) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } +} + +func (r *datasourceAzureCredential) convertTagFilters(ctx context.Context, apiTagFilters []cloudproviderapi.TagFilter) (types.List, diag.Diagnostics) { + tagFiltersTF := make([]TagFilter, len(apiTagFilters)) + conversionDiags := diag.Diagnostics{} + tagFilterListObjType := types.ObjectType{AttrTypes: TagFilter{}.attrTypes()} + + for i, apiTagFilter := range apiTagFilters { + tagFiltersTF[i] = TagFilter{ + Key: types.StringValue(apiTagFilter.Key), + Value: types.StringValue(apiTagFilter.Value), + } + } + + tagFiltersTFList, diags := types.ListValueFrom(ctx, tagFilterListObjType, tagFiltersTF) + conversionDiags.Append(diags...) + if conversionDiags.HasError() { + return types.ListNull(tagFilterListObjType), conversionDiags + } + return tagFiltersTFList, conversionDiags } diff --git a/internal/resources/cloudprovider/resource_azure_credential.go b/internal/resources/cloudprovider/resource_azure_credential.go index c360c54ef..802a2145d 100644 --- a/internal/resources/cloudprovider/resource_azure_credential.go +++ b/internal/resources/cloudprovider/resource_azure_credential.go @@ -370,8 +370,6 @@ func (r *resourceAzureCredential) Update(ctx context.Context, req resource.Updat return } diags = resp.State.SetAttribute(ctx, path.Root("resource_discovery_tag_filter"), convertedTagFilters) - resp.Diagnostics.Append(diags...) - resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return