github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/client/instances.go (about) 1 package client 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "github.com/emc-advanced-dev/pkg/errors" 7 "github.com/solo-io/unik/pkg/daemon" 8 "github.com/solo-io/unik/pkg/types" 9 "github.com/layer-x/layerx-commons/lxhttpclient" 10 "io" 11 "net/http" 12 ) 13 14 type instances struct { 15 unikIP string 16 } 17 18 func (i *instances) All() ([]*types.Instance, error) { 19 resp, body, err := lxhttpclient.Get(i.unikIP, "/instances", nil) 20 if err != nil { 21 return nil, errors.New("request failed", err) 22 } 23 if resp.StatusCode != http.StatusOK { 24 return nil, errors.New(fmt.Sprintf("failed with status %v: %s", resp.StatusCode, string(body)), nil) 25 } 26 var instances []*types.Instance 27 if err := json.Unmarshal(body, &instances); err != nil { 28 return nil, errors.New(fmt.Sprintf("response body %s did not unmarshal to type []*types.Instance", string(body)), err) 29 } 30 return instances, nil 31 } 32 33 func (i *instances) Get(id string) (*types.Instance, error) { 34 resp, body, err := lxhttpclient.Get(i.unikIP, "/instances/"+id, nil) 35 if err != nil { 36 return nil, errors.New("request failed", err) 37 } 38 if resp.StatusCode != http.StatusOK { 39 return nil, errors.New(fmt.Sprintf("failed with status %v: %s", resp.StatusCode, string(body)), nil) 40 } 41 var instance types.Instance 42 if err := json.Unmarshal(body, &instance); err != nil { 43 return nil, errors.New(fmt.Sprintf("response body %s did not unmarshal to type *types.Instance", string(body)), err) 44 } 45 return &instance, nil 46 } 47 48 func (i *instances) Delete(id string, force bool) error { 49 query := buildQuery(map[string]interface{}{ 50 "force": force, 51 }) 52 resp, body, err := lxhttpclient.Delete(i.unikIP, "/instances/"+id+query, nil) 53 if err != nil { 54 return errors.New("request failed", err) 55 } 56 if resp.StatusCode != http.StatusNoContent { 57 return errors.New(fmt.Sprintf("failed with status %v: %s", resp.StatusCode, string(body)), err) 58 } 59 return nil 60 } 61 62 func (i *instances) GetLogs(id string) (string, error) { 63 resp, body, err := lxhttpclient.Get(i.unikIP, "/instances/"+id+"/logs", nil) 64 if err != nil { 65 return "", errors.New("request failed", err) 66 } 67 if resp.StatusCode != http.StatusOK { 68 return "", errors.New(fmt.Sprintf("failed with status %v: %s", resp.StatusCode, string(body)), err) 69 } 70 return string(body), nil 71 } 72 73 func (i *instances) AttachLogs(id string, deleteOnDisconnect bool) (io.ReadCloser, error) { 74 query := buildQuery(map[string]interface{}{ 75 "follow": true, 76 "delete": deleteOnDisconnect, 77 }) 78 resp, err := lxhttpclient.GetAsync(i.unikIP, "/instances/"+id+"/logs"+query, nil) 79 if err != nil { 80 return nil, errors.New("request failed", err) 81 } 82 if resp.StatusCode != http.StatusOK { 83 return nil, errors.New(fmt.Sprintf("failed with status %v", resp.StatusCode), err) 84 } 85 return resp.Body, nil 86 } 87 88 func (i *instances) Run(instanceName, imageName string, mountPointsToVols, env map[string]string, memoryMb int, noCleanup, debugMode bool) (*types.Instance, error) { 89 runInstanceRequest := daemon.RunInstanceRequest{ 90 InstanceName: instanceName, 91 ImageName: imageName, 92 Mounts: mountPointsToVols, 93 Env: env, 94 MemoryMb: memoryMb, 95 NoCleanup: noCleanup, 96 DebugMode: debugMode, 97 } 98 resp, body, err := lxhttpclient.Post(i.unikIP, "/instances/run", nil, runInstanceRequest) 99 if err != nil { 100 return nil, errors.New("request failed", err) 101 } 102 if resp.StatusCode != http.StatusCreated { 103 return nil, errors.New(fmt.Sprintf("failed with status %v: %s", resp.StatusCode, string(body)), err) 104 } 105 var instance types.Instance 106 if err := json.Unmarshal(body, &instance); err != nil { 107 return nil, errors.New(fmt.Sprintf("response body %s did not unmarshal to type *types.Instance", string(body)), err) 108 } 109 return &instance, nil 110 } 111 112 func (i *instances) Start(id string) error { 113 resp, body, err := lxhttpclient.Post(i.unikIP, "/instances/"+id+"/start", nil, nil) 114 if err != nil { 115 return errors.New("request failed", err) 116 } 117 if resp.StatusCode != http.StatusOK { 118 return errors.New(fmt.Sprintf("failed with status %v: %s", resp.StatusCode, string(body)), err) 119 } 120 return nil 121 } 122 123 func (i *instances) Stop(id string) error { 124 resp, body, err := lxhttpclient.Post(i.unikIP, "/instances/"+id+"/stop", nil, nil) 125 if err != nil { 126 return errors.New("request failed", err) 127 } 128 if resp.StatusCode != http.StatusOK { 129 return errors.New(fmt.Sprintf("failed with status %v: %s", resp.StatusCode, string(body)), err) 130 } 131 return nil 132 }