forked from integrations/terraform-provider-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource_github_repository_environment_deployment_policy.go
More file actions
217 lines (189 loc) · 6.41 KB
/
resource_github_repository_environment_deployment_policy.go
File metadata and controls
217 lines (189 loc) · 6.41 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package github
import (
"context"
"errors"
"fmt"
"log"
"net/http"
"net/url"
"strconv"
"github.com/google/go-github/v67/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceGithubRepositoryEnvironmentDeploymentPolicy() *schema.Resource {
return &schema.Resource{
Create: resourceGithubRepositoryEnvironmentDeploymentPolicyCreate,
Read: resourceGithubRepositoryEnvironmentDeploymentPolicyRead,
Update: resourceGithubRepositoryEnvironmentDeploymentPolicyUpdate,
Delete: resourceGithubRepositoryEnvironmentDeploymentPolicyDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"repository": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The name of the GitHub repository.",
},
"environment": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The name of the environment.",
},
"branch_pattern": {
Type: schema.TypeString,
Optional: true,
ForceNew: false,
ConflictsWith: []string{"tag_pattern"},
Description: "The name pattern that branches must match in order to deploy to the environment.",
},
"tag_pattern": {
Type: schema.TypeString,
Optional: true,
ForceNew: false,
ConflictsWith: []string{"branch_pattern"},
Description: "The name pattern that tags must match in order to deploy to the environment.",
},
},
CustomizeDiff: customDeploymentPolicyDiffFunction,
}
}
func resourceGithubRepositoryEnvironmentDeploymentPolicyCreate(d *schema.ResourceData, meta any) error {
client := meta.(*Owner).v3client
ctx := context.Background()
owner := meta.(*Owner).name
repoName := d.Get("repository").(string)
envName := d.Get("environment").(string)
escapedEnvName := url.PathEscape(envName)
var createData github.DeploymentBranchPolicyRequest
if v, ok := d.GetOk("branch_pattern"); ok {
createData = github.DeploymentBranchPolicyRequest{
Name: github.String(v.(string)),
Type: github.String("branch"),
}
} else if v, ok := d.GetOk("tag_pattern"); ok {
createData = github.DeploymentBranchPolicyRequest{
Name: github.String(v.(string)),
Type: github.String("tag"),
}
} else {
return fmt.Errorf("exactly one of %q and %q must be specified", "branch_pattern", "tag_pattern")
}
resultKey, _, err := client.Repositories.CreateDeploymentBranchPolicy(ctx, owner, repoName, escapedEnvName, &createData)
if err != nil {
return err
}
d.SetId(buildThreePartID(repoName, escapedEnvName, strconv.FormatInt(resultKey.GetID(), 10)))
return resourceGithubRepositoryEnvironmentDeploymentPolicyRead(d, meta)
}
func resourceGithubRepositoryEnvironmentDeploymentPolicyRead(d *schema.ResourceData, meta any) error {
client := meta.(*Owner).v3client
ctx := context.WithValue(context.Background(), ctxId, d.Id())
owner := meta.(*Owner).name
repoName, envName, branchPolicyIdString, err := parseThreePartID(d.Id(), "repository", "environment", "branchPolicyId")
if err != nil {
return err
}
branchPolicyId, err := strconv.ParseInt(branchPolicyIdString, 10, 64)
if err != nil {
return err
}
branchPolicy, _, err := client.Repositories.GetDeploymentBranchPolicy(ctx, owner, repoName, envName, branchPolicyId)
if err != nil {
ghErr := &github.ErrorResponse{}
if errors.As(err, &ghErr) {
if ghErr.Response.StatusCode == http.StatusNotModified {
return nil
}
if ghErr.Response.StatusCode == http.StatusNotFound {
log.Printf("[INFO] Removing branch deployment policy for %s/%s/%s from state because it no longer exists in GitHub",
owner, repoName, envName)
d.SetId("")
return nil
}
}
return err
}
if branchPolicy.GetType() == "branch" {
_ = d.Set("branch_pattern", branchPolicy.GetName())
} else {
_ = d.Set("tag_pattern", branchPolicy.GetName())
}
return nil
}
func resourceGithubRepositoryEnvironmentDeploymentPolicyUpdate(d *schema.ResourceData, meta any) error {
client := meta.(*Owner).v3client
ctx := context.Background()
owner := meta.(*Owner).name
repoName := d.Get("repository").(string)
envName := d.Get("environment").(string)
branchPattern := d.Get("branch_pattern").(string)
tagPattern := d.Get("tag_pattern").(string)
escapedEnvName := url.PathEscape(envName)
_, _, branchPolicyIdString, err := parseThreePartID(d.Id(), "repository", "environment", "branchPolicyId")
if err != nil {
return err
}
branchPolicyId, err := strconv.ParseInt(branchPolicyIdString, 10, 64)
if err != nil {
return err
}
pattern := branchPattern
if branchPattern == "" {
pattern = tagPattern
}
updateData := github.DeploymentBranchPolicyRequest{
Name: github.String(pattern),
}
resultKey, _, err := client.Repositories.UpdateDeploymentBranchPolicy(ctx, owner, repoName, escapedEnvName, branchPolicyId, &updateData)
if err != nil {
return err
}
d.SetId(buildThreePartID(repoName, escapedEnvName, strconv.FormatInt(resultKey.GetID(), 10)))
return resourceGithubRepositoryEnvironmentDeploymentPolicyRead(d, meta)
}
func resourceGithubRepositoryEnvironmentDeploymentPolicyDelete(d *schema.ResourceData, meta any) error {
client := meta.(*Owner).v3client
ctx := context.Background()
owner := meta.(*Owner).name
repoName, envName, branchPolicyIdString, err := parseThreePartID(d.Id(), "repository", "environment", "branchPolicyId")
if err != nil {
return err
}
branchPolicyId, err := strconv.ParseInt(branchPolicyIdString, 10, 64)
if err != nil {
return err
}
_, err = client.Repositories.DeleteDeploymentBranchPolicy(ctx, owner, repoName, envName, branchPolicyId)
if err != nil {
return err
}
return nil
}
func customDeploymentPolicyDiffFunction(_ context.Context, diff *schema.ResourceDiff, v any) error {
oldBranchPattern, newBranchPattern := diff.GetChange("branch_pattern")
if oldBranchPattern != "" && newBranchPattern == "" {
if err := diff.ForceNew("branch_pattern"); err != nil {
return err
}
}
if oldBranchPattern == "" && newBranchPattern != "" {
if err := diff.ForceNew("branch_pattern"); err != nil {
return err
}
}
oldTagPattern, newTagPattern := diff.GetChange("tag_pattern")
if oldTagPattern != "" && newTagPattern == "" {
if err := diff.ForceNew("tag_pattern"); err != nil {
return err
}
}
if oldTagPattern == "" && newTagPattern != "" {
if err := diff.ForceNew("tag_pattern"); err != nil {
return err
}
}
return nil
}