|
| 1 | +package bitbucket |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "log" |
| 10 | + "net/http" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 14 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 15 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 16 | +) |
| 17 | + |
| 18 | +type RepositoryUserPermission struct { |
| 19 | + Permission string `json:"permission"` |
| 20 | + User *RepositoryUser `json:"user,omitempty"` |
| 21 | +} |
| 22 | + |
| 23 | +type RepositoryUser struct { |
| 24 | + UUID string `json:"uuid,omitempty"` |
| 25 | +} |
| 26 | + |
| 27 | +func resourceRepositoryUserPermission() *schema.Resource { |
| 28 | + return &schema.Resource{ |
| 29 | + CreateWithoutTimeout: resourceRepositoryUserPermissionPut, |
| 30 | + ReadWithoutTimeout: resourceRepositoryUserPermissionRead, |
| 31 | + UpdateWithoutTimeout: resourceRepositoryUserPermissionPut, |
| 32 | + DeleteWithoutTimeout: resourceRepositoryUserPermissionDelete, |
| 33 | + Importer: &schema.ResourceImporter{ |
| 34 | + StateContext: schema.ImportStatePassthroughContext, |
| 35 | + }, |
| 36 | + |
| 37 | + Schema: map[string]*schema.Schema{ |
| 38 | + "workspace": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Required: true, |
| 41 | + ForceNew: true, |
| 42 | + }, |
| 43 | + "repo_slug": { |
| 44 | + Type: schema.TypeString, |
| 45 | + Required: true, |
| 46 | + ForceNew: true, |
| 47 | + }, |
| 48 | + "user_id": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Required: true, |
| 51 | + ForceNew: true, |
| 52 | + }, |
| 53 | + "permission": { |
| 54 | + Type: schema.TypeString, |
| 55 | + Required: true, |
| 56 | + ValidateFunc: validation.StringInSlice([]string{"admin", "write", "read", "none"}, false), |
| 57 | + }, |
| 58 | + }, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func createRepositoryUserPermission(d *schema.ResourceData) *RepositoryUserPermission { |
| 63 | + |
| 64 | + permission := &RepositoryUserPermission{ |
| 65 | + Permission: d.Get("permission").(string), |
| 66 | + } |
| 67 | + |
| 68 | + return permission |
| 69 | +} |
| 70 | + |
| 71 | +func resourceRepositoryUserPermissionPut(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 72 | + client := m.(Clients).httpClient |
| 73 | + permission := createRepositoryUserPermission(d) |
| 74 | + |
| 75 | + payload, err := json.Marshal(permission) |
| 76 | + if err != nil { |
| 77 | + return diag.FromErr(err) |
| 78 | + } |
| 79 | + |
| 80 | + workspace := d.Get("workspace").(string) |
| 81 | + repoSlug := d.Get("repo_slug").(string) |
| 82 | + userSlug := d.Get("user_id").(string) |
| 83 | + |
| 84 | + permissionReq, err := client.Put(fmt.Sprintf("2.0/repositories/%s/%s/permissions-config/users/%s", |
| 85 | + workspace, |
| 86 | + repoSlug, |
| 87 | + userSlug, |
| 88 | + ), bytes.NewBuffer(payload)) |
| 89 | + |
| 90 | + if err != nil { |
| 91 | + return diag.FromErr(err) |
| 92 | + } |
| 93 | + |
| 94 | + body, readerr := io.ReadAll(permissionReq.Body) |
| 95 | + if readerr != nil { |
| 96 | + return diag.FromErr(readerr) |
| 97 | + } |
| 98 | + |
| 99 | + decodeerr := json.Unmarshal(body, &permission) |
| 100 | + if decodeerr != nil { |
| 101 | + return diag.FromErr(decodeerr) |
| 102 | + } |
| 103 | + |
| 104 | + if d.IsNewResource() { |
| 105 | + d.SetId(fmt.Sprintf("%s:%s:%s", workspace, repoSlug, userSlug)) |
| 106 | + } |
| 107 | + |
| 108 | + return resourceRepositoryUserPermissionRead(ctx, d, m) |
| 109 | +} |
| 110 | + |
| 111 | +func resourceRepositoryUserPermissionRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 112 | + client := m.(Clients).httpClient |
| 113 | + |
| 114 | + workspace, repoSlug, userSlug, err := repositoryUserPermissionId(d.Id()) |
| 115 | + if err != nil { |
| 116 | + return diag.FromErr(err) |
| 117 | + } |
| 118 | + |
| 119 | + permissionReq, err := client.Get(fmt.Sprintf("2.0/repositories/%s/%s/permissions-config/users/%s", |
| 120 | + workspace, |
| 121 | + repoSlug, |
| 122 | + userSlug, |
| 123 | + )) |
| 124 | + |
| 125 | + if permissionReq.StatusCode == http.StatusNotFound { |
| 126 | + log.Printf("[WARN] Repository User Permission (%s) not found, removing from state", d.Id()) |
| 127 | + d.SetId("") |
| 128 | + return nil |
| 129 | + } |
| 130 | + |
| 131 | + if err != nil { |
| 132 | + return diag.FromErr(err) |
| 133 | + } |
| 134 | + |
| 135 | + var permission RepositoryUserPermission |
| 136 | + |
| 137 | + body, readerr := io.ReadAll(permissionReq.Body) |
| 138 | + if readerr != nil { |
| 139 | + return diag.FromErr(readerr) |
| 140 | + } |
| 141 | + |
| 142 | + log.Printf("Repository User Permission raw is: %#v", string(body)) |
| 143 | + |
| 144 | + decodeerr := json.Unmarshal(body, &permission) |
| 145 | + if decodeerr != nil { |
| 146 | + return diag.FromErr(decodeerr) |
| 147 | + } |
| 148 | + |
| 149 | + log.Printf("Repository User Permission decoded is: %#v", permission) |
| 150 | + |
| 151 | + d.Set("permission", permission.Permission) |
| 152 | + d.Set("user_id", permission.User.UUID) |
| 153 | + d.Set("workspace", workspace) |
| 154 | + d.Set("repo_slug", repoSlug) |
| 155 | + |
| 156 | + return nil |
| 157 | +} |
| 158 | + |
| 159 | +func resourceRepositoryUserPermissionDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 160 | + client := m.(Clients).httpClient |
| 161 | + |
| 162 | + workspace, repoSlug, userSlug, err := repositoryUserPermissionId(d.Id()) |
| 163 | + if err != nil { |
| 164 | + return diag.FromErr(err) |
| 165 | + } |
| 166 | + |
| 167 | + _, err = client.Delete(fmt.Sprintf("2.0/repositories/%s/%s/permissions-config/users/%s", |
| 168 | + workspace, |
| 169 | + repoSlug, |
| 170 | + userSlug, |
| 171 | + )) |
| 172 | + |
| 173 | + return diag.FromErr(err) |
| 174 | +} |
| 175 | + |
| 176 | +func repositoryUserPermissionId(id string) (string, string, string, error) { |
| 177 | + parts := strings.Split(id, ":") |
| 178 | + |
| 179 | + if len(parts) != 3 { |
| 180 | + return "", "", "", fmt.Errorf("unexpected format of ID (%q), expected WORKSPACE:REPO-SLUG:GROUP-SLUG", id) |
| 181 | + } |
| 182 | + |
| 183 | + return parts[0], parts[1], parts[2], nil |
| 184 | +} |
0 commit comments