Skip to content

Commit 0adf6ac

Browse files
SharaevNtimofurrer
authored andcommitted
Added gitlab_tag as a resource and data source
1 parent bbf4656 commit 0adf6ac

File tree

12 files changed

+710
-11
lines changed

12 files changed

+710
-11
lines changed

docs/data-sources/project_tag.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gitlab_project_tag Data Source - terraform-provider-gitlab"
4+
subcategory: ""
5+
description: |-
6+
Provide details about a gitlab project tag
7+
Upstream API : GitLab API docs https://docs.gitlab.com/ee/api/tags.html
8+
---
9+
10+
# gitlab_project_tag (Data Source)
11+
12+
Provide details about a gitlab project tag
13+
14+
**Upstream API** : [GitLab API docs](https://docs.gitlab.com/ee/api/tags.html)
15+
16+
## Example Usage
17+
18+
```terraform
19+
# By project ID
20+
data "gitlab_project_tag" "foo" {
21+
name = "example"
22+
project = "12345"
23+
}
24+
25+
# By project full path
26+
data "gitlab_project_tag" "foo" {
27+
name = "example"
28+
project = "foo/bar"
29+
}
30+
```
31+
32+
<!-- schema generated by tfplugindocs -->
33+
## Schema
34+
35+
### Required
36+
37+
- **name** (String) The name of a tag.
38+
- **project** (String) The ID or URL-encoded path of the project owned by the authenticated user.
39+
40+
### Optional
41+
42+
- **id** (String) The ID of this resource.
43+
44+
### Read-Only
45+
46+
- **commit** (Set of Object) The commit associated with the tag ref. (see [below for nested schema](#nestedatt--commit))
47+
- **message** (String) Creates annotated tag.
48+
- **protected** (Boolean) Bool, true if tag has tag protection.
49+
- **release** (Set of Object) The release associated with the tag. (see [below for nested schema](#nestedatt--release))
50+
- **target** (String) The unique id assigned to the commit by Gitlab.
51+
52+
<a id="nestedatt--commit"></a>
53+
### Nested Schema for `commit`
54+
55+
Read-Only:
56+
57+
- **author_email** (String)
58+
- **author_name** (String)
59+
- **authored_date** (String)
60+
- **committed_date** (String)
61+
- **committer_email** (String)
62+
- **committer_name** (String)
63+
- **id** (String)
64+
- **message** (String)
65+
- **parent_ids** (Set of String)
66+
- **short_id** (String)
67+
- **title** (String)
68+
69+
70+
<a id="nestedatt--release"></a>
71+
### Nested Schema for `release`
72+
73+
Read-Only:
74+
75+
- **description** (String)
76+
- **tag_name** (String)
77+
78+

docs/resources/project_tag.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gitlab_project_tag Resource - terraform-provider-gitlab"
4+
subcategory: ""
5+
description: |-
6+
This resource allows you to create and manage GitLab tags.
7+
Upstream API : GitLab API docs https://docs.gitlab.com/ee/api/tags.html
8+
---
9+
10+
# gitlab_project_tag (Resource)
11+
12+
This resource allows you to create and manage GitLab tags.
13+
14+
**Upstream API** : [GitLab API docs](https://docs.gitlab.com/ee/api/tags.html)
15+
16+
## Example Usage
17+
18+
```terraform
19+
# Create a project for the tag to use
20+
resource "gitlab_project" "example" {
21+
name = "example"
22+
description = "An example project"
23+
namespace_id = gitlab_group.example.id
24+
}
25+
26+
resource "gitlab_project_tag" "example" {
27+
name = "example"
28+
ref = "main"
29+
project = gitlab_project.example.id
30+
}
31+
```
32+
33+
<!-- schema generated by tfplugindocs -->
34+
## Schema
35+
36+
### Required
37+
38+
- **name** (String) The name of a tag.
39+
- **project** (String) The ID or URL-encoded path of the project owned by the authenticated user.
40+
- **ref** (String) Create tag using commit SHA, another tag name, or branch name.
41+
42+
### Optional
43+
44+
- **id** (String) The ID of this resource.
45+
- **message** (String) Creates annotated tag.
46+
47+
### Read-Only
48+
49+
- **commit** (Set of Object) The commit associated with the tag. (see [below for nested schema](#nestedatt--commit))
50+
- **protected** (Boolean) Bool, true if tag has tag protection.
51+
- **release** (Set of Object) The release associated with the tag. (see [below for nested schema](#nestedatt--release))
52+
- **target** (String) The unique id assigned to the commit by Gitlab.
53+
54+
<a id="nestedatt--commit"></a>
55+
### Nested Schema for `commit`
56+
57+
Read-Only:
58+
59+
- **author_email** (String)
60+
- **author_name** (String)
61+
- **authored_date** (String)
62+
- **committed_date** (String)
63+
- **committer_email** (String)
64+
- **committer_name** (String)
65+
- **id** (String)
66+
- **message** (String)
67+
- **parent_ids** (Set of String)
68+
- **short_id** (String)
69+
- **title** (String)
70+
71+
72+
<a id="nestedatt--release"></a>
73+
### Nested Schema for `release`
74+
75+
Read-Only:
76+
77+
- **description** (String)
78+
- **tag_name** (String)
79+
80+
## Import
81+
82+
Import is supported using the following syntax:
83+
84+
```shell
85+
# Gitlab project tags can be imported with a key composed of `<project_id>:<tag_name>`, e.g.
86+
terraform import gitlab_project_tag.example "12345:develop"
87+
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# By project ID
2+
data "gitlab_project_tag" "foo" {
3+
name = "example"
4+
project = "12345"
5+
}
6+
7+
# By project full path
8+
data "gitlab_project_tag" "foo" {
9+
name = "example"
10+
project = "foo/bar"
11+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Gitlab project tags can be imported with a key composed of `<project_id>:<tag_name>`, e.g.
2+
terraform import gitlab_project_tag.example "12345:develop"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Create a project for the tag to use
2+
resource "gitlab_project" "example" {
3+
name = "example"
4+
description = "An example project"
5+
namespace_id = gitlab_group.example.id
6+
}
7+
8+
resource "gitlab_project_tag" "example" {
9+
name = "example"
10+
ref = "main"
11+
project = gitlab_project.example.id
12+
}
13+

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ require (
1111
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1
1212
github.com/mitchellh/hashstructure v1.1.0
1313
github.com/onsi/gomega v1.18.1
14-
github.com/xanzy/go-gitlab v0.56.0
14+
github.com/xanzy/go-gitlab v0.57.0
1515
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect
1616
google.golang.org/api v0.34.0 // indirect
1717
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,8 @@ github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaU
344344
github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
345345
github.com/vmihailenco/msgpack/v4 v4.3.12/go.mod h1:gborTTJjAo/GWTqqRjrLCn9pgNN+NXzzngzBKDPIqw4=
346346
github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
347-
github.com/xanzy/go-gitlab v0.56.0 h1:/QHBvk3IKVNwvXB/UOWVb5J6VCN6r2bg9/WxjUbFY/0=
348-
github.com/xanzy/go-gitlab v0.56.0/go.mod h1:F0QEXwmqiBUxCgJm8fE9S+1veX4XC9Z4cfaAbqwk4YM=
347+
github.com/xanzy/go-gitlab v0.57.0 h1:Ju93UFoEqe+/cHP7vhC3PlkHHli9dWiVHAm2luESA2M=
348+
github.com/xanzy/go-gitlab v0.57.0/go.mod h1:F0QEXwmqiBUxCgJm8fE9S+1veX4XC9Z4cfaAbqwk4YM=
349349
github.com/xanzy/ssh-agent v0.3.0 h1:wUMzuKtKilRgBAD1sUb8gOwwRr2FGoBVumcjoOACClI=
350350
github.com/xanzy/ssh-agent v0.3.0/go.mod h1:3s9xbODqPuuhK9JV1R321M/FlMZSBvE5aY6eAcqrDh0=
351351
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"log"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/xanzy/go-gitlab"
10+
)
11+
12+
var _ = registerDataSource("gitlab_project_tag", func() *schema.Resource {
13+
return &schema.Resource{
14+
Description: `Provide details about a gitlab project tag
15+
16+
**Upstream API** : [GitLab API docs](https://docs.gitlab.com/ee/api/tags.html)`,
17+
18+
ReadContext: dataSourceGitlabTagRead,
19+
Schema: map[string]*schema.Schema{
20+
"name": {
21+
Description: "The name of a tag.",
22+
Type: schema.TypeString,
23+
Required: true,
24+
},
25+
"project": {
26+
Description: "The ID or URL-encoded path of the project owned by the authenticated user.",
27+
Type: schema.TypeString,
28+
Required: true,
29+
},
30+
"message": {
31+
Description: "Creates annotated tag.",
32+
Type: schema.TypeString,
33+
Computed: true,
34+
},
35+
"protected": {
36+
Description: "Bool, true if tag has tag protection.",
37+
Type: schema.TypeBool,
38+
Computed: true,
39+
},
40+
"target": {
41+
Description: "The unique id assigned to the commit by Gitlab.",
42+
Type: schema.TypeString,
43+
Computed: true,
44+
},
45+
"release": {
46+
Description: "The release associated with the tag.",
47+
Type: schema.TypeSet,
48+
Computed: true,
49+
Set: schema.HashResource(releaseNoteSchema),
50+
Elem: releaseNoteSchema,
51+
},
52+
"commit": {
53+
Description: "The commit associated with the tag ref.",
54+
Type: schema.TypeSet,
55+
Computed: true,
56+
Set: schema.HashResource(commitSchema),
57+
Elem: commitSchema,
58+
},
59+
},
60+
}
61+
})
62+
63+
func dataSourceGitlabTagRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
64+
client := meta.(*gitlab.Client)
65+
name := d.Get("name").(string)
66+
project := d.Get("project").(string)
67+
log.Printf("[DEBUG] read gitlab tag %s/%s", project, name)
68+
tag, resp, err := client.Tags.GetTag(project, name, gitlab.WithContext(ctx))
69+
if err != nil {
70+
log.Printf("[DEBUG] failed to read gitlab tag %s/%s response %v", project, name, resp)
71+
return diag.FromErr(err)
72+
}
73+
74+
d.SetId(buildTwoPartID(&project, &name))
75+
d.Set("name", tag.Name)
76+
d.Set("project", project)
77+
d.Set("message", tag.Message)
78+
d.Set("protected", tag.Protected)
79+
d.Set("target", tag.Target)
80+
releaseNote := flattenReleaseNote(tag.Release)
81+
if err := d.Set("release", releaseNote); err != nil {
82+
return diag.FromErr(err)
83+
}
84+
if err := d.Set("commit", flattenCommit(tag.Commit)); err != nil {
85+
return diag.FromErr(err)
86+
}
87+
return nil
88+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package provider
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
10+
)
11+
12+
func TestAccDataGitlabTag_basic(t *testing.T) {
13+
testAccCheck(t)
14+
rInt := acctest.RandInt()
15+
project := testAccCreateProject(t)
16+
17+
resource.Test(t, resource.TestCase{
18+
PreCheck: func() { testAccPreCheck(t) },
19+
ProviderFactories: providerFactories,
20+
Steps: []resource.TestStep{
21+
{
22+
Config: testAccDataGitlabTag(rInt, project.PathWithNamespace),
23+
Check: resource.ComposeTestCheckFunc(
24+
testAccDataSourceGitlabTag("gitlab_project_tag.foo", "data.gitlab_project_tag.foo"),
25+
),
26+
},
27+
},
28+
})
29+
}
30+
31+
func testAccDataSourceGitlabTag(src, n string) resource.TestCheckFunc {
32+
return func(s *terraform.State) error {
33+
34+
tag := s.RootModule().Resources[src]
35+
tagAttr := tag.Primary.Attributes
36+
37+
search := s.RootModule().Resources[n]
38+
searchAttr := search.Primary.Attributes
39+
40+
testAttributes := []string{
41+
"id",
42+
"name",
43+
"project",
44+
"message",
45+
"protected",
46+
"target",
47+
"release",
48+
"commit",
49+
}
50+
51+
for _, attribute := range testAttributes {
52+
if searchAttr[attribute] != tagAttr[attribute] {
53+
return fmt.Errorf("expected the parameter of tag `%s` to be: %s, but got: `%s`", attribute, tagAttr[attribute], searchAttr[attribute])
54+
}
55+
}
56+
return nil
57+
}
58+
}
59+
60+
func testAccDataGitlabTag(rInt int, project string) string {
61+
return fmt.Sprintf(`
62+
%s
63+
data "gitlab_project_tag" "foo" {
64+
name = "${gitlab_project_tag.foo.name}"
65+
project = "%s"
66+
}
67+
`, testAccDataGitlabTagSetup(rInt, project), project)
68+
}
69+
70+
func testAccDataGitlabTagSetup(rInt int, project string) string {
71+
return fmt.Sprintf(`
72+
resource "gitlab_project_tag" "foo" {
73+
name = "tag-%[1]d"
74+
ref = "main"
75+
project = "%s"
76+
}
77+
`, rInt, project)
78+
}

0 commit comments

Comments
 (0)