From 6b0318bd04ece27c03adfc8cad214c05e0e9ab4d Mon Sep 17 00:00:00 2001 From: Heather Lanigan Date: Fri, 1 Jul 2022 14:17:32 -0400 Subject: [PATCH] Add AllocatePublicIP to constraints. Increments constraints to version 4. --- constraints.go | 83 +++++++++++++++++++++++++++++---------------- constraints_test.go | 65 ++++++++++++++++++++++++++++------- go.mod | 2 +- 3 files changed, 107 insertions(+), 43 deletions(-) diff --git a/constraints.go b/constraints.go index e188842..5784d0c 100644 --- a/constraints.go +++ b/constraints.go @@ -18,6 +18,7 @@ type HasConstraints interface { // Constraints holds information about particular deployment // constraints for entities. type Constraints interface { + AllocatePublicIP() bool Architecture() string Container() string CpuCores() uint64 @@ -36,14 +37,15 @@ type Constraints interface { // ConstraintsArgs is an argument struct to construct Constraints. type ConstraintsArgs struct { - Architecture string - Container string - CpuCores uint64 - CpuPower uint64 - InstanceType string - Memory uint64 - RootDisk uint64 - RootDiskSource string + AllocatePublicIP bool + Architecture string + Container string + CpuCores uint64 + CpuPower uint64 + InstanceType string + Memory uint64 + RootDisk uint64 + RootDiskSource string Spaces []string Tags []string @@ -67,33 +69,35 @@ func newConstraints(args ConstraintsArgs) *constraints { copy(zones, args.Zones) return &constraints{ - Version: 3, - Architecture_: args.Architecture, - Container_: args.Container, - CpuCores_: args.CpuCores, - CpuPower_: args.CpuPower, - InstanceType_: args.InstanceType, - Memory_: args.Memory, - RootDisk_: args.RootDisk, - RootDiskSource_: args.RootDiskSource, - Spaces_: spaces, - Tags_: tags, - Zones_: zones, - VirtType_: args.VirtType, + Version: 4, + AllocatePublicIP_: args.AllocatePublicIP, + Architecture_: args.Architecture, + Container_: args.Container, + CpuCores_: args.CpuCores, + CpuPower_: args.CpuPower, + InstanceType_: args.InstanceType, + Memory_: args.Memory, + RootDisk_: args.RootDisk, + RootDiskSource_: args.RootDiskSource, + Spaces_: spaces, + Tags_: tags, + Zones_: zones, + VirtType_: args.VirtType, } } type constraints struct { Version int `yaml:"version"` - Architecture_ string `yaml:"architecture,omitempty"` - Container_ string `yaml:"container,omitempty"` - CpuCores_ uint64 `yaml:"cores,omitempty"` - CpuPower_ uint64 `yaml:"cpu-power,omitempty"` - InstanceType_ string `yaml:"instance-type,omitempty"` - Memory_ uint64 `yaml:"memory,omitempty"` - RootDisk_ uint64 `yaml:"root-disk,omitempty"` - RootDiskSource_ string `yaml:"root-disk-source,omitempty"` + AllocatePublicIP_ bool `yaml:"allocate-public-ip,omitempty"` + Architecture_ string `yaml:"architecture,omitempty"` + Container_ string `yaml:"container,omitempty"` + CpuCores_ uint64 `yaml:"cores,omitempty"` + CpuPower_ uint64 `yaml:"cpu-power,omitempty"` + InstanceType_ string `yaml:"instance-type,omitempty"` + Memory_ uint64 `yaml:"memory,omitempty"` + RootDisk_ uint64 `yaml:"root-disk,omitempty"` + RootDiskSource_ string `yaml:"root-disk-source,omitempty"` Spaces_ []string `yaml:"spaces,omitempty"` Tags_ []string `yaml:"tags,omitempty"` @@ -102,6 +106,11 @@ type constraints struct { VirtType_ string `yaml:"virt-type,omitempty"` } +// AllocatePublicIP implements Constraints. +func (c *constraints) AllocatePublicIP() bool { + return c.AllocatePublicIP_ +} + // Architecture implements Constraints. func (c *constraints) Architecture() string { return c.Architecture_ @@ -206,6 +215,7 @@ var constraintsFieldsFuncs = map[int]fieldsFunc{ 1: constraintsV1Fields, 2: constraintsV2Fields, 3: constraintsV3Fields, + 4: constraintsV4Fields, } func constraintsV1Fields() (schema.Fields, schema.Defaults) { @@ -256,6 +266,13 @@ func constraintsV3Fields() (schema.Fields, schema.Defaults) { return fields, defaults } +func constraintsV4Fields() (schema.Fields, schema.Defaults) { + fields, defaults := constraintsV3Fields() + fields["allocate-public-ip"] = schema.Bool() + defaults["allocate-public-ip"] = schema.Omit + return fields, defaults +} + // constraintsValidCPUCores returns an error if both aliases for CPU core count // are present in the list of fields. // If correctly specified, the cores value is returned. @@ -302,6 +319,11 @@ func validatedConstraints(version int, valid map[string]interface{}, cores uint6 if version > 2 { cons.RootDiskSource_ = valid["root-disk-source"].(string) } + if version > 3 { + if value, ok := valid["allocate-public-ip"]; ok { + cons.AllocatePublicIP_ = value.(bool) + } + } return cons } @@ -323,5 +345,6 @@ func (c ConstraintsArgs) empty() bool { c.Spaces == nil && c.Tags == nil && c.Zones == nil && - c.VirtType == "" + c.VirtType == "" && + c.AllocatePublicIP == false } diff --git a/constraints_test.go b/constraints_test.go index 712a4cf..79d69d2 100644 --- a/constraints_test.go +++ b/constraints_test.go @@ -25,18 +25,19 @@ func (s *ConstraintsSerializationSuite) SetUpTest(c *gc.C) { func (s *ConstraintsSerializationSuite) allArgs() ConstraintsArgs { // NOTE: using gig from package_test.go return ConstraintsArgs{ - Architecture: "amd64", - Container: "lxd", - CpuCores: 8, - CpuPower: 4000, - InstanceType: "magic", - Memory: 16 * gig, - RootDisk: 200 * gig, - RootDiskSource: "somewhere-good", - Spaces: []string{"my", "own"}, - Tags: []string{"much", "strong"}, - Zones: []string{"az1", "az2"}, - VirtType: "something", + AllocatePublicIP: true, + Architecture: "amd64", + Container: "lxd", + CpuCores: 8, + CpuPower: 4000, + InstanceType: "magic", + Memory: 16 * gig, + RootDisk: 200 * gig, + RootDiskSource: "somewhere-good", + Spaces: []string{"my", "own"}, + Tags: []string{"much", "strong"}, + Zones: []string{"az1", "az2"}, + VirtType: "something", } } @@ -52,6 +53,7 @@ func (s *ConstraintsSerializationSuite) TestNewConstraints(c *gc.C) { c.Assert(instance.Memory(), gc.Equals, args.Memory) c.Assert(instance.RootDisk(), gc.Equals, args.RootDisk) c.Assert(instance.RootDiskSource(), gc.Equals, args.RootDiskSource) + c.Assert(instance.AllocatePublicIP(), gc.Equals, args.AllocatePublicIP) // Before we check tags, spaces and zones, modify args to make sure that // the instance ones do not change. @@ -156,6 +158,7 @@ func (s *ConstraintsSerializationSuite) TestParsingV1Full(c *gc.C) { expected := s.testConstraints() expected.Zones_ = nil expected.RootDiskSource_ = "" + expected.AllocatePublicIP_ = false expected.Version = 1 c.Assert(imported, gc.DeepEquals, expected) } @@ -198,6 +201,7 @@ func (s *ConstraintsSerializationSuite) TestParsingV2Full(c *gc.C) { imported := s.importConstraints(c, original) expected := s.testConstraints() expected.RootDiskSource_ = "" + expected.AllocatePublicIP_ = false expected.Version = 2 c.Assert(imported, gc.DeepEquals, expected) } @@ -240,6 +244,8 @@ func (s *ConstraintsSerializationSuite) TestParsingV3Full(c *gc.C) { original := s.allV3Map() imported := s.importConstraints(c, original) expected := s.testConstraints() + expected.AllocatePublicIP_ = false + expected.Version = 3 c.Assert(imported, gc.DeepEquals, expected) } @@ -251,3 +257,38 @@ func (s *ConstraintsSerializationSuite) TestParsingV3Minimal(c *gc.C) { expected := &constraints{Version: 3} c.Assert(imported, gc.DeepEquals, expected) } + +func (s *ConstraintsSerializationSuite) allV4Map() map[string]interface{} { + return map[string]interface{}{ + "version": 4, + "allocate-public-ip": true, + "architecture": "amd64", + "container": "lxd", + "cores": 8, + "cpu-power": 4000, + "instance-type": "magic", + "memory": 16 * gig, + "root-disk": 200 * gig, + "root-disk-source": "somewhere-good", + "spaces": []interface{}{"my", "own"}, + "tags": []interface{}{"much", "strong"}, + "zones": []interface{}{"az1", "az2"}, + "virt-type": "something", + } +} + +func (s *ConstraintsSerializationSuite) TestParsingV4Full(c *gc.C) { + original := s.allV4Map() + imported := s.importConstraints(c, original) + expected := s.testConstraints() + c.Assert(imported, gc.DeepEquals, expected) +} + +func (s *ConstraintsSerializationSuite) TestParsingV4Minimal(c *gc.C) { + original := map[string]interface{}{ + "version": 4, + } + imported := s.importConstraints(c, original) + expected := &constraints{Version: 4} + c.Assert(imported, gc.DeepEquals, expected) +} diff --git a/go.mod b/go.mod index f15ff47..51e4a1c 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( require ( github.com/juju/clock v0.0.0-20220203021603-d9deb868a28a // indirect - github.com/juju/loggo v0.0.0-20210728185423-eebad3a902c4 // indirect + github.com/juju/loggo v0.0.0-20210728185423-eebad3a902c4 github.com/juju/mgo/v2 v2.0.0-20220111072304-f200228f1090 // indirect github.com/juju/retry v0.0.0-20220204093819-62423bf33287 // indirect github.com/juju/utils/v3 v3.0.0-20220203023959-c3fbc78a33b0 // indirect