Skip to content

Commit 8859f6c

Browse files
authored
Add support for the additional tags for the Azure metrics (#2009)
* Add support for the additional tags for the Azure metrics * Update documents * Fix imports
1 parent 397e224 commit 8859f6c

File tree

8 files changed

+70
-0
lines changed

8 files changed

+70
-0
lines changed

docs/data-sources/cloud_provider_azure_credential.md

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ resource "grafana_cloud_provider_azure_credential" "test" {
2020
client_secret = "my-client-secret"
2121
tenant_id = "my-tenant-id"
2222
23+
resource_tags_to_add_to_metrics = ["tag1", "tag2"]
24+
2325
resource_discovery_tag_filter {
2426
key = "key-1"
2527
value = "value-1"
@@ -76,6 +78,7 @@ data "grafana_cloud_provider_azure_credential" "test" {
7678
- `id` (String) The Terraform Resource ID. This has the format "{{ stack_id }}:{{ resource_id }}".
7779
- `name` (String) The name of the Azure Credential.
7880
- `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))
81+
- `resource_tags_to_add_to_metrics` (Set of String) A set of regions that this AWS Account resource applies to.
7982
- `tenant_id` (String) The tenant ID of the Azure Credential.
8083

8184
<a id="nestedblock--auto_discovery_configuration"></a>

docs/resources/cloud_provider_azure_credential.md

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ resource "grafana_cloud_provider_azure_credential" "test" {
2020
client_secret = "my-client-secret"
2121
tenant_id = "my-tenant-id"
2222
23+
resource_tags_to_add_to_metrics = ["tag1", "tag2"]
24+
2325
resource_discovery_tag_filter {
2426
key = "key-1"
2527
value = "value-1"
@@ -70,6 +72,7 @@ resource "grafana_cloud_provider_azure_credential" "test" {
7072

7173
- `auto_discovery_configuration` (Block List) The list of auto discovery configurations. (see [below for nested schema](#nestedblock--auto_discovery_configuration))
7274
- `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))
75+
- `resource_tags_to_add_to_metrics` (Set of String) A set of regions that this AWS Account resource applies to.
7376

7477
### Read-Only
7578

examples/data-sources/grafana_cloud_provider_azure_credential/data-source.tf

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ resource "grafana_cloud_provider_azure_credential" "test" {
55
client_secret = "my-client-secret"
66
tenant_id = "my-tenant-id"
77

8+
resource_tags_to_add_to_metrics = ["tag1", "tag2"]
9+
810
resource_discovery_tag_filter {
911
key = "key-1"
1012
value = "value-1"

examples/resources/grafana_cloud_provider_azure_credential/resource.tf

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ resource "grafana_cloud_provider_azure_credential" "test" {
55
client_secret = "my-client-secret"
66
tenant_id = "my-tenant-id"
77

8+
resource_tags_to_add_to_metrics = ["tag1", "tag2"]
9+
810
resource_discovery_tag_filter {
911
key = "key-1"
1012
value = "value-1"

internal/common/cloudproviderapi/client.go

+3
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ type AzureCredential struct {
220220

221221
// AutoDiscoveryConfiguration is the configuration for auto-discovery of Azure resources.
222222
AutoDiscoveryConfiguration []AutoDiscoveryConfiguration `json:"auto_discovery_configuration"`
223+
224+
// ResourceTagsToAddToMetrics is the list of Azure resource tags to add to metrics.
225+
ResourceTagsToAddToMetrics []string `json:"resource_tags_to_add_to_metrics"`
223226
}
224227

225228
type AutoDiscoveryConfiguration struct {

internal/resources/cloudprovider/azure_credential_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func TestAcc_AzureCredential(t *testing.T) {
3737
"value": "value-2"
3838
}
3939
],
40+
"resource_tags_to_add_to_metrics" : ["tag1", "tag2"],
4041
"auto_discovery_configuration": [
4142
{
4243
"subscription_id": "my-subscription_id",
@@ -94,6 +95,7 @@ func TestAcc_AzureCredential(t *testing.T) {
9495
"value": "value-2"
9596
}
9697
],
98+
"resource_tags_to_add_to_metrics" : ["tag1", "tag2"],
9799
"auto_discovery_configuration": [
98100
{
99101
"subscription_id": "my-subscription_id",
@@ -146,6 +148,7 @@ func TestAcc_AzureCredential(t *testing.T) {
146148
"value": "value-2"
147149
}
148150
],
151+
"resource_tags_to_add_to_metrics" : ["tag1", "tag2"],
149152
"auto_discovery_configuration": [
150153
{
151154
"subscription_id": "my-subscription_id",
@@ -218,6 +221,8 @@ func TestAcc_AzureCredential(t *testing.T) {
218221
resource.TestCheckResourceAttr("grafana_cloud_provider_azure_credential.test", "auto_discovery_configuration.0.resource_type_configurations.1.metric_configuration.0.dimensions.0", "GeoType"),
219222
resource.TestCheckResourceAttr("grafana_cloud_provider_azure_credential.test", "auto_discovery_configuration.0.resource_type_configurations.1.metric_configuration.0.dimensions.1", "ApiName"),
220223
resource.TestCheckResourceAttr("grafana_cloud_provider_azure_credential.test", "auto_discovery_configuration.0.resource_type_configurations.1.metric_configuration.0.aggregations.0", "Average"),
224+
resource.TestCheckResourceAttr("grafana_cloud_provider_azure_credential.test", "resource_tags_to_add_to_metrics.0", "tag1"),
225+
resource.TestCheckResourceAttr("grafana_cloud_provider_azure_credential.test", "resource_tags_to_add_to_metrics.1", "tag2"),
221226
),
222227
},
223228
{
@@ -243,6 +248,8 @@ func TestAcc_AzureCredential(t *testing.T) {
243248
resource.TestCheckResourceAttr("data.grafana_cloud_provider_azure_credential.test", "auto_discovery_configuration.0.resource_type_configurations.1.metric_configuration.0.dimensions.0", "GeoType"),
244249
resource.TestCheckResourceAttr("data.grafana_cloud_provider_azure_credential.test", "auto_discovery_configuration.0.resource_type_configurations.1.metric_configuration.0.dimensions.1", "ApiName"),
245250
resource.TestCheckResourceAttr("data.grafana_cloud_provider_azure_credential.test", "auto_discovery_configuration.0.resource_type_configurations.1.metric_configuration.0.aggregations.0", "Average"),
251+
resource.TestCheckResourceAttr("data.grafana_cloud_provider_azure_credential.test", "resource_tags_to_add_to_metrics.0", "tag1"),
252+
resource.TestCheckResourceAttr("data.grafana_cloud_provider_azure_credential.test", "resource_tags_to_add_to_metrics.1", "tag2"),
246253
),
247254
},
248255
},

internal/resources/cloudprovider/data_source_azure_credential.go

+11
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ func (r *datasourceAzureCredential) Schema(ctx context.Context, req datasource.S
7575
Computed: true,
7676
Sensitive: true,
7777
},
78+
"resource_tags_to_add_to_metrics": schema.SetAttribute{
79+
Description: "A set of regions that this AWS Account resource applies to.",
80+
Computed: true,
81+
ElementType: types.StringType,
82+
},
7883
},
7984
Blocks: map[string]schema.Block{
8085
"resource_discovery_tag_filter": schema.ListNestedBlock{
@@ -176,6 +181,12 @@ func (r *datasourceAzureCredential) Read(ctx context.Context, req datasource.Rea
176181
diags = resp.State.SetAttribute(ctx, path.Root("auto_discovery_configuration"), convertedAutoDiscoveryConfigurations)
177182
resp.Diagnostics.Append(diags...)
178183

184+
diags = resp.State.SetAttribute(ctx, path.Root("resource_tags_to_add_to_metrics"), credential.ResourceTagsToAddToMetrics)
185+
resp.Diagnostics.Append(diags...)
186+
if resp.Diagnostics.HasError() {
187+
return
188+
}
189+
179190
if resp.Diagnostics.HasError() {
180191
return
181192
}

internal/resources/cloudprovider/resource_azure_credential.go

+39
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
1414
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
1515
"github.com/hashicorp/terraform-plugin-framework/types"
16+
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
1617

1718
"github.com/grafana/terraform-provider-grafana/v3/internal/common"
1819
"github.com/grafana/terraform-provider-grafana/v3/internal/common/cloudproviderapi"
@@ -34,6 +35,7 @@ type resourceAzureCredentialModel struct {
3435
ResourceID types.String `tfsdk:"resource_id"`
3536
ResourceTagFilters types.List `tfsdk:"resource_discovery_tag_filter"`
3637
AutoDiscoveryConfiguration types.List `tfsdk:"auto_discovery_configuration"`
38+
ResourceTagsToAddToMetrics types.Set `tfsdk:"resource_tags_to_add_to_metrics"`
3739
}
3840

3941
type TagFilter struct {
@@ -170,6 +172,11 @@ func (r *resourceAzureCredential) Schema(ctx context.Context, req resource.Schem
170172
Required: true,
171173
Sensitive: true,
172174
},
175+
"resource_tags_to_add_to_metrics": schema.SetAttribute{
176+
Description: "A set of regions that this AWS Account resource applies to.",
177+
Optional: true,
178+
ElementType: types.StringType,
179+
},
173180
},
174181
Blocks: map[string]schema.Block{
175182
"resource_discovery_tag_filter": schema.ListNestedBlock{
@@ -238,6 +245,12 @@ func (r *resourceAzureCredential) ImportState(ctx context.Context, req resource.
238245
return
239246
}
240247

248+
resourceTagsToAddToMetrics, diags := types.SetValueFrom(ctx, basetypes.StringType{}, credentials.ResourceTagsToAddToMetrics)
249+
resp.Diagnostics.Append(diags...)
250+
if resp.Diagnostics.HasError() {
251+
return
252+
}
253+
241254
resp.State.Set(ctx, &resourceAzureCredentialModel{
242255
ID: types.StringValue(req.ID),
243256
Name: types.StringValue(credentials.Name),
@@ -248,6 +261,7 @@ func (r *resourceAzureCredential) ImportState(ctx context.Context, req resource.
248261
ClientSecret: types.StringValue(""), // We don't import the client secret
249262
ResourceTagFilters: tagFilters,
250263
AutoDiscoveryConfiguration: autoconfiguration,
264+
ResourceTagsToAddToMetrics: resourceTagsToAddToMetrics,
251265
})
252266
}
253267

@@ -292,6 +306,12 @@ func (r *resourceAzureCredential) Create(ctx context.Context, req resource.Creat
292306
AutoDiscoveryConfiguration: requestAutoDiscoveryConfiguration,
293307
}
294308

309+
diags = data.ResourceTagsToAddToMetrics.ElementsAs(ctx, &azureCredential.ResourceTagsToAddToMetrics, false)
310+
resp.Diagnostics.Append(diags...)
311+
if resp.Diagnostics.HasError() {
312+
return
313+
}
314+
295315
credential, err := r.client.CreateAzureCredential(
296316
ctx,
297317
data.StackID.ValueString(),
@@ -312,6 +332,7 @@ func (r *resourceAzureCredential) Create(ctx context.Context, req resource.Creat
312332
ResourceID: types.StringValue(credential.ID),
313333
ResourceTagFilters: data.ResourceTagFilters,
314334
AutoDiscoveryConfiguration: data.AutoDiscoveryConfiguration,
335+
ResourceTagsToAddToMetrics: data.ResourceTagsToAddToMetrics,
315336
})
316337
}
317338

@@ -386,6 +407,12 @@ func (r *resourceAzureCredential) Read(ctx context.Context, req resource.ReadReq
386407
if resp.Diagnostics.HasError() {
387408
return
388409
}
410+
411+
diags = resp.State.SetAttribute(ctx, path.Root("resource_tags_to_add_to_metrics"), credential.ResourceTagsToAddToMetrics)
412+
resp.Diagnostics.Append(diags...)
413+
if resp.Diagnostics.HasError() {
414+
return
415+
}
389416
}
390417

391418
func (r *resourceAzureCredential) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
@@ -426,6 +453,12 @@ func (r *resourceAzureCredential) Update(ctx context.Context, req resource.Updat
426453

427454
credential.AutoDiscoveryConfiguration = autoDiscoveryConfigurations
428455

456+
diags = planData.ResourceTagsToAddToMetrics.ElementsAs(ctx, &credential.ResourceTagsToAddToMetrics, false)
457+
resp.Diagnostics.Append(diags...)
458+
if resp.Diagnostics.HasError() {
459+
return
460+
}
461+
429462
credentialResponse, err := r.client.UpdateAzureCredential(
430463
ctx,
431464
planData.StackID.ValueString(),
@@ -482,6 +515,12 @@ func (r *resourceAzureCredential) Update(ctx context.Context, req resource.Updat
482515
if resp.Diagnostics.HasError() {
483516
return
484517
}
518+
519+
diags = resp.State.SetAttribute(ctx, path.Root("resource_tags_to_add_to_metrics"), planData.ResourceTagsToAddToMetrics)
520+
resp.Diagnostics.Append(diags...)
521+
if resp.Diagnostics.HasError() {
522+
return
523+
}
485524
}
486525

487526
func (r *resourceAzureCredential) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {

0 commit comments

Comments
 (0)