Skip to content

Commit 5b5acce

Browse files
committed
feat: add new data source for getting runner token
Added a new data source for retrieving a runner token (an exchange token). This is used when creating a runner for authentication. Added as a data source as the API only returns tokens that expire after 24 hours and does not otherwise manage them.
1 parent e700a98 commit 5b5acce

8 files changed

Lines changed: 149 additions & 1 deletion

File tree

AGENTS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Terraform provider for managing Gitpod resources on ona.com. The provider uses t
1010
- Provider type name: `ona`
1111
- Provider configuration: `api_key`, `base_url`, `max_retries`, `request_timeout`
1212
- Resources: `ona_project`, `ona_runner`, `ona_runner_scm_integration`, `ona_secret`
13-
- Data sources: `ona_authenticated_identity`, `ona_group`, `ona_groups`, `ona_project`, `ona_runner`, `ona_runner_environment_classes`, `ona_runners`
13+
- Data sources: `ona_authenticated_identity`, `ona_group`, `ona_groups`, `ona_project`, `ona_runner`, `ona_runner_environment_classes`, `ona_runners`, `ona_runner_token`
1414

1515
## Build and test commands
1616

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Terraform provider for managing [Gitpod](https://gitpod.io) resources on [ona.co
2828
- [Groups Data Source Docs](https://github.com/combor/terraform-provider-ona/blob/main/docs/data-sources/groups.md)
2929
- [Runner Environment Classes Data Source Docs](https://github.com/combor/terraform-provider-ona/blob/main/docs/data-sources/runner_environment_classes.md)
3030
- [Runners Data Source Docs](https://github.com/combor/terraform-provider-ona/blob/main/docs/data-sources/runners.md)
31+
- [Runner Token Data Source Docs](https://github.com/combor/terraform-provider-ona/blob/main/docs/data-sources/runner_token.md)
3132
- [Integration Example](https://github.com/combor/terraform-provider-ona/blob/main/examples/main.tf)
3233

3334
## Supported Types
@@ -48,6 +49,7 @@ Data sources:
4849
- `ona_runner`
4950
- `ona_runner_environment_classes`
5051
- `ona_runners`
52+
- `ona_runner_token`
5153

5254
## Using the Provider
5355

docs/data-sources/runner_token.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "ona_runner_token Data Source - terraform-provider-ona"
4+
subcategory: ""
5+
description: |-
6+
Retrieve a new authentication token for a Gitpod runner.
7+
---
8+
9+
# ona_runner_token (Data Source)
10+
11+
Retrieve a new authentication token for a Gitpod runner.
12+
13+
## Example Usage
14+
15+
```terraform
16+
data "ona_runner_token" "example" {
17+
runner_id = "<runner-id>"
18+
}
19+
```
20+
21+
<!-- schema generated by tfplugindocs -->
22+
## Schema
23+
24+
### Required
25+
26+
- `runner_id` (String) Runner ID.
27+
28+
### Read-Only
29+
30+
- `exchange_token` (String, Sensitive) 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.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
data "ona_runner_token" "example" {
2+
runner_id = "<runner-id>"
3+
}

examples/main.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ data "ona_runner" "example" {
3737
id = ona_runner.example.id
3838
}
3939

40+
data "ona_runner_token" "example" {
41+
runner_id = ona_runner.example.id
42+
}
43+
4044
data "ona_runner_environment_classes" "example" {
4145
runner_id = ona_runner.example.id
4246
}

internal/provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ func (p *onaProvider) DataSources(_ context.Context) []func() datasource.DataSou
182182
NewRunnerEnvironmentClassesDataSource,
183183
NewRunnerDataSource,
184184
NewRunnersDataSource,
185+
NewRunnerTokenDataSource,
185186
}
186187
}
187188

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
Attributes: map[string]schema.Attribute{
31+
"runner_id": schema.StringAttribute{
32+
Required: true,
33+
MarkdownDescription: "Runner ID.",
34+
},
35+
"exchange_token": schema.StringAttribute{
36+
Computed: true,
37+
Sensitive: true,
38+
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.",
39+
},
40+
},
41+
}
42+
}
43+
44+
func (d *runnerTokenDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
45+
client, ok := clientFromProviderData(req.ProviderData, &resp.Diagnostics)
46+
if !ok {
47+
return
48+
}
49+
50+
d.client = client
51+
}
52+
53+
func (d *runnerTokenDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
54+
var config runnerTokenDataSourceModel
55+
resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)
56+
if resp.Diagnostics.HasError() {
57+
return
58+
}
59+
60+
runnerID := config.RunnerID.ValueString()
61+
62+
token, err := d.client.Runners.NewRunnerToken(ctx, gitpod.RunnerNewRunnerTokenParams{
63+
RunnerID: gitpod.F(runnerID),
64+
})
65+
if err != nil {
66+
if isAPINotFound(err) {
67+
resp.Diagnostics.AddError("Runner not found",
68+
fmt.Sprintf("No runner found with ID %s", runnerID))
69+
return
70+
}
71+
72+
resp.Diagnostics.AddError("Failed to retrieve runner token", err.Error())
73+
return
74+
}
75+
76+
state := mapRunnerTokenToDataSourceModel(runnerID, token.ExchangeToken)
77+
if resp.Diagnostics.HasError() {
78+
return
79+
}
80+
81+
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
82+
}
83+
84+
func mapRunnerTokenToDataSourceModel(runnerID string, token string) runnerTokenDataSourceModel {
85+
return runnerTokenDataSourceModel{
86+
RunnerID: types.StringValue(runnerID),
87+
ExchangeToken: types.StringValue(token),
88+
}
89+
}
90+
91+
type runnerTokenDataSourceModel struct {
92+
RunnerID types.String `tfsdk:"runner_id"`
93+
ExchangeToken types.String `tfsdk:"exchange_token"`
94+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package provider
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestMapRunnerTokenToDataSourceModel(t *testing.T) {
10+
got := mapRunnerTokenToDataSourceModel("runner-123", "abcdefghijklmnopqrstuvwxyz")
11+
12+
assert.Equal(t, "runner-123", got.RunnerID.ValueString())
13+
assert.Equal(t, "abcdefghijklmnopqrstuvwxyz", got.ExchangeToken.ValueString())
14+
}

0 commit comments

Comments
 (0)