|
| 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 | +} |
0 commit comments