Skip to content

Commit 64e7255

Browse files
authored
[datadog_metric_tags] add datadog_metric_tags data source (#2966)
* feat: add `datadog_metric_tags` data source This addresses issue #2965 and implements a basic `datadog_metric_tags` data source. Signed-off-by: Mike Ball <[email protected]>
1 parent 1696e43 commit 64e7255

File tree

7 files changed

+263
-0
lines changed

7 files changed

+263
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package fwprovider
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strings"
7+
8+
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV2"
9+
"github.com/hashicorp/terraform-plugin-framework/datasource"
10+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
11+
"github.com/hashicorp/terraform-plugin-framework/types"
12+
13+
"github.com/terraform-providers/terraform-provider-datadog/datadog/internal/utils"
14+
)
15+
16+
var (
17+
_ datasource.DataSource = &datadogUsersDataSource{}
18+
)
19+
20+
type datadogMetricTagsModel struct {
21+
ID types.String `tfsdk:"id"`
22+
Metric types.String `tfsdk:"metric"`
23+
Tags types.List `tfsdk:"tags"`
24+
}
25+
26+
type datadogMetricTagsDataSource struct {
27+
Api *datadogV2.MetricsApi
28+
Auth context.Context
29+
}
30+
31+
func NewDatadogMetricTagsDataSource() datasource.DataSource {
32+
return &datadogMetricTagsDataSource{}
33+
}
34+
35+
func (d *datadogMetricTagsDataSource) Configure(_ context.Context, request datasource.ConfigureRequest, response *datasource.ConfigureResponse) {
36+
providerData, _ := request.ProviderData.(*FrameworkProvider)
37+
d.Api = providerData.DatadogApiInstances.GetMetricsApiV2()
38+
d.Auth = providerData.Auth
39+
}
40+
41+
func (d *datadogMetricTagsDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
42+
resp.TypeName = "metric_tags"
43+
}
44+
45+
func (d *datadogMetricTagsDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
46+
resp.Schema = schema.Schema{
47+
Description: "Use this data source to retrieve tags associated with a metric to use in other resources.",
48+
Attributes: map[string]schema.Attribute{
49+
"id": utils.ResourceIDAttribute(),
50+
"metric": schema.StringAttribute{
51+
Description: "The metric for which to fetch tags.",
52+
Required: true,
53+
},
54+
// Computed values
55+
"tags": schema.ListAttribute{
56+
Description: "The tags associated with the metric.",
57+
Computed: true,
58+
ElementType: types.StringType,
59+
},
60+
},
61+
}
62+
}
63+
64+
func (d *datadogMetricTagsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
65+
var state datadogMetricTagsModel
66+
67+
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)
68+
if resp.Diagnostics.HasError() {
69+
return
70+
}
71+
72+
ddResp, _, err := d.Api.ListTagsByMetricName(d.Auth, state.Metric.String())
73+
if err != nil {
74+
resp.Diagnostics.Append(utils.FrameworkErrorDiag(err, "error listing metric tags"))
75+
return
76+
}
77+
78+
tagsData := ddResp.GetData()
79+
tags := tagsData.Attributes.GetTags()
80+
81+
d.updateState(ctx, &state, tags)
82+
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
83+
}
84+
85+
func (d *datadogMetricTagsDataSource) updateState(ctx context.Context, state *datadogMetricTagsModel, tags []string) {
86+
hashingData := fmt.Sprintf("%s:%s", state.Metric.ValueString(), strings.Join(tags, ":"))
87+
88+
state.ID = types.StringValue(utils.ConvertToSha256(hashingData))
89+
state.Tags, _ = types.ListValueFrom(ctx, types.StringType, tags)
90+
}

datadog/fwprovider/framework_provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ var Datasources = []func() datasource.DataSource{
9393
NewDatadogApmRetentionFiltersOrderDataSource,
9494
NewDatadogDashboardListDataSource,
9595
NewDatadogIntegrationAWSNamespaceRulesDatasource,
96+
NewDatadogMetricTagsDataSource,
9697
NewDatadogPowerpackDataSource,
9798
NewDatadogServiceAccountDatasource,
9899
NewDatadogSoftwareCatalogDataSource,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2025-04-18T11:16:46.898512-04:00
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
---
2+
version: 2
3+
interactions:
4+
- id: 0
5+
request:
6+
proto: HTTP/1.1
7+
proto_major: 1
8+
proto_minor: 1
9+
content_length: 0
10+
transfer_encoding: []
11+
trailer: {}
12+
host: api.datadoghq.com
13+
remote_addr: ""
14+
request_uri: ""
15+
body: ""
16+
form: {}
17+
headers:
18+
Accept:
19+
- application/json
20+
url: https://api.datadoghq.com/api/v2/metrics/%22datadog.agent.running%22/all-tags
21+
method: GET
22+
response:
23+
proto: HTTP/1.1
24+
proto_major: 1
25+
proto_minor: 1
26+
transfer_encoding:
27+
- chunked
28+
trailer: {}
29+
content_length: -1
30+
uncompressed: true
31+
body: |
32+
{"data":{"type":"metrics","id":"datadog.agent.running","attributes":{"tags":["foo:bar"]}}}
33+
headers:
34+
Content-Type:
35+
- application/json
36+
status: 200 OK
37+
code: 200
38+
duration: 205.560625ms
39+
- id: 1
40+
request:
41+
proto: HTTP/1.1
42+
proto_major: 1
43+
proto_minor: 1
44+
content_length: 0
45+
transfer_encoding: []
46+
trailer: {}
47+
host: api.datadoghq.com
48+
remote_addr: ""
49+
request_uri: ""
50+
body: ""
51+
form: {}
52+
headers:
53+
Accept:
54+
- application/json
55+
url: https://api.datadoghq.com/api/v2/metrics/%22datadog.agent.running%22/all-tags
56+
method: GET
57+
response:
58+
proto: HTTP/1.1
59+
proto_major: 1
60+
proto_minor: 1
61+
transfer_encoding:
62+
- chunked
63+
trailer: {}
64+
content_length: -1
65+
uncompressed: true
66+
body: |
67+
{"data":{"type":"metrics","id":"datadog.agent.running","attributes":{"tags":["foo:bar"]}}}
68+
headers:
69+
Content-Type:
70+
- application/json
71+
status: 200 OK
72+
code: 200
73+
duration: 105.948209ms
74+
- id: 2
75+
request:
76+
proto: HTTP/1.1
77+
proto_major: 1
78+
proto_minor: 1
79+
content_length: 0
80+
transfer_encoding: []
81+
trailer: {}
82+
host: api.datadoghq.com
83+
remote_addr: ""
84+
request_uri: ""
85+
body: ""
86+
form: {}
87+
headers:
88+
Accept:
89+
- application/json
90+
url: https://api.datadoghq.com/api/v2/metrics/%22datadog.agent.running%22/all-tags
91+
method: GET
92+
response:
93+
proto: HTTP/1.1
94+
proto_major: 1
95+
proto_minor: 1
96+
transfer_encoding:
97+
- chunked
98+
trailer: {}
99+
content_length: -1
100+
uncompressed: true
101+
body: |
102+
{"data":{"type":"metrics","id":"datadog.agent.running","attributes":{"tags":["foo:bar"]}}}
103+
headers:
104+
Content-Type:
105+
- application/json
106+
status: 200 OK
107+
code: 200
108+
duration: 105.951167ms
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
9+
)
10+
11+
func TestAccDatadogMetricTagsDatasource(t *testing.T) {
12+
t.Parallel()
13+
_, _, accProviders := testAccFrameworkMuxProviders(context.Background(), t)
14+
metric := "datadog.agent.running"
15+
16+
resource.Test(t, resource.TestCase{
17+
PreCheck: func() { testAccPreCheck(t) },
18+
ProtoV5ProviderFactories: accProviders,
19+
Steps: []resource.TestStep{
20+
{
21+
Config: testAccDatasourceMetricTagsConfig(metric),
22+
Check: resource.ComposeTestCheckFunc(
23+
resource.TestCheckResourceAttr("data.datadog_metric_tags.agent", "metric", metric),
24+
resource.TestCheckResourceAttr("data.datadog_metric_tags.agent", "tags.0", "foo:bar"),
25+
),
26+
},
27+
},
28+
})
29+
}
30+
31+
func testAccDatasourceMetricTagsConfig(metric string) string {
32+
return fmt.Sprintf(`
33+
data "datadog_metric_tags" "agent" {
34+
metric = "%s"
35+
}
36+
`, metric)
37+
}

datadog/tests/provider_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ var testFiles2EndpointTags = map[string]string{
7474
"tests/data_source_datadog_logs_pipelines_test": "logs-pipelines",
7575
"tests/data_source_datadog_monitor_config_policies_test": "monitor-config-policies",
7676
"tests/data_source_datadog_monitor_config_policy_test": "monitor-config-policies",
77+
"tests/data_source_datadog_metric_tags_test": "metrics",
7778
"tests/data_source_datadog_monitor_test": "monitors",
7879
"tests/data_source_datadog_monitors_test": "monitors",
7980
"tests/data_source_datadog_permissions_test": "permissions",

docs/data-sources/metric_tags.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "datadog_metric_tags Data Source - terraform-provider-datadog"
4+
subcategory: ""
5+
description: |-
6+
Use this data source to retrieve tags associated with a metric to use in other resources.
7+
---
8+
9+
# datadog_metric_tags (Data Source)
10+
11+
Use this data source to retrieve tags associated with a metric to use in other resources.
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `metric` (String) The metric for which to fetch tags.
21+
22+
### Read-Only
23+
24+
- `id` (String) The ID of this resource.
25+
- `tags` (List of String) The tags associated with the metric.

0 commit comments

Comments
 (0)