Skip to content

Commit febeaf4

Browse files
committed
Refactored unit tests, updated import documentation, fixed imports using project namespaces (+7 squashed commit)
Squashed commit: [3e3f44e] Fixed capitalization on an acceptance test and remove the project attribute check on deploy key enable tests [a0db074] Fixing up string conversion, fixing a lint error, refactoring the check destroy function for deploy key enable resource, fixing up check destroy in deploy key enable test [153342e] Reverting deploy key id to original + refactoring deploy key enable a tad [3f62ca1] Linter fixes I missed [50c408c] Linter Fixes [db9de1a] refactoring deploy key resources to remove the custom importers [db6ebdb] Refactored Deploy Keys acceptance tests and made logging more helpful
1 parent e08329b commit febeaf4

File tree

8 files changed

+157
-300
lines changed

8 files changed

+157
-300
lines changed

docs/resources/deploy_key.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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: 23 additions & 18 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,7 +50,7 @@ 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,
5756
Default: false,
@@ -64,7 +63,11 @@ var _ = registerResource("gitlab_deploy_key_enable", func() *schema.Resource {
6463
func resourceGitlabDeployKeyEnableCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
6564
client := meta.(*gitlab.Client)
6665
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+
}
6871

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

@@ -88,11 +91,12 @@ func resourceGitlabDeployKeyEnableCreate(ctx context.Context, d *schema.Resource
8891

8992
func resourceGitlabDeployKeyEnableRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
9093
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())
9396
if err != nil {
9497
return diag.FromErr(err)
9598
}
99+
96100
log.Printf("[DEBUG] read gitlab deploy key %s/%d", project, deployKeyID)
97101

98102
deployKey, _, err := client.DeployKeys.GetDeployKey(project, deployKeyID, gitlab.WithContext(ctx))
@@ -110,16 +114,18 @@ func resourceGitlabDeployKeyEnableRead(ctx context.Context, d *schema.ResourceDa
110114
d.Set("key", deployKey.Key)
111115
d.Set("can_push", deployKey.CanPush)
112116
d.Set("project", project)
117+
113118
return nil
114119
}
115120

116121
func resourceGitlabDeployKeyEnableDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
117122
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())
120125
if err != nil {
121126
return diag.FromErr(err)
122127
}
128+
123129
log.Printf("[DEBUG] Delete gitlab deploy key %s/%d", project, deployKeyID)
124130

125131
response, err := client.DeployKeys.DeleteDeployKey(project, deployKeyID, gitlab.WithContext(ctx))
@@ -136,17 +142,16 @@ func resourceGitlabDeployKeyEnableDelete(ctx context.Context, d *schema.Resource
136142
return nil
137143
}
138144

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
144149
}
145-
project, id := s[0], s[1]
146150

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

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

0 commit comments

Comments
 (0)