|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "strconv" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 11 | + gitlab "github.com/xanzy/go-gitlab" |
| 12 | +) |
| 13 | + |
| 14 | +var _ = registerResource("gitlab_cluster_agent", func() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Description: `The ` + "`" + `gitlab_cluster_agent` + "`" + ` resource allows to manage the lifecycle of a GitLab Agent for Kubernetes. |
| 17 | +
|
| 18 | +-> Note that this resource only registers the agent, but doesn't configure it. |
| 19 | + The configuration needs to be manually added as described in |
| 20 | + [the docs](https://docs.gitlab.com/ee/user/clusters/agent/install/index.html#create-an-agent-configuration-file). |
| 21 | + However, a ` + "`gitlab_repository_file`" + ` resource may be used to achieve that. |
| 22 | +
|
| 23 | +-> Requires at least maintainer permissions on the project. |
| 24 | +
|
| 25 | +-> Requires at least GitLab 14.10 |
| 26 | +
|
| 27 | +**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/cluster_agents.html)`, |
| 28 | + |
| 29 | + CreateContext: resourceGitlabClusterAgentCreate, |
| 30 | + ReadContext: resourceGitlabClusterAgentRead, |
| 31 | + DeleteContext: resourceGitlabClusterAgentDelete, |
| 32 | + Importer: &schema.ResourceImporter{ |
| 33 | + StateContext: schema.ImportStatePassthroughContext, |
| 34 | + }, |
| 35 | + |
| 36 | + Schema: gitlabClusterAgentSchema(), |
| 37 | + } |
| 38 | +}) |
| 39 | + |
| 40 | +func resourceGitlabClusterAgentCreate(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.RegisterAgentOptions{ |
| 45 | + Name: gitlab.String(d.Get("name").(string)), |
| 46 | + } |
| 47 | + |
| 48 | + log.Printf("[DEBUG] create GitLab Agent for Kubernetes in project %s with name '%v'", project, options.Name) |
| 49 | + clusterAgent, _, err := client.ClusterAgents.RegisterAgent(project, &options, gitlab.WithContext(ctx)) |
| 50 | + if err != nil { |
| 51 | + return diag.FromErr(err) |
| 52 | + } |
| 53 | + |
| 54 | + d.SetId(resourceGitlabClusterAgentBuildID(project, clusterAgent.ID)) |
| 55 | + return resourceGitlabClusterAgentRead(ctx, d, meta) |
| 56 | +} |
| 57 | + |
| 58 | +func resourceGitlabClusterAgentRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 59 | + client := meta.(*gitlab.Client) |
| 60 | + project, agentID, err := resourceGitlabClusterAgentParseID(d.Id()) |
| 61 | + if err != nil { |
| 62 | + return diag.FromErr(err) |
| 63 | + } |
| 64 | + |
| 65 | + log.Printf("[DEBUG] read GitLab Agent for Kubernetes in project %s with id %d", project, agentID) |
| 66 | + clusterAgent, _, err := client.ClusterAgents.GetAgent(project, agentID, gitlab.WithContext(ctx)) |
| 67 | + if err != nil { |
| 68 | + if is404(err) { |
| 69 | + log.Printf("[DEBUG] read GitLab Agent for Kubernetes in project %s with id %d not found, removing from state", project, agentID) |
| 70 | + d.SetId("") |
| 71 | + return nil |
| 72 | + } |
| 73 | + return diag.FromErr(err) |
| 74 | + } |
| 75 | + |
| 76 | + stateMap := gitlabClusterAgentToStateMap(project, clusterAgent) |
| 77 | + if err = setStateMapInResourceData(stateMap, d); err != nil { |
| 78 | + return diag.FromErr(err) |
| 79 | + } |
| 80 | + return nil |
| 81 | +} |
| 82 | + |
| 83 | +func resourceGitlabClusterAgentDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 84 | + client := meta.(*gitlab.Client) |
| 85 | + project, agentID, err := resourceGitlabClusterAgentParseID(d.Id()) |
| 86 | + if err != nil { |
| 87 | + return diag.FromErr(err) |
| 88 | + } |
| 89 | + |
| 90 | + log.Printf("[DEBUG] delete GitLab Agent for Kubernetes in project %s with id %d", project, agentID) |
| 91 | + if _, err := client.ClusterAgents.DeleteAgent(project, agentID, gitlab.WithContext(ctx)); err != nil { |
| 92 | + return diag.FromErr(err) |
| 93 | + } |
| 94 | + |
| 95 | + return nil |
| 96 | +} |
| 97 | + |
| 98 | +func resourceGitlabClusterAgentBuildID(project string, agentID int) string { |
| 99 | + return fmt.Sprintf("%s:%d", project, agentID) |
| 100 | +} |
| 101 | + |
| 102 | +func resourceGitlabClusterAgentParseID(id string) (string, int, error) { |
| 103 | + project, rawAgentID, err := parseTwoPartID(id) |
| 104 | + if err != nil { |
| 105 | + return "", 0, err |
| 106 | + } |
| 107 | + |
| 108 | + agentID, err := strconv.Atoi(rawAgentID) |
| 109 | + if err != nil { |
| 110 | + return "", 0, err |
| 111 | + } |
| 112 | + |
| 113 | + return project, agentID, nil |
| 114 | +} |
0 commit comments