Skip to content

Commit 78a0cf7

Browse files
committed
Implement new data source gitlab_cluster_agents
1 parent b304faf commit 78a0cf7

File tree

4 files changed

+171
-0
lines changed

4 files changed

+171
-0
lines changed

docs/data-sources/cluster_agents.md

Lines changed: 50 additions & 0 deletions
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+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
data "gitlab_cluster_agents" "agents" {
2+
project = "12345"
3+
}
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+
"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+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//go:build acceptance
2+
// +build acceptance
3+
4+
package provider
5+
6+
import (
7+
"fmt"
8+
"testing"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
11+
)
12+
13+
func TestAccDataSourceGitlabClusterAgents_basic(t *testing.T) {
14+
testAccRequiresAtLeast(t, "14.10")
15+
16+
testProject := testAccCreateProject(t)
17+
testClusterAgents := testAccCreateClusterAgents(t, testProject.ID, 25)
18+
19+
resource.Test(t, resource.TestCase{
20+
ProviderFactories: providerFactories,
21+
Steps: []resource.TestStep{
22+
{
23+
Config: fmt.Sprintf(`
24+
data "gitlab_cluster_agents" "this" {
25+
project = "%d"
26+
}
27+
`, testProject.ID),
28+
Check: resource.ComposeTestCheckFunc(
29+
resource.TestCheckResourceAttr("data.gitlab_cluster_agents.this", "cluster_agents.#", fmt.Sprintf("%d", len(testClusterAgents))),
30+
resource.TestCheckResourceAttrSet("data.gitlab_cluster_agents.this", "cluster_agents.0.name"),
31+
resource.TestCheckResourceAttrSet("data.gitlab_cluster_agents.this", "cluster_agents.0.created_at"),
32+
resource.TestCheckResourceAttrSet("data.gitlab_cluster_agents.this", "cluster_agents.0.created_by_user_id"),
33+
resource.TestCheckResourceAttrSet("data.gitlab_cluster_agents.this", "cluster_agents.1.name"),
34+
resource.TestCheckResourceAttrSet("data.gitlab_cluster_agents.this", "cluster_agents.1.created_at"),
35+
resource.TestCheckResourceAttrSet("data.gitlab_cluster_agents.this", "cluster_agents.1.created_by_user_id"),
36+
),
37+
},
38+
},
39+
})
40+
}

0 commit comments

Comments
 (0)