Skip to content

Commit 2c92c1e

Browse files
Add support for resource locks (#852)
* Add support for resource locks * Patch unexpected API response header in the test fixture Actual fix from the API side is expected in the next few weeks * Update fixtures
1 parent ae3ce60 commit 2c92c1e

File tree

6 files changed

+1450
-229
lines changed

6 files changed

+1450
-229
lines changed

instances.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ type Instance struct {
8181

8282
// NOTE: MaintenancePolicy can only be used with v4beta.
8383
MaintenancePolicy string `json:"maintenance_policy"`
84+
85+
// NOTE: Locks can only be used with v4beta.
86+
Locks []LockType `json:"locks"`
8487
}
8588

8689
// InstanceSpec represents a linode spec

locks.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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

Comments
 (0)