Skip to content

Commit 0ac952d

Browse files
committed
Fix tests
1 parent 4813a03 commit 0ac952d

File tree

6 files changed

+112
-20
lines changed

6 files changed

+112
-20
lines changed

docs/resources/release_link.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gitlab_release_link Resource - terraform-provider-gitlab"
4+
subcategory: ""
5+
description: |-
6+
The gitlab_release_link resource allows to manage the lifecycle of a release links.
7+
Upstream API: GitLab REST API docs https://docs.gitlab.com/ee/api/releases/links.html
8+
---
9+
10+
# gitlab_release_link (Resource)
11+
12+
The `gitlab_release_link` resource allows to manage the lifecycle of a release links.
13+
14+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/releases/links.html)
15+
16+
## Example Usage
17+
18+
```terraform
19+
# Create a project
20+
resource "gitlab_project" "example" {
21+
name = "example"
22+
description = "An example project"
23+
}
24+
25+
# Can create release link only to a tag associated with a release
26+
resource "gitlab_release_link" "example" {
27+
project = gitlab_project.example.id
28+
tag_name = "tag_name_associated_with_release"
29+
name = "test"
30+
url = "https://test/"
31+
}
32+
```
33+
34+
<!-- schema generated by tfplugindocs -->
35+
## Schema
36+
37+
### Required
38+
39+
- `name` (String) The name of the link. Link names must be unique within the release.
40+
- `project` (String) The ID or [URL-encoded path of the project](https://docs.gitlab.com/ee/api/index.html#namespaced-path-encoding).
41+
- `tag_name` (String) The tag associated with the Release.
42+
- `url` (String) The URL of the link. Link URLs must be unique within the release.
43+
44+
### Optional
45+
46+
- `filepath` (String) Relatively path for a [Direct Asset link](https://docs.gitlab.com/ee/user/project/releases/index.html#permanent-links-to-release-assets).
47+
- `link_type` (String) The type of the link: `other`, `runbook`, `image`, `package`. Defaults to other.
48+
49+
### Read-Only
50+
51+
- `direct_asset_link` (String) Full path for a [Direct Asset link](https://docs.gitlab.com/ee/user/project/releases/index.html#permanent-links-to-release-assets).
52+
- `external` (Boolean) External or internal link.
53+
- `id` (String) The ID of this resource.
54+
- `link_id` (Number) The ID of the link.
55+
56+
## Import
57+
58+
Import is supported using the following syntax:
59+
60+
```shell
61+
# Gitlab release link can be imported with a key composed of `<project>:<tag_name>:<link_id>`, e.g.
62+
terraform import gitlab_release_link.example "12345:test:2"
63+
```
Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
# Create a project
22
resource "gitlab_project" "example" {
3-
name = "example"
4-
description = "An example project"
5-
namespace_id = gitlab_group.example.id
6-
}
7-
8-
# Create a tag
9-
resource "gitlab_project_tag" "example" {
10-
name = "example"
11-
ref = gitlab_project.example.default_branch
12-
project = gitlab_project.example.id
3+
name = "example"
4+
description = "An example project"
135
}
146

7+
# Can create release link only to a tag associated with a release
158
resource "gitlab_release_link" "example" {
169
project = gitlab_project.example.id
17-
tag_name = gitlab_project_tag.example.name
10+
tag_name = "tag_name_associated_with_release"
1811
name = "test"
1912
url = "https://test/"
2013
}

internal/provider/helper_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,27 @@ func testAccCreateTags(t *testing.T, project *gitlab.Project, n int) []*gitlab.T
288288
return tags
289289
}
290290

291+
// testAccCreateReleases is a test helper for creating a specified number of releases.
292+
// It assumes the project will be destroyed at the end of the test and will not cleanup created releases.
293+
func testAccCreateReleases(t *testing.T, project *gitlab.Project, tags []*gitlab.Tag) []*gitlab.Release {
294+
t.Helper()
295+
296+
releases := make([]*gitlab.Release, len(tags))
297+
298+
for i := range releases {
299+
var err error
300+
releases[i], _, err = testGitlabClient.Releases.CreateRelease(project.ID, &gitlab.CreateReleaseOptions{
301+
Name: gitlab.String(acctest.RandomWithPrefix("acctest")),
302+
TagName: &tags[i].Name,
303+
})
304+
if err != nil {
305+
t.Fatalf("could not create test releases: %v", err)
306+
}
307+
}
308+
309+
return releases
310+
}
311+
291312
// testAccAddProjectMembers is a test helper for adding users as members of a project.
292313
// It assumes the project will be destroyed at the end of the test and will not cleanup members.
293314
func testAccAddProjectMembers(t *testing.T, pid interface{}, users []*gitlab.User) {

internal/provider/resource_gitlab_release_link.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func resourceGitlabReleaseLinkCreate(ctx context.Context, d *schema.ResourceData
4040
Name: &name,
4141
URL: &url,
4242
}
43-
if filePath, ok := d.GetOk("file_path"); ok {
43+
if filePath, ok := d.GetOk("filepath"); ok {
4444
options.FilePath = gitlab.String(filePath.(string))
4545
}
4646
if linkType, ok := d.GetOk("link_type"); ok {
@@ -99,8 +99,8 @@ func resourceGitlabReleaseLinkUpdate(ctx context.Context, d *schema.ResourceData
9999
if d.HasChange("url") {
100100
options.URL = gitlab.String(d.Get("url").(string))
101101
}
102-
if d.HasChange("file_path") {
103-
options.FilePath = gitlab.String(d.Get("file_path").(string))
102+
if d.HasChange("filepath") {
103+
options.FilePath = gitlab.String(d.Get("filepath").(string))
104104
}
105105
if d.HasChange("link_type") {
106106
linkTypeValue := gitlab.LinkTypeValue(d.Get("link_type").(string))

internal/provider/resource_gitlab_release_link_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ func TestAccGitlabReleaseLink_basic(t *testing.T) {
1717

1818
rInt1, rInt2 := acctest.RandInt(), acctest.RandInt()
1919
project := testAccCreateProject(t)
20-
tag := testAccCreateTags(t, project, 1)[0]
20+
tags := testAccCreateTags(t, project, 1)
21+
releases := testAccCreateReleases(t, project, tags)
2122

2223
resource.Test(t, resource.TestCase{
2324
ProviderFactories: providerFactories,
@@ -31,9 +32,10 @@ func TestAccGitlabReleaseLink_basic(t *testing.T) {
3132
tag_name = "%s"
3233
name = "test-%d"
3334
url = "https://test/%d"
34-
}`, project.PathWithNamespace, tag.Name, rInt1, rInt1),
35+
}`, project.PathWithNamespace, releases[0].TagName, rInt1, rInt1),
3536
Check: resource.ComposeTestCheckFunc(
3637
resource.TestCheckResourceAttrSet("gitlab_release_link.this", "link_id"),
38+
resource.TestCheckResourceAttrSet("gitlab_release_link.this", "direct_asset_link"),
3739
resource.TestCheckResourceAttrSet("gitlab_release_link.this", "external"),
3840
),
3941
},
@@ -51,11 +53,12 @@ func TestAccGitlabReleaseLink_basic(t *testing.T) {
5153
tag_name = "%s"
5254
name = "test-%d"
5355
url = "https://test/%d"
54-
filepath = "http://test/%d"
56+
filepath = "/test/%d"
5557
link_type = "runbook"
56-
}`, project.ID, tag.Name, rInt2, rInt2, rInt2),
58+
}`, project.ID, releases[0].TagName, rInt2, rInt2, rInt2),
5759
Check: resource.ComposeTestCheckFunc(
5860
resource.TestCheckResourceAttrSet("gitlab_release_link.this", "link_id"),
61+
resource.TestCheckResourceAttrSet("gitlab_release_link.this", "direct_asset_link"),
5962
resource.TestCheckResourceAttrSet("gitlab_release_link.this", "external"),
6063
),
6164
},

internal/provider/schema_gitlab_release_link.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package provider
22

33
import (
44
"fmt"
5+
"strings"
56

67
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
78
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
@@ -34,7 +35,7 @@ func gitlabReleaseLinkGetSchema() map[string]*schema.Schema {
3435
Required: true,
3536
},
3637
"filepath": {
37-
Description: "Optional path for a [Direct Asset link](https://docs.gitlab.com/ee/user/project/releases/index.html#permanent-links-to-release-assets).",
38+
Description: "Relatively path for a [Direct Asset link](https://docs.gitlab.com/ee/user/project/releases/index.html#permanent-links-to-release-assets).",
3839
Type: schema.TypeString,
3940
Optional: true,
4041
},
@@ -50,6 +51,11 @@ func gitlabReleaseLinkGetSchema() map[string]*schema.Schema {
5051
Type: schema.TypeInt,
5152
Computed: true,
5253
},
54+
"direct_asset_link": {
55+
Description: "Full path for a [Direct Asset link](https://docs.gitlab.com/ee/user/project/releases/index.html#permanent-links-to-release-assets).",
56+
Type: schema.TypeString,
57+
Computed: true,
58+
},
5359
"external": {
5460
Description: "External or internal link.",
5561
Type: schema.TypeBool,
@@ -64,9 +70,15 @@ func gitlabReleaseLinkToStateMap(project string, tagName string, releaseLink *gi
6470
stateMap["tag_name"] = tagName
6571
stateMap["name"] = releaseLink.Name
6672
stateMap["url"] = releaseLink.URL
67-
stateMap["file_path"] = releaseLink.DirectAssetURL
73+
directAssetLinkArray := strings.SplitN(releaseLink.DirectAssetURL, "downloads", 2)
74+
if len(directAssetLinkArray) > 1 {
75+
stateMap["filepath"] = directAssetLinkArray[1]
76+
} else {
77+
stateMap["filepath"] = ""
78+
}
6879
stateMap["link_type"] = releaseLink.LinkType
6980
stateMap["link_id"] = releaseLink.ID
81+
stateMap["direct_asset_link"] = releaseLink.DirectAssetURL
7082
stateMap["external"] = releaseLink.External
7183

7284
return stateMap

0 commit comments

Comments
 (0)