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

     1  package client
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"github.com/emc-advanced-dev/pkg/errors"
     7  	"github.com/layer-x/layerx-commons/lxhttpclient"
     8  	"net/http"
     9  	"net/url"
    10  	"strings"
    11  )
    12  
    13  type client struct {
    14  	unikIP string
    15  }
    16  
    17  func UnikClient(unikIP string) *client {
    18  	return &client{unikIP: unikIP}
    19  }
    20  
    21  func (c *client) Images() *images {
    22  	return &images{unikIP: c.unikIP}
    23  }
    24  
    25  func (c *client) Instances() *instances {
    26  	return &instances{unikIP: c.unikIP}
    27  }
    28  
    29  func (c *client) Volumes() *volumes {
    30  	return &volumes{unikIP: c.unikIP}
    31  }
    32  
    33  func (c *client) AvailableCompilers() ([]string, error) {
    34  	resp, body, err := lxhttpclient.Get(c.unikIP, "/available_compilers", 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 compilers []string
    42  	if err := json.Unmarshal(body, &compilers); 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 compilers, nil
    46  }
    47  
    48  func (c *client) AvailableProviders() ([]string, error) {
    49  	resp, body, err := lxhttpclient.Get(c.unikIP, "/available_providers", nil)
    50  	if err != nil {
    51  		return nil, errors.New("request failed", err)
    52  	}
    53  	if resp.StatusCode != http.StatusOK {
    54  		return nil, errors.New(fmt.Sprintf("failed with status %v: %s", resp.StatusCode, string(body)), nil)
    55  	}
    56  	var compilers []string
    57  	if err := json.Unmarshal(body, &compilers); err != nil {
    58  		return nil, errors.New(fmt.Sprintf("response body %s did not unmarshal to type *types.Image", string(body)), err)
    59  	}
    60  	return compilers, nil
    61  }
    62  
    63  func (c *client) DescribeCompiler(base string, lang string, provider string) (string, error) {
    64  	query := buildQuery(map[string]interface{}{
    65  		"base":     base,
    66  		"lang":     lang,
    67  		"provider": provider,
    68  	})
    69  	resp, body, err := lxhttpclient.Get(c.unikIP, "/describe_compiler"+query, nil)
    70  	if err != nil {
    71  		return "", errors.New("request failed", err)
    72  	}
    73  	if resp.StatusCode != http.StatusOK {
    74  		return "", errors.New(fmt.Sprintf("failed with status %v: %s", resp.StatusCode, string(body)), err)
    75  	}
    76  	return string(body), nil
    77  }
    78  
    79  func buildQuery(params map[string]interface{}) string {
    80  	queryArray := []string{}
    81  	for key, val := range params {
    82  		queryArray = append(queryArray, url.QueryEscape(fmt.Sprintf("%s", key))+"="+url.QueryEscape(fmt.Sprintf("%v", val)))
    83  	}
    84  	queryString := "?" + strings.Join(queryArray, "&")
    85  	return queryString
    86  }