|
| 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 | +} |
0 commit comments