github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/clients/rancher/client.go (about)

     1  package rancher
     2  
     3  import (
     4  	"github.com/johnnyeven/libtools/courier/client"
     5  	"fmt"
     6  	"github.com/johnnyeven/libtools/courier/status_error"
     7  	"github.com/johnnyeven/libtools/courier"
     8  	"github.com/johnnyeven/libtools/courier/httpx"
     9  )
    10  
    11  type ClientRancher struct {
    12  	AccessKey    string `conf:"env"`
    13  	AccessSecret string `conf:"env"`
    14  	client.Client
    15  }
    16  
    17  func (ClientRancher) MarshalDefaults(v interface{}) {
    18  	if cl, ok := v.(*ClientRancher); ok {
    19  		cl.Name = "rancher"
    20  		cl.Client.MarshalDefaults(&cl.Client)
    21  	}
    22  }
    23  
    24  func (c ClientRancher) Init() {
    25  	c.CheckService()
    26  }
    27  
    28  func (c ClientRancher) CheckService() {
    29  	err := c.Request(c.Name+".Check", "HEAD", "/", nil).
    30  		Do().
    31  		Into(nil)
    32  	statusErr := status_error.FromError(err)
    33  	if statusErr.Code == int64(status_error.RequestTimeout) {
    34  		panic(fmt.Errorf("service %s have some error %s", c.Name, statusErr))
    35  	}
    36  }
    37  
    38  func (c ClientRancher) GetServices(stackId string) (resp *GetServicesResponse, err error) {
    39  	resp = &GetServicesResponse{}
    40  	resp.Meta = courier.Metadata{}
    41  
    42  	metas := courier.Metadata{}
    43  	metas.Set(httpx.HeaderAuthorization, c.AccessKey, c.AccessSecret)
    44  
    45  	err = c.Request(c.Name+".GetServices", "GET", fmt.Sprintf("/v2-beta/stacks/%s/services", stackId), nil, metas).
    46  		Do().
    47  		BindMeta(resp.Meta).
    48  		Into(&resp.Body)
    49  
    50  	return
    51  }
    52  
    53  type GetServicesResponse struct {
    54  	Meta courier.Metadata
    55  	Body Services
    56  }