|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + gitpod "github.com/gitpod-io/gitpod-sdk-go" |
| 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 | +var _ datasource.DataSource = &runnerTokenDataSource{} |
| 14 | + |
| 15 | +type runnerTokenDataSource struct { |
| 16 | + client *gitpod.Client |
| 17 | +} |
| 18 | + |
| 19 | +func NewRunnerTokenDataSource() datasource.DataSource { |
| 20 | + return &runnerTokenDataSource{} |
| 21 | +} |
| 22 | + |
| 23 | +func (d *runnerTokenDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 24 | + resp.TypeName = req.ProviderTypeName + "_runner_token" |
| 25 | +} |
| 26 | + |
| 27 | +func (d *runnerTokenDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 28 | + resp.Schema = schema.Schema{ |
| 29 | + MarkdownDescription: `Retrieve a new authentication token for a Gitpod runner. |
| 30 | +
|
| 31 | +!> The ` + "`" + `exchange_token` + "`" + ` attribute is persisted to state.`, |
| 32 | + Attributes: map[string]schema.Attribute{ |
| 33 | + "runner_id": schema.StringAttribute{ |
| 34 | + Required: true, |
| 35 | + MarkdownDescription: "Runner ID.", |
| 36 | + }, |
| 37 | + "exchange_token": schema.StringAttribute{ |
| 38 | + Computed: true, |
| 39 | + Sensitive: true, |
| 40 | + MarkdownDescription: "A one-time use token that should be exchanged by the runner for an access token, using the IdentityService.ExchangeToken rpc. The token expires after 24 hours.", |
| 41 | + }, |
| 42 | + }, |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func (d *runnerTokenDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 47 | + client, ok := clientFromProviderData(req.ProviderData, &resp.Diagnostics) |
| 48 | + if !ok { |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + d.client = client |
| 53 | +} |
| 54 | + |
| 55 | +func (d *runnerTokenDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 56 | + var config runnerTokenDataSourceModel |
| 57 | + resp.Diagnostics.Append(req.Config.Get(ctx, &config)...) |
| 58 | + if resp.Diagnostics.HasError() { |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + runnerID := config.RunnerID.ValueString() |
| 63 | + |
| 64 | + token, err := d.client.Runners.NewRunnerToken(ctx, gitpod.RunnerNewRunnerTokenParams{ |
| 65 | + RunnerID: gitpod.F(runnerID), |
| 66 | + }) |
| 67 | + if err != nil { |
| 68 | + if isAPINotFound(err) { |
| 69 | + resp.Diagnostics.AddError("Runner not found", |
| 70 | + fmt.Sprintf("No runner found with ID %s", runnerID)) |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + resp.Diagnostics.AddError("Failed to retrieve runner token", err.Error()) |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + state := mapRunnerTokenToDataSourceModel(runnerID, token.ExchangeToken) |
| 79 | + if resp.Diagnostics.HasError() { |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + resp.Diagnostics.Append(resp.State.Set(ctx, &state)...) |
| 84 | +} |
| 85 | + |
| 86 | +func mapRunnerTokenToDataSourceModel(runnerID string, token string) runnerTokenDataSourceModel { |
| 87 | + return runnerTokenDataSourceModel{ |
| 88 | + RunnerID: types.StringValue(runnerID), |
| 89 | + ExchangeToken: types.StringValue(token), |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +type runnerTokenDataSourceModel struct { |
| 94 | + RunnerID types.String `tfsdk:"runner_id"` |
| 95 | + ExchangeToken types.String `tfsdk:"exchange_token"` |
| 96 | +} |
0 commit comments