|
| 1 | +// Copyright (c) 2025 - Stacklet, Inc. |
| 2 | + |
| 3 | +package datasources |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 11 | + |
| 12 | + "github.com/stacklet/terraform-provider-stacklet/internal/api" |
| 13 | + "github.com/stacklet/terraform-provider-stacklet/internal/errors" |
| 14 | + "github.com/stacklet/terraform-provider-stacklet/internal/models" |
| 15 | + "github.com/stacklet/terraform-provider-stacklet/internal/providerdata" |
| 16 | + tftypes "github.com/stacklet/terraform-provider-stacklet/internal/types" |
| 17 | +) |
| 18 | + |
| 19 | +var ( |
| 20 | + _ datasource.DataSource = &platformDataSource{} |
| 21 | +) |
| 22 | + |
| 23 | +func NewPlatformDataSource() datasource.DataSource { |
| 24 | + return &platformDataSource{} |
| 25 | +} |
| 26 | + |
| 27 | +type platformDataSource struct { |
| 28 | + api *api.API |
| 29 | +} |
| 30 | + |
| 31 | +func (d *platformDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 32 | + resp.TypeName = req.ProviderTypeName + "_platform" |
| 33 | +} |
| 34 | + |
| 35 | +func (d *platformDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 36 | + resp.Schema = schema.Schema{ |
| 37 | + Description: "Retrieve information about the Stacklet platform.", |
| 38 | + Attributes: map[string]schema.Attribute{ |
| 39 | + "id": schema.StringAttribute{ |
| 40 | + Description: "The GraphQL Node ID.", |
| 41 | + Computed: true, |
| 42 | + }, |
| 43 | + "external_id": schema.StringAttribute{ |
| 44 | + Description: "The external ID for the deployment.", |
| 45 | + Computed: true, |
| 46 | + }, |
| 47 | + "execution_regions": schema.ListAttribute{ |
| 48 | + Description: "List of regions for which execution is enabled.", |
| 49 | + Computed: true, |
| 50 | + ElementType: types.StringType, |
| 51 | + }, |
| 52 | + "default_role": schema.StringAttribute{ |
| 53 | + Description: "Default role for users.", |
| 54 | + Computed: true, |
| 55 | + }, |
| 56 | + }, |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func (d *platformDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 61 | + if pd, err := providerdata.GetDataSourceProviderData(req); err != nil { |
| 62 | + errors.AddDiagError(&resp.Diagnostics, err) |
| 63 | + } else if pd != nil { |
| 64 | + d.api = pd.API |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func (d *platformDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 69 | + var data models.PlatformDataSource |
| 70 | + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) |
| 71 | + if resp.Diagnostics.HasError() { |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + platform, err := d.api.Platform.Read(ctx) |
| 76 | + if err != nil { |
| 77 | + errors.AddDiagError(&resp.Diagnostics, err) |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + data.ID = types.StringValue(platform.ID) |
| 82 | + data.ExternalID = tftypes.NullableString(platform.ExternalID) |
| 83 | + data.ExecutionRegions = tftypes.StringsList(platform.ExecutionRegions) |
| 84 | + data.DefaultRole = tftypes.NullableString(platform.DefaultRole) |
| 85 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 86 | +} |
0 commit comments