2016-11-05 17:09:05 -07:00
|
|
|
/*
|
|
|
|
|
Copyright 2016 The Rook Authors. All rights reserved.
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
2016-10-11 21:05:33 -07:00
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
2017-06-14 16:07:36 -07:00
|
|
|
"fmt"
|
2016-10-11 21:05:33 -07:00
|
|
|
"net/http"
|
|
|
|
|
|
2017-12-04 08:33:19 -08:00
|
|
|
ceph "github.com/rook/rook/pkg/daemon/ceph/client"
|
2016-10-26 00:38:39 +02:00
|
|
|
"github.com/rook/rook/pkg/model"
|
2016-10-11 21:05:33 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Gets the images that have been created in this cluster.
|
|
|
|
|
// GET
|
|
|
|
|
// /image
|
|
|
|
|
func (h *Handler) GetImages(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
// first list all the pools so that we can retrieve images from all pools
|
2017-10-05 10:00:19 -07:00
|
|
|
pools, err := ceph.ListPoolSummaries(h.context, h.config.clusterInfo.Name)
|
2016-10-11 21:05:33 -07:00
|
|
|
if err != nil {
|
2016-11-11 17:37:08 -08:00
|
|
|
logger.Errorf("failed to list pools: %+v", err)
|
2016-10-11 21:05:33 -07:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result := []model.BlockImage{}
|
|
|
|
|
|
2017-06-14 16:07:36 -07:00
|
|
|
// for each pool, get further details about all the images in the pool
|
2016-10-11 21:05:33 -07:00
|
|
|
for _, p := range pools {
|
2017-06-14 16:07:36 -07:00
|
|
|
images, ok := h.getImagesForPool(w, p.Name)
|
2017-03-20 21:38:03 -07:00
|
|
|
if !ok {
|
2016-10-11 21:05:33 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result = append(result, images...)
|
|
|
|
|
}
|
2017-06-14 16:07:36 -07:00
|
|
|
|
|
|
|
|
FormatJsonResponse(w, result)
|
2016-10-11 21:05:33 -07:00
|
|
|
}
|
|
|
|
|
|
2017-06-14 16:07:36 -07:00
|
|
|
func (h *Handler) getImagesForPool(w http.ResponseWriter, poolName string) ([]model.BlockImage, bool) {
|
2017-10-05 10:00:19 -07:00
|
|
|
cephImages, err := ceph.ListImages(h.context, h.config.clusterInfo.Name, poolName)
|
2017-03-20 21:38:03 -07:00
|
|
|
if err != nil {
|
2017-06-14 16:07:36 -07:00
|
|
|
logger.Errorf("failed to get images from pool %s: %+v", poolName, err)
|
2017-03-20 21:38:03 -07:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
return nil, false
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-14 16:07:36 -07:00
|
|
|
images := make([]model.BlockImage, len(cephImages))
|
|
|
|
|
for i, image := range cephImages {
|
2017-03-20 21:38:03 -07:00
|
|
|
// add the current image's details to the result set
|
|
|
|
|
images[i] = model.BlockImage{
|
2017-06-14 16:07:36 -07:00
|
|
|
Name: image.Name,
|
2017-03-20 21:38:03 -07:00
|
|
|
PoolName: poolName,
|
2017-06-14 16:07:36 -07:00
|
|
|
Size: image.Size,
|
2017-03-20 21:38:03 -07:00
|
|
|
}
|
|
|
|
|
}
|
2017-06-14 16:07:36 -07:00
|
|
|
|
|
|
|
|
return images, true
|
2017-03-20 21:38:03 -07:00
|
|
|
}
|
|
|
|
|
|
2016-10-11 21:05:33 -07:00
|
|
|
// Creates a new image in this cluster.
|
|
|
|
|
// POST
|
|
|
|
|
// /image
|
|
|
|
|
func (h *Handler) CreateImage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var newImage model.BlockImage
|
|
|
|
|
body, ok := handleReadBody(w, r, "create image")
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := json.Unmarshal(body, &newImage); err != nil {
|
2016-11-11 17:37:08 -08:00
|
|
|
logger.Errorf("failed to unmarshal create image request body '%s': %+v", string(body), err)
|
2016-10-11 21:05:33 -07:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if newImage.Name == "" || newImage.PoolName == "" || newImage.Size == 0 {
|
2016-11-11 17:37:08 -08:00
|
|
|
logger.Errorf("image missing required fields: %+v", newImage)
|
2016-10-11 21:05:33 -07:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-05 10:00:19 -07:00
|
|
|
createdImage, err := ceph.CreateImage(h.context, h.config.clusterInfo.Name, newImage.Name,
|
2017-06-14 16:07:36 -07:00
|
|
|
newImage.PoolName, newImage.Size)
|
2016-10-11 21:05:33 -07:00
|
|
|
if err != nil {
|
2016-11-11 17:37:08 -08:00
|
|
|
logger.Errorf("failed to create image %+v: %+v", newImage, err)
|
2016-10-11 21:05:33 -07:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-14 16:07:36 -07:00
|
|
|
w.Write([]byte(fmt.Sprintf("succeeded created image %s", createdImage.Name)))
|
2016-10-11 21:05:33 -07:00
|
|
|
}
|
2017-03-20 21:38:03 -07:00
|
|
|
|
|
|
|
|
// Deletes a block image from this cluster.
|
2017-03-21 21:17:18 -07:00
|
|
|
// DELETE
|
|
|
|
|
// /image?name=<imageName>&pool=<imagePool>
|
2017-03-20 21:38:03 -07:00
|
|
|
func (h *Handler) DeleteImage(w http.ResponseWriter, r *http.Request) {
|
2017-03-21 21:17:18 -07:00
|
|
|
imageName := r.URL.Query().Get("name")
|
|
|
|
|
imagePool := r.URL.Query().Get("pool")
|
2017-03-20 21:38:03 -07:00
|
|
|
|
2017-03-21 21:17:18 -07:00
|
|
|
deleteImageReq := model.BlockImage{
|
|
|
|
|
Name: imageName,
|
|
|
|
|
PoolName: imagePool,
|
2017-03-20 21:38:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if deleteImageReq.Name == "" || deleteImageReq.PoolName == "" {
|
|
|
|
|
logger.Errorf("image missing required fields: %+v", deleteImageReq)
|
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-05 10:00:19 -07:00
|
|
|
err := ceph.DeleteImage(h.context, h.config.clusterInfo.Name, deleteImageReq.Name, deleteImageReq.PoolName)
|
2017-03-20 21:38:03 -07:00
|
|
|
if err != nil {
|
|
|
|
|
logger.Errorf("failed to delete image %+v: %+v", deleteImageReq, err)
|
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-14 16:07:36 -07:00
|
|
|
w.Write([]byte(fmt.Sprintf("succeeded deleting image %s", deleteImageReq.Name)))
|
2017-03-20 21:38:03 -07:00
|
|
|
}
|