@@ -3,70 +3,16 @@ package repos
33import (
44 "context"
55 "fmt"
6+ "strconv"
67 "strings"
78
9+ "github.com/databricks/databricks-sdk-go/service/gitcredentials"
810 "github.com/databricks/terraform-provider-databricks/common"
911 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1012)
1113
12- // GitCredentialsAPI exposes the Git Credentials API
13- type GitCredentialsAPI struct {
14- client * common.DatabricksClient
15- context context.Context
16- }
17-
18- // GitCredentialsAPI creates GitCredentialsAPI instance from provider meta
19- func NewGitCredentialsAPI (ctx context.Context , m any ) GitCredentialsAPI {
20- return GitCredentialsAPI {m .(* common.DatabricksClient ), ctx }
21- }
22-
23- type GitCredentialRequest struct {
24- PAT string `json:"personal_access_token" tf:"sensitive"`
25- Provider string `json:"git_provider" tf:"suppress_diff"`
26- UserName string `json:"git_username"`
27- }
28-
29- type GitCredentialResponse struct {
30- ID int64 `json:"credential_id"`
31- UserName string `json:"git_username"`
32- Provider string `json:"git_provider"`
33- }
34-
35- type GitCredentialList struct {
36- Credentials []GitCredentialResponse `json:"credentials,omitempty"`
37- }
38-
39- // ID returns job id as string
40- func (r GitCredentialResponse ) GitCredentialID () string {
41- return fmt .Sprintf ("%d" , r .ID )
42- }
43-
44- func (a GitCredentialsAPI ) Delete (id string ) error {
45- return a .client .Delete (a .context , fmt .Sprintf ("/git-credentials/%s" , id ), nil )
46- }
47-
48- func (a GitCredentialsAPI ) List () ([]GitCredentialResponse , error ) {
49- resp := GitCredentialList {}
50- err := a .client .Get (a .context , "/git-credentials" , nil , & resp )
51- return resp .Credentials , err
52- }
53-
54- func (a GitCredentialsAPI ) Read (id string ) (resp GitCredentialResponse , err error ) {
55- err = a .client .Get (a .context , fmt .Sprintf ("/git-credentials/%s" , id ), nil , & resp )
56- return
57- }
58-
59- func (a GitCredentialsAPI ) Create (req GitCredentialRequest ) (resp GitCredentialResponse , err error ) {
60- err = a .client .Post (a .context , "/git-credentials" , & req , & resp )
61- return
62- }
63-
64- func (a GitCredentialsAPI ) Update (id string , req GitCredentialRequest ) error {
65- return a .client .Patch (a .context , fmt .Sprintf ("/git-credentials/%s" , id ), & req )
66- }
67-
6814func ResourceGitCredential () * schema.Resource {
69- s := common .StructToSchema (GitCredentialRequest {}, func (s map [string ]* schema.Schema ) map [string ]* schema.Schema {
15+ s := common .StructToSchema (gitcredentials. CreateCredentials {}, func (s map [string ]* schema.Schema ) map [string ]* schema.Schema {
7016 s ["force" ] = & schema.Schema {
7117 Type : schema .TypeBool ,
7218 Optional : true ,
@@ -83,47 +29,81 @@ func ResourceGitCredential() *schema.Resource {
8329 Schema : s ,
8430 SchemaVersion : 1 ,
8531 Create : func (ctx context.Context , d * schema.ResourceData , c * common.DatabricksClient ) error {
86- api := NewGitCredentialsAPI (ctx , c )
87- var req GitCredentialRequest
32+ w , err := c .WorkspaceClient ()
33+ if err != nil {
34+ return err
35+ }
36+
37+ var req gitcredentials.CreateCredentials
8838 common .DataToStructPointer (d , s , & req )
89- resp , err := api . Create (req )
39+ resp , err := w . GitCredentials . Create (ctx , req )
9040
9141 if err != nil {
9242 if ! d .Get ("force" ).(bool ) || ! strings .HasPrefix (err .Error (), "Only one Git credential is supported at this time" ) {
9343 return err
9444 }
95- creds , err := api . List ( )
45+ creds , err := w . GitCredentials . ListAll ( ctx )
9646 if err != nil {
9747 return err
9848 }
9949 if len (creds ) != 1 {
10050 return fmt .Errorf ("list of credentials is either empty or have more than one entry (%d)" , len (creds ))
10151 }
102- resp = creds [0 ]
103- err = api .Update (resp .GitCredentialID (), req )
52+ var req gitcredentials.UpdateCredentials
53+ common .DataToStructPointer (d , s , & req )
54+ req .CredentialId = creds [0 ].CredentialId
55+
56+ err = w .GitCredentials .Update (ctx , req )
10457 if err != nil {
10558 return err
10659 }
60+ resp .CredentialId = creds [0 ].CredentialId
10761 }
108- d .SetId (resp . GitCredentialID ( ))
62+ d .SetId (fmt . Sprintf ( "%d" , resp . CredentialId ))
10963 return nil
11064 },
11165 Read : func (ctx context.Context , d * schema.ResourceData , c * common.DatabricksClient ) error {
112- resp , err := NewGitCredentialsAPI (ctx , c ).Read (d .Id ())
66+ w , err := c .WorkspaceClient ()
67+ if err != nil {
68+ return err
69+ }
70+ cred_id , err := strconv .ParseInt (d .Id (), 10 , 0 )
71+ if err != nil {
72+ return err
73+ }
74+ resp , err := w .GitCredentials .Get (ctx , gitcredentials.Get {CredentialId : cred_id })
11375 if err != nil {
11476 return err
11577 }
116- d .Set ("git_provider" , resp .Provider )
117- d .Set ("git_username" , resp .UserName )
78+ d .Set ("git_provider" , resp .GitProvider )
79+ d .Set ("git_username" , resp .GitUsername )
11880 return nil
11981 },
12082 Update : func (ctx context.Context , d * schema.ResourceData , c * common.DatabricksClient ) error {
121- var req GitCredentialRequest
83+ var req gitcredentials.UpdateCredentials
84+
12285 common .DataToStructPointer (d , s , & req )
123- return NewGitCredentialsAPI (ctx , c ).Update (d .Id (), req )
86+ cred_id , err := strconv .ParseInt (d .Id (), 10 , 0 )
87+ if err != nil {
88+ return err
89+ }
90+ req .CredentialId = cred_id
91+ w , err := c .WorkspaceClient ()
92+ if err != nil {
93+ return err
94+ }
95+ return w .GitCredentials .Update (ctx , req )
12496 },
12597 Delete : func (ctx context.Context , d * schema.ResourceData , c * common.DatabricksClient ) error {
126- return NewGitCredentialsAPI (ctx , c ).Delete (d .Id ())
98+ w , err := c .WorkspaceClient ()
99+ if err != nil {
100+ return err
101+ }
102+ cred_id , err := strconv .ParseInt (d .Id (), 10 , 0 )
103+ if err != nil {
104+ return err
105+ }
106+ return w .GitCredentials .DeleteByCredentialId (ctx , cred_id )
127107 },
128108 }.ToResource ()
129109}
0 commit comments