Skip to content

Commit ea345ff

Browse files
Fleet Management collector data sources (#2111)
1 parent c63db55 commit ea345ff

File tree

13 files changed

+494
-20
lines changed

13 files changed

+494
-20
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "grafana_fleet_management_collector Data Source - terraform-provider-grafana"
4+
subcategory: "Fleet Management"
5+
description: |-
6+
Represents a Grafana Fleet Management collector.
7+
Official documentation https://grafana.com/docs/grafana-cloud/send-data/fleet-management/API documentation https://grafana.com/docs/grafana-cloud/send-data/fleet-management/api-reference/collector-api/
8+
Required access policy scopes:
9+
fleet-management:read
10+
---
11+
12+
# grafana_fleet_management_collector (Data Source)
13+
14+
Represents a Grafana Fleet Management collector.
15+
16+
* [Official documentation](https://grafana.com/docs/grafana-cloud/send-data/fleet-management/)
17+
* [API documentation](https://grafana.com/docs/grafana-cloud/send-data/fleet-management/api-reference/collector-api/)
18+
19+
Required access policy scopes:
20+
21+
* fleet-management:read
22+
23+
## Example Usage
24+
25+
```terraform
26+
data "grafana_fleet_management_collector" "test" {
27+
id = "my_collector"
28+
}
29+
```
30+
31+
<!-- schema generated by tfplugindocs -->
32+
## Schema
33+
34+
### Required
35+
36+
- `id` (String) ID of the collector
37+
38+
### Read-Only
39+
40+
- `enabled` (Boolean) Whether the collector is enabled or not
41+
- `local_attributes` (Map of String) Local attributes for the collector
42+
- `remote_attributes` (Map of String) Remote attributes for the collector
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "grafana_fleet_management_collectors Data Source - terraform-provider-grafana"
4+
subcategory: "Fleet Management"
5+
description: |-
6+
Represents a list of Grafana Fleet Management collectors.
7+
Official documentation https://grafana.com/docs/grafana-cloud/send-data/fleet-management/API documentation https://grafana.com/docs/grafana-cloud/send-data/fleet-management/api-reference/collector-api/
8+
Required access policy scopes:
9+
fleet-management:read
10+
---
11+
12+
# grafana_fleet_management_collectors (Data Source)
13+
14+
Represents a list of Grafana Fleet Management collectors.
15+
16+
* [Official documentation](https://grafana.com/docs/grafana-cloud/send-data/fleet-management/)
17+
* [API documentation](https://grafana.com/docs/grafana-cloud/send-data/fleet-management/api-reference/collector-api/)
18+
19+
Required access policy scopes:
20+
21+
* fleet-management:read
22+
23+
## Example Usage
24+
25+
```terraform
26+
data "grafana_fleet_management_collectors" "test" {}
27+
```
28+
29+
<!-- schema generated by tfplugindocs -->
30+
## Schema
31+
32+
### Read-Only
33+
34+
- `collectors` (List of Object) List of collectors (see [below for nested schema](#nestedatt--collectors))
35+
36+
<a id="nestedatt--collectors"></a>
37+
### Nested Schema for `collectors`
38+
39+
Read-Only:
40+
41+
- `enabled` (Boolean)
42+
- `id` (String)
43+
- `local_attributes` (Map of String)
44+
- `remote_attributes` (Map of String)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
data "grafana_fleet_management_collector" "test" {
2+
id = "my_collector"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
data "grafana_fleet_management_collectors" "test" {}

internal/resources/fleetmanagement/common.go

+24
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,33 @@ import (
55

66
"github.com/grafana/terraform-provider-grafana/v3/internal/common"
77
"github.com/grafana/terraform-provider-grafana/v3/internal/common/fleetmanagementapi"
8+
"github.com/hashicorp/terraform-plugin-framework/datasource"
89
"github.com/hashicorp/terraform-plugin-framework/resource"
910
)
1011

12+
func withClientForDataSource(req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) (*fleetmanagementapi.Client, error) {
13+
client, ok := req.ProviderData.(*common.Client)
14+
if !ok {
15+
resp.Diagnostics.AddError(
16+
"Unexpected Data Source Configure Type",
17+
fmt.Sprintf("Expected *common.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
18+
)
19+
20+
return nil, fmt.Errorf("unexpected Data Source Configure Type: %T, expected *common.Client", req.ProviderData)
21+
}
22+
23+
if client.FleetManagementClient == nil {
24+
resp.Diagnostics.AddError(
25+
"The Grafana Provider is missing a configuration for the Fleet Management API.",
26+
"Please ensure that fleet_management_auth and fleet_management_url are set in the provider configuration.",
27+
)
28+
29+
return nil, fmt.Errorf("the Fleet Management client is nil")
30+
}
31+
32+
return client.FleetManagementClient, nil
33+
}
34+
1135
func withClientForResource(req resource.ConfigureRequest, resp *resource.ConfigureResponse) (*fleetmanagementapi.Client, error) {
1236
client, ok := req.ProviderData.(*common.Client)
1337
if !ok {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package fleetmanagement
2+
3+
import (
4+
"context"
5+
6+
"connectrpc.com/connect"
7+
collectorv1 "github.com/grafana/fleet-management-api/api/gen/proto/go/collector/v1"
8+
"github.com/grafana/fleet-management-api/api/gen/proto/go/collector/v1/collectorv1connect"
9+
"github.com/grafana/terraform-provider-grafana/v3/internal/common"
10+
"github.com/hashicorp/terraform-plugin-framework/datasource"
11+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
12+
"github.com/hashicorp/terraform-plugin-framework/types"
13+
)
14+
15+
var (
16+
_ datasource.DataSource = &collectorDataSource{}
17+
_ datasource.DataSourceWithConfigure = &collectorDataSource{}
18+
)
19+
20+
type collectorDataSource struct {
21+
client collectorv1connect.CollectorServiceClient
22+
}
23+
24+
func newCollectorDataSource() *common.DataSource {
25+
return common.NewDataSource(
26+
common.CategoryFleetManagement,
27+
collectorTypeName,
28+
&collectorDataSource{},
29+
)
30+
}
31+
32+
func (d *collectorDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
33+
if req.ProviderData == nil || d.client != nil {
34+
return
35+
}
36+
37+
client, err := withClientForDataSource(req, resp)
38+
if err != nil {
39+
return
40+
}
41+
42+
d.client = client.CollectorServiceClient
43+
}
44+
45+
func (d *collectorDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
46+
resp.TypeName = collectorTypeName
47+
}
48+
49+
func (d *collectorDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
50+
resp.Schema = schema.Schema{
51+
Description: `
52+
Represents a Grafana Fleet Management collector.
53+
54+
* [Official documentation](https://grafana.com/docs/grafana-cloud/send-data/fleet-management/)
55+
* [API documentation](https://grafana.com/docs/grafana-cloud/send-data/fleet-management/api-reference/collector-api/)
56+
57+
Required access policy scopes:
58+
59+
* fleet-management:read
60+
`,
61+
Attributes: map[string]schema.Attribute{
62+
"id": schema.StringAttribute{
63+
Description: "ID of the collector",
64+
Required: true,
65+
},
66+
"remote_attributes": schema.MapAttribute{
67+
Description: "Remote attributes for the collector",
68+
Computed: true,
69+
ElementType: types.StringType,
70+
},
71+
"local_attributes": schema.MapAttribute{
72+
Description: "Local attributes for the collector",
73+
Computed: true,
74+
ElementType: types.StringType,
75+
},
76+
"enabled": schema.BoolAttribute{
77+
Description: "Whether the collector is enabled or not",
78+
Computed: true,
79+
},
80+
},
81+
}
82+
}
83+
84+
func (d *collectorDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
85+
data := &collectorDataSourceModel{}
86+
diags := req.Config.Get(ctx, data)
87+
resp.Diagnostics.Append(diags...)
88+
if resp.Diagnostics.HasError() {
89+
return
90+
}
91+
92+
getReq := &collectorv1.GetCollectorRequest{
93+
Id: data.ID.ValueString(),
94+
}
95+
getResp, err := d.client.GetCollector(ctx, connect.NewRequest(getReq))
96+
if err != nil {
97+
resp.Diagnostics.AddError("Failed to get collector", err.Error())
98+
return
99+
}
100+
101+
state, diags := collectorMessageToDataSourceModel(ctx, getResp.Msg)
102+
resp.Diagnostics.Append(diags...)
103+
if resp.Diagnostics.HasError() {
104+
return
105+
}
106+
107+
diags = resp.State.Set(ctx, state)
108+
resp.Diagnostics.Append(diags...)
109+
if resp.Diagnostics.HasError() {
110+
return
111+
}
112+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package fleetmanagement_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/grafana/terraform-provider-grafana/v3/internal/testutils"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
10+
)
11+
12+
var (
13+
collectorDataSourceConfig = `
14+
resource "grafana_fleet_management_collector" "test" {
15+
id = "%s"
16+
remote_attributes = {
17+
"env" = "PROD",
18+
"owner" = "TEAM-A"
19+
}
20+
enabled = false
21+
}
22+
23+
data "grafana_fleet_management_collector" "test" {
24+
id = grafana_fleet_management_collector.test.id
25+
}
26+
`
27+
)
28+
29+
func TestAccCollectorDataSource(t *testing.T) {
30+
testutils.CheckCloudInstanceTestsEnabled(t)
31+
32+
dataSourceName := "data.grafana_fleet_management_collector.test"
33+
collectorID := fmt.Sprintf("testacc_%s", acctest.RandString(8))
34+
35+
resource.ParallelTest(t, resource.TestCase{
36+
ProtoV5ProviderFactories: testutils.ProtoV5ProviderFactories,
37+
Steps: []resource.TestStep{
38+
{
39+
Config: fmt.Sprintf(collectorDataSourceConfig, collectorID),
40+
Check: resource.ComposeTestCheckFunc(
41+
resource.TestCheckResourceAttr(dataSourceName, "id", collectorID),
42+
resource.TestCheckResourceAttr(dataSourceName, "remote_attributes.%", "2"),
43+
resource.TestCheckResourceAttr(dataSourceName, "remote_attributes.env", "PROD"),
44+
resource.TestCheckResourceAttr(dataSourceName, "remote_attributes.owner", "TEAM-A"),
45+
resource.TestCheckResourceAttr(dataSourceName, "local_attributes.%", "1"),
46+
resource.TestCheckResourceAttr(dataSourceName, "local_attributes.collector.ID", collectorID),
47+
resource.TestCheckResourceAttr(dataSourceName, "enabled", "false"),
48+
),
49+
},
50+
},
51+
})
52+
}

0 commit comments

Comments
 (0)