|
| 1 | +package linodego |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | +) |
| 6 | + |
| 7 | +// LockType represents the type of lock that can be applied to a resource |
| 8 | +// NOTE: Locks can only be used with v4beta. |
| 9 | +type LockType string |
| 10 | + |
| 11 | +// LockType enums |
| 12 | +const ( |
| 13 | + LockTypeCannotDelete LockType = "cannot_delete" |
| 14 | + LockTypeCannotDeleteWithSubresources LockType = "cannot_delete_with_subresources" |
| 15 | +) |
| 16 | + |
| 17 | +// LockedEntity represents the entity that is locked |
| 18 | +// NOTE: Locks can only be used with v4beta. |
| 19 | +type LockedEntity struct { |
| 20 | + ID int `json:"id"` |
| 21 | + Type EntityType `json:"type"` |
| 22 | + Label string `json:"label"` |
| 23 | + URL string `json:"url"` |
| 24 | +} |
| 25 | + |
| 26 | +// Lock represents a resource lock |
| 27 | +// NOTE: Locks can only be used with v4beta. |
| 28 | +type Lock struct { |
| 29 | + ID int `json:"id"` |
| 30 | + LockType LockType `json:"lock_type"` |
| 31 | + Entity LockedEntity `json:"entity"` |
| 32 | +} |
| 33 | + |
| 34 | +// LockCreateOptions fields are those accepted by CreateLock |
| 35 | +// NOTE: Locks can only be used with v4beta. |
| 36 | +type LockCreateOptions struct { |
| 37 | + EntityType EntityType `json:"entity_type"` |
| 38 | + EntityID int `json:"entity_id"` |
| 39 | + LockType LockType `json:"lock_type"` |
| 40 | +} |
| 41 | + |
| 42 | +// ListLocks returns a paginated list of Locks |
| 43 | +// NOTE: Locks can only be used with v4beta. |
| 44 | +func (c *Client) ListLocks(ctx context.Context, opts *ListOptions) ([]Lock, error) { |
| 45 | + return getPaginatedResults[Lock](ctx, c, "locks", opts) |
| 46 | +} |
| 47 | + |
| 48 | +// GetLock gets a single Lock with the provided ID |
| 49 | +// NOTE: Locks can only be used with v4beta. |
| 50 | +func (c *Client) GetLock(ctx context.Context, lockID int) (*Lock, error) { |
| 51 | + e := formatAPIPath("locks/%d", lockID) |
| 52 | + return doGETRequest[Lock](ctx, c, e) |
| 53 | +} |
| 54 | + |
| 55 | +// CreateLock creates a lock for a resource |
| 56 | +// NOTE: Locks can only be used with v4beta. |
| 57 | +func (c *Client) CreateLock(ctx context.Context, opts LockCreateOptions) (*Lock, error) { |
| 58 | + return doPOSTRequest[Lock](ctx, c, "locks", opts) |
| 59 | +} |
| 60 | + |
| 61 | +// DeleteLock deletes a single Lock with the provided ID |
| 62 | +// NOTE: Locks can only be used with v4beta. |
| 63 | +func (c *Client) DeleteLock(ctx context.Context, lockID int) error { |
| 64 | + e := formatAPIPath("locks/%d", lockID) |
| 65 | + return doDELETERequest(ctx, c, e) |
| 66 | +} |
0 commit comments