forked from integrations/terraform-provider-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource_github_actions_organization_variable_repository.go
More file actions
152 lines (123 loc) · 3.76 KB
/
resource_github_actions_organization_variable_repository.go
File metadata and controls
152 lines (123 loc) · 3.76 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package github
import (
"context"
"log"
"strconv"
"github.com/google/go-github/v83/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceGithubActionsOrganizationVariableRepository() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"variable_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateDiagFunc: validateSecretNameFunc,
Description: "Name of the existing variable.",
},
"repository_id": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
Description: "The repository ID that can access the organization variable.",
},
},
CreateContext: resourceGithubActionsOrganizationVariableRepositoryCreate,
ReadContext: resourceGithubActionsOrganizationVariableRepositoryRead,
DeleteContext: resourceGithubActionsOrganizationVariableRepositoryDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceGithubActionsOrganizationVariableRepositoryImport,
},
}
}
func resourceGithubActionsOrganizationVariableRepositoryCreate(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
if err := checkOrganization(m); err != nil {
return diag.FromErr(err)
}
meta := m.(*Owner)
client := meta.v3client
owner := meta.name
variableName := d.Get("variable_name").(string)
repoID := d.Get("repository_id").(int)
repository := &github.Repository{
ID: new(int64(repoID)),
}
_, err := client.Actions.AddSelectedRepoToOrgVariable(ctx, owner, variableName, repository)
if err != nil {
return diag.FromErr(err)
}
id, err := buildID(variableName, strconv.Itoa(repoID))
if err != nil {
return diag.FromErr(err)
}
d.SetId(id)
return nil
}
func resourceGithubActionsOrganizationVariableRepositoryRead(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
if err := checkOrganization(m); err != nil {
return diag.FromErr(err)
}
meta := m.(*Owner)
client := meta.v3client
owner := meta.name
variableName := d.Get("variable_name").(string)
repoID := int64(d.Get("repository_id").(int))
opt := &github.ListOptions{
PerPage: maxPerPage,
}
for {
repos, resp, err := client.Actions.ListSelectedReposForOrgVariable(ctx, owner, variableName, opt)
if err != nil {
return diag.FromErr(err)
}
for _, repo := range repos.Repositories {
if repo.GetID() == repoID {
return nil
}
}
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
log.Printf("[INFO] Removing variable repository association %s from state because it no longer exists in GitHub", d.Id())
d.SetId("")
return nil
}
func resourceGithubActionsOrganizationVariableRepositoryDelete(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
if err := checkOrganization(m); err != nil {
return diag.FromErr(err)
}
meta := m.(*Owner)
client := meta.v3client
owner := meta.name
variableName := d.Get("variable_name").(string)
repoID := d.Get("repository_id").(int)
repository := &github.Repository{
ID: new(int64(repoID)),
}
_, err := client.Actions.RemoveSelectedRepoFromOrgVariable(ctx, owner, variableName, repository)
if err != nil {
return diag.FromErr(err)
}
return nil
}
func resourceGithubActionsOrganizationVariableRepositoryImport(ctx context.Context, d *schema.ResourceData, _ any) ([]*schema.ResourceData, error) {
variableName, repoIDStr, err := parseID2(d.Id())
if err != nil {
return nil, err
}
repoID, err := strconv.Atoi(repoIDStr)
if err != nil {
return nil, err
}
if err := d.Set("variable_name", variableName); err != nil {
return nil, err
}
if err := d.Set("repository_id", repoID); err != nil {
return nil, err
}
return []*schema.ResourceData{d}, nil
}