|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 12 | + gitlab "github.com/xanzy/go-gitlab" |
| 13 | +) |
| 14 | + |
| 15 | +var _ = registerResource("gitlab_cluster_agent_token", func() *schema.Resource { |
| 16 | + return &schema.Resource{ |
| 17 | + Description: `The ` + "`" + `gitlab_cluster_agent_token` + "`" + ` resource allows to manage the lifecycle of a token for a GitLab Agent for Kubernetes. |
| 18 | +
|
| 19 | +-> Requires at least maintainer permissions on the project. |
| 20 | +
|
| 21 | +-> Requires at least GitLab 15.0 |
| 22 | +
|
| 23 | +**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/cluster_agents.html#create-an-agent-token)`, |
| 24 | + |
| 25 | + CreateContext: resourceGitlabClusterAgentTokenCreate, |
| 26 | + ReadContext: resourceGitlabClusterAgentTokenRead, |
| 27 | + DeleteContext: resourceGitlabClusterAgentTokenDelete, |
| 28 | + Importer: &schema.ResourceImporter{ |
| 29 | + StateContext: schema.ImportStatePassthroughContext, |
| 30 | + }, |
| 31 | + |
| 32 | + Schema: gitlabClusterAgentTokenSchema(), |
| 33 | + } |
| 34 | +}) |
| 35 | + |
| 36 | +func resourceGitlabClusterAgentTokenCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 37 | + client := meta.(*gitlab.Client) |
| 38 | + |
| 39 | + project := d.Get("project").(string) |
| 40 | + agentID := d.Get("agent_id").(int) |
| 41 | + options := gitlab.CreateAgentTokenOptions{ |
| 42 | + Name: gitlab.String(d.Get("name").(string)), |
| 43 | + } |
| 44 | + |
| 45 | + if v, ok := d.GetOk("description"); ok { |
| 46 | + options.Description = gitlab.String(v.(string)) |
| 47 | + } |
| 48 | + |
| 49 | + log.Printf("[DEBUG] create token for GitLab Agent for Kubernetes %d in project %s with name '%v'", agentID, project, options.Name) |
| 50 | + clusterAgentToken, _, err := client.ClusterAgents.CreateAgentToken(project, agentID, &options, gitlab.WithContext(ctx)) |
| 51 | + if err != nil { |
| 52 | + return diag.FromErr(err) |
| 53 | + } |
| 54 | + |
| 55 | + d.SetId(resourceGitlabClusterAgentTokenBuildID(project, agentID, clusterAgentToken.ID)) |
| 56 | + // NOTE: the token is only returned with the direct response from the create API. |
| 57 | + d.Set("token", clusterAgentToken.Token) |
| 58 | + return resourceGitlabClusterAgentTokenRead(ctx, d, meta) |
| 59 | +} |
| 60 | + |
| 61 | +func resourceGitlabClusterAgentTokenRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 62 | + client := meta.(*gitlab.Client) |
| 63 | + project, agentID, tokenID, err := resourceGitlabClusterAgentTokenParseID(d.Id()) |
| 64 | + if err != nil { |
| 65 | + return diag.FromErr(err) |
| 66 | + } |
| 67 | + |
| 68 | + log.Printf("[DEBUG] read token for GitLab Agent for Kubernetes %d in project %s with id %d", agentID, project, tokenID) |
| 69 | + clusterAgentToken, _, err := client.ClusterAgents.GetAgentToken(project, agentID, tokenID, gitlab.WithContext(ctx)) |
| 70 | + if err != nil { |
| 71 | + if is404(err) { |
| 72 | + log.Printf("[DEBUG] read token for GitLab Agent for Kubernetes %d in project %s with id %d not found, removing from state", agentID, project, tokenID) |
| 73 | + d.SetId("") |
| 74 | + return nil |
| 75 | + } |
| 76 | + return diag.FromErr(err) |
| 77 | + } |
| 78 | + |
| 79 | + stateMap := gitlabClusterAgentTokenToStateMap(project, clusterAgentToken) |
| 80 | + if err = setStateMapInResourceData(stateMap, d); err != nil { |
| 81 | + return diag.FromErr(err) |
| 82 | + } |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +func resourceGitlabClusterAgentTokenDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 87 | + client := meta.(*gitlab.Client) |
| 88 | + project, agentID, tokenID, err := resourceGitlabClusterAgentTokenParseID(d.Id()) |
| 89 | + if err != nil { |
| 90 | + return diag.FromErr(err) |
| 91 | + } |
| 92 | + |
| 93 | + log.Printf("[DEBUG] delete token for GitLab Agent for Kubernetes %d in project %s with id %d", agentID, project, tokenID) |
| 94 | + if _, err := client.ClusterAgents.RevokeAgentToken(project, agentID, tokenID, gitlab.WithContext(ctx)); err != nil { |
| 95 | + return diag.FromErr(err) |
| 96 | + } |
| 97 | + |
| 98 | + return nil |
| 99 | +} |
| 100 | + |
| 101 | +func resourceGitlabClusterAgentTokenBuildID(project string, agentID int, tokenID int) string { |
| 102 | + return fmt.Sprintf("%s:%d:%d", project, agentID, tokenID) |
| 103 | +} |
| 104 | + |
| 105 | +func resourceGitlabClusterAgentTokenParseID(id string) (string, int, int, error) { |
| 106 | + parts := strings.Split(id, ":") |
| 107 | + if len(parts) != 3 { |
| 108 | + return "", 0, 0, fmt.Errorf("invalid cluster agent token id %q, expected format '{project}:{agent_id}:{token_id}", id) |
| 109 | + } |
| 110 | + project, rawAgentID, rawTokenID := parts[0], parts[1], parts[2] |
| 111 | + agentID, err := strconv.Atoi(rawAgentID) |
| 112 | + if err != nil { |
| 113 | + return "", 0, 0, fmt.Errorf("invalid cluster agent token id %q with 'agent_id' %q, expected integer", id, rawAgentID) |
| 114 | + } |
| 115 | + tokenID, err := strconv.Atoi(rawTokenID) |
| 116 | + if err != nil { |
| 117 | + return "", 0, 0, fmt.Errorf("invalid cluster agent token id %q with 'token_id' %q, expected integer", id, rawTokenID) |
| 118 | + } |
| 119 | + |
| 120 | + return project, agentID, tokenID, nil |
| 121 | +} |
0 commit comments