/
dashboard.go
103 lines (92 loc) · 3.54 KB
/
dashboard.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package controller
import (
"bytes"
"encoding/json"
"io"
"net/http"
"net/url"
"strings"
"github.com/juju/errors"
"github.com/juju/version/v2"
"github.com/juju/juju/apiserver/params"
)
const (
dashboardArchivePath = "/dashboard-archive"
dashboardVersionPath = "/dashboard-version"
incompatibleClientMsg = "juju client not supported on this controller\nrefresh your juju client to match the version running on the controller"
)
// DashboardArchives retrieves information about Juju Dashboard archives currently present
// in the Juju controller.
func (c *Client) DashboardArchives() ([]params.DashboardArchiveVersion, error) {
httpClient, err := c.facade.RawAPICaller().HTTPClient()
if err != nil {
return nil, errors.Annotate(err, "cannot retrieve HTTP client")
}
var resp params.DashboardArchiveResponse
if err = httpClient.Get(c.facade.RawAPICaller().Context(), dashboardArchivePath, &resp); err != nil {
// We don't have access to the http error code, but make a best effort to
// handle a missing dashboard as opposed to a connection error
if strings.Contains(err.Error(), "404 ") {
return nil, errors.NewNotSupported(nil, incompatibleClientMsg)
}
return nil, errors.Annotate(err, "cannot retrieve Dashboard archives info")
}
return resp.Versions, nil
}
// UploadDashboardArchive uploads a Dashboard archive to the controller over HTTPS, and
// reports about whether the upload updated the current Dashboard served by Juju.
func (c *Client) UploadDashboardArchive(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", dashboardArchivePath+"?"+v.Encode(), r)
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.DashboardArchiveVersion
if err = httpClient.Do(c.facade.RawAPICaller().Context(), req, &resp); err != nil {
if strings.Contains(err.Error(), "404 ") {
return false, errors.NewNotSupported(nil, incompatibleClientMsg)
}
return false, errors.Annotate(err, "cannot upload the Dashboard archive")
}
return resp.Current, nil
}
// SelectDashboardVersion selects which version of the Juju Dashboard is served by the
// controller.
func (c *Client) SelectDashboardVersion(vers version.Number) error {
// Prepare the request.
content, err := json.Marshal(params.DashboardVersionRequest{
Version: vers,
})
if err != nil {
return errors.Annotate(err, "cannot marshal request body")
}
req, err := http.NewRequest("PUT", dashboardVersionPath, bytes.NewReader(content))
if err != nil {
return errors.Annotate(err, "cannot create PUT request")
}
req.Header.Set("Content-Type", params.ContentTypeJSON)
// 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(c.facade.RawAPICaller().Context(), req, nil); err != nil {
if strings.Contains(err.Error(), "404 ") {
return errors.NewNotSupported(nil, incompatibleClientMsg)
}
return errors.Annotate(err, "cannot select Dashboard version")
}
return nil
}