forked from digitalocean/godo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnfs_actions.go
More file actions
180 lines (150 loc) · 5.38 KB
/
Copy pathnfs_actions.go
File metadata and controls
180 lines (150 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package godo
import (
"context"
"fmt"
"net/http"
)
// NfsActionsService is an interface for interacting with the NFS actions
// endpoints of the DigitalOcean API
// See: https://docs.digitalocean.com/reference/api/api-reference/#tag/NFS-Actions
type NfsActionsService interface {
Resize(ctx context.Context, nfsShareId string, size uint64, region string) (*NfsAction, *Response, error)
Snapshot(ctx context.Context, nfsShareId string, nfsSnapshotName string, region string) (*NfsAction, *Response, error)
Attach(ctx context.Context, nfsShareId string, vpcID string, region string) (*NfsAction, *Response, error)
Detach(ctx context.Context, nfsShareId string, vpcID string, region string) (*NfsAction, *Response, error)
Reassign(ctx context.Context, nfsShareId, oldVpcID, newVpcID string) (*NfsAction, *Response, error)
SwitchPerformanceTier(ctx context.Context, nfsShareId string, tier string) (*NfsAction, *Response, error)
}
// NfsActionsServiceOp handles communication with the NFS action related
// methods of the DigitalOcean API.
type NfsActionsServiceOp struct {
client *Client
}
var _ NfsActionsService = &NfsActionsServiceOp{}
// NfsAction represents an NFS action
type NfsAction struct {
ID string `json:"id"`
Status string `json:"status"`
Type string `json:"type"`
StartedAt *Timestamp `json:"started_at"`
CompletedAt *Timestamp `json:"completed_at"`
ResourceID string `json:"resource_id"`
ResourceType string `json:"resource_type"`
Region *Region `json:"region,omitempty"`
RegionSlug string `json:"region_slug,omitempty"`
}
// nfsActionRoot represents the response wrapper for NFS actions
type nfsActionRoot struct {
Event *NfsAction `json:"action"`
}
// NfsActionRequest represents a generic NFS action request
type NfsActionRequest struct {
Type string `json:"type"`
Region string `json:"region"`
Params interface{} `json:"params"`
}
// NfsResizeParams represents parameters for resizing an NFS share
type NfsResizeParams struct {
SizeGib uint64 `json:"size_gib"`
}
// NfsSnapshotParams represents parameters for creating an NFS snapshot
type NfsSnapshotParams struct {
Name string `json:"name"`
}
// NfsAttachParams represents parameters for attaching an NFS share to a VPC
type NfsAttachParams struct {
VpcID string `json:"vpc_id"`
}
// NfsDetachParams represents parameters for detaching an NFS share from a VPC
type NfsDetachParams struct {
VpcID string `json:"vpc_id"`
}
// NfsReassignParams represents parameters for reassigning an NFS share from one VPC to another.
type NfsReassignParams struct {
OldVpcID string `json:"old_vpc_id"`
NewVpcID string `json:"new_vpc_id"`
}
// NfsSwitchPerformanceTierParams represents parameters to switch the performance tier of an NFS share.
type NfsSwitchPerformanceTierParams struct {
PerformanceTier string `json:"performance_tier"`
}
// Resize an NFS share
func (s *NfsActionsServiceOp) Resize(ctx context.Context, nfsShareId string, size uint64, region string) (*NfsAction, *Response, error) {
request := &NfsActionRequest{
Type: "resize",
Params: &NfsResizeParams{
SizeGib: size,
},
}
return s.doAction(ctx, nfsShareId, request)
}
// Snapshot an NFS share
func (s *NfsActionsServiceOp) Snapshot(ctx context.Context, nfsShareId, nfsSnapshotName, region string) (*NfsAction, *Response, error) {
request := &NfsActionRequest{
Type: "snapshot",
Params: &NfsSnapshotParams{
Name: nfsSnapshotName,
},
}
return s.doAction(ctx, nfsShareId, request)
}
// Attach an NFS share
func (s *NfsActionsServiceOp) Attach(ctx context.Context, nfsShareId, vpcID, region string) (*NfsAction, *Response, error) {
request := &NfsActionRequest{
Type: "attach",
Params: &NfsAttachParams{
VpcID: vpcID,
},
}
return s.doAction(ctx, nfsShareId, request)
}
// Detach an NFS share
func (s *NfsActionsServiceOp) Detach(ctx context.Context, nfsShareId, vpcID, region string) (*NfsAction, *Response, error) {
request := &NfsActionRequest{
Type: "detach",
Params: &NfsAttachParams{
VpcID: vpcID,
},
}
return s.doAction(ctx, nfsShareId, request)
}
// Reassign an NFS share from one VPC to another.
func (s *NfsActionsServiceOp) Reassign(ctx context.Context, nfsShareId, oldVpcID, newVpcID string) (*NfsAction, *Response, error) {
request := &NfsActionRequest{
Type: "reassign",
Params: &NfsReassignParams{
OldVpcID: oldVpcID,
NewVpcID: newVpcID,
},
}
return s.doAction(ctx, nfsShareId, request)
}
// Switch performance tier of an NFS share
func (s *NfsActionsServiceOp) SwitchPerformanceTier(ctx context.Context, nfsShareId string, tier string) (*NfsAction, *Response, error) {
request := &NfsActionRequest{
Type: "switch_performance_tier",
Params: &NfsSwitchPerformanceTierParams{
PerformanceTier: tier,
},
}
return s.doAction(ctx, nfsShareId, request)
}
func (s *NfsActionsServiceOp) doAction(ctx context.Context, nfsShareId string, request *NfsActionRequest) (*NfsAction, *Response, error) {
if request == nil {
return nil, nil, NewArgError("request", "request can't be nil")
}
path := nfsActionPath(nfsShareId)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, request)
if err != nil {
return nil, nil, err
}
root := new(nfsActionRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Event, resp, err
}
func nfsActionPath(nfsID string) string {
return fmt.Sprintf("v2/nfs/%v/actions", nfsID)
}