[WIP] More work implementing create/delete volume and publish/unpublish volume
This commit is contained in:
@@ -4,8 +4,6 @@ import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"io"
|
||||
"k8s.io/klog"
|
||||
"net/http"
|
||||
@@ -29,6 +27,7 @@ func (p5x *p5xApi) CreateVolume(name string, sizeInBytes int64) (*p5xVolume, err
|
||||
Name: name,
|
||||
SizeInBytes: sizeInBytes,
|
||||
}
|
||||
klog.Info("p5x.CreateVolume: Attempting to create: ", vol)
|
||||
|
||||
body, err := json.Marshal(vol)
|
||||
if err != nil {
|
||||
@@ -41,47 +40,67 @@ func (p5x *p5xApi) CreateVolume(name string, sizeInBytes int64) (*p5xVolume, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(resBody, vol)
|
||||
klog.Info("Successfully created volume: ", vol)
|
||||
klog.Info("p5x.CreateVolume: Successfully created volume: ", vol)
|
||||
|
||||
return vol, nil
|
||||
}
|
||||
|
||||
func (p5x *p5xApi) GetVolumeByName(name string) (*p5xVolume, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "")
|
||||
}
|
||||
klog.Infof("p5x.GetVolumeByName: %s", name)
|
||||
route := fmt.Sprintf("volumes/%s", name)
|
||||
resBody, err := p5x.MakeRequest(http.MethodGet, route, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func (p5x *p5xApi) GetVolumeById(volumeId int64) (*p5xVolume, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "")
|
||||
vol := &p5xVolume{}
|
||||
err = json.Unmarshal(resBody, vol)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
klog.Info("p5x.GetVolumeByName: Retrieved volume: ", vol)
|
||||
return vol, nil
|
||||
}
|
||||
|
||||
func (p5x *p5xApi) DeleteVolume(volume *p5xVolume) error {
|
||||
klog.Info("p5x.DeleteVolume: ", volume)
|
||||
route := fmt.Sprintf("volumes/%s", volume.Name)
|
||||
resBody, err := p5x.MakeRequest(http.MethodDelete, route, []byte(`{}`))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
klog.Infof("Successfully deleted volume %s: %s", volume.Name, resBody)
|
||||
klog.Infof("p5x.DeleteVolume: Successfully deleted volume %s: %s", volume.Name, resBody)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p5x *p5xApi) MakeRequest(method string, route string, body []byte) ([]byte, error) {
|
||||
bodyReader := bytes.NewReader(body)
|
||||
|
||||
url := fmt.Sprintf("%s:%d/api/v1/%s", p5x.endpoint, p5x.port, route)
|
||||
klog.Infof("p5x.MakeRequest: [%s] %s", method, url)
|
||||
klog.Infof("p5x.MakeRequest: %s", body)
|
||||
req, err := http.NewRequest(method, url, bodyReader)
|
||||
if err != nil {
|
||||
klog.Errorf("p5x.MakeRequest: could not create request: %s\n", err)
|
||||
return nil, err
|
||||
|
||||
var res *http.Response
|
||||
var err error
|
||||
if method == http.MethodGet {
|
||||
res, err = http.Get(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
klog.Infof("p5x.MakeRequest: body: %s", body)
|
||||
bodyReader := bytes.NewReader(body)
|
||||
req, err2 := http.NewRequest(method, url, bodyReader)
|
||||
if err2 != nil {
|
||||
klog.Errorf("p5x.MakeRequest: could not create request: %s\n", err)
|
||||
return nil, err2
|
||||
}
|
||||
|
||||
req.Header.Set("Accept", "application/json")
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", p5x.token))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
res, err = http.DefaultClient.Do(req)
|
||||
}
|
||||
|
||||
req.Header.Set("Accept", "application/json")
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", p5x.token))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
klog.Errorf("p5x.MakeRequest: error executing request: %s\n", err)
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user