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,7 +50,7 @@ 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
56
Default : false ,
@@ -64,7 +63,11 @@ var _ = registerResource("gitlab_deploy_key_enable", func() *schema.Resource {
64
63
func resourceGitlabDeployKeyEnableCreate (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
65
64
client := meta .(* gitlab.Client )
66
65
project := d .Get ("project" ).(string )
67
- 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
+ }
68
71
69
72
log .Printf ("[DEBUG] enable gitlab deploy key %s/%d" , project , key_id )
70
73
@@ -88,11 +91,12 @@ func resourceGitlabDeployKeyEnableCreate(ctx context.Context, d *schema.Resource
88
91
89
92
func resourceGitlabDeployKeyEnableRead (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
90
93
client := meta .(* gitlab.Client )
91
- project := d . Get ( "project" ).( string )
92
- deployKeyID , err := strconv . Atoi (d .Get ( "key_id" ).( string ))
94
+
95
+ project , deployKeyID , err := resourceGitLabDeployKeyEnableParseId (d .Id ( ))
93
96
if err != nil {
94
97
return diag .FromErr (err )
95
98
}
99
+
96
100
log .Printf ("[DEBUG] read gitlab deploy key %s/%d" , project , deployKeyID )
97
101
98
102
deployKey , _ , err := client .DeployKeys .GetDeployKey (project , deployKeyID , gitlab .WithContext (ctx ))
@@ -110,16 +114,18 @@ func resourceGitlabDeployKeyEnableRead(ctx context.Context, d *schema.ResourceDa
110
114
d .Set ("key" , deployKey .Key )
111
115
d .Set ("can_push" , deployKey .CanPush )
112
116
d .Set ("project" , project )
117
+
113
118
return nil
114
119
}
115
120
116
121
func resourceGitlabDeployKeyEnableDelete (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
117
122
client := meta .(* gitlab.Client )
118
- project := d . Get ( "project" ).( string )
119
- deployKeyID , err := strconv . Atoi (d .Get ( "key_id" ).( string ))
123
+
124
+ project , deployKeyID , err := resourceGitLabDeployKeyEnableParseId (d .Id ( ))
120
125
if err != nil {
121
126
return diag .FromErr (err )
122
127
}
128
+
123
129
log .Printf ("[DEBUG] Delete gitlab deploy key %s/%d" , project , deployKeyID )
124
130
125
131
response , err := client .DeployKeys .DeleteDeployKey (project , deployKeyID , gitlab .WithContext (ctx ))
@@ -136,17 +142,16 @@ func resourceGitlabDeployKeyEnableDelete(ctx context.Context, d *schema.Resource
136
142
return nil
137
143
}
138
144
139
- func resourceGitlabDeployKeyEnableStateImporter (ctx context.Context , d * schema.ResourceData , meta interface {}) ([]* schema.ResourceData , error ) {
140
- s := strings .Split (d .Id (), ":" )
141
- if len (s ) != 2 {
142
- d .SetId ("" )
143
- 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
144
149
}
145
- project , id := s [0 ], s [1 ]
146
150
147
- d .SetId (fmt .Sprintf ("%s:%s" , project , id ))
148
- d .Set ("key_id" , id )
149
- d .Set ("project" , project )
151
+ deployTokenIID , err := strconv .Atoi (deployTokenID )
152
+ if err != nil {
153
+ return "" , 0 , err
154
+ }
150
155
151
- return [] * schema. ResourceData { d } , nil
156
+ return projectID , deployTokenIID , nil
152
157
}
0 commit comments