Skip to content

Commit 35f15e2

Browse files
authored
Add static labels to the CloudWatch jobs (#1974)
* Add static labels to the CloudWatch jobs * Fix type * Update docs * Update tests * Remove fmt function * Rename static_labels -> static_label * apply comment * update test * Fix issue with an inconsistent static labels order * Fix imports * Change List to the Map for the static labels * Fix docs
1 parent 484ce41 commit 35f15e2

File tree

12 files changed

+87
-0
lines changed

12 files changed

+87
-0
lines changed

docs/data-sources/cloud_provider_aws_cloudwatch_scrape_job.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ resource "grafana_cloud_provider_aws_cloudwatch_scrape_job" "test" {
6464
}
6565
scrape_interval_seconds = 300
6666
}
67+
68+
static_labels = {
69+
"label1" = "value1"
70+
"label2" = "value2"
71+
}
6772
}
6873
6974
@@ -93,6 +98,7 @@ data "grafana_cloud_provider_aws_cloudwatch_scrape_job" "test" {
9398
- `regions_subset_override_used` (Boolean) When true, the `regions` attribute will be the set of regions configured in the override. When false, the `regions` attribute will be the set of regions belonging to the AWS Account resource that is associated with this CloudWatch Scrape Job.
9499
- `role_arn` (String) The AWS ARN of the IAM role associated with the AWS Account resource that is being used by this CloudWatch Scrape Job.
95100
- `service` (Block List) One or more configuration blocks to dictate what this CloudWatch Scrape Job should scrape. Each block must have a distinct `name` attribute. When accessing this as an attribute reference, it is a list of objects. (see [below for nested schema](#nestedblock--service))
101+
- `static_labels` (Map of String) A set of static labels to add to all metrics exported by this scrape job.
96102

97103
<a id="nestedblock--custom_namespace"></a>
98104
### Nested Schema for `custom_namespace`

docs/data-sources/cloud_provider_aws_cloudwatch_scrape_jobs.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Read-Only:
5151
- `role_arn` (String) The AWS ARN of the IAM role associated with the AWS Account resource that is being used by this CloudWatch Scrape Job.
5252
- `service` (Block List) One or more configuration blocks to dictate what this CloudWatch Scrape Job should scrape. Each block must have a distinct `name` attribute. When accessing this as an attribute reference, it is a list of objects. (see [below for nested schema](#nestedblock--scrape_job--service))
5353
- `stack_id` (String) The Stack ID of the Grafana Cloud instance. Part of the Terraform Resource ID.
54+
- `static_labels` (Map of String) A set of static labels to add to all metrics exported by this scrape job.
5455

5556
<a id="nestedblock--scrape_job--custom_namespace"></a>
5657
### Nested Schema for `scrape_job.custom_namespace`

docs/index.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,11 @@ resource "grafana_cloud_provider_aws_cloudwatch_scrape_job" "test" {
391391
}
392392
scrape_interval_seconds = 300
393393
}
394+
395+
static_labels = {
396+
"label1" = "value1"
397+
"label2" = "value2"
398+
}
394399
}
395400
```
396401

docs/resources/cloud_provider_aws_cloudwatch_scrape_job.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ resource "grafana_cloud_provider_aws_cloudwatch_scrape_job" "test" {
6363
}
6464
scrape_interval_seconds = 300
6565
}
66+
67+
static_labels = {
68+
"label1" = "value1"
69+
"label2" = "value2"
70+
}
6671
}
6772
```
6873

@@ -82,6 +87,7 @@ resource "grafana_cloud_provider_aws_cloudwatch_scrape_job" "test" {
8287
- `export_tags` (Boolean) When enabled, AWS resource tags are exported as Prometheus labels to metrics formatted as `aws_<service_name>_info`.
8388
- `regions_subset_override` (Set of String) A subset of the regions that are configured in the associated AWS Account resource to apply to this scrape job. If not set or empty, all of the Account resource's regions are scraped.
8489
- `service` (Block List) One or more configuration blocks to configure AWS services for the CloudWatch Scrape Job to scrape. Each block must have a distinct `name` attribute. When accessing this as an attribute reference, it is a list of objects. (see [below for nested schema](#nestedblock--service))
90+
- `static_labels` (Map of String) A set of static labels to add to all metrics exported by this scrape job.
8591

8692
### Read-Only
8793

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ resource "grafana_cloud_provider_aws_cloudwatch_scrape_job" "test" {
4949
}
5050
scrape_interval_seconds = 300
5151
}
52+
53+
static_labels = {
54+
"label1" = "value1"
55+
"label2" = "value2"
56+
}
5257
}
5358

5459

examples/resources/grafana_cloud_provider_aws_cloudwatch_scrape_job/resource.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,9 @@ resource "grafana_cloud_provider_aws_cloudwatch_scrape_job" "test" {
4848
}
4949
scrape_interval_seconds = 300
5050
}
51+
52+
static_labels = {
53+
"label1" = "value1"
54+
"label2" = "value2"
55+
}
5156
}

internal/common/cloudproviderapi/client.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ type AWSCloudWatchScrapeJobRequest struct {
111111
ExportTags bool `json:"exportTags"`
112112
Services []AWSCloudWatchService `json:"services"`
113113
CustomNamespaces []AWSCloudWatchCustomNamespace `json:"customNamespaces"`
114+
StaticLabels map[string]string `json:"staticLabels"`
114115
}
115116
type AWSCloudWatchScrapeJobResponse struct {
116117
Name string `json:"name"`
@@ -119,6 +120,8 @@ type AWSCloudWatchScrapeJobResponse struct {
119120
ExportTags bool `json:"exportTags"`
120121
Services []AWSCloudWatchService `json:"services"`
121122
CustomNamespaces []AWSCloudWatchCustomNamespace `json:"customNamespaces"`
123+
StaticLabels map[string]string `json:"staticLabels"`
124+
122125
// computed fields beyond the original request
123126
RoleARN string `json:"roleARN"`
124127
Regions []string `json:"regions"`

internal/resources/cloudprovider/data_source_aws_cloudwatch_scrape_job.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/hashicorp/terraform-plugin-framework/datasource"
99
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
1010
"github.com/hashicorp/terraform-plugin-framework/types"
11+
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
1112
)
1213

1314
var (
@@ -56,6 +57,11 @@ var (
5657
Description: "When the CloudWatch Scrape Job is disabled, this will show the reason that it is in that state.",
5758
Computed: true,
5859
},
60+
"static_labels": schema.MapAttribute{
61+
Description: "A set of static labels to add to all metrics exported by this scrape job.",
62+
Computed: true,
63+
ElementType: basetypes.StringType{},
64+
},
5965
},
6066
Blocks: map[string]schema.Block{
6167
"service": schema.ListNestedBlock{

internal/resources/cloudprovider/data_source_aws_cloudwatch_scrape_job_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ func TestAccDataSourceAWSCloudWatchScrapeJob(t *testing.T) {
6262
resource.TestCheckResourceAttr("data.grafana_cloud_provider_aws_cloudwatch_scrape_job.test", "custom_namespace.0.metric.0.statistics.#", fmt.Sprintf("%d", len(testAWSCloudWatchScrapeJobData.CustomNamespaces[0].Metrics[0].Statistics))),
6363
resource.TestCheckResourceAttr("data.grafana_cloud_provider_aws_cloudwatch_scrape_job.test", "custom_namespace.0.metric.0.statistics.0", testAWSCloudWatchScrapeJobData.CustomNamespaces[0].Metrics[0].Statistics[0]),
6464
resource.TestCheckResourceAttr("data.grafana_cloud_provider_aws_cloudwatch_scrape_job.test", "custom_namespace.0.scrape_interval_seconds", fmt.Sprintf("%d", testAWSCloudWatchScrapeJobData.CustomNamespaces[0].ScrapeIntervalSeconds)),
65+
resource.TestCheckResourceAttr("data.grafana_cloud_provider_aws_cloudwatch_scrape_job.test", "static_labels.label1", testAWSCloudWatchScrapeJobData.StaticLabels["label1"]),
66+
resource.TestCheckResourceAttr("data.grafana_cloud_provider_aws_cloudwatch_scrape_job.test", "static_labels.label2", testAWSCloudWatchScrapeJobData.StaticLabels["label2"]),
6567
),
6668
},
6769
// update to remove regions_subset_override so that the account's regions are used instead

internal/resources/cloudprovider/models.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type awsCWScrapeJobTFResourceModel struct {
2626
// See https://developer.hashicorp.com/terraform/plugin/framework/handling-data/attributes#nested-attribute-types
2727
Services types.List `tfsdk:"service"`
2828
CustomNamespaces types.List `tfsdk:"custom_namespace"`
29+
StaticLabels types.Map `tfsdk:"static_labels"`
2930
}
3031

3132
type awsCWScrapeJobTFDataSourceModel struct {
@@ -44,6 +45,7 @@ type awsCWScrapeJobTFDataSourceModel struct {
4445
// See https://developer.hashicorp.com/terraform/plugin/framework/handling-data/attributes#nested-attribute-types
4546
Services types.List `tfsdk:"service"`
4647
CustomNamespaces types.List `tfsdk:"custom_namespace"`
48+
StaticLabels types.Map `tfsdk:"static_labels"`
4749
}
4850

4951
type awsCWScrapeJobServiceTFModel struct {
@@ -301,6 +303,12 @@ func (tfData awsCWScrapeJobTFResourceModel) toClientModel(ctx context.Context) (
301303
}
302304
}
303305

306+
diags = tfData.StaticLabels.ElementsAs(ctx, &converted.StaticLabels, false)
307+
conversionDiags.Append(diags...)
308+
if conversionDiags.HasError() {
309+
return cloudproviderapi.AWSCloudWatchScrapeJobRequest{}, conversionDiags
310+
}
311+
304312
return converted, conversionDiags
305313
}
306314

@@ -342,6 +350,16 @@ func generateCloudWatchScrapeJobTFResourceModel(ctx context.Context, stackID str
342350
}
343351
converted.CustomNamespaces = customNamespaces
344352

353+
staticLabelsMap := types.MapValueMust(types.StringType, map[string]attr.Value{})
354+
if scrapeJobData.StaticLabels != nil {
355+
staticLabelsMap, diags = types.MapValueFrom(ctx, basetypes.StringType{}, scrapeJobData.StaticLabels)
356+
conversionDiags.Append(diags...)
357+
if conversionDiags.HasError() {
358+
return awsCWScrapeJobTFResourceModel{}, conversionDiags
359+
}
360+
}
361+
converted.StaticLabels = staticLabelsMap
362+
345363
return converted, conversionDiags
346364
}
347365

@@ -381,6 +399,16 @@ func generateCloudWatchScrapeJobDataSourceTFModel(ctx context.Context, stackID s
381399
}
382400
converted.CustomNamespaces = customNamespaces
383401

402+
staticLabelsMap := types.MapValueMust(types.StringType, map[string]attr.Value{})
403+
if scrapeJobData.StaticLabels != nil {
404+
staticLabelsMap, diags = types.MapValueFrom(ctx, basetypes.StringType{}, scrapeJobData.StaticLabels)
405+
conversionDiags.Append(diags...)
406+
if conversionDiags.HasError() {
407+
return awsCWScrapeJobTFDataSourceModel{}, conversionDiags
408+
}
409+
}
410+
converted.StaticLabels = staticLabelsMap
411+
384412
return converted, conversionDiags
385413
}
386414

0 commit comments

Comments
 (0)