/
resources.go
153 lines (135 loc) · 3.65 KB
/
resources.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
package main
import (
"net/http"
"net/url"
"github.com/gorilla/mux"
"github.com/lxc/lxd/lxd/resources"
"github.com/lxc/lxd/lxd/response"
storagePools "github.com/lxc/lxd/lxd/storage"
"github.com/lxc/lxd/shared/api"
)
var api10ResourcesCmd = APIEndpoint{
Path: "resources",
Get: APIEndpointAction{Handler: api10ResourcesGet, AccessHandler: allowAuthenticated},
}
var storagePoolResourcesCmd = APIEndpoint{
Path: "storage-pools/{name}/resources",
Get: APIEndpointAction{Handler: storagePoolResourcesGet, AccessHandler: allowAuthenticated},
}
// swagger:operation GET /1.0/resources server resources_get
//
// Get system resources information
//
// Gets the hardware information profile of the LXD server.
//
// ---
// produces:
// - application/json
// parameters:
// - in: query
// name: target
// description: Cluster member name
// type: string
// example: lxd01
// responses:
// "200":
// description: Hardware resources
// 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/Resources"
// "403":
// $ref: "#/responses/Forbidden"
// "500":
// $ref: "#/responses/InternalServerError"
func api10ResourcesGet(d *Daemon, r *http.Request) response.Response {
s := d.State()
// If a target was specified, forward the request to the relevant node.
resp := forwardedResponseIfTargetIsRemote(s, r)
if resp != nil {
return resp
}
// Get the local resource usage
res, err := resources.GetResources()
if err != nil {
return response.SmartError(err)
}
return response.SyncResponse(true, res)
}
// swagger:operation GET /1.0/storage-pools/{name}/resources storage storage_pool_resources
//
// Get storage pool resources information
//
// Gets the usage information for the storage pool.
//
// ---
// produces:
// - application/json
// parameters:
// - in: query
// name: target
// description: Cluster member name
// type: string
// example: lxd01
// responses:
// "200":
// description: Hardware resources
// 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/ResourcesStoragePool"
// "403":
// $ref: "#/responses/Forbidden"
// "500":
// $ref: "#/responses/InternalServerError"
func storagePoolResourcesGet(d *Daemon, r *http.Request) response.Response {
s := d.State()
// If a target was specified, forward the request to the relevant node.
resp := forwardedResponseIfTargetIsRemote(s, r)
if resp != nil {
return resp
}
// Get the existing storage pool
poolName, err := url.PathUnescape(mux.Vars(r)["name"])
if err != nil {
return response.SmartError(err)
}
var res *api.ResourcesStoragePool
pool, err := storagePools.LoadByName(s, poolName)
if err != nil {
return response.InternalError(err)
}
res, err = pool.GetResources()
if err != nil {
return response.InternalError(err)
}
return response.SyncResponse(true, res)
}