generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 41
/
resource_gitops_repo_cred.go
378 lines (330 loc) · 12.9 KB
/
resource_gitops_repo_cred.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package repository_credentials
import (
"context"
"net/http"
"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 ResourceGitopsRepoCred() *schema.Resource {
resource := &schema.Resource{
Description: "Resource for creating a Harness Gitops Repositories Credentials.",
CreateContext: resourceGitopsRepoCredCreate,
ReadContext: resourceGitopsRepoCredRead,
UpdateContext: resourceGitopsRepoCredUpdate,
DeleteContext: resourceGitopsRepoCredDelete,
Importer: helpers.GitopsAgentResourceImporter,
Schema: map[string]*schema.Schema{
"identifier": {
Description: "Identifier of the Repository Credential.",
Type: schema.TypeString,
Required: true,
},
"agent_id": {
Description: "Agent identifier of the Repository Credential.",
Type: schema.TypeString,
Required: true,
},
"account_id": {
Description: "Account identifier of the Repository Credential.",
Type: schema.TypeString,
Required: true,
},
"org_id": {
Description: "Organization identifier of the Repository Credential.",
Type: schema.TypeString,
Optional: true,
},
"project_id": {
Description: "Project identifier of the Repository Credential.",
Type: schema.TypeString,
Optional: true,
},
"upsert": {
Description: "if the Repository credential should be upserted.",
Type: schema.TypeBool,
Optional: true,
},
"creds": {
Description: "credential details.",
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"url": {
Description: "url representing this object.",
Type: schema.TypeString,
Optional: true,
},
"username": {
Description: "Username for authenticating at the repo server.",
Type: schema.TypeString,
Optional: true,
},
"password": {
Description: "Password for authenticating at the repo server.",
Type: schema.TypeString,
Optional: true,
},
"ssh_private_key": {
Description: "Contains the private key data for authenticating at the repo server using SSH (only Git repos).",
Type: schema.TypeString,
Optional: true,
},
"tls_client_cert_data": {
Description: "Specifies the TLS client cert data for authenticating at the repo server.",
Type: schema.TypeString,
Optional: true,
},
"tls_client_cert_key": {
Description: "Specifies the TLS client cert key for authenticating at the repo server.",
Type: schema.TypeString,
Optional: true,
},
"github_app_private_key": {
Description: "github_app_private_key specifies the private key PEM data for authentication via GitHub app.",
Type: schema.TypeString,
Optional: true,
},
"github_app_id": {
Description: "Specifies the Github App ID of the app used to access the repo for GitHub app authentication.",
Type: schema.TypeString,
Optional: true,
},
"github_app_installation_id": {
Description: "Specifies the ID of the installed GitHub App for GitHub app authentication.",
Type: schema.TypeString,
Optional: true,
},
"github_app_enterprise_base_url": {
Description: "Specifies the GitHub API URL for GitHub app authentication.",
Type: schema.TypeString,
Optional: true,
},
"enable_oci": {
Description: "Specifies whether helm-oci support should be enabled for this repo.",
Type: schema.TypeBool,
Optional: true,
},
"type": {
Description: "Type specifies the type of the repoCreds.Can be either 'git' or 'helm. 'git' is assumed if empty or absent",
Type: schema.TypeString,
Optional: true,
},
},
},
},
},
}
return resource
}
func resourceGitopsRepoCredCreate(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, identifier string
accountIdentifier = c.AccountId
if attr, ok := d.GetOk("identifier"); ok {
identifier = attr.(string)
}
if attr, ok := d.GetOk("agent_id"); ok {
agentIdentifier = attr.(string)
}
if attr, ok := d.GetOk("org_id"); ok {
orgIdentifier = attr.(string)
}
if attr, ok := d.GetOk("project_id"); ok {
projectIdentifier = attr.(string)
}
var err error
var httpResp *http.Response
var resp nextgen.Servicev1RepositoryCredentials
repoCredCreateRequest := buildRepoCredCreateRequest(d)
resp, httpResp, err = c.RepositoryCredentialsApi.AgentRepositoryCredentialsServiceCreateRepositoryCredentials(ctx, *repoCredCreateRequest, accountIdentifier, agentIdentifier,
&nextgen.RepositoryCredentialsApiAgentRepositoryCredentialsServiceCreateRepositoryCredentialsOpts{
Identifier: optional.NewString(identifier),
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
}
setGitopsRepositoriesCredential(d, &resp)
return nil
}
func resourceGitopsRepoCredUpdate(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, identifier string
accountIdentifier = c.AccountId
if attr, ok := d.GetOk("identifier"); ok {
identifier = attr.(string)
}
if attr, ok := d.GetOk("agent_id"); ok {
agentIdentifier = attr.(string)
}
if attr, ok := d.GetOk("org_id"); ok {
orgIdentifier = attr.(string)
}
if attr, ok := d.GetOk("project_id"); ok {
projectIdentifier = attr.(string)
}
var err error
var httpResp *http.Response
var resp nextgen.Servicev1RepositoryCredentials
repoCredUpdateRequest := buildRepoCredUpdateRequest(d)
resp, httpResp, err = c.RepositoryCredentialsApi.AgentRepositoryCredentialsServiceUpdateRepositoryCredentials(ctx, *repoCredUpdateRequest, agentIdentifier, identifier,
&nextgen.RepositoryCredentialsApiAgentRepositoryCredentialsServiceUpdateRepositoryCredentialsOpts{
AccountIdentifier: optional.NewString(accountIdentifier),
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
}
setGitopsRepositoriesCredential(d, &resp)
return nil
}
func resourceGitopsRepoCredRead(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())
agentIdentifier := d.Get("agent_id").(string)
identifier := d.Get("identifier").(string)
resp, httpResp, err := c.RepositoryCredentialsApi.AgentRepositoryCredentialsServiceGetRepositoryCredentials(ctx, agentIdentifier, identifier, c.AccountId, &nextgen.RepositoryCredentialsApiAgentRepositoryCredentialsServiceGetRepositoryCredentialsOpts{
OrgIdentifier: optional.NewString(d.Get("org_id").(string)),
ProjectIdentifier: optional.NewString(d.Get("project_id").(string)),
})
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
}
setGitopsRepositoriesCredential(d, &resp)
return nil
}
func resourceGitopsRepoCredDelete(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())
agentIdentifier := d.Get("agent_id").(string)
identifier := d.Get("identifier").(string)
_, httpResp, err := c.RepositoryCredentialsApi.AgentRepositoryCredentialsServiceDeleteRepositoryCredentials(ctx, agentIdentifier, identifier, &nextgen.RepositoryCredentialsApiAgentRepositoryCredentialsServiceDeleteRepositoryCredentialsOpts{
AccountIdentifier: optional.NewString(c.AccountId),
OrgIdentifier: optional.NewString(d.Get("org_id").(string)),
ProjectIdentifier: optional.NewString(d.Get("project_id").(string)),
QueryUrl: optional.NewString(d.Get("creds.0.url").(string)),
QueryRepoCredsType: optional.NewString(d.Get("creds.0.type").(string)),
})
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
return nil
}
func setGitopsRepositoriesCredential(d *schema.ResourceData, repoCred *nextgen.Servicev1RepositoryCredentials) {
d.SetId(repoCred.Identifier)
d.Set("account_id", repoCred.AccountIdentifier)
d.Set("agent_id", repoCred.AgentIdentifier)
d.Set("identifier", repoCred.Identifier)
d.Set("org_id", repoCred.OrgIdentifier)
d.Set("project_id", repoCred.ProjectIdentifier)
if repoCred.RepoCreds != nil {
credList := []interface{}{}
cred := map[string]interface{}{}
cred["url"] = repoCred.RepoCreds.Url
cred["username"] = repoCred.RepoCreds.Username
cred["password"] = repoCred.RepoCreds.Password
cred["ssh_private_key"] = repoCred.RepoCreds.SshPrivateKey
cred["tls_client_cert_data"] = repoCred.RepoCreds.TlsClientCertData
cred["tls_client_cert_key"] = repoCred.RepoCreds.TlsClientCertKey
cred["github_app_private_key"] = repoCred.RepoCreds.GithubAppPrivateKey
cred["github_app_id"] = repoCred.RepoCreds.GithubAppID
cred["github_app_installation_id"] = repoCred.RepoCreds.GithubAppInstallationID
cred["github_app_enterprise_base_url"] = repoCred.RepoCreds.GithubAppEnterpriseBaseUrl
cred["enable_oci"] = repoCred.RepoCreds.EnableOCI
cred["type"] = repoCred.RepoCreds.Type_
credList = append(credList, cred)
d.Set("creds", credList)
}
}
func buildRepoCredCreateRequest(d *schema.ResourceData) *nextgen.HrepocredsRepoCredsCreateRequest {
var upsert bool
if attr, ok := d.GetOk("upsert"); ok {
upsert = attr.(bool)
}
return &nextgen.HrepocredsRepoCredsCreateRequest{
Upsert: upsert,
Creds: buildRepoCred(d),
}
}
func buildRepoCredUpdateRequest(d *schema.ResourceData) *nextgen.HrepocredsRepoCredsUpdateRequest {
return &nextgen.HrepocredsRepoCredsUpdateRequest{
Creds: buildRepoCred(d),
}
}
func buildRepoCred(d *schema.ResourceData) *nextgen.HrepocredsRepoCreds {
var repoCred nextgen.HrepocredsRepoCreds
if attr, ok := d.GetOk("creds"); ok {
if attr != nil && len(attr.([]interface{})) > 0 {
var requestCreds = attr.([]interface{})[0].(map[string]interface{})
if requestCreds["url"] != nil {
repoCred.Url = requestCreds["url"].(string)
}
if requestCreds["username"] != nil {
repoCred.Username = requestCreds["username"].(string)
}
if requestCreds["password"] != nil {
repoCred.Password = requestCreds["password"].(string)
}
if requestCreds["ssh_private_key"] != nil {
repoCred.SshPrivateKey = requestCreds["ssh_private_key"].(string)
}
if requestCreds["tls_client_cert_data"] != nil {
repoCred.TlsClientCertData = requestCreds["tls_client_cert_data"].(string)
}
if requestCreds["tls_client_cert_key"] != nil {
repoCred.TlsClientCertKey = requestCreds["tls_client_cert_key"].(string)
}
if requestCreds["github_app_private_key"] != nil {
repoCred.GithubAppPrivateKey = requestCreds["github_app_private_key"].(string)
}
if requestCreds["github_app_id"] != nil {
repoCred.GithubAppID = requestCreds["github_app_id"].(string)
}
if requestCreds["github_app_installation_id"] != nil {
repoCred.GithubAppInstallationID = requestCreds["github_app_installation_id"].(string)
}
if requestCreds["github_app_enterprise_base_url"] != nil {
repoCred.GithubAppEnterpriseBaseUrl = requestCreds["github_app_enterprise_base_url"].(string)
}
if requestCreds["enable_oci"] != nil {
repoCred.EnableOCI = requestCreds["enable_oci"].(bool)
}
if requestCreds["type"] != nil {
repoCred.Type_ = requestCreds["type"].(string)
}
}
}
return &repoCred
}