forked from databricks/terraform-provider-databricks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_git_credential.go
109 lines (101 loc) · 3.2 KB
/
resource_git_credential.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
package repos
import (
"context"
"fmt"
"strconv"
"strings"
"github.com/databricks/databricks-sdk-go/service/workspace"
"github.com/databricks/terraform-provider-databricks/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func ResourceGitCredential() common.Resource {
s := common.StructToSchema(workspace.CreateCredentials{}, func(s map[string]*schema.Schema) map[string]*schema.Schema {
s["force"] = &schema.Schema{
Type: schema.TypeBool,
Optional: true,
}
s["personal_access_token"].DefaultFunc = schema.MultiEnvDefaultFunc([]string{
"GITHUB_TOKEN", // https://registry.terraform.io/providers/integrations/github/latest/docs
"GITLAB_TOKEN", // https://registry.terraform.io/providers/gitlabhq/gitlab/latest/docs
"AZDO_PERSONAL_ACCESS_TOKEN", // https://registry.terraform.io/providers/microsoft/azuredevops/latest/docs
}, nil)
return s
})
return common.Resource{
Schema: s,
SchemaVersion: 1,
Create: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
w, err := c.WorkspaceClient()
if err != nil {
return err
}
var req workspace.CreateCredentials
common.DataToStructPointer(d, s, &req)
resp, err := w.GitCredentials.Create(ctx, req)
if err != nil {
if !d.Get("force").(bool) || !strings.HasPrefix(err.Error(), "Only one Git credential is supported at this time") {
return err
}
creds, err := w.GitCredentials.ListAll(ctx)
if err != nil {
return err
}
if len(creds) != 1 {
return fmt.Errorf("list of credentials is either empty or have more than one entry (%d)", len(creds))
}
var req workspace.UpdateCredentials
common.DataToStructPointer(d, s, &req)
req.CredentialId = creds[0].CredentialId
err = w.GitCredentials.Update(ctx, req)
if err != nil {
return err
}
resp.CredentialId = creds[0].CredentialId
}
d.SetId(fmt.Sprintf("%d", resp.CredentialId))
return nil
},
Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
w, err := c.WorkspaceClient()
if err != nil {
return err
}
cred_id, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return err
}
resp, err := w.GitCredentials.Get(ctx, workspace.GetGitCredentialRequest{CredentialId: cred_id})
if err != nil {
return err
}
d.Set("git_provider", resp.GitProvider)
d.Set("git_username", resp.GitUsername)
return nil
},
Update: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
var req workspace.UpdateCredentials
common.DataToStructPointer(d, s, &req)
cred_id, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return err
}
req.CredentialId = cred_id
w, err := c.WorkspaceClient()
if err != nil {
return err
}
return w.GitCredentials.Update(ctx, req)
},
Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
w, err := c.WorkspaceClient()
if err != nil {
return err
}
cred_id, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return err
}
return w.GitCredentials.DeleteByCredentialId(ctx, cred_id)
},
}
}