/
resource_github_repository_deploy_key.go
158 lines (134 loc) · 4.06 KB
/
resource_github_repository_deploy_key.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
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
package github
import (
"context"
"log"
"net/http"
"regexp"
"strconv"
"strings"
"github.com/google/go-github/github"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceGithubRepositoryDeployKey() *schema.Resource {
return &schema.Resource{
Create: resourceGithubRepositoryDeployKeyCreate,
Read: resourceGithubRepositoryDeployKeyRead,
Delete: resourceGithubRepositoryDeployKeyDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
// Deploy keys are defined immutable in the API. Updating results in force new.
Schema: map[string]*schema.Schema{
"key": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: suppressDeployKeyDiff,
},
"read_only": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Default: true,
},
"repository": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"title": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"etag": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceGithubRepositoryDeployKeyCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
repoName := d.Get("repository").(string)
key := d.Get("key").(string)
title := d.Get("title").(string)
readOnly := d.Get("read_only").(bool)
owner := meta.(*Organization).name
ctx := context.Background()
log.Printf("[DEBUG] Creating repository deploy key: %s (%s/%s)", title, owner, repoName)
resultKey, _, err := client.Repositories.CreateKey(ctx, owner, repoName, &github.Key{
Key: github.String(key),
Title: github.String(title),
ReadOnly: github.Bool(readOnly),
})
if err != nil {
return err
}
id := strconv.FormatInt(*resultKey.ID, 10)
d.SetId(buildTwoPartID(&repoName, &id))
return resourceGithubRepositoryDeployKeyRead(d, meta)
}
func resourceGithubRepositoryDeployKeyRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
owner := meta.(*Organization).name
repoName, idString, err := parseTwoPartID(d.Id())
if err != nil {
return err
}
id, err := strconv.ParseInt(idString, 10, 64)
if err != nil {
return unconvertibleIdErr(idString, err)
}
ctx := context.WithValue(context.Background(), ctxId, d.Id())
if !d.IsNewResource() {
ctx = context.WithValue(ctx, ctxEtag, d.Get("etag").(string))
}
log.Printf("[DEBUG] Reading repository deploy key: %s (%s/%s)", d.Id(), owner, repoName)
key, resp, err := client.Repositories.GetKey(ctx, owner, repoName, id)
if err != nil {
if ghErr, ok := err.(*github.ErrorResponse); ok {
if ghErr.Response.StatusCode == http.StatusNotModified {
return nil
}
if ghErr.Response.StatusCode == http.StatusNotFound {
log.Printf("[WARN] Removing repository deploy key %s from state because it no longer exists in GitHub",
d.Id())
d.SetId("")
return nil
}
}
return err
}
d.Set("etag", resp.Header.Get("ETag"))
d.Set("key", key.Key)
d.Set("read_only", key.ReadOnly)
d.Set("repository", repoName)
d.Set("title", key.Title)
return nil
}
func resourceGithubRepositoryDeployKeyDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
owner := meta.(*Organization).name
repoName, idString, err := parseTwoPartID(d.Id())
if err != nil {
return err
}
id, err := strconv.ParseInt(idString, 10, 64)
if err != nil {
return unconvertibleIdErr(idString, err)
}
ctx := context.WithValue(context.Background(), ctxId, d.Id())
log.Printf("[DEBUG] Deleting repository deploy key: %s (%s/%s)", idString, owner, repoName)
_, err = client.Repositories.DeleteKey(ctx, owner, repoName, id)
if err != nil {
return err
}
return err
}
func suppressDeployKeyDiff(k, oldV, newV string, d *schema.ResourceData) bool {
newV = strings.TrimSpace(newV)
keyRe := regexp.MustCompile(`^([a-z0-9-]+ [^\s]+)( [^\s]+)?$`)
newTrimmed := keyRe.ReplaceAllString(newV, "$1")
return oldV == newTrimmed
}