|
| 1 | +package gitlab |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "net/http" |
| 7 | + "strconv" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/helper/validation" |
| 12 | + "github.com/xanzy/go-gitlab" |
| 13 | +) |
| 14 | + |
| 15 | +func resourceGitlabDeployToken() *schema.Resource { |
| 16 | + return &schema.Resource{ |
| 17 | + Create: resourceGitlabDeployTokenCreate, |
| 18 | + Read: resourceGitlabDeployTokenRead, |
| 19 | + Delete: resourceGitlabDeployTokenDelete, |
| 20 | + |
| 21 | + Schema: map[string]*schema.Schema{ |
| 22 | + "project": { |
| 23 | + Type: schema.TypeString, |
| 24 | + Optional: true, |
| 25 | + ExactlyOneOf: []string{"project", "group"}, |
| 26 | + ForceNew: true, |
| 27 | + }, |
| 28 | + "group": { |
| 29 | + Type: schema.TypeString, |
| 30 | + Optional: true, |
| 31 | + ExactlyOneOf: []string{"project", "group"}, |
| 32 | + ForceNew: true, |
| 33 | + }, |
| 34 | + "name": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Required: true, |
| 37 | + ForceNew: true, |
| 38 | + }, |
| 39 | + "username": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Optional: true, |
| 42 | + Computed: true, |
| 43 | + ForceNew: true, |
| 44 | + }, |
| 45 | + "expires_at": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Optional: true, |
| 48 | + ValidateFunc: validation.ValidateRFC3339TimeString, |
| 49 | + ForceNew: true, |
| 50 | + }, |
| 51 | + "scopes": { |
| 52 | + Type: schema.TypeSet, |
| 53 | + Required: true, |
| 54 | + ForceNew: true, |
| 55 | + Elem: &schema.Schema{ |
| 56 | + Type: schema.TypeString, |
| 57 | + ValidateFunc: validation.StringInSlice([]string{"read_registry", "read_repository"}, false), |
| 58 | + }, |
| 59 | + }, |
| 60 | + |
| 61 | + "token": { |
| 62 | + Type: schema.TypeString, |
| 63 | + Computed: true, |
| 64 | + Sensitive: true, |
| 65 | + }, |
| 66 | + }, |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func resourceGitlabDeployTokenCreate(d *schema.ResourceData, meta interface{}) error { |
| 71 | + client := meta.(*gitlab.Client) |
| 72 | + project, isProject := d.GetOk("project") |
| 73 | + group, isGroup := d.GetOk("group") |
| 74 | + |
| 75 | + var expiresAt time.Time |
| 76 | + var err error |
| 77 | + |
| 78 | + if exp, ok := d.GetOk("expires_at"); ok { |
| 79 | + expiresAt, err = time.Parse(time.RFC3339, exp.(string)) |
| 80 | + if err != nil { |
| 81 | + return fmt.Errorf("Invalid expires_at date: %v", err) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + scopes := stringSetToStringSlice(d.Get("scopes").(*schema.Set)) |
| 86 | + |
| 87 | + var deployToken *gitlab.DeployToken |
| 88 | + |
| 89 | + if isProject { |
| 90 | + options := &gitlab.CreateProjectDeployTokenOptions{ |
| 91 | + Name: gitlab.String(d.Get("name").(string)), |
| 92 | + Username: gitlab.String(d.Get("username").(string)), |
| 93 | + ExpiresAt: gitlab.Time(expiresAt), |
| 94 | + Scopes: *scopes, |
| 95 | + } |
| 96 | + |
| 97 | + log.Printf("[DEBUG] Create GitLab deploy token %s in project %s", *options.Name, project.(string)) |
| 98 | + |
| 99 | + deployToken, _, err = client.DeployTokens.CreateProjectDeployToken(project, options) |
| 100 | + |
| 101 | + } else if isGroup { |
| 102 | + options := &gitlab.CreateGroupDeployTokenOptions{ |
| 103 | + Name: gitlab.String(d.Get("name").(string)), |
| 104 | + Username: gitlab.String(d.Get("username").(string)), |
| 105 | + ExpiresAt: gitlab.Time(expiresAt), |
| 106 | + Scopes: *scopes, |
| 107 | + } |
| 108 | + |
| 109 | + log.Printf("[DEBUG] Create GitLab deploy token %s in group %s", *options.Name, group.(string)) |
| 110 | + |
| 111 | + deployToken, _, err = client.DeployTokens.CreateGroupDeployToken(group, options) |
| 112 | + } |
| 113 | + |
| 114 | + if err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + |
| 118 | + d.SetId(fmt.Sprintf("%d", deployToken.ID)) |
| 119 | + |
| 120 | + // Token is only available on creation |
| 121 | + d.Set("token", deployToken.Token) |
| 122 | + d.Set("username", deployToken.Username) |
| 123 | + |
| 124 | + return nil |
| 125 | +} |
| 126 | + |
| 127 | +func resourceGitlabDeployTokenRead(d *schema.ResourceData, meta interface{}) error { |
| 128 | + client := meta.(*gitlab.Client) |
| 129 | + project, isProject := d.GetOk("project") |
| 130 | + group, isGroup := d.GetOk("group") |
| 131 | + deployTokenID, err := strconv.Atoi(d.Id()) |
| 132 | + if err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + |
| 136 | + var deployTokens []*gitlab.DeployToken |
| 137 | + |
| 138 | + if isProject { |
| 139 | + log.Printf("[DEBUG] Read GitLab deploy token %d in project %s", deployTokenID, project.(string)) |
| 140 | + deployTokens, _, err = client.DeployTokens.ListProjectDeployTokens(project, nil) |
| 141 | + |
| 142 | + } else if isGroup { |
| 143 | + log.Printf("[DEBUG] Read GitLab deploy token %d in group %s", deployTokenID, group.(string)) |
| 144 | + deployTokens, _, err = client.DeployTokens.ListGroupDeployTokens(group, nil) |
| 145 | + } |
| 146 | + if err != nil { |
| 147 | + return err |
| 148 | + } |
| 149 | + |
| 150 | + for _, token := range deployTokens { |
| 151 | + if token.ID == deployTokenID { |
| 152 | + d.Set("name", token.Name) |
| 153 | + d.Set("username", token.Username) |
| 154 | + d.Set("expires_at", token.ExpiresAt.Format(time.RFC3339)) |
| 155 | + |
| 156 | + for _, scope := range token.Scopes { |
| 157 | + if scope == "read_repository" { |
| 158 | + d.Set("scopes.read_repository", true) |
| 159 | + } |
| 160 | + |
| 161 | + if scope == "read_registry" { |
| 162 | + d.Set("scopes.read_registry", true) |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + return nil |
| 169 | +} |
| 170 | + |
| 171 | +func resourceGitlabDeployTokenDelete(d *schema.ResourceData, meta interface{}) error { |
| 172 | + client := meta.(*gitlab.Client) |
| 173 | + project, isProject := d.GetOk("project") |
| 174 | + group, isGroup := d.GetOk("group") |
| 175 | + deployTokenID, err := strconv.Atoi(d.Id()) |
| 176 | + if err != nil { |
| 177 | + return err |
| 178 | + } |
| 179 | + |
| 180 | + var response *gitlab.Response |
| 181 | + |
| 182 | + if isProject { |
| 183 | + log.Printf("[DEBUG] Delete GitLab deploy token %d in project %s", deployTokenID, project.(string)) |
| 184 | + response, err = client.DeployTokens.DeleteProjectDeployToken(project, deployTokenID) |
| 185 | + |
| 186 | + } else if isGroup { |
| 187 | + log.Printf("[DEBUG] Delete GitLab deploy token %d in group %s", deployTokenID, group.(string)) |
| 188 | + response, err = client.DeployTokens.DeleteGroupDeployToken(group, deployTokenID) |
| 189 | + } |
| 190 | + if err != nil { |
| 191 | + return err |
| 192 | + } |
| 193 | + |
| 194 | + // StatusNoContent = 204 |
| 195 | + // Success with no body |
| 196 | + if response.StatusCode != http.StatusNoContent { |
| 197 | + return fmt.Errorf("Invalid status code returned: %s", response.Status) |
| 198 | + } |
| 199 | + |
| 200 | + return nil |
| 201 | +} |
0 commit comments