Skip to content

Commit 9e522d6

Browse files
bpaquetkfcampbell
andauthored
[Feature] Add data source to read secrets (#1245)
* Add github_actions_secrets * Update sdk version * Improve doc * Fix merge * Reorder github.erb for alphabetization (is that a word?) Co-authored-by: Keegan Campbell <[email protected]>
1 parent ce9a54a commit 9e522d6

14 files changed

+763
-24
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package github
2+
3+
import (
4+
"context"
5+
6+
"github.com/google/go-github/v47/github"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
9+
)
10+
11+
func dataSourceGithubActionsOrganizationSecrets() *schema.Resource {
12+
return &schema.Resource{
13+
Read: dataSourceGithubActionsOrganizationSecretsRead,
14+
15+
Schema: map[string]*schema.Schema{
16+
"secrets": {
17+
Type: schema.TypeList,
18+
Computed: true,
19+
Elem: &schema.Resource{
20+
Schema: map[string]*schema.Schema{
21+
"name": {
22+
Type: schema.TypeString,
23+
Computed: true,
24+
},
25+
"visibility": {
26+
Type: schema.TypeString,
27+
Computed: true,
28+
},
29+
"created_at": {
30+
Type: schema.TypeString,
31+
Computed: true,
32+
},
33+
"updated_at": {
34+
Type: schema.TypeString,
35+
Computed: true,
36+
},
37+
},
38+
},
39+
},
40+
},
41+
}
42+
}
43+
44+
func dataSourceGithubActionsOrganizationSecretsRead(d *schema.ResourceData, meta interface{}) error {
45+
client := meta.(*Owner).v3client
46+
owner := meta.(*Owner).name
47+
48+
options := github.ListOptions{
49+
PerPage: 100,
50+
}
51+
52+
var all_secrets []map[string]string
53+
for {
54+
secrets, resp, err := client.Actions.ListOrgSecrets(context.TODO(), owner, &options)
55+
if err != nil {
56+
return err
57+
}
58+
for _, secret := range secrets.Secrets {
59+
new_secret := map[string]string{
60+
"name": secret.Name,
61+
"created_at": secret.CreatedAt.String(),
62+
"updated_at": secret.UpdatedAt.String(),
63+
}
64+
all_secrets = append(all_secrets, new_secret)
65+
66+
}
67+
if resp.NextPage == 0 {
68+
break
69+
}
70+
options.Page = resp.NextPage
71+
}
72+
73+
d.SetId(owner)
74+
d.Set("secrets", all_secrets)
75+
76+
return nil
77+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package github
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
9+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
10+
)
11+
12+
func TestAccGithubActionsOrganizationSecretsDataSource(t *testing.T) {
13+
14+
t.Run("queries organization actions secrets from a repository", func(t *testing.T) {
15+
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
16+
17+
config := fmt.Sprintf(`
18+
resource "github_actions_organization_secret" "test" {
19+
secret_name = "org_secret_1_%s"
20+
plaintext_value = "foo"
21+
visibility = "private"
22+
}
23+
`, randomID)
24+
25+
config2 := config + `
26+
data "github_actions_organization_secrets" "test" {
27+
}
28+
`
29+
30+
check := resource.ComposeTestCheckFunc(
31+
resource.TestCheckResourceAttr("data.github_actions_organization_secrets.test", "secrets.#", "1"),
32+
resource.TestCheckResourceAttr("data.github_actions_organization_secrets.test", "secrets.0.name", strings.ToUpper(fmt.Sprintf("ORG_SECRET_1_%s", randomID))),
33+
resource.TestCheckResourceAttr("data.github_actions_organization_secrets.test", "secrets.0.visibility", "private"),
34+
resource.TestCheckResourceAttrSet("data.github_actions_organization_secrets.test", "secrets.0.created_at"),
35+
resource.TestCheckResourceAttrSet("data.github_actions_organization_secrets.test", "secrets.0.updated_at"),
36+
)
37+
38+
testCase := func(t *testing.T, mode string) {
39+
resource.Test(t, resource.TestCase{
40+
PreCheck: func() { skipUnlessMode(t, mode) },
41+
Providers: testAccProviders,
42+
Steps: []resource.TestStep{
43+
{
44+
Config: config,
45+
Check: resource.ComposeTestCheckFunc(),
46+
},
47+
{
48+
Config: config2,
49+
Check: check,
50+
},
51+
},
52+
})
53+
}
54+
55+
t.Run("with an organization account", func(t *testing.T) {
56+
testCase(t, organization)
57+
})
58+
})
59+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/google/go-github/v47/github"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
10+
)
11+
12+
func dataSourceGithubActionsSecrets() *schema.Resource {
13+
return &schema.Resource{
14+
Read: dataSourceGithubActionsSecretsRead,
15+
16+
Schema: map[string]*schema.Schema{
17+
"full_name": {
18+
Type: schema.TypeString,
19+
Optional: true,
20+
Computed: true,
21+
ConflictsWith: []string{"name"},
22+
},
23+
"name": {
24+
Type: schema.TypeString,
25+
Optional: true,
26+
Computed: true,
27+
ConflictsWith: []string{"full_name"},
28+
},
29+
"secrets": {
30+
Type: schema.TypeList,
31+
Computed: true,
32+
Elem: &schema.Resource{
33+
Schema: map[string]*schema.Schema{
34+
"name": {
35+
Type: schema.TypeString,
36+
Computed: true,
37+
},
38+
"created_at": {
39+
Type: schema.TypeString,
40+
Computed: true,
41+
},
42+
"updated_at": {
43+
Type: schema.TypeString,
44+
Computed: true,
45+
},
46+
},
47+
},
48+
},
49+
},
50+
}
51+
}
52+
53+
func dataSourceGithubActionsSecretsRead(d *schema.ResourceData, meta interface{}) error {
54+
client := meta.(*Owner).v3client
55+
owner := meta.(*Owner).name
56+
var repoName string
57+
58+
if fullName, ok := d.GetOk("full_name"); ok {
59+
var err error
60+
owner, repoName, err = splitRepoFullName(fullName.(string))
61+
if err != nil {
62+
return err
63+
}
64+
}
65+
66+
if name, ok := d.GetOk("name"); ok {
67+
repoName = name.(string)
68+
}
69+
70+
if repoName == "" {
71+
return fmt.Errorf("one of %q or %q has to be provided", "full_name", "name")
72+
}
73+
74+
options := github.ListOptions{
75+
PerPage: 100,
76+
}
77+
78+
var all_secrets []map[string]string
79+
for {
80+
secrets, resp, err := client.Actions.ListRepoSecrets(context.TODO(), owner, repoName, &options)
81+
if err != nil {
82+
return err
83+
}
84+
for _, secret := range secrets.Secrets {
85+
new_secret := map[string]string{
86+
"name": secret.Name,
87+
"created_at": secret.CreatedAt.String(),
88+
"updated_at": secret.UpdatedAt.String(),
89+
}
90+
all_secrets = append(all_secrets, new_secret)
91+
}
92+
if resp.NextPage == 0 {
93+
break
94+
}
95+
options.Page = resp.NextPage
96+
}
97+
98+
d.SetId(repoName)
99+
d.Set("secrets", all_secrets)
100+
101+
return nil
102+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package github
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
9+
)
10+
11+
func TestAccGithubActionsSecretsDataSource(t *testing.T) {
12+
13+
t.Run("queries actions secrets from a repository", func(t *testing.T) {
14+
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
15+
16+
config := fmt.Sprintf(`
17+
resource "github_repository" "test" {
18+
name = "tf-acc-test-%s"
19+
auto_init = true
20+
}
21+
22+
resource "github_actions_secret" "test" {
23+
secret_name = "secret_1"
24+
repository = github_repository.test.name
25+
plaintext_value = "foo"
26+
}
27+
`, randomID)
28+
29+
config2 := config + `
30+
data "github_actions_secrets" "test" {
31+
name = github_repository.test.name
32+
}
33+
`
34+
35+
check := resource.ComposeTestCheckFunc(
36+
resource.TestCheckResourceAttr("data.github_actions_secrets.test", "name", fmt.Sprintf("tf-acc-test-%s", randomID)),
37+
resource.TestCheckResourceAttr("data.github_actions_secrets.test", "secrets.#", "1"),
38+
resource.TestCheckResourceAttr("data.github_actions_secrets.test", "secrets.0.name", "SECRET_1"),
39+
resource.TestCheckResourceAttrSet("data.github_actions_secrets.test", "secrets.0.created_at"),
40+
resource.TestCheckResourceAttrSet("data.github_actions_secrets.test", "secrets.0.updated_at"),
41+
)
42+
43+
testCase := func(t *testing.T, mode string) {
44+
resource.Test(t, resource.TestCase{
45+
PreCheck: func() { skipUnlessMode(t, mode) },
46+
Providers: testAccProviders,
47+
Steps: []resource.TestStep{
48+
{
49+
Config: config,
50+
Check: resource.ComposeTestCheckFunc(),
51+
},
52+
{
53+
Config: config2,
54+
Check: check,
55+
},
56+
},
57+
})
58+
}
59+
60+
t.Run("with an organization account", func(t *testing.T) {
61+
testCase(t, organization)
62+
})
63+
})
64+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package github
2+
3+
import (
4+
"context"
5+
6+
"github.com/google/go-github/v47/github"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
9+
)
10+
11+
func dataSourceGithubDependabotOrganizationSecrets() *schema.Resource {
12+
return &schema.Resource{
13+
Read: dataSourceGithubDependabotOrganizationSecretsRead,
14+
15+
Schema: map[string]*schema.Schema{
16+
"secrets": {
17+
Type: schema.TypeList,
18+
Computed: true,
19+
Elem: &schema.Resource{
20+
Schema: map[string]*schema.Schema{
21+
"name": {
22+
Type: schema.TypeString,
23+
Computed: true,
24+
},
25+
"visibility": {
26+
Type: schema.TypeString,
27+
Computed: true,
28+
},
29+
"created_at": {
30+
Type: schema.TypeString,
31+
Computed: true,
32+
},
33+
"updated_at": {
34+
Type: schema.TypeString,
35+
Computed: true,
36+
},
37+
},
38+
},
39+
},
40+
},
41+
}
42+
}
43+
44+
func dataSourceGithubDependabotOrganizationSecretsRead(d *schema.ResourceData, meta interface{}) error {
45+
client := meta.(*Owner).v3client
46+
owner := meta.(*Owner).name
47+
48+
options := github.ListOptions{
49+
PerPage: 100,
50+
}
51+
52+
var all_secrets []map[string]string
53+
for {
54+
secrets, resp, err := client.Dependabot.ListOrgSecrets(context.TODO(), owner, &options)
55+
if err != nil {
56+
return err
57+
}
58+
for _, secret := range secrets.Secrets {
59+
new_secret := map[string]string{
60+
"name": secret.Name,
61+
"visibility": secret.Visibility,
62+
"created_at": secret.CreatedAt.String(),
63+
"updated_at": secret.UpdatedAt.String(),
64+
}
65+
all_secrets = append(all_secrets, new_secret)
66+
67+
}
68+
if resp.NextPage == 0 {
69+
break
70+
}
71+
options.Page = resp.NextPage
72+
}
73+
74+
d.SetId(owner)
75+
d.Set("secrets", all_secrets)
76+
77+
return nil
78+
}

0 commit comments

Comments
 (0)