forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
77 lines (68 loc) · 2.08 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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package block
import (
"github.com/juju/errors"
"github.com/juju/juju/api/base"
"github.com/juju/juju/apiserver/params"
)
// Client allows access to the block API end point.
type Client struct {
base.ClientFacade
facade base.FacadeCaller
}
// NewClient creates a new client for accessing the block API.
func NewClient(st base.APICallCloser) *Client {
frontend, backend := base.NewClientFacade(st, "Block")
return &Client{ClientFacade: frontend, facade: backend}
}
// List returns blocks that are switched on for current model.
func (c *Client) List() ([]params.Block, error) {
var blocks params.BlockResults
if err := c.facade.FacadeCall("List", nil, &blocks); err != nil {
return nil, errors.Trace(err)
}
var all []params.Block
var allErr params.ErrorResults
for _, result := range blocks.Results {
if result.Error != nil {
allErr.Results = append(allErr.Results, params.ErrorResult{result.Error})
continue
}
all = append(all, result.Result)
}
return all, allErr.Combine()
}
// SwitchBlockOn switches desired block on for the current model.
// Valid block types are "BlockDestroy", "BlockRemove" and "BlockChange".
func (c *Client) SwitchBlockOn(blockType, msg string) error {
args := params.BlockSwitchParams{
Type: blockType,
Message: msg,
}
var result params.ErrorResult
if err := c.facade.FacadeCall("SwitchBlockOn", args, &result); err != nil {
return errors.Trace(err)
}
if result.Error != nil {
// cope with typed error
return errors.Trace(result.Error)
}
return nil
}
// SwitchBlockOff switches desired block off for the current model.
// Valid block types are "BlockDestroy", "BlockRemove" and "BlockChange".
func (c *Client) SwitchBlockOff(blockType string) error {
args := params.BlockSwitchParams{
Type: blockType,
}
var result params.ErrorResult
if err := c.facade.FacadeCall("SwitchBlockOff", args, &result); err != nil {
return errors.Trace(err)
}
if result.Error != nil {
// cope with typed error
return errors.Trace(result.Error)
}
return nil
}