Skip to content

Commit 5ef55c7

Browse files
authored
Merge pull request #1073 from timofurrer/feature/gitlab_project_cluster_agent
Implement resources and data sources for GitLab Agent for Kubernetes API
2 parents 36c66a1 + 0f4b954 commit 5ef55c7

16 files changed

+742
-1
lines changed

.vscode/settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
"GITLAB_TOKEN": "ACCTEST1234567890123",
55
"GITLAB_BASE_URL": "http://127.0.0.1:8080"
66
},
7-
"go.testFlags": ["-count=1", "-v", "-tags=acceptance", "-tags=acceptance"]
7+
"go.testFlags": ["-count=1", "-v", "-tags=acceptance"],
8+
"go.buildFlags": ["-tags=acceptance"]
89
}

docs/data-sources/cluster_agent.md

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gitlab_cluster_agent Data Source - terraform-provider-gitlab"
4+
subcategory: ""
5+
description: |-
6+
The gitlab_cluster_agent data source allows to retrieve details about a GitLab Agent for Kubernetes.
7+
-> Requires at least GitLab 14.10
8+
Upstream API: GitLab REST API docs https://docs.gitlab.com/ee/api/cluster_agents.html
9+
---
10+
11+
# gitlab_cluster_agent (Data Source)
12+
13+
The `gitlab_cluster_agent` data source allows to retrieve details about a GitLab Agent for Kubernetes.
14+
15+
-> Requires at least GitLab 14.10
16+
17+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/cluster_agents.html)
18+
19+
## Example Usage
20+
21+
```terraform
22+
data "gitlab_cluster_agent" "example" {
23+
project = "12345"
24+
agent_id = 1
25+
}
26+
```
27+
28+
<!-- schema generated by tfplugindocs -->
29+
## Schema
30+
31+
### Required
32+
33+
- `agent_id` (Number) The ID of the agent.
34+
- `project` (String) ID or full path of the project maintained by the authenticated user.
35+
36+
### Read-Only
37+
38+
- `created_at` (String) The ISO8601 datetime when the agent was created.
39+
- `created_by_user_id` (Number) The ID of the user who created the agent.
40+
- `id` (String) The ID of this resource.
41+
- `name` (String) The Name of the agent.
42+
43+

docs/data-sources/cluster_agents.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gitlab_cluster_agents Data Source - terraform-provider-gitlab"
4+
subcategory: ""
5+
description: |-
6+
The gitlab_cluster_agents data source allows details of GitLab Agents for Kubernetes in a project.
7+
-> Requires at least GitLab 14.10
8+
Upstream API: GitLab REST API docs https://docs.gitlab.com/ee/api/cluster_agents.html
9+
---
10+
11+
# gitlab_cluster_agents (Data Source)
12+
13+
The `gitlab_cluster_agents` data source allows details of GitLab Agents for Kubernetes in a project.
14+
15+
-> Requires at least GitLab 14.10
16+
17+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/cluster_agents.html)
18+
19+
## Example Usage
20+
21+
```terraform
22+
data "gitlab_cluster_agents" "agents" {
23+
project = "12345"
24+
}
25+
```
26+
27+
<!-- schema generated by tfplugindocs -->
28+
## Schema
29+
30+
### Required
31+
32+
- `project` (String) The ID or full path of the project owned by the authenticated user.
33+
34+
### Read-Only
35+
36+
- `cluster_agents` (List of Object) List of the registered agents. (see [below for nested schema](#nestedatt--cluster_agents))
37+
- `id` (String) The ID of this resource.
38+
39+
<a id="nestedatt--cluster_agents"></a>
40+
### Nested Schema for `cluster_agents`
41+
42+
Read-Only:
43+
44+
- `agent_id` (Number)
45+
- `created_at` (String)
46+
- `created_by_user_id` (Number)
47+
- `name` (String)
48+
- `project` (String)
49+
50+

docs/resources/cluster_agent.md

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gitlab_cluster_agent Resource - terraform-provider-gitlab"
4+
subcategory: ""
5+
description: |-
6+
The gitlab_cluster_agent resource allows to manage the lifecycle of a GitLab Agent for Kubernetes.
7+
-> Note that this resource only registers the agent, but doesn't configure it.
8+
The configuration needs to be manually added as described in
9+
the docs https://docs.gitlab.com/ee/user/clusters/agent/install/index.html#create-an-agent-configuration-file.
10+
However, a gitlab_repository_file resource may be used to achieve that.
11+
-> Requires at least maintainer permissions on the project.
12+
-> Requires at least GitLab 14.10
13+
Upstream API: GitLab REST API docs https://docs.gitlab.com/ee/api/cluster_agents.html
14+
---
15+
16+
# gitlab_cluster_agent (Resource)
17+
18+
The `gitlab_cluster_agent` resource allows to manage the lifecycle of a GitLab Agent for Kubernetes.
19+
20+
-> Note that this resource only registers the agent, but doesn't configure it.
21+
The configuration needs to be manually added as described in
22+
[the docs](https://docs.gitlab.com/ee/user/clusters/agent/install/index.html#create-an-agent-configuration-file).
23+
However, a `gitlab_repository_file` resource may be used to achieve that.
24+
25+
-> Requires at least maintainer permissions on the project.
26+
27+
-> Requires at least GitLab 14.10
28+
29+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/cluster_agents.html)
30+
31+
## Example Usage
32+
33+
```terraform
34+
resource "gitlab_cluster_agent" "example" {
35+
project = "12345"
36+
name = "agent-1"
37+
}
38+
39+
// Optionally, configure the agent as described in
40+
// https://docs.gitlab.com/ee/user/clusters/agent/install/index.html#create-an-agent-configuration-file
41+
resource "gitlab_repository_file" "example_agent_config" {
42+
project = gitlab_cluster_agent.example.project
43+
branch = "main" // or use the `default_branch` attribute from a project data source / resource
44+
file_path = ".gitlab/agents/${gitlab_cluster_agent.example.name}"
45+
content = <<CONTENT
46+
gitops:
47+
...
48+
CONTENT
49+
author_email = "[email protected]"
50+
author_name = "Terraform"
51+
commit_message = "feature: add agent config for ${gitlab_cluster_agent.example.name}"
52+
}
53+
```
54+
55+
<!-- schema generated by tfplugindocs -->
56+
## Schema
57+
58+
### Required
59+
60+
- `name` (String) The Name of the agent.
61+
- `project` (String) ID or full path of the project maintained by the authenticated user.
62+
63+
### Read-Only
64+
65+
- `agent_id` (Number) The ID of the agent.
66+
- `created_at` (String) The ISO8601 datetime when the agent was created.
67+
- `created_by_user_id` (Number) The ID of the user who created the agent.
68+
- `id` (String) The ID of this resource.
69+
70+
## Import
71+
72+
Import is supported using the following syntax:
73+
74+
```shell
75+
# GitLab Agent for Kubernetes can be imported with the following command and the id pattern `<project>:<agent-id>`
76+
terraform import gitlab_cluster_agent.example '12345:42'
77+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
data "gitlab_cluster_agent" "example" {
2+
project = "12345"
3+
agent_id = 1
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
data "gitlab_cluster_agents" "agents" {
2+
project = "12345"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# GitLab Agent for Kubernetes can be imported with the following command and the id pattern `<project>:<agent-id>`
2+
terraform import gitlab_cluster_agent.example '12345:42'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
resource "gitlab_cluster_agent" "example" {
2+
project = "12345"
3+
name = "agent-1"
4+
}
5+
6+
// Optionally, configure the agent as described in
7+
// https://docs.gitlab.com/ee/user/clusters/agent/install/index.html#create-an-agent-configuration-file
8+
resource "gitlab_repository_file" "example_agent_config" {
9+
project = gitlab_cluster_agent.example.project
10+
branch = "main" // or use the `default_branch` attribute from a project data source / resource
11+
file_path = ".gitlab/agents/${gitlab_cluster_agent.example.name}"
12+
content = <<CONTENT
13+
gitops:
14+
...
15+
CONTENT
16+
author_email = "[email protected]"
17+
author_name = "Terraform"
18+
commit_message = "feature: add agent config for ${gitlab_cluster_agent.example.name}"
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"fmt"
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_cluster_agent", func() *schema.Resource {
13+
return &schema.Resource{
14+
Description: `The ` + "`gitlab_cluster_agent`" + ` data source allows to retrieve details about a GitLab Agent for Kubernetes.
15+
16+
-> Requires at least GitLab 14.10
17+
18+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/cluster_agents.html)`,
19+
20+
ReadContext: dataSourceGitlabClusterAgentRead,
21+
Schema: datasourceSchemaFromResourceSchema(gitlabClusterAgentSchema(), []string{"project", "agent_id"}, nil),
22+
}
23+
})
24+
25+
func dataSourceGitlabClusterAgentRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
26+
client := meta.(*gitlab.Client)
27+
28+
project := d.Get("project").(string)
29+
agentID := d.Get("agent_id").(int)
30+
31+
clusterAgent, _, err := client.ClusterAgents.GetAgent(project, agentID, gitlab.WithContext(ctx))
32+
if err != nil {
33+
return diag.FromErr(err)
34+
}
35+
36+
d.SetId(fmt.Sprintf("%s:%d", project, agentID))
37+
stateMap := gitlabClusterAgentToStateMap(project, clusterAgent)
38+
if err := setStateMapInResourceData(stateMap, d); err != nil {
39+
return diag.FromErr(err)
40+
}
41+
return nil
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//go:build acceptance
2+
// +build acceptance
3+
4+
package provider
5+
6+
import (
7+
"fmt"
8+
"testing"
9+
"time"
10+
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
12+
)
13+
14+
func TestAccDataSourceGitlabClusterAgent_basic(t *testing.T) {
15+
testAccRequiresAtLeast(t, "14.10")
16+
17+
testProject := testAccCreateProject(t)
18+
testAgent := testAccCreateClusterAgents(t, testProject.ID, 1)[0]
19+
20+
resource.Test(t, resource.TestCase{
21+
ProviderFactories: providerFactories,
22+
Steps: []resource.TestStep{
23+
{
24+
Config: fmt.Sprintf(`
25+
data "gitlab_cluster_agent" "this" {
26+
project = "%d"
27+
agent_id = %d
28+
}
29+
`, testProject.ID, testAgent.ID,
30+
),
31+
Check: resource.ComposeTestCheckFunc(
32+
resource.TestCheckResourceAttr("data.gitlab_cluster_agent.this", "name", testAgent.Name),
33+
resource.TestCheckResourceAttr("data.gitlab_cluster_agent.this", "created_at", testAgent.CreatedAt.Format(time.RFC3339)),
34+
resource.TestCheckResourceAttr("data.gitlab_cluster_agent.this", "created_by_user_id", fmt.Sprintf("%d", testAgent.CreatedByUserID)),
35+
),
36+
},
37+
},
38+
})
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"log"
6+
"time"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
"github.com/xanzy/go-gitlab"
11+
)
12+
13+
var _ = registerDataSource("gitlab_cluster_agents", func() *schema.Resource {
14+
return &schema.Resource{
15+
Description: `The ` + "`gitlab_cluster_agents`" + ` data source allows details of GitLab Agents for Kubernetes in a project.
16+
17+
-> Requires at least GitLab 14.10
18+
19+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/cluster_agents.html)`,
20+
21+
ReadContext: dataSourceGitlabClusterAgentsRead,
22+
Schema: map[string]*schema.Schema{
23+
"project": {
24+
Description: "The ID or full path of the project owned by the authenticated user.",
25+
Type: schema.TypeString,
26+
Required: true,
27+
},
28+
"cluster_agents": {
29+
Description: "List of the registered agents.",
30+
Type: schema.TypeList,
31+
Computed: true,
32+
Elem: &schema.Resource{
33+
Schema: datasourceSchemaFromResourceSchema(gitlabClusterAgentSchema(), nil, nil),
34+
},
35+
},
36+
},
37+
}
38+
})
39+
40+
func dataSourceGitlabClusterAgentsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
41+
client := meta.(*gitlab.Client)
42+
43+
project := d.Get("project").(string)
44+
options := gitlab.ListAgentsOptions{
45+
PerPage: 20,
46+
Page: 1,
47+
}
48+
49+
var clusterAgents []*gitlab.Agent
50+
for options.Page != 0 {
51+
paginatedClusterAgents, resp, err := client.ClusterAgents.ListAgents(project, &options, gitlab.WithContext(ctx))
52+
if err != nil {
53+
return diag.FromErr(err)
54+
}
55+
56+
clusterAgents = append(clusterAgents, paginatedClusterAgents...)
57+
options.Page = resp.NextPage
58+
}
59+
60+
log.Printf("[DEBUG] list GitLab Agents for Kubernetes in project %s", project)
61+
d.SetId(project)
62+
d.Set("project", project)
63+
if err := d.Set("cluster_agents", flattenClusterAgentsForState(clusterAgents)); err != nil {
64+
return diag.Errorf("Failed to set cluster agents to state: %v", err)
65+
}
66+
return nil
67+
}
68+
69+
func flattenClusterAgentsForState(clusterAgents []*gitlab.Agent) (values []map[string]interface{}) {
70+
for _, clusterAgent := range clusterAgents {
71+
values = append(values, map[string]interface{}{
72+
"name": clusterAgent.Name,
73+
"created_at": clusterAgent.CreatedAt.Format(time.RFC3339),
74+
"created_by_user_id": clusterAgent.CreatedByUserID,
75+
})
76+
}
77+
return values
78+
}

0 commit comments

Comments
 (0)