-
Notifications
You must be signed in to change notification settings - Fork 907
/
storage_volumes_state.go
164 lines (143 loc) · 4.54 KB
/
storage_volumes_state.go
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
package main
import (
"fmt"
"net/http"
"net/url"
"github.com/gorilla/mux"
"github.com/canonical/lxd/lxd/auth"
"github.com/canonical/lxd/lxd/db/cluster"
"github.com/canonical/lxd/lxd/instance"
"github.com/canonical/lxd/lxd/instance/instancetype"
"github.com/canonical/lxd/lxd/project"
"github.com/canonical/lxd/lxd/request"
"github.com/canonical/lxd/lxd/response"
storagePools "github.com/canonical/lxd/lxd/storage"
"github.com/canonical/lxd/shared"
"github.com/canonical/lxd/shared/api"
"github.com/canonical/lxd/shared/entity"
)
var storagePoolVolumeTypeStateCmd = APIEndpoint{
Path: "storage-pools/{poolName}/volumes/{type}/{volumeName}/state",
Get: APIEndpointAction{Handler: storagePoolVolumeTypeStateGet, AccessHandler: allowPermission(entity.TypeStorageVolume, auth.EntitlementCanView, "poolName", "type", "volumeName")},
}
// swagger:operation GET /1.0/storage-pools/{poolName}/volumes/{type}/{volumeName}/state storage storage_pool_volume_type_state_get
//
// Get the storage volume state
//
// Gets a specific storage volume state (usage data).
//
// ---
// produces:
// - application/json
// parameters:
// - in: query
// name: project
// description: Project name
// type: string
// example: default
// - in: query
// name: target
// description: Cluster member name
// type: string
// example: lxd01
// responses:
// "200":
// description: Storage pool
// schema:
// type: object
// description: Sync response
// properties:
// type:
// type: string
// description: Response type
// example: sync
// status:
// type: string
// description: Status description
// example: Success
// status_code:
// type: integer
// description: Status code
// example: 200
// metadata:
// $ref: "#/definitions/StorageVolumeState"
// "403":
// $ref: "#/responses/Forbidden"
// "500":
// $ref: "#/responses/InternalServerError"
func storagePoolVolumeTypeStateGet(d *Daemon, r *http.Request) response.Response {
s := d.State()
// Get the name of the pool the storage volume is supposed to be attached to.
poolName, err := url.PathUnescape(mux.Vars(r)["poolName"])
if err != nil {
return response.SmartError(err)
}
// Get the name of the volume type.
volumeTypeName, err := url.PathUnescape(mux.Vars(r)["type"])
if err != nil {
return response.SmartError(err)
}
// Get the name of the volume type.
volumeName, err := url.PathUnescape(mux.Vars(r)["volumeName"])
if err != nil {
return response.SmartError(err)
}
// Convert the volume type name to our internal integer representation.
volumeType, err := storagePools.VolumeTypeNameToDBType(volumeTypeName)
if err != nil {
return response.BadRequest(err)
}
// Check that the storage volume type is valid.
if !shared.ValueInSlice(volumeType, []int{cluster.StoragePoolVolumeTypeCustom, cluster.StoragePoolVolumeTypeContainer, cluster.StoragePoolVolumeTypeVM}) {
return response.BadRequest(fmt.Errorf("Invalid storage volume type %q", volumeTypeName))
}
// Get the storage project name.
requestProjectName := request.ProjectParam(r)
projectName, err := project.StorageVolumeProject(s.DB.Cluster, requestProjectName, volumeType)
if err != nil {
return response.SmartError(err)
}
// Load the storage pool.
pool, err := storagePools.LoadByName(s, poolName)
if err != nil {
return response.SmartError(err)
}
// Fetch the current usage.
var usage *storagePools.VolumeUsage
if volumeType == cluster.StoragePoolVolumeTypeCustom {
// Custom volumes.
usage, err = pool.GetCustomVolumeUsage(projectName, volumeName)
if err != nil {
return response.SmartError(err)
}
} else {
resp, err := forwardedResponseIfInstanceIsRemote(s, r, projectName, volumeName, instancetype.Any)
if err != nil {
return response.SmartError(err)
}
if resp != nil {
return resp
}
// Instance volumes.
inst, err := instance.LoadByProjectAndName(s, projectName, volumeName)
if err != nil {
return response.SmartError(err)
}
usage, err = pool.GetInstanceUsage(inst)
if err != nil {
return response.SmartError(err)
}
}
// Prepare the state struct.
state := api.StorageVolumeState{}
state.Usage = &api.StorageVolumeStateUsage{}
// Only fill 'used' field if receiving a valid value.
if usage.Used >= 0 {
state.Usage.Used = uint64(usage.Used)
}
// Only fill 'total' field if receiving a valid value.
if usage.Total >= 0 {
state.Usage.Total = usage.Total
}
return response.SyncResponse(true, state)
}