forked from juju/juju
/
client.go
137 lines (123 loc) · 4.28 KB
/
client.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package caasoperatorprovisioner
import (
"github.com/juju/errors"
"github.com/juju/version"
"gopkg.in/juju/names.v2"
"github.com/juju/juju/api/base"
apiwatcher "github.com/juju/juju/api/watcher"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/core/life"
"github.com/juju/juju/core/watcher"
"github.com/juju/juju/storage"
)
// Client allows access to the CAAS operator provisioner API endpoint.
type Client struct {
facade base.FacadeCaller
}
// NewClient returns a client used to access the CAAS Operator Provisioner API.
func NewClient(caller base.APICaller) *Client {
facadeCaller := base.NewFacadeCaller(caller, "CAASOperatorProvisioner")
return &Client{
facade: facadeCaller,
}
}
// WatchApplications returns a StringsWatcher that notifies of
// changes to the lifecycles of CAAS applications in the current model.
func (c *Client) WatchApplications() (watcher.StringsWatcher, error) {
var result params.StringsWatchResult
if err := c.facade.FacadeCall("WatchApplications", nil, &result); err != nil {
return nil, err
}
if err := result.Error; err != nil {
return nil, result.Error
}
w := apiwatcher.NewStringsWatcher(c.facade.RawAPICaller(), result)
return w, nil
}
// ApplicationPassword holds parameters for setting
// an application password.
type ApplicationPassword struct {
Name string
Password string
}
// SetPasswords sets API passwords for the specified applications.
func (c *Client) SetPasswords(appPasswords []ApplicationPassword) (params.ErrorResults, error) {
var result params.ErrorResults
args := params.EntityPasswords{Changes: make([]params.EntityPassword, len(appPasswords))}
for i, p := range appPasswords {
args.Changes[i] = params.EntityPassword{
Tag: names.NewApplicationTag(p.Name).String(), Password: p.Password,
}
}
err := c.facade.FacadeCall("SetPasswords", args, &result)
if err != nil {
return params.ErrorResults{}, err
}
if len(result.Results) != len(args.Changes) {
return params.ErrorResults{}, errors.Errorf("expected %d result(s), got %d", len(args.Changes), len(result.Results))
}
return result, nil
}
// maybeNotFound returns an error satisfying errors.IsNotFound
// if the supplied error has a CodeNotFound error.
func maybeNotFound(err *params.Error) error {
if err == nil || !params.IsCodeNotFound(err) {
return err
}
return errors.NewNotFound(err, "")
}
// Life returns the lifecycle state for the specified CAAS application
// or unit in the current model.
func (c *Client) Life(appName string) (life.Value, error) {
if !names.IsValidApplication(appName) {
return "", errors.NotValidf("application name %q", appName)
}
args := params.Entities{
Entities: []params.Entity{{Tag: names.NewApplicationTag(appName).String()}},
}
var results params.LifeResults
if err := c.facade.FacadeCall("Life", args, &results); err != nil {
return "", err
}
if n := len(results.Results); n != 1 {
return "", errors.Errorf("expected 1 result, got %d", n)
}
if err := results.Results[0].Error; err != nil {
return "", maybeNotFound(err)
}
return life.Value(results.Results[0].Life), nil
}
// OperatorProvisioningInfo holds the info needed to provision an operator.
type OperatorProvisioningInfo struct {
ImagePath string
Version version.Number
APIAddresses []string
Tags map[string]string
CharmStorage storage.KubernetesFilesystemParams
}
// OperatorProvisioningInfo returns the info needed to provision an operator.
func (c *Client) OperatorProvisioningInfo() (OperatorProvisioningInfo, error) {
var result params.OperatorProvisioningInfo
if err := c.facade.FacadeCall("OperatorProvisioningInfo", nil, &result); err != nil {
return OperatorProvisioningInfo{}, err
}
info := OperatorProvisioningInfo{
ImagePath: result.ImagePath,
Version: result.Version,
APIAddresses: result.APIAddresses,
Tags: result.Tags,
CharmStorage: filesystemFromParams(result.CharmStorage),
}
return info, nil
}
func filesystemFromParams(in params.KubernetesFilesystemParams) storage.KubernetesFilesystemParams {
return storage.KubernetesFilesystemParams{
StorageName: in.StorageName,
Provider: storage.ProviderType(in.Provider),
Size: in.Size,
Attributes: in.Attributes,
ResourceTags: in.Tags,
}
}