5
5
"fmt"
6
6
"log"
7
7
"strconv"
8
- "strings"
9
8
10
9
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11
10
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -22,7 +21,7 @@ var _ = registerResource("gitlab_deploy_key_enable", func() *schema.Resource {
22
21
ReadContext : resourceGitlabDeployKeyEnableRead ,
23
22
DeleteContext : resourceGitlabDeployKeyEnableDelete ,
24
23
Importer : & schema.ResourceImporter {
25
- StateContext : resourceGitlabDeployKeyEnableStateImporter ,
24
+ StateContext : schema . ImportStatePassthroughContext ,
26
25
},
27
26
28
27
Schema : map [string ]* schema.Schema {
@@ -51,10 +50,11 @@ var _ = registerResource("gitlab_deploy_key_enable", func() *schema.Resource {
51
50
Computed : true ,
52
51
},
53
52
"can_push" : {
54
- Description : "Can deploy key push to the project’ s repository." ,
53
+ Description : "Can deploy key push to the project' s repository." ,
55
54
Type : schema .TypeBool ,
56
55
Optional : true ,
57
- Computed : true ,
56
+ Default : false ,
57
+ ForceNew : true ,
58
58
},
59
59
},
60
60
}
@@ -63,7 +63,11 @@ var _ = registerResource("gitlab_deploy_key_enable", func() *schema.Resource {
63
63
func resourceGitlabDeployKeyEnableCreate (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
64
64
client := meta .(* gitlab.Client )
65
65
project := d .Get ("project" ).(string )
66
- key_id , err := strconv .Atoi (d .Get ("key_id" ).(string )) // nolint // TODO: Resolve this golangci-lint issue: ineffectual assignment to err (ineffassign)
66
+
67
+ key_id , err := strconv .Atoi (d .Get ("key_id" ).(string ))
68
+ if err != nil {
69
+ return diag .FromErr (err )
70
+ }
67
71
68
72
log .Printf ("[DEBUG] enable gitlab deploy key %s/%d" , project , key_id )
69
73
@@ -72,18 +76,27 @@ func resourceGitlabDeployKeyEnableCreate(ctx context.Context, d *schema.Resource
72
76
return diag .FromErr (err )
73
77
}
74
78
79
+ options := & gitlab.UpdateDeployKeyOptions {
80
+ CanPush : gitlab .Bool (d .Get ("can_push" ).(bool )),
81
+ }
82
+ _ , _ , err = client .DeployKeys .UpdateDeployKey (project , key_id , options )
83
+ if err != nil {
84
+ return diag .FromErr (err )
85
+ }
86
+
75
87
d .SetId (fmt .Sprintf ("%s:%d" , project , deployKey .ID ))
76
88
77
89
return resourceGitlabDeployKeyEnableRead (ctx , d , meta )
78
90
}
79
91
80
92
func resourceGitlabDeployKeyEnableRead (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
81
93
client := meta .(* gitlab.Client )
82
- project := d . Get ( "project" ).( string )
83
- deployKeyID , err := strconv . Atoi (d .Get ( "key_id" ).( string ))
94
+
95
+ project , deployKeyID , err := resourceGitLabDeployKeyEnableParseId (d .Id ( ))
84
96
if err != nil {
85
97
return diag .FromErr (err )
86
98
}
99
+
87
100
log .Printf ("[DEBUG] read gitlab deploy key %s/%d" , project , deployKeyID )
88
101
89
102
deployKey , _ , err := client .DeployKeys .GetDeployKey (project , deployKeyID , gitlab .WithContext (ctx ))
@@ -101,16 +114,18 @@ func resourceGitlabDeployKeyEnableRead(ctx context.Context, d *schema.ResourceDa
101
114
d .Set ("key" , deployKey .Key )
102
115
d .Set ("can_push" , deployKey .CanPush )
103
116
d .Set ("project" , project )
117
+
104
118
return nil
105
119
}
106
120
107
121
func resourceGitlabDeployKeyEnableDelete (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
108
122
client := meta .(* gitlab.Client )
109
- project := d . Get ( "project" ).( string )
110
- deployKeyID , err := strconv . Atoi (d .Get ( "key_id" ).( string ))
123
+
124
+ project , deployKeyID , err := resourceGitLabDeployKeyEnableParseId (d .Id ( ))
111
125
if err != nil {
112
126
return diag .FromErr (err )
113
127
}
128
+
114
129
log .Printf ("[DEBUG] Delete gitlab deploy key %s/%d" , project , deployKeyID )
115
130
116
131
response , err := client .DeployKeys .DeleteDeployKey (project , deployKeyID , gitlab .WithContext (ctx ))
@@ -127,17 +142,16 @@ func resourceGitlabDeployKeyEnableDelete(ctx context.Context, d *schema.Resource
127
142
return nil
128
143
}
129
144
130
- func resourceGitlabDeployKeyEnableStateImporter (ctx context.Context , d * schema.ResourceData , meta interface {}) ([]* schema.ResourceData , error ) {
131
- s := strings .Split (d .Id (), ":" )
132
- if len (s ) != 2 {
133
- d .SetId ("" )
134
- return nil , fmt .Errorf ("Invalid Deploy Key import format; expected '{project_id}:{deploy_key_id}'" )
145
+ func resourceGitLabDeployKeyEnableParseId (id string ) (string , int , error ) {
146
+ projectID , deployTokenID , err := parseTwoPartID (id )
147
+ if err != nil {
148
+ return "" , 0 , err
135
149
}
136
- project , id := s [0 ], s [1 ]
137
150
138
- d .SetId (fmt .Sprintf ("%s:%s" , project , id ))
139
- d .Set ("key_id" , id )
140
- d .Set ("project" , project )
151
+ deployTokenIID , err := strconv .Atoi (deployTokenID )
152
+ if err != nil {
153
+ return "" , 0 , err
154
+ }
141
155
142
- return [] * schema. ResourceData { d } , nil
156
+ return projectID , deployTokenIID , nil
143
157
}
0 commit comments