-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
coder_git_auth
data source (#100)
This data source enables template authors to require git authentication for specific providers on workspace build.
- Loading branch information
Showing
5 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "coder_git_auth Data Source - terraform-provider-coder" | ||
subcategory: "" | ||
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. | ||
--- | ||
|
||
# coder_git_auth (Data Source) | ||
|
||
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. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
provider "coder" { | ||
} | ||
data "coder_git_auth" "github" { | ||
# Matches the ID of the git auth provider in Coder. | ||
id = "github" | ||
} | ||
resource "coder_agent" "dev" { | ||
os = "linux" | ||
arch = "amd64" | ||
dir = "~/coder" | ||
env = { | ||
GITHUB_TOKEN : data.coder_git_auth.github.access_token | ||
} | ||
startup_script = <<EOF | ||
if [ ! -d ~/coder ]; then | ||
git clone https://github.com/coder/coder | ||
fi | ||
EOF | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `id` (String) The identifier of a configured git auth provider set up in your Coder deployment. | ||
|
||
### Read-Only | ||
|
||
- `access_token` (String) The access token returned by the git authentication provider. This can be used to pre-authenticate command-line tools. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
provider "coder" { | ||
} | ||
|
||
data "coder_git_auth" "github" { | ||
# Matches the ID of the git auth provider in Coder. | ||
id = "github" | ||
} | ||
|
||
resource "coder_agent" "dev" { | ||
os = "linux" | ||
arch = "amd64" | ||
dir = "~/coder" | ||
env = { | ||
GITHUB_TOKEN : data.coder_git_auth.github.access_token | ||
} | ||
startup_script = <<EOF | ||
if [ ! -d ~/coder ]; then | ||
git clone https://github.com/coder/coder | ||
fi | ||
EOF | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
// gitAuthDataSource returns a schema for a Git authentication data source. | ||
func gitAuthDataSource() *schema.Resource { | ||
return &schema.Resource{ | ||
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.", | ||
ReadContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics { | ||
rawID, ok := rd.GetOk("id") | ||
if !ok { | ||
return diag.Errorf("id is required") | ||
} | ||
id, ok := rawID.(string) | ||
if !ok { | ||
return diag.Errorf("unexpected type %q for id", rawID) | ||
} | ||
rd.SetId(id) | ||
|
||
accessToken := os.Getenv(GitAuthAccessTokenEnvironmentVariable(id)) | ||
rd.Set("access_token", accessToken) | ||
|
||
return nil | ||
}, | ||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "The identifier of a configured git auth provider set up in your Coder deployment.", | ||
}, | ||
"access_token": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "The access token returned by the git authentication provider. This can be used to pre-authenticate command-line tools.", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func GitAuthAccessTokenEnvironmentVariable(id string) string { | ||
return fmt.Sprintf("CODER_GIT_AUTH_ACCESS_TOKEN_%s", id) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package provider_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/coder/terraform-provider-coder/provider" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGitAuth(t *testing.T) { | ||
t.Parallel() | ||
|
||
resource.Test(t, resource.TestCase{ | ||
Providers: map[string]*schema.Provider{ | ||
"coder": provider.New(), | ||
}, | ||
IsUnitTest: true, | ||
Steps: []resource.TestStep{{ | ||
Config: ` | ||
provider "coder" { | ||
} | ||
data "coder_git_auth" "github" { | ||
id = "github" | ||
} | ||
`, | ||
Check: func(state *terraform.State) error { | ||
require.Len(t, state.Modules, 1) | ||
require.Len(t, state.Modules[0].Resources, 1) | ||
resource := state.Modules[0].Resources["data.coder_git_auth.github"] | ||
require.NotNil(t, resource) | ||
|
||
attribs := resource.Primary.Attributes | ||
require.Equal(t, "github", attribs["id"]) | ||
|
||
return nil | ||
}, | ||
}}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters