github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/client/images.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/config"
     8  	"github.com/solo-io/unik/pkg/types"
     9  	"github.com/layer-x/layerx-commons/lxhttpclient"
    10  	"net/http"
    11  	"strings"
    12  )
    13  
    14  type images struct {
    15  	unikIP string
    16  }
    17  
    18  func (i *images) All() ([]*types.Image, error) {
    19  	resp, body, err := lxhttpclient.Get(i.unikIP, "/images", 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 images []*types.Image
    27  	if err := json.Unmarshal(body, &images); err != nil {
    28  		return nil, errors.New(fmt.Sprintf("response body %s did not unmarshal to type []*types.Image", string(body)), err)
    29  	}
    30  	return images, nil
    31  }
    32  
    33  func (i *images) Get(id string) (*types.Image, error) {
    34  	resp, body, err := lxhttpclient.Get(i.unikIP, "/images/"+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 image types.Image
    42  	if err := json.Unmarshal(body, &image); err != nil {
    43  		return nil, errors.New(fmt.Sprintf("response body %s did not unmarshal to type *types.Image", string(body)), err)
    44  	}
    45  	return &image, nil
    46  }
    47  
    48  func (i *images) Build(name, sourceTar, base, lang, provider, args string, mounts []string, force, noCleanup bool) (*types.Image, error) {
    49  	query := buildQuery(map[string]interface{}{
    50  		"base":       base,
    51  		"lang":       lang,
    52  		"provider":   provider,
    53  		"args":       args,
    54  		"mounts":     strings.Join(mounts, ","),
    55  		"force":      force,
    56  		"no_cleanup": noCleanup,
    57  	})
    58  	resp, body, err := lxhttpclient.PostFile(i.unikIP, "/images/"+name+"/create"+query, "tarfile", sourceTar)
    59  	if err != nil {
    60  		return nil, errors.New("request failed", err)
    61  	}
    62  	if resp.StatusCode != http.StatusCreated {
    63  		return nil, errors.New(fmt.Sprintf("failed with status %v: %s", resp.StatusCode, string(body)), err)
    64  	}
    65  	var image types.Image
    66  	if err := json.Unmarshal(body, &image); err != nil {
    67  		return nil, errors.New(fmt.Sprintf("response body %s did not unmarshal to type *types.Image", string(body)), err)
    68  	}
    69  	return &image, nil
    70  }
    71  
    72  func (i *images) Delete(id string, force bool) error {
    73  	query := buildQuery(map[string]interface{}{
    74  		"force": force,
    75  	})
    76  	resp, body, err := lxhttpclient.Delete(i.unikIP, "/images/"+id+query, nil)
    77  	if err != nil {
    78  		return errors.New("request failed", err)
    79  	}
    80  	if resp.StatusCode != http.StatusNoContent {
    81  		return errors.New(fmt.Sprintf("failed with status %v: %s", resp.StatusCode, string(body)), err)
    82  	}
    83  	return nil
    84  }
    85  
    86  func (i *images) Push(c config.HubConfig, imageName string) error {
    87  	resp, body, err := lxhttpclient.Post(i.unikIP, "/images/push/"+imageName, nil, c)
    88  	if err != nil {
    89  		return errors.New("request failed", err)
    90  	}
    91  	if resp.StatusCode != http.StatusAccepted {
    92  		return errors.New(fmt.Sprintf("failed with status %v: %s", resp.StatusCode, string(body)), err)
    93  	}
    94  	return nil
    95  }
    96  
    97  func (i *images) Pull(c config.HubConfig, imageName, provider string, force bool) error {
    98  	query := buildQuery(map[string]interface{}{
    99  		"provider": provider,
   100  		"force":    force,
   101  	})
   102  	resp, body, err := lxhttpclient.Post(i.unikIP, "/images/pull/"+imageName+query, nil, c)
   103  	if err != nil {
   104  		return errors.New("request failed", err)
   105  	}
   106  	if resp.StatusCode != http.StatusAccepted {
   107  		return errors.New(fmt.Sprintf("failed with status %v: %s", resp.StatusCode, string(body)), err)
   108  	}
   109  	return nil
   110  }
   111  
   112  func (i *images) RemoteDelete(c config.HubConfig, imageName string) error {
   113  	resp, body, err := lxhttpclient.Post(i.unikIP, "/images/remote-delete/"+imageName, nil, c)
   114  	if err != nil {
   115  		return errors.New("request failed", err)
   116  	}
   117  	if resp.StatusCode != http.StatusAccepted {
   118  		return errors.New(fmt.Sprintf("failed with status %v: %s", resp.StatusCode, string(body)), err)
   119  	}
   120  	return nil
   121  }