|
| 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