github.com/naphatkrit/deis@v1.12.3/client/controller/models/releases/releases.go (about)

     1  package releases
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	"github.com/deis/deis/client/controller/api"
     8  	"github.com/deis/deis/client/controller/client"
     9  )
    10  
    11  // List lists an app's releases.
    12  func List(c *client.Client, appID string, results int) ([]api.Release, int, error) {
    13  	u := fmt.Sprintf("/v1/apps/%s/releases/", appID)
    14  
    15  	body, count, err := c.LimitedRequest(u, results)
    16  
    17  	if err != nil {
    18  		return []api.Release{}, -1, err
    19  	}
    20  
    21  	var releases []api.Release
    22  	if err = json.Unmarshal([]byte(body), &releases); err != nil {
    23  		return []api.Release{}, -1, err
    24  	}
    25  
    26  	return releases, count, nil
    27  }
    28  
    29  // Get a release of an app.
    30  func Get(c *client.Client, appID string, version int) (api.Release, error) {
    31  	u := fmt.Sprintf("/v1/apps/%s/releases/v%d/", appID, version)
    32  
    33  	body, err := c.BasicRequest("GET", u, nil)
    34  
    35  	if err != nil {
    36  		return api.Release{}, err
    37  	}
    38  
    39  	release := api.Release{}
    40  	if err = json.Unmarshal([]byte(body), &release); err != nil {
    41  		return api.Release{}, err
    42  	}
    43  
    44  	return release, nil
    45  }
    46  
    47  // Rollback rolls back an app to a previous release.
    48  func Rollback(c *client.Client, appID string, version int) (int, error) {
    49  	u := fmt.Sprintf("/v1/apps/%s/releases/rollback/", appID)
    50  
    51  	req := api.ReleaseRollback{Version: version}
    52  
    53  	var err error
    54  	var reqBody []byte
    55  	if version != -1 {
    56  		reqBody, err = json.Marshal(req)
    57  
    58  		if err != nil {
    59  			return -1, err
    60  		}
    61  	}
    62  
    63  	body, err := c.BasicRequest("POST", u, reqBody)
    64  
    65  	if err != nil {
    66  		return -1, err
    67  	}
    68  
    69  	response := api.ReleaseRollback{}
    70  
    71  	if err = json.Unmarshal([]byte(body), &response); err != nil {
    72  		return -1, err
    73  	}
    74  
    75  	return response.Version, nil
    76  }