Skip to content

Commit 61d908e

Browse files
authored
Merge pull request #1009 from Shocktrooper/can-push-deploy-keys
Can push deploy keys
2 parents 449bb82 + febeaf4 commit 61d908e

File tree

8 files changed

+188
-249
lines changed

8 files changed

+188
-249
lines changed

docs/resources/deploy_key.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ resource "gitlab_deploy_key" "example" {
3737

3838
### Optional
3939

40-
- `can_push` (Boolean) Allow this deploy key to be used to push changes to the project. Defaults to `false`. **NOTE::** this cannot currently be managed.
40+
- `can_push` (Boolean) Allow this deploy key to be used to push changes to the project. Defaults to `false`.
4141
- `id` (String) The ID of this resource.
4242

4343
## Import
@@ -46,5 +46,8 @@ Import is supported using the following syntax:
4646

4747
```shell
4848
# GitLab deploy keys can be imported using an id made up of `{project_id}:{deploy_key_id}`, e.g.
49+
# `project_id` can be whatever the [get single project api][get_single_project] takes for
50+
# its `:id` value, so for example:
4951
terraform import gitlab_deploy_key.test 1:3
52+
terraform import gitlab_deploy_key.test richardc/example:3
5053
```

docs/resources/deploy_key_enable.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ resource "gitlab_deploy_key_enable" "foo" {
5050

5151
### Optional
5252

53-
- `can_push` (Boolean) Can deploy key push to the projects repository.
53+
- `can_push` (Boolean) Can deploy key push to the project's repository.
5454
- `id` (String) The ID of this resource.
5555
- `key` (String) Deploy key.
5656
- `title` (String) Deploy key's title.
@@ -61,5 +61,8 @@ Import is supported using the following syntax:
6161

6262
```shell
6363
# GitLab enabled deploy keys can be imported using an id made up of `{project_id}:{deploy_key_id}`, e.g.
64+
# `project_id` can be whatever the [get single project api][get_single_project] takes for
65+
# its `:id` value, so for example:
6466
terraform import gitlab_deploy_key_enable.example 12345:67890
67+
terraform import gitlab_deploy_key_enable.example richardc/example:67890
6568
```
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
# GitLab deploy keys can be imported using an id made up of `{project_id}:{deploy_key_id}`, e.g.
2+
# `project_id` can be whatever the [get single project api][get_single_project] takes for
3+
# its `:id` value, so for example:
24
terraform import gitlab_deploy_key.test 1:3
5+
terraform import gitlab_deploy_key.test richardc/example:3
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
# GitLab enabled deploy keys can be imported using an id made up of `{project_id}:{deploy_key_id}`, e.g.
2+
# `project_id` can be whatever the [get single project api][get_single_project] takes for
3+
# its `:id` value, so for example:
24
terraform import gitlab_deploy_key_enable.example 12345:67890
5+
terraform import gitlab_deploy_key_enable.example richardc/example:67890

internal/provider/resource_gitlab_deploy_key.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ var _ = registerResource("gitlab_deploy_key", func() *schema.Resource {
5050
},
5151
},
5252
"can_push": {
53-
Description: "Allow this deploy key to be used to push changes to the project. Defaults to `false`. **NOTE::** this cannot currently be managed.",
53+
Description: "Allow this deploy key to be used to push changes to the project. Defaults to `false`.",
5454
Type: schema.TypeBool,
5555
Optional: true,
5656
Default: false,
@@ -84,10 +84,12 @@ func resourceGitlabDeployKeyCreate(ctx context.Context, d *schema.ResourceData,
8484
func resourceGitlabDeployKeyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
8585
client := meta.(*gitlab.Client)
8686
project := d.Get("project").(string)
87+
8788
deployKeyID, err := strconv.Atoi(d.Id())
8889
if err != nil {
8990
return diag.FromErr(err)
9091
}
92+
9193
log.Printf("[DEBUG] read gitlab deploy key %s/%d", project, deployKeyID)
9294

9395
deployKey, _, err := client.DeployKeys.GetDeployKey(project, deployKeyID, gitlab.WithContext(ctx))
@@ -103,16 +105,19 @@ func resourceGitlabDeployKeyRead(ctx context.Context, d *schema.ResourceData, me
103105
d.Set("title", deployKey.Title)
104106
d.Set("key", deployKey.Key)
105107
d.Set("can_push", deployKey.CanPush)
108+
106109
return nil
107110
}
108111

109112
func resourceGitlabDeployKeyDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
110113
client := meta.(*gitlab.Client)
111114
project := d.Get("project").(string)
115+
112116
deployKeyID, err := strconv.Atoi(d.Id())
113117
if err != nil {
114118
return diag.FromErr(err)
115119
}
120+
116121
log.Printf("[DEBUG] Delete gitlab deploy key %s", d.Id())
117122

118123
_, err = client.DeployKeys.DeleteDeployKey(project, deployKeyID, gitlab.WithContext(ctx))
@@ -128,7 +133,7 @@ func resourceGitlabDeployKeyStateImporter(ctx context.Context, d *schema.Resourc
128133
s := strings.Split(d.Id(), ":")
129134
if len(s) != 2 {
130135
d.SetId("")
131-
return nil, fmt.Errorf("Invalid Deploy Key import format; expected '{project_id}:{deploy_key_id}'")
136+
return nil, fmt.Errorf("invalid Deploy Key import format; expected '{project_id}:{deploy_key_id}' was %v", s)
132137
}
133138
project, id := s[0], s[1]
134139

internal/provider/resource_gitlab_deploy_key_enable.go

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"log"
77
"strconv"
8-
"strings"
98

109
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1110
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -22,7 +21,7 @@ var _ = registerResource("gitlab_deploy_key_enable", func() *schema.Resource {
2221
ReadContext: resourceGitlabDeployKeyEnableRead,
2322
DeleteContext: resourceGitlabDeployKeyEnableDelete,
2423
Importer: &schema.ResourceImporter{
25-
StateContext: resourceGitlabDeployKeyEnableStateImporter,
24+
StateContext: schema.ImportStatePassthroughContext,
2625
},
2726

2827
Schema: map[string]*schema.Schema{
@@ -51,10 +50,11 @@ var _ = registerResource("gitlab_deploy_key_enable", func() *schema.Resource {
5150
Computed: true,
5251
},
5352
"can_push": {
54-
Description: "Can deploy key push to the projects repository.",
53+
Description: "Can deploy key push to the project's repository.",
5554
Type: schema.TypeBool,
5655
Optional: true,
57-
Computed: true,
56+
Default: false,
57+
ForceNew: true,
5858
},
5959
},
6060
}
@@ -63,7 +63,11 @@ var _ = registerResource("gitlab_deploy_key_enable", func() *schema.Resource {
6363
func resourceGitlabDeployKeyEnableCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
6464
client := meta.(*gitlab.Client)
6565
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+
}
6771

6872
log.Printf("[DEBUG] enable gitlab deploy key %s/%d", project, key_id)
6973

@@ -72,18 +76,27 @@ func resourceGitlabDeployKeyEnableCreate(ctx context.Context, d *schema.Resource
7276
return diag.FromErr(err)
7377
}
7478

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+
7587
d.SetId(fmt.Sprintf("%s:%d", project, deployKey.ID))
7688

7789
return resourceGitlabDeployKeyEnableRead(ctx, d, meta)
7890
}
7991

8092
func resourceGitlabDeployKeyEnableRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
8193
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())
8496
if err != nil {
8597
return diag.FromErr(err)
8698
}
99+
87100
log.Printf("[DEBUG] read gitlab deploy key %s/%d", project, deployKeyID)
88101

89102
deployKey, _, err := client.DeployKeys.GetDeployKey(project, deployKeyID, gitlab.WithContext(ctx))
@@ -101,16 +114,18 @@ func resourceGitlabDeployKeyEnableRead(ctx context.Context, d *schema.ResourceDa
101114
d.Set("key", deployKey.Key)
102115
d.Set("can_push", deployKey.CanPush)
103116
d.Set("project", project)
117+
104118
return nil
105119
}
106120

107121
func resourceGitlabDeployKeyEnableDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
108122
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())
111125
if err != nil {
112126
return diag.FromErr(err)
113127
}
128+
114129
log.Printf("[DEBUG] Delete gitlab deploy key %s/%d", project, deployKeyID)
115130

116131
response, err := client.DeployKeys.DeleteDeployKey(project, deployKeyID, gitlab.WithContext(ctx))
@@ -127,17 +142,16 @@ func resourceGitlabDeployKeyEnableDelete(ctx context.Context, d *schema.Resource
127142
return nil
128143
}
129144

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
135149
}
136-
project, id := s[0], s[1]
137150

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+
}
141155

142-
return []*schema.ResourceData{d}, nil
156+
return projectID, deployTokenIID, nil
143157
}

0 commit comments

Comments
 (0)