forked from integrations/terraform-provider-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_github_dependabot_organization_secret_repositories.go
120 lines (98 loc) · 3.01 KB
/
resource_github_dependabot_organization_secret_repositories.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
110
111
112
113
114
115
116
117
118
119
120
package github
import (
"context"
"github.com/google/go-github/v46/github"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func resourceGithubDependabotOrganizationSecretRepositories() *schema.Resource {
return &schema.Resource{
Create: resourceGithubDependabotOrganizationSecretRepositoriesCreateOrUpdate,
Read: resourceGithubDependabotOrganizationSecretRepositoriesRead,
Update: resourceGithubDependabotOrganizationSecretRepositoriesCreateOrUpdate,
Delete: resourceGithubDependabotOrganizationSecretRepositoriesDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"secret_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateSecretNameFunc,
},
"selected_repository_ids": {
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
Set: schema.HashInt,
Required: true,
},
},
}
}
func resourceGithubDependabotOrganizationSecretRepositoriesCreateOrUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
ctx := context.Background()
err := checkOrganization(meta)
if err != nil {
return err
}
secretName := d.Get("secret_name").(string)
selectedRepositories := d.Get("selected_repository_ids")
selectedRepositoryIDs := []string{}
ids := selectedRepositories.(*schema.Set).List()
for _, id := range ids {
selectedRepositoryIDs = append(selectedRepositoryIDs, id.(string))
}
_, err = client.Dependabot.SetSelectedReposForOrgSecret(ctx, owner, secretName, selectedRepositoryIDs)
if err != nil {
return err
}
d.SetId(secretName)
return resourceGithubDependabotOrganizationSecretRepositoriesRead(d, meta)
}
func resourceGithubDependabotOrganizationSecretRepositoriesRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
ctx := context.Background()
err := checkOrganization(meta)
if err != nil {
return err
}
selectedRepositoryIDs := []int64{}
opt := &github.ListOptions{
PerPage: maxPerPage,
}
for {
results, resp, err := client.Dependabot.ListSelectedReposForOrgSecret(ctx, owner, d.Id(), opt)
if err != nil {
return err
}
for _, repo := range results.Repositories {
selectedRepositoryIDs = append(selectedRepositoryIDs, repo.GetID())
}
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
d.Set("selected_repository_ids", selectedRepositoryIDs)
return nil
}
func resourceGithubDependabotOrganizationSecretRepositoriesDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
ctx := context.WithValue(context.Background(), ctxId, d.Id())
err := checkOrganization(meta)
if err != nil {
return err
}
selectedRepositoryIDs := []string{}
_, err = client.Dependabot.SetSelectedReposForOrgSecret(ctx, owner, d.Id(), selectedRepositoryIDs)
if err != nil {
return err
}
return nil
}