|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 7 | +) |
| 8 | + |
| 9 | +func dataSourceGithubAppToken() *schema.Resource { |
| 10 | + return &schema.Resource{ |
| 11 | + Read: dataSourceGithubAppTokenRead, |
| 12 | + |
| 13 | + Schema: map[string]*schema.Schema{ |
| 14 | + "app_id": { |
| 15 | + Type: schema.TypeString, |
| 16 | + Required: true, |
| 17 | + Description: descriptions["app_auth.id"], |
| 18 | + }, |
| 19 | + "installation_id": { |
| 20 | + Type: schema.TypeString, |
| 21 | + Required: true, |
| 22 | + Description: descriptions["app_auth.installation_id"], |
| 23 | + }, |
| 24 | + "pem_file": { |
| 25 | + Type: schema.TypeString, |
| 26 | + Required: true, |
| 27 | + Description: descriptions["app_auth.pem_file"], |
| 28 | + }, |
| 29 | + "token": { |
| 30 | + Type: schema.TypeString, |
| 31 | + Computed: true, |
| 32 | + Sensitive: true, |
| 33 | + Description: "The generated token from the credentials.", |
| 34 | + }, |
| 35 | + }, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func dataSourceGithubAppTokenRead(d *schema.ResourceData, meta interface{}) error { |
| 40 | + appID := d.Get("app_id").(string) |
| 41 | + installationID := d.Get("installation_id").(string) |
| 42 | + pemFile := d.Get("pem_file").(string) |
| 43 | + |
| 44 | + baseURL := meta.(*Owner).v3client.BaseURL.String() |
| 45 | + |
| 46 | + // The Go encoding/pem package only decodes PEM formatted blocks |
| 47 | + // that contain new lines. Some platforms, like Terraform Cloud, |
| 48 | + // do not support new lines within Environment Variables. |
| 49 | + // Any occurrence of \n in the `pem_file` argument's value |
| 50 | + // (explicit value, or default value taken from |
| 51 | + // GITHUB_APP_PEM_FILE Environment Variable) is replaced with an |
| 52 | + // actual new line character before decoding. |
| 53 | + pemFile = strings.Replace(pemFile, `\n`, "\n", -1) |
| 54 | + |
| 55 | + token, err := GenerateOAuthTokenFromApp(baseURL, appID, installationID, pemFile) |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + d.Set("token", token) |
| 60 | + d.SetId("id") |
| 61 | + |
| 62 | + return nil |
| 63 | +} |
0 commit comments