package csi import ( "bytes" "encoding/json" "fmt" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "io" "k8s.io/klog" "net/http" ) type p5xApi struct { endpoint string port int64 token string } type p5xVolume struct { VolumeId int64 `json:"volumeId"` Name string `json:"name"` SizeInBytes int64 `json:"sizeInBytes"` } func (p5x *p5xApi) CreateVolume(name string, sizeInBytes int64) (*p5xVolume, error) { vol := &p5xVolume{ VolumeId: 0, Name: name, SizeInBytes: sizeInBytes, } body, err := json.Marshal(vol) if err != nil { return nil, err } resBody, err := p5x.MakeRequest(http.MethodPost, "volumes", body) if err != nil { return nil, err } err = json.Unmarshal(resBody, vol) klog.Info("Successfully created volume: ", vol) return vol, nil } func (p5x *p5xApi) GetVolumeByName(name string) (*p5xVolume, error) { return nil, status.Error(codes.Unimplemented, "") } func (p5x *p5xApi) GetVolumeById(volumeId int64) (*p5xVolume, error) { return nil, status.Error(codes.Unimplemented, "") } func (p5x *p5xApi) DeleteVolume(volume *p5xVolume) error { 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) 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 } 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 } resBody, err := io.ReadAll(res.Body) if err != nil { klog.Errorf("p5x.MakeRequest: could not read response body: %s\n", err) return nil, err } klog.Infof("p5x.MakeRequest: response body: %s\n", resBody) return resBody, nil }