Files
my-rook-config/pkg/rook/client/client.go
T

175 lines
4.7 KiB
Go
Raw Normal View History

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.
*/
package client
import (
2016-11-28 17:38:06 -08:00
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
2016-10-26 00:38:39 +02:00
"github.com/rook/rook/pkg/model"
)
2016-11-28 17:38:06 -08:00
const (
clientQueryName = "client"
successStatuses
2016-11-28 17:38:06 -08:00
)
2016-10-27 13:26:25 +02:00
type RookRestClient interface {
URL() string
GetNodes() ([]model.Node, error)
GetPools() ([]model.Pool, error)
CreatePool(pool model.Pool) (string, error)
DeletePool(name string) error
GetBlockImages() ([]model.BlockImage, error)
CreateBlockImage(image model.BlockImage) (string, error)
DeleteBlockImage(image model.BlockImage) (string, error)
2016-11-28 17:38:06 -08:00
GetClientAccessInfo() (model.ClientAccessInfo, error)
GetFilesystems() ([]model.Filesystem, error)
CreateFilesystem(model.FilesystemRequest) (string, error)
DeleteFilesystem(model.FilesystemRequest) (string, error)
GetStatusDetails() (model.StatusDetails, error)
2017-09-11 14:58:13 -07:00
GetObjectStores() ([]model.ObjectStoreResponse, error)
CreateObjectStore(store model.ObjectStore) (string, error)
DeleteObjectStore(storeName string) error
GetObjectStoreConnectionInfo(storeName string) (*model.ObjectStoreConnectInfo, error)
ListBuckets(storeName string) ([]model.ObjectBucket, error)
GetBucket(storeName, bucketName string) (*model.ObjectBucket, error)
DeleteBucket(storeName, bucketName string, purge bool) error
ListObjectUsers(storeName string) ([]model.ObjectUser, error)
GetObjectUser(storeName, id string) (*model.ObjectUser, error)
CreateObjectUser(storeName string, user model.ObjectUser) (*model.ObjectUser, error)
UpdateObjectUser(storeName string, user model.ObjectUser) (*model.ObjectUser, error)
DeleteObjectUser(storeName, id string) error
}
2016-10-27 13:26:25 +02:00
type RookNetworkRestClient struct {
RestURL string
HttpClient *http.Client
}
2016-10-27 13:26:25 +02:00
func NewRookNetworkRestClient(url string, httpClient *http.Client) *RookNetworkRestClient {
return &RookNetworkRestClient{
RestURL: url,
HttpClient: httpClient,
}
}
func GetRestURL(endPoint string) string {
return fmt.Sprintf("http://%s", endPoint)
}
2016-10-27 13:26:25 +02:00
type RookRestError struct {
Query string
Status int
Body []byte
}
2016-10-27 13:26:25 +02:00
func (e RookRestError) Error() string {
return fmt.Sprintf("HTTP status code %d for query %s: '%s'", e.Status, e.Query, string(e.Body))
}
2016-11-28 17:38:06 -08:00
func IsHttpAccepted(err error) bool {
return IsHttpStatusCode(err, http.StatusAccepted)
}
func IsHttpNotFound(err error) bool {
return IsHttpStatusCode(err, http.StatusNotFound)
}
func IsHttpStatusCode(err error, statusCode int) bool {
2016-11-28 17:38:06 -08:00
if err == nil {
return false
}
rrErr, ok := err.(RookRestError)
return ok && rrErr.Status == statusCode
2016-11-28 17:38:06 -08:00
}
2016-10-27 13:26:25 +02:00
func (a *RookNetworkRestClient) URL() string {
return a.RestURL
}
2016-10-27 13:26:25 +02:00
func (a *RookNetworkRestClient) DoGet(query string) ([]byte, error) {
return a.Do("GET", query, nil)
}
func (a *RookNetworkRestClient) DoDelete(query string) ([]byte, error) {
return a.Do("DELETE", query, nil)
}
2016-10-27 13:26:25 +02:00
func (a *RookNetworkRestClient) DoPost(query string, body io.Reader) ([]byte, error) {
return a.Do("POST", query, body)
}
func (a *RookNetworkRestClient) DoPut(query string, body io.Reader) ([]byte, error) {
return a.Do("PUT", query, body)
}
2016-10-27 13:26:25 +02:00
func (a *RookNetworkRestClient) Do(method, query string, body io.Reader) ([]byte, error) {
request, err := http.NewRequest(method, fmt.Sprintf("%s/%s", a.RestURL, query), body)
if err != nil {
return nil, err
}
request.Header.Add("Accept", "application/json; charset=UTF-8")
if body != nil {
request.Header.Add("Content-type", "application/octet-stream")
}
response, err := a.HttpClient.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
respBody, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
code := response.StatusCode
if code != http.StatusOK {
// non 200 OK response, return an error with the details
2016-10-27 13:26:25 +02:00
RookRestError := RookRestError{
Query: query,
Status: code,
Body: respBody,
}
return respBody, RookRestError
}
return respBody, nil
}
2016-11-28 17:38:06 -08:00
func (c *RookNetworkRestClient) GetClientAccessInfo() (model.ClientAccessInfo, error) {
body, err := c.DoGet(clientQueryName)
if err != nil {
return model.ClientAccessInfo{}, err
}
var clientAccessInfo model.ClientAccessInfo
err = json.Unmarshal(body, &clientAccessInfo)
if err != nil {
return model.ClientAccessInfo{}, err
}
return clientAccessInfo, nil
}