Skip to content

Commit

Permalink
apply comments from the PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
andriikushch committed Dec 18, 2024
1 parent 30cf77c commit 9550f51
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 29 deletions.
17 changes: 2 additions & 15 deletions docs/data-sources/cloud_provider_azure_credential.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
```

Expand All @@ -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.

<a id="nestedblock--resource_discovery_tag_filter"></a>
### 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.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
37 changes: 35 additions & 2 deletions internal/resources/cloudprovider/data_source_azure_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
},
},
},
Expand Down Expand Up @@ -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
}
2 changes: 0 additions & 2 deletions internal/resources/cloudprovider/resource_azure_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9550f51

Please sign in to comment.