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