-
Notifications
You must be signed in to change notification settings - Fork 927
feat: add environment secret import capabilities #2462
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
a1a61d8
d083de0
96f2a87
3bc2804
f5cc064
f493238
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,9 +3,11 @@ package github | |
| import ( | ||
| "context" | ||
| "encoding/base64" | ||
| "fmt" | ||
| "log" | ||
| "net/http" | ||
| "net/url" | ||
| "strings" | ||
|
|
||
| "github.com/google/go-github/v66/github" | ||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
|
@@ -17,6 +19,9 @@ func resourceGithubActionsEnvironmentSecret() *schema.Resource { | |
| Create: resourceGithubActionsEnvironmentSecretCreateOrUpdate, | ||
| Read: resourceGithubActionsEnvironmentSecretRead, | ||
| Delete: resourceGithubActionsEnvironmentSecretDelete, | ||
| Importer: &schema.ResourceImporter{ | ||
| State: resourceGithubActionsEnvironmentSecretImport, | ||
| }, | ||
|
|
||
| Schema: map[string]*schema.Schema{ | ||
| "repository": { | ||
|
|
@@ -211,6 +216,44 @@ func resourceGithubActionsEnvironmentSecretDelete(d *schema.ResourceData, meta i | |
| return err | ||
| } | ||
|
|
||
| func resourceGithubActionsEnvironmentSecretImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { | ||
| client := meta.(*Owner).v3client | ||
| owner := meta.(*Owner).name | ||
| ctx := context.Background() | ||
|
|
||
| parts := strings.Split(d.Id(), "/") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you use colon for the import separator? Then you could use |
||
| if len(parts) != 3 { | ||
| return nil, fmt.Errorf("invalid ID specified: supplied ID must be written as <repository>/<environment>/<secret_name>") | ||
| } | ||
|
|
||
| d.SetId(buildThreePartID(parts[0], parts[1], parts[2])) | ||
|
|
||
| repoName, envName, secretName, err := parseThreePartID(d.Id(), "repository", "environment", "secret_name") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| repo, _, err := client.Repositories.Get(ctx, owner, repoName) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This shouldn't be necessary if the import ID contains enough information to be able to successfully run Read afterwards. |
||
|
|
||
| escapedEnvName := url.PathEscape(envName) | ||
|
|
||
| secret, _, err := client.Actions.GetEnvSecret(ctx, int(repo.GetID()), escapedEnvName, secretName) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| d.Set("repository", repoName) | ||
| d.Set("secret_name", secretName) | ||
| d.Set("environment", envName) | ||
|
|
||
| // encrypted_value or plaintext_value can not be imported | ||
|
|
||
| d.Set("created_at", secret.CreatedAt.String()) | ||
| d.Set("updated_at", secret.UpdatedAt.String()) | ||
|
|
||
| return []*schema.ResourceData{d}, nil | ||
| } | ||
|
|
||
| func getEnvironmentPublicKeyDetails(repoID int64, envName string, meta interface{}) (keyId, pkValue string, err error) { | ||
| client := meta.(*Owner).v3client | ||
| ctx := context.Background() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -167,4 +167,59 @@ func TestAccGithubActionsEnvironmentSecret(t *testing.T) { | |
|
|
||
| }) | ||
|
|
||
| t.Run("imports secret without error", func(t *testing.T) { | ||
| secretValue := "super_secret_value" | ||
| envName := "environment / test" | ||
| secretName := "test_plaintext_secret_name" | ||
| secretValue := "secret_value" | ||
|
|
||
| config := fmt.Sprintf(` | ||
| resource "github_repository" "test" { | ||
| name = "tf-acc-test-%s" | ||
| } | ||
|
|
||
| resource "github_repository_environment" "test" { | ||
| repository = github_repository.test.name | ||
| environment = "%s" | ||
| } | ||
|
|
||
| resource "github_actions_environment_secret" "secret" { | ||
| repository = github_repository.test.name | ||
| environment = github_repository_environment.test.environment | ||
| secret_name = "%s" | ||
| plaintext_value = "%s" | ||
| } | ||
| `, randomID, envName, secretName, secretValue) | ||
|
|
||
| testCase := func(t *testing.T, mode string) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We've abandoned this pattern in tests, could you conform to the new pattern? |
||
| resource.Test(t, resource.TestCase{ | ||
| PreCheck: func() { skipUnlessMode(t, mode) }, | ||
| Providers: testAccProviders, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| Config: config, | ||
| }, | ||
| { | ||
| ResourceName: "github_actions_environment_secret.secret", | ||
| ImportStateId: fmt.Sprintf(`tf-acc-test-%s:%s:%s`, randomID, envName, secretName), | ||
| ImportState: true, | ||
| ImportStateVerify: true, | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| t.Run("with an anonymous account", func(t *testing.T) { | ||
| t.Skip("anonymous account not supported for this operation") | ||
| }) | ||
|
|
||
| t.Run("with an individual account", func(t *testing.T) { | ||
| testCase(t, individual) | ||
| }) | ||
|
|
||
| t.Run("with an organization account", func(t *testing.T) { | ||
| testCase(t, organization) | ||
| }) | ||
|
|
||
| }) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you use Context-aware provider functions instead?