github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/client/volumes.go (about)

     1  package client
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/http"
     7  
     8  	"github.com/emc-advanced-dev/pkg/errors"
     9  	"github.com/solo-io/unik/pkg/types"
    10  	"github.com/layer-x/layerx-commons/lxhttpclient"
    11  )
    12  
    13  type volumes struct {
    14  	unikIP string
    15  }
    16  
    17  func (v *volumes) All() ([]*types.Volume, error) {
    18  	resp, body, err := lxhttpclient.Get(v.unikIP, "/volumes", nil)
    19  	if err != nil {
    20  		return nil, errors.New("request failed", err)
    21  	}
    22  	if resp.StatusCode != http.StatusOK {
    23  		return nil, errors.New(fmt.Sprintf("failed with status %v: %s", resp.StatusCode, string(body)), nil)
    24  	}
    25  	var volumes []*types.Volume
    26  	if err := json.Unmarshal(body, &volumes); err != nil {
    27  		return nil, errors.New(fmt.Sprintf("response body %s did not unmarshal to type []*types.Volume", string(body)), err)
    28  	}
    29  	return volumes, nil
    30  }
    31  
    32  func (v *volumes) Get(id string) (*types.Volume, error) {
    33  	resp, body, err := lxhttpclient.Get(v.unikIP, "/volumes/"+id, nil)
    34  	if err != nil {
    35  		return nil, errors.New("request failed", err)
    36  	}
    37  	if resp.StatusCode != http.StatusOK {
    38  		return nil, errors.New(fmt.Sprintf("failed with status %v: %s", resp.StatusCode, string(body)), nil)
    39  	}
    40  	var volume types.Volume
    41  	if err := json.Unmarshal(body, &volume); err != nil {
    42  		return nil, errors.New(fmt.Sprintf("response body %s did not unmarshal to type *types.Volume", string(body)), err)
    43  	}
    44  	return &volume, nil
    45  }
    46  
    47  func (v *volumes) Delete(id string, force bool) error {
    48  	query := buildQuery(map[string]interface{}{
    49  		"force": force,
    50  	})
    51  	resp, body, err := lxhttpclient.Delete(v.unikIP, "/volumes/"+id+query, nil)
    52  	if err != nil {
    53  		return errors.New("request failed", err)
    54  	}
    55  	if resp.StatusCode != http.StatusNoContent {
    56  		return errors.New(fmt.Sprintf("failed with status %v: %s", resp.StatusCode, string(body)), err)
    57  	}
    58  	return nil
    59  }
    60  
    61  func (v *volumes) Create(name, dataTar, provider string, raw bool, size int, volType string, noCleanup bool) (*types.Volume, error) {
    62  	query := buildQuery(map[string]interface{}{
    63  		"size":       size,
    64  		"provider":   provider,
    65  		"type":       volType,
    66  		"no_cleanup": noCleanup,
    67  		"raw":        raw,
    68  	})
    69  	//no data provided
    70  	var (
    71  		resp *http.Response
    72  		body []byte
    73  		err  error
    74  	)
    75  	if dataTar == "" {
    76  		resp, body, err = lxhttpclient.Post(v.unikIP, "/volumes/"+name+query, nil, nil)
    77  		if err != nil {
    78  			return nil, errors.New("request failed", err)
    79  		}
    80  		if resp.StatusCode != http.StatusCreated {
    81  			return nil, errors.New(fmt.Sprintf("failed with status %v: %s", resp.StatusCode, string(body)), err)
    82  		}
    83  	} else {
    84  		resp, body, err = lxhttpclient.PostFile(v.unikIP, "/volumes/"+name+query, "tarfile", dataTar)
    85  		if err != nil {
    86  			return nil, errors.New("request failed", err)
    87  		}
    88  		if resp.StatusCode != http.StatusCreated {
    89  			return nil, errors.New(fmt.Sprintf("failed with status %v: %s", resp.StatusCode, string(body)), err)
    90  		}
    91  	}
    92  	var volume types.Volume
    93  	if err := json.Unmarshal(body, &volume); err != nil {
    94  		return nil, errors.New(fmt.Sprintf("response body %s did not unmarshal to type *types.Volume", string(body)), err)
    95  	}
    96  	return &volume, nil
    97  }
    98  
    99  func (v *volumes) Attach(id, instanceId, mountPoint string) error {
   100  	query := buildQuery(map[string]interface{}{
   101  		"mount": mountPoint,
   102  	})
   103  	resp, body, err := lxhttpclient.Post(v.unikIP, "/volumes/"+id+"/attach/"+instanceId+query, nil, nil)
   104  	if err != nil {
   105  		return errors.New("request failed", err)
   106  	}
   107  	if resp.StatusCode != http.StatusAccepted {
   108  		return errors.New(fmt.Sprintf("failed with status %v: %s", resp.StatusCode, string(body)), err)
   109  	}
   110  	return nil
   111  }
   112  
   113  func (v *volumes) Detach(id string) error {
   114  	resp, body, err := lxhttpclient.Post(v.unikIP, "/volumes/"+id+"/detach", nil, nil)
   115  	if err != nil {
   116  		return errors.New("request failed", err)
   117  	}
   118  	if resp.StatusCode != http.StatusAccepted {
   119  		return errors.New(fmt.Sprintf("failed with status %v: %s", resp.StatusCode, string(body)), err)
   120  	}
   121  	return nil
   122  }