Skip to content

Commit

Permalink
Fix export of storage in bundles
Browse files Browse the repository at this point in the history
  • Loading branch information
wallyworld committed Jun 9, 2022
1 parent 0ae91c6 commit dd61a54
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 9 deletions.
13 changes: 9 additions & 4 deletions apiserver/facades/client/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,11 +701,16 @@ func (b *BundleAPI) bundleDataApplications(
if result := b.constraints(application.Constraints()); len(result) != 0 {
newApplication.Constraints = strings.Join(result, " ")
}
if len(application.StorageConstraints()) != 0 {
if cons := application.StorageConstraints(); len(cons) != 0 {
newApplication.Storage = make(map[string]string)
for name, constr := range application.StorageConstraints() {
newApplication.Storage[name] = fmt.Sprintf("%s,%d,%d",
constr.Pool(), constr.Count(), constr.Size())
for name, constr := range cons {
if newApplication.Storage[name], err = storage.ToString(storage.Constraints{
Pool: constr.Pool(),
Size: constr.Size(),
Count: constr.Count(),
}); err != nil {
return nil, nil, nil, errors.NotValidf("storage %q for %q", name, application.Name())
}
}
}

Expand Down
13 changes: 8 additions & 5 deletions apiserver/facades/client/bundle/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1083,9 +1083,11 @@ func (s *bundleSuite) TestExportBundleWithApplicationStorage(c *gc.C) {
Count: 3,
},
"storage2": {
Pool: "pool2",
Size: 4096,
Count: 1,
Pool: "pool2",
Size: 4096,
},
"storage3": {
Size: 2048,
},
}
app := s.st.model.AddApplication(args)
Expand All @@ -1110,8 +1112,9 @@ applications:
options:
key: value
storage:
storage1: pool1,3,1024
storage2: pool2,1,4096
storage1: pool1,3,1024M
storage2: pool2,4096M
storage3: 2048M
bindings:
another: alpha
juju-info: vlan2
Expand Down
20 changes: 20 additions & 0 deletions storage/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package storage

import (
"fmt"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -167,3 +168,22 @@ func parseSize(s string) (uint64, bool, error) {
}
return size, true, nil
}

// ToString returns a parsable string representation of the storage constraints.
func ToString(c Constraints) (string, error) {
if c.Pool == "" && c.Size <= 0 && c.Count <= 0 {
return "", errors.Errorf("must provide one of pool or size or count")
}

var parts []string
if c.Pool != "" {
parts = append(parts, c.Pool)
}
if c.Count > 0 {
parts = append(parts, fmt.Sprintf("%d", c.Count))
}
if c.Size > 0 {
parts = append(parts, fmt.Sprintf("%dM", c.Size))
}
return strings.Join(parts, ","), nil
}
38 changes: 38 additions & 0 deletions storage/constraints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,41 @@ func (*ConstraintsSuite) testStorageConstraintsError(c *gc.C, s []string, mustHa
_, err := storage.ParseConstraintsMap(s, mustHave)
c.Check(err, gc.ErrorMatches, e)
}

func (s *ConstraintsSuite) TestToString(c *gc.C) {
_, err := storage.ToString(storage.Constraints{})
c.Assert(err, gc.ErrorMatches, "must provide one of pool or size or count")

for _, t := range []struct {
pool string
count uint64
size uint64
expected string
}{
{"loop", 0, 0, "loop"},
{"loop", 1, 0, "loop,1"},
{"loop", 0, 1024, "loop,1024M"},
{"loop", 1, 1024, "loop,1,1024M"},
{"", 0, 1024, "1024M"},
{"", 1, 0, "1"},
{"", 1, 1024, "1,1024M"},
} {
str, err := storage.ToString(storage.Constraints{
Pool: t.pool,
Size: t.size,
Count: t.count,
})
c.Assert(err, jc.ErrorIsNil)
c.Assert(str, gc.Equals, t.expected)

// Test roundtrip, count defaults to 1.
if t.count == 0 {
t.count = 1
}
s.testParse(c, str, storage.Constraints{
Pool: t.pool,
Size: t.size,
Count: t.count,
})
}
}

0 comments on commit dd61a54

Please sign in to comment.