github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/api/controller/gui.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package controller
     5  
     6  import (
     7  	"bytes"
     8  	"encoding/json"
     9  	"io"
    10  	"net/http"
    11  	"net/url"
    12  
    13  	"github.com/juju/errors"
    14  	"github.com/juju/version"
    15  
    16  	"github.com/juju/juju/apiserver/params"
    17  )
    18  
    19  const (
    20  	guiArchivePath = "/gui-archive"
    21  	guiVersionPath = "/gui-version"
    22  )
    23  
    24  // GUIArchives retrieves information about Juju GUI archives currently present
    25  // in the Juju controller.
    26  func (c *Client) GUIArchives() ([]params.GUIArchiveVersion, error) {
    27  	httpClient, err := c.facade.RawAPICaller().HTTPClient()
    28  	if err != nil {
    29  		return nil, errors.Annotate(err, "cannot retrieve HTTP client")
    30  	}
    31  	var resp params.GUIArchiveResponse
    32  	if err = httpClient.Get(guiArchivePath, &resp); err != nil {
    33  		return nil, errors.Annotate(err, "cannot retrieve GUI archives info")
    34  	}
    35  	return resp.Versions, nil
    36  }
    37  
    38  // UploadGUIArchive uploads a GUI archive to the controller over HTTPS, and
    39  // reports about whether the upload updated the current GUI served by Juju.
    40  func (c *Client) UploadGUIArchive(r io.ReadSeeker, hash string, size int64, vers version.Number) (current bool, err error) {
    41  	// Prepare the request.
    42  	v := url.Values{}
    43  	v.Set("version", vers.String())
    44  	v.Set("hash", hash)
    45  	req, err := http.NewRequest("POST", guiArchivePath+"?"+v.Encode(), nil)
    46  	if err != nil {
    47  		return false, errors.Annotate(err, "cannot create upload request")
    48  	}
    49  	req.Header.Set("Content-Type", "application/x-tar-bzip2")
    50  	req.ContentLength = size
    51  
    52  	// Retrieve a client and send the request.
    53  	httpClient, err := c.facade.RawAPICaller().HTTPClient()
    54  	if err != nil {
    55  		return false, errors.Annotate(err, "cannot retrieve HTTP client")
    56  	}
    57  	var resp params.GUIArchiveVersion
    58  	if err = httpClient.Do(req, r, &resp); err != nil {
    59  		return false, errors.Annotate(err, "cannot upload the GUI archive")
    60  	}
    61  	return resp.Current, nil
    62  }
    63  
    64  // SelectGUIVersion selects which version of the Juju GUI is served by the
    65  // controller.
    66  func (c *Client) SelectGUIVersion(vers version.Number) error {
    67  	// Prepare the request.
    68  	req, err := http.NewRequest("PUT", guiVersionPath, nil)
    69  	if err != nil {
    70  		return errors.Annotate(err, "cannot create PUT request")
    71  	}
    72  	req.Header.Set("Content-Type", params.ContentTypeJSON)
    73  	content, err := json.Marshal(params.GUIVersionRequest{
    74  		Version: vers,
    75  	})
    76  	if err != nil {
    77  		errors.Annotate(err, "cannot marshal request body")
    78  	}
    79  
    80  	// Retrieve a client and send the request.
    81  	httpClient, err := c.facade.RawAPICaller().HTTPClient()
    82  	if err != nil {
    83  		return errors.Annotate(err, "cannot retrieve HTTP client")
    84  	}
    85  	if err = httpClient.Do(req, bytes.NewReader(content), nil); err != nil {
    86  		return errors.Annotate(err, "cannot select GUI version")
    87  	}
    88  	return nil
    89  }