Skip to content

Commit 46524ee

Browse files
authored
Merge pull request #19 from beekeep/feat/runner-token-data-source
Thanks for this!
2 parents 43bfdca + 1bec190 commit 46524ee

8 files changed

Lines changed: 154 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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
!> The exchange_token attribute is persisted to state.
8+
---
9+
10+
# ona_runner_token (Data Source)
11+
12+
Retrieve a new authentication token for a Gitpod runner.
13+
14+
!> The `exchange_token` attribute is persisted to state.
15+
16+
## Example Usage
17+
18+
```terraform
19+
data "ona_runner_token" "example" {
20+
runner_id = "<runner-id>"
21+
}
22+
```
23+
24+
<!-- schema generated by tfplugindocs -->
25+
## Schema
26+
27+
### Required
28+
29+
- `runner_id` (String) Runner ID.
30+
31+
### Read-Only
32+
33+
- `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: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
}
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)