|
| 1 | +//go:build !(pacific || quincy || reef) && ceph_preview |
| 2 | + |
| 3 | +package admin |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "net/http" |
| 10 | +) |
| 11 | + |
| 12 | +// Account represents an RGW account |
| 13 | +type Account struct { |
| 14 | + ID string `json:"id" url:"id"` |
| 15 | + Name string `json:"name" url:"name"` |
| 16 | + Email string `json:"email" url:"email"` |
| 17 | + Tenant string `json:"tenant" url:"tenant"` |
| 18 | + MaxUsers *int64 `json:"max_users" url:"max-users"` |
| 19 | + MaxRoles *int64 `json:"max_roles" url:"max-roles"` |
| 20 | + MaxGroups *int64 `json:"max_groups" url:"max-groups"` |
| 21 | + MaxAccessKeys *int64 `json:"max_access_keys" url:"max-access-keys"` |
| 22 | + MaxBuckets *int64 `json:"max_buckets" url:"max-buckets"` |
| 23 | + Quota QuotaSpec `json:"quota"` |
| 24 | + BucketQuota QuotaSpec `json:"bucket_quota"` |
| 25 | +} |
| 26 | + |
| 27 | +// CreateAccount will create a new RGW account |
| 28 | +// https://docs.ceph.com/en/latest/radosgw/adminops/#create-account |
| 29 | +func (api *API) CreateAccount(ctx context.Context, account Account) (Account, error) { |
| 30 | + |
| 31 | + body, err := api.call(ctx, http.MethodPost, "/account", valueToURLParams(account, []string{"id", "name", "email", "tenant", "max-users", "max-roles", "max-groups", "max-access-keys", "max-buckets"})) |
| 32 | + if err != nil { |
| 33 | + return Account{}, err |
| 34 | + } |
| 35 | + |
| 36 | + a := Account{} |
| 37 | + err = json.Unmarshal(body, &a) |
| 38 | + if err != nil { |
| 39 | + return Account{}, fmt.Errorf("%s. %s. %w", unmarshalError, string(body), err) |
| 40 | + } |
| 41 | + |
| 42 | + return a, nil |
| 43 | +} |
| 44 | + |
| 45 | +// GetAccount will return the RGW account details |
| 46 | +// https://docs.ceph.com/en/latest/radosgw/adminops/#get-account-info |
| 47 | +func (api *API) GetAccount(ctx context.Context, accountID string) (Account, error) { |
| 48 | + if accountID == "" { |
| 49 | + return Account{}, ErrInvalidArgument |
| 50 | + } |
| 51 | + |
| 52 | + body, err := api.call(ctx, http.MethodGet, "/account", valueToURLParams(Account{ID: accountID}, []string{"id"})) |
| 53 | + if err != nil { |
| 54 | + return Account{}, err |
| 55 | + } |
| 56 | + |
| 57 | + a := Account{} |
| 58 | + err = json.Unmarshal(body, &a) |
| 59 | + if err != nil { |
| 60 | + return Account{}, fmt.Errorf("%s. %s. %w", unmarshalError, string(body), err) |
| 61 | + } |
| 62 | + |
| 63 | + return a, nil |
| 64 | +} |
| 65 | + |
| 66 | +// DeleteAccount will delete the RGW account |
| 67 | +// https://docs.ceph.com/en/latest/radosgw/adminops/#remove-account |
| 68 | +func (api *API) DeleteAccount(ctx context.Context, accountID string) error { |
| 69 | + if accountID == "" { |
| 70 | + return ErrInvalidArgument |
| 71 | + } |
| 72 | + |
| 73 | + _, err := api.call(ctx, http.MethodDelete, "/account", valueToURLParams(Account{ID: accountID}, []string{"id"})) |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + |
| 78 | + return nil |
| 79 | +} |
| 80 | + |
| 81 | +// ModifyAccount will modify the RGW account |
| 82 | +// https://docs.ceph.com/en/latest/radosgw/adminops/#modify-account |
| 83 | +func (api *API) ModifyAccount(ctx context.Context, account Account) (Account, error) { |
| 84 | + if account.ID == "" { |
| 85 | + return Account{}, ErrInvalidArgument |
| 86 | + } |
| 87 | + |
| 88 | + body, err := api.call(ctx, http.MethodPut, "/account", valueToURLParams(account, []string{"id", "name", "email", "tenant", "max-users", "max-roles", "max-groups", "max-access-keys", "max-buckets"})) |
| 89 | + if err != nil { |
| 90 | + return Account{}, err |
| 91 | + } |
| 92 | + |
| 93 | + a := Account{} |
| 94 | + err = json.Unmarshal(body, &a) |
| 95 | + if err != nil { |
| 96 | + return Account{}, fmt.Errorf("%s. %s. %w", unmarshalError, string(body), err) |
| 97 | + } |
| 98 | + |
| 99 | + return a, nil |
| 100 | +} |
0 commit comments