Skip to content

Commit b304faf

Browse files
committed
Implement new data source gitlab_cluster_agent
1 parent 2a2a70a commit b304faf

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

docs/data-sources/cluster_agent.md

Lines changed: 43 additions & 0 deletions
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+
Lines changed: 4 additions & 0 deletions
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+
}
Lines changed: 42 additions & 0 deletions
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+
}
Lines changed: 39 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)