Skip to content

Commit 101f35b

Browse files
authored
feat: add coder_git_auth data source (#100)
This data source enables template authors to require git authentication for specific providers on workspace build.
1 parent 47888bd commit 101f35b

File tree

5 files changed

+165
-0
lines changed

5 files changed

+165
-0
lines changed

docs/data-sources/git_auth.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "coder_git_auth Data Source - terraform-provider-coder"
4+
subcategory: ""
5+
description: |-
6+
Use this data source to require users to authenticate with a Git provider prior to workspace creation. This can be used to perform an authenticated git clone in startup scripts.
7+
---
8+
9+
# coder_git_auth (Data Source)
10+
11+
Use this data source to require users to authenticate with a Git provider prior to workspace creation. This can be used to perform an authenticated `git clone` in startup scripts.
12+
13+
## Example Usage
14+
15+
```terraform
16+
provider "coder" {
17+
}
18+
19+
data "coder_git_auth" "github" {
20+
# Matches the ID of the git auth provider in Coder.
21+
id = "github"
22+
}
23+
24+
resource "coder_agent" "dev" {
25+
os = "linux"
26+
arch = "amd64"
27+
dir = "~/coder"
28+
env = {
29+
GITHUB_TOKEN : data.coder_git_auth.github.access_token
30+
}
31+
startup_script = <<EOF
32+
if [ ! -d ~/coder ]; then
33+
git clone https://github.com/coder/coder
34+
fi
35+
EOF
36+
}
37+
```
38+
39+
<!-- schema generated by tfplugindocs -->
40+
## Schema
41+
42+
### Required
43+
44+
- `id` (String) The identifier of a configured git auth provider set up in your Coder deployment.
45+
46+
### Read-Only
47+
48+
- `access_token` (String) The access token returned by the git authentication provider. This can be used to pre-authenticate command-line tools.
49+
50+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
provider "coder" {
2+
}
3+
4+
data "coder_git_auth" "github" {
5+
# Matches the ID of the git auth provider in Coder.
6+
id = "github"
7+
}
8+
9+
resource "coder_agent" "dev" {
10+
os = "linux"
11+
arch = "amd64"
12+
dir = "~/coder"
13+
env = {
14+
GITHUB_TOKEN : data.coder_git_auth.github.access_token
15+
}
16+
startup_script = <<EOF
17+
if [ ! -d ~/coder ]; then
18+
git clone https://github.com/coder/coder
19+
fi
20+
EOF
21+
}

provider/gitauth.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
)
11+
12+
// gitAuthDataSource returns a schema for a Git authentication data source.
13+
func gitAuthDataSource() *schema.Resource {
14+
return &schema.Resource{
15+
Description: "Use this data source to require users to authenticate with a Git provider prior to workspace creation. This can be used to perform an authenticated `git clone` in startup scripts.",
16+
ReadContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics {
17+
rawID, ok := rd.GetOk("id")
18+
if !ok {
19+
return diag.Errorf("id is required")
20+
}
21+
id, ok := rawID.(string)
22+
if !ok {
23+
return diag.Errorf("unexpected type %q for id", rawID)
24+
}
25+
rd.SetId(id)
26+
27+
accessToken := os.Getenv(GitAuthAccessTokenEnvironmentVariable(id))
28+
rd.Set("access_token", accessToken)
29+
30+
return nil
31+
},
32+
Schema: map[string]*schema.Schema{
33+
"id": {
34+
Type: schema.TypeString,
35+
Required: true,
36+
Description: "The identifier of a configured git auth provider set up in your Coder deployment.",
37+
},
38+
"access_token": {
39+
Type: schema.TypeString,
40+
Computed: true,
41+
Description: "The access token returned by the git authentication provider. This can be used to pre-authenticate command-line tools.",
42+
},
43+
},
44+
}
45+
}
46+
47+
func GitAuthAccessTokenEnvironmentVariable(id string) string {
48+
return fmt.Sprintf("CODER_GIT_AUTH_ACCESS_TOKEN_%s", id)
49+
}

provider/gitauth_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package provider_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/coder/terraform-provider-coder/provider"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
11+
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
func TestGitAuth(t *testing.T) {
16+
t.Parallel()
17+
18+
resource.Test(t, resource.TestCase{
19+
Providers: map[string]*schema.Provider{
20+
"coder": provider.New(),
21+
},
22+
IsUnitTest: true,
23+
Steps: []resource.TestStep{{
24+
Config: `
25+
provider "coder" {
26+
}
27+
data "coder_git_auth" "github" {
28+
id = "github"
29+
}
30+
`,
31+
Check: func(state *terraform.State) error {
32+
require.Len(t, state.Modules, 1)
33+
require.Len(t, state.Modules[0].Resources, 1)
34+
resource := state.Modules[0].Resources["data.coder_git_auth.github"]
35+
require.NotNil(t, resource)
36+
37+
attribs := resource.Primary.Attributes
38+
require.Equal(t, "github", attribs["id"])
39+
40+
return nil
41+
},
42+
}},
43+
})
44+
}

provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func New() *schema.Provider {
6969
"coder_workspace": workspaceDataSource(),
7070
"coder_provisioner": provisionerDataSource(),
7171
"coder_parameter": parameterDataSource(),
72+
"coder_git_auth": gitAuthDataSource(),
7273
},
7374
ResourcesMap: map[string]*schema.Resource{
7475
"coder_agent": agentResource(),

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy