github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/instances/instances.go (about)

     1  package instances
     2  
     3  import (
     4  	"context"
     5  	"net/url"
     6  	"sort"
     7  
     8  	"github.com/henvic/wedeploycli/apihelper"
     9  	"github.com/henvic/wedeploycli/config"
    10  )
    11  
    12  // Client for the services
    13  type Client struct {
    14  	*apihelper.Client
    15  }
    16  
    17  // New Client
    18  func New(wectx config.Context) *Client {
    19  	return &Client{
    20  		apihelper.New(wectx),
    21  	}
    22  }
    23  
    24  // Instance structure
    25  type Instance struct {
    26  	InstanceID string `json:"instanceId"`
    27  	ServiceID  string `json:"serviceId"`
    28  	ProjectID  string `json:"projectId"`
    29  }
    30  
    31  // Filter for the instances
    32  type Filter Instance
    33  
    34  // Instances structure
    35  type Instances []Instance
    36  
    37  // List instances
    38  func (c *Client) List(ctx context.Context, f Filter) (l Instances, err error) {
    39  	var q = url.Values{}
    40  
    41  	if f.InstanceID != "" {
    42  		q.Set("instanceId", f.InstanceID)
    43  	}
    44  
    45  	if f.ServiceID != "" {
    46  		q.Set("serviceId", f.ServiceID)
    47  	}
    48  
    49  	if f.ProjectID != "" {
    50  		q.Set("projectId", f.ProjectID)
    51  	}
    52  
    53  	u := "/instances"
    54  
    55  	if len(q) != 0 {
    56  		u += "?" + q.Encode()
    57  	}
    58  
    59  	err = c.Client.AuthGet(ctx, u, &l)
    60  
    61  	sort.Slice(l, func(i, j int) bool {
    62  		return l[i].InstanceID < l[j].InstanceID
    63  	})
    64  
    65  	return l, err
    66  }