/
data_source_github_codespaces_secrets.go
109 lines (95 loc) · 2.33 KB
/
data_source_github_codespaces_secrets.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package github
import (
"context"
"fmt"
"github.com/google/go-github/v57/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceGithubCodespacesSecrets() *schema.Resource {
return &schema.Resource{
Read: dataSourceGithubCodespacesSecretsRead,
Schema: map[string]*schema.Schema{
"full_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Full name of the repository (in `org/name` format).",
ConflictsWith: []string{"name"},
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"full_name"},
Description: "The name of the repository.",
},
"secrets": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceGithubCodespacesSecretsRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
ctx := context.Background()
var repoName string
if fullName, ok := d.GetOk("full_name"); ok {
var err error
owner, repoName, err = splitRepoFullName(fullName.(string))
if err != nil {
return err
}
}
if name, ok := d.GetOk("name"); ok {
repoName = name.(string)
}
if repoName == "" {
return fmt.Errorf("one of %q or %q has to be provided", "full_name", "name")
}
options := github.ListOptions{
PerPage: 100,
}
var all_secrets []map[string]string
for {
secrets, resp, err := client.Codespaces.ListRepoSecrets(ctx, owner, repoName, &options)
if err != nil {
return err
}
for _, secret := range secrets.Secrets {
new_secret := map[string]string{
"name": secret.Name,
"created_at": secret.CreatedAt.String(),
"updated_at": secret.UpdatedAt.String(),
}
all_secrets = append(all_secrets, new_secret)
}
if resp.NextPage == 0 {
break
}
options.Page = resp.NextPage
}
d.SetId(repoName)
err := d.Set("secrets", all_secrets)
if err != nil {
return err
}
return nil
}