/
bundles.go
61 lines (55 loc) · 1.81 KB
/
bundles.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package client
import (
"strings"
"github.com/juju/bundlechanges"
"github.com/juju/errors"
"gopkg.in/juju/charm.v6-unstable"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/constraints"
"github.com/juju/juju/storage"
)
// GetBundleChanges returns the list of changes required to deploy the given
// bundle data. The changes are sorted by requirements, so that they can be
// applied in order.
func (c *Client) GetBundleChanges(args params.GetBundleChangesParams) (params.GetBundleChangesResults, error) {
if err := c.checkCanRead(); err != nil {
return params.GetBundleChangesResults{}, err
}
var results params.GetBundleChangesResults
data, err := charm.ReadBundleData(strings.NewReader(args.BundleDataYAML))
if err != nil {
return results, errors.Annotate(err, "cannot read bundle YAML")
}
verifyConstraints := func(s string) error {
_, err := constraints.Parse(s)
return err
}
verifyStorage := func(s string) error {
_, err := storage.ParseConstraints(s)
return err
}
if err := data.Verify(verifyConstraints, verifyStorage); err != nil {
if err, ok := err.(*charm.VerificationError); ok {
results.Errors = make([]string, len(err.Errors))
for i, e := range err.Errors {
results.Errors[i] = e.Error()
}
return results, nil
}
// This should never happen as Verify only returns verification errors.
return results, errors.Annotate(err, "cannot verify bundle")
}
changes := bundlechanges.FromData(data)
results.Changes = make([]*params.BundleChangesChange, len(changes))
for i, c := range changes {
results.Changes[i] = ¶ms.BundleChangesChange{
Id: c.Id(),
Method: c.Method(),
Args: c.GUIArgs(),
Requires: c.Requires(),
}
}
return results, nil
}