|
| 1 | +package datasources |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/datarootsio/terraform-provider-dagster/internal/client" |
| 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 | + |
| 13 | +// Ensure the implementation satisfies the expected interfaces. |
| 14 | +var ( |
| 15 | + _ datasource.DataSource = &TeamDataSource{} |
| 16 | + _ datasource.DataSourceWithConfigure = &TeamDataSource{} |
| 17 | +) |
| 18 | + |
| 19 | +type TeamDataSource struct { |
| 20 | + client client.DagsterClient |
| 21 | +} |
| 22 | + |
| 23 | +type TeamDataSourceModel struct { |
| 24 | + Name types.String `tfsdk:"name"` |
| 25 | + Id types.String `tfsdk:"id"` |
| 26 | +} |
| 27 | + |
| 28 | +//nolint:ireturn // required by Terraform API |
| 29 | +func NewTeamDataSource() datasource.DataSource { |
| 30 | + return &TeamDataSource{} |
| 31 | +} |
| 32 | + |
| 33 | +// Metadata returns the data source type name. |
| 34 | +func (d *TeamDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 35 | + resp.TypeName = req.ProviderTypeName + "_team" |
| 36 | +} |
| 37 | + |
| 38 | +var teamAttributes = map[string]schema.Attribute{ |
| 39 | + "name": schema.StringAttribute{ |
| 40 | + Required: true, |
| 41 | + Computed: false, |
| 42 | + Description: "Name the Dagster Cloud team", |
| 43 | + }, |
| 44 | + "id": schema.StringAttribute{ |
| 45 | + Computed: true, |
| 46 | + Description: "Team id", |
| 47 | + }, |
| 48 | +} |
| 49 | + |
| 50 | +// Schema defines the schema for the data source. |
| 51 | +func (d *TeamDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 52 | + resp.Schema = schema.Schema{ |
| 53 | + Description: `Retrieve information about a Dagster Cloud team.`, |
| 54 | + Attributes: teamAttributes, |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// Configure adds the provider-configured client to the data source. |
| 59 | +func (d *TeamDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 60 | + if req.ProviderData == nil { |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + client, ok := req.ProviderData.(client.DagsterClient) |
| 65 | + if !ok { |
| 66 | + resp.Diagnostics.AddError( |
| 67 | + "Unexpected Data Source Configure Type", |
| 68 | + fmt.Sprintf("Expected client.DagsterClient, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 69 | + ) |
| 70 | + |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + d.client = client |
| 75 | +} |
| 76 | + |
| 77 | +// Read refreshes the Terraform state with the latest data. |
| 78 | +func (d *TeamDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 79 | + var data TeamDataSourceModel |
| 80 | + |
| 81 | + // Read Terraform configuration data into the model |
| 82 | + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) |
| 83 | + |
| 84 | + if resp.Diagnostics.HasError() { |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + team, err := d.client.TeamsClient.GetTeamByName(ctx, data.Name.ValueString()) |
| 89 | + if err != nil { |
| 90 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get team information, got error: %s", err)) |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + data.Id = types.StringValue(team.Id) |
| 95 | + data.Name = types.StringValue(team.Name) |
| 96 | + |
| 97 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 98 | +} |
0 commit comments