generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 44
/
resource_gnupg.go
285 lines (256 loc) · 9.53 KB
/
resource_gnupg.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package gnupg
import (
"context"
"github.com/antihax/optional"
hh "github.com/harness/harness-go-sdk/harness/helpers"
"github.com/harness/harness-go-sdk/harness/nextgen"
"github.com/harness/terraform-provider-harness/helpers"
"github.com/harness/terraform-provider-harness/internal"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func ResourceGitopsGnupg() *schema.Resource {
resource := &schema.Resource{
Description: "GPG public key in the server's configuration.",
CreateContext: resourceGitopsGnupgCreate,
ReadContext: resourceGitopsGnupgRead,
UpdateContext: resourceGitopsGnupgCreate,
DeleteContext: resourceGitopsGnupgDelete,
Importer: helpers.GitopsAgentResourceImporter,
Schema: map[string]*schema.Schema{
"account_id": {
Description: "Account Identifier for the GnuPG Key.",
Type: schema.TypeString,
Required: true,
},
"agent_id": {
Description: "Agent identifier for the GnuPG Key.",
Type: schema.TypeString,
Required: true,
},
"org_id": {
Description: "Organization Identifier for the GnuPG Key.",
Type: schema.TypeString,
Optional: true,
},
"project_id": {
Description: "Project Identifier for the GnuPG Key.",
Type: schema.TypeString,
Optional: true,
},
"identifier": {
Description: "Identifier for the GnuPG Key.",
Type: schema.TypeString,
Computed: true,
},
"request": {
Description: "GnuPGPublicKey is a representation of a GnuPG public key",
Type: schema.TypeList,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"upsert": {
Description: "Indicates if the GnuPG Key should be inserted if not present or updated if present.",
Type: schema.TypeBool,
Required: true,
},
"publickey": {
Description: "Public key details.",
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key_id": {
Description: "KeyID specifies the key ID, in hexadecimal string format.",
Type: schema.TypeString,
Computed: true,
},
"fingerprint": {
Description: "Fingerprint is the fingerprint of the key",
Type: schema.TypeString,
Computed: true,
},
"owner": {
Description: "Owner holds the owner identification, e.g. a name and e-mail address",
Type: schema.TypeString,
Computed: true,
},
"trust": {
Description: "Trust holds the level of trust assigned to this key",
Type: schema.TypeString,
Computed: true,
},
"sub_type": {
Description: "SubType holds the key's sub type",
Type: schema.TypeString,
Computed: true,
},
"key_data": {
Description: "KeyData holds the raw key data, in base64 encoded format",
Type: schema.TypeString,
Required: true,
},
},
},
},
},
},
},
},
}
return resource
}
func resourceGitopsGnupgCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
ctx = context.WithValue(ctx, nextgen.ContextAccessToken, hh.EnvVars.BearerToken.Get())
var agentIdentifier, accountIdentifier, orgIdentifier, projectIdentifier string
accountIdentifier = c.AccountId
if attr, ok := d.GetOk("agent_id"); ok {
agentIdentifier = attr.(string)
}
if attr, ok := d.GetOk("project_id"); ok {
projectIdentifier = attr.(string)
}
if attr, ok := d.GetOk("org_id"); ok {
orgIdentifier = attr.(string)
}
createGnupgRequest := buildGnupgCreateRequest(d)
respCreate, httpRespCreate, errCreate := c.GnuPGPKeysApi.AgentGPGKeyServiceCreate(ctx, *createGnupgRequest, agentIdentifier,
&nextgen.GnuPGPKeysApiAgentGPGKeyServiceCreateOpts{
AccountIdentifier: optional.NewString(accountIdentifier),
OrgIdentifier: optional.NewString(orgIdentifier),
ProjectIdentifier: optional.NewString(projectIdentifier),
})
if errCreate != nil {
return helpers.HandleApiError(errCreate, d, httpRespCreate)
}
// Soft delete lookup error handling
// https://harness.atlassian.net/browse/PL-23765
if &respCreate == nil {
d.SetId("")
d.MarkNewResource()
return nil
}
readGnupgKeyCreate(d, &respCreate, accountIdentifier, agentIdentifier, orgIdentifier, projectIdentifier)
return nil
}
func resourceGitopsGnupgRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
ctx = context.WithValue(ctx, nextgen.ContextAccessToken, hh.EnvVars.BearerToken.Get())
var agentIdentifier, orgIdentifier, projectIdentifier string
keyId := d.Get("identifier").(string)
if attr, ok := d.GetOk("agent_id"); ok {
agentIdentifier = attr.(string)
}
if attr, ok := d.GetOk("project_id"); ok {
projectIdentifier = attr.(string)
}
if attr, ok := d.GetOk("org_id"); ok {
orgIdentifier = attr.(string)
}
resp, httpResp, err := c.GnuPGPKeysApi.AgentGPGKeyServiceGet(ctx, agentIdentifier, keyId, c.AccountId, &nextgen.GnuPGPKeysApiAgentGPGKeyServiceGetOpts{
OrgIdentifier: optional.NewString(orgIdentifier),
ProjectIdentifier: optional.NewString(projectIdentifier),
})
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
// Soft delete lookup error handling
// https://harness.atlassian.net/browse/PL-23765
if &resp == nil {
d.SetId("")
d.MarkNewResource()
return nil
}
readGnupgKey(d, &resp, c.AccountId, agentIdentifier, orgIdentifier, projectIdentifier)
return nil
}
func resourceGitopsGnupgDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
ctx = context.WithValue(ctx, nextgen.ContextAccessToken, hh.EnvVars.BearerToken.Get())
var agentIdentifier, orgIdentifier, projectIdentifier string
keyId := d.Get("identifier").(string)
if attr, ok := d.GetOk("agent_id"); ok {
agentIdentifier = attr.(string)
}
if attr, ok := d.GetOk("project_id"); ok {
projectIdentifier = attr.(string)
}
if attr, ok := d.GetOk("org_id"); ok {
orgIdentifier = attr.(string)
}
_, httpResp, err := c.GnuPGPKeysApi.AgentGPGKeyServiceDelete(ctx, agentIdentifier, keyId, &nextgen.GnuPGPKeysApiAgentGPGKeyServiceDeleteOpts{
AccountIdentifier: optional.NewString(c.AccountId),
OrgIdentifier: optional.NewString(orgIdentifier),
ProjectIdentifier: optional.NewString(projectIdentifier),
})
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
return nil
}
func readGnupgKeyCreate(d *schema.ResourceData, gpgKey *nextgen.GpgkeysGnuPgPublicKeyCreateResponse, accountIdentifier string, agentIdentifier string, orgIdentifier string, projectIdentifier string) {
readGnupgKey(d, &gpgKey.Created.Items[0], accountIdentifier, agentIdentifier, orgIdentifier, projectIdentifier)
}
func readGnupgKey(d *schema.ResourceData, gpgkey *nextgen.GpgkeysGnuPgPublicKey, accountIdentifier string, agentIdentifier string, orgIdentifier string, projectIdentifier string) {
d.SetId(gpgkey.KeyID)
d.Set("identifier", gpgkey.KeyID)
d.Set("account_id", accountIdentifier)
d.Set("agent_id", agentIdentifier)
d.Set("org_id", orgIdentifier)
d.Set("project_id", projectIdentifier)
request := map[string]interface{}{}
requestList := []interface{}{}
publickey := map[string]interface{}{}
publickeyList := []interface{}{}
publickey["key_id"] = gpgkey.KeyID
publickey["fingerprint"] = gpgkey.Fingerprint
publickey["owner"] = gpgkey.Owner
publickey["trust"] = gpgkey.Trust
publickey["sub_type"] = gpgkey.SubType
publickey["key_data"] = gpgkey.KeyData
publickeyList = append(publickeyList, publickey)
request["publickey"] = publickeyList
requestList = append(requestList, request)
d.Set("request", requestList)
}
func buildGnupgCreateRequest(d *schema.ResourceData) *nextgen.GpgkeysGnuPgPublicKeyCreateRequest {
var upsert bool
if attr, ok := d.GetOk("request"); ok {
request := attr.([]interface{})[0].(map[string]interface{})
upsert = request["upsert"].(bool)
}
return &nextgen.GpgkeysGnuPgPublicKeyCreateRequest{
Upsert: upsert,
Publickey: buildPublickeyDetails(d),
}
}
func buildPublickeyDetails(d *schema.ResourceData) *nextgen.GpgkeysGnuPgPublicKey {
var publickeyDetails nextgen.GpgkeysGnuPgPublicKey
var request map[string]interface{}
if attr, ok := d.GetOk("request"); ok {
request = attr.([]interface{})[0].(map[string]interface{})
if request["publickey"] != nil && len(request["publickey"].([]interface{})) > 0 {
requestPublicKey := request["publickey"].([]interface{})[0].(map[string]interface{})
if requestPublicKey["key_id"] != nil {
publickeyDetails.KeyID = requestPublicKey["key_id"].(string)
}
if requestPublicKey["fingerprint"] != nil {
publickeyDetails.Fingerprint = requestPublicKey["fingerprint"].(string)
}
if requestPublicKey["owner"] != nil {
publickeyDetails.Owner = requestPublicKey["owner"].(string)
}
if requestPublicKey["trust"] != nil {
publickeyDetails.Trust = requestPublicKey["trust"].(string)
}
if requestPublicKey["sub_type"] != nil {
publickeyDetails.SubType = requestPublicKey["sub_type"].(string)
}
if requestPublicKey["key_data"] != nil {
publickeyDetails.KeyData = requestPublicKey["key_data"].(string)
}
}
}
return &publickeyDetails
}