Skip to content

Commit

Permalink
Merge pull request #104 from hmlanigan/allocate-public-ip-constraint
Browse files Browse the repository at this point in the history
#104

Increments constraints to version 4, and implements allocate-public-ip as a constraint.
  • Loading branch information
jujubot authored Jul 5, 2022
2 parents e60bc7b + 6b0318b commit 6a522a3
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 43 deletions.
83 changes: 53 additions & 30 deletions constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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"`
Expand All @@ -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_
Expand Down Expand Up @@ -206,6 +215,7 @@ var constraintsFieldsFuncs = map[int]fieldsFunc{
1: constraintsV1Fields,
2: constraintsV2Fields,
3: constraintsV3Fields,
4: constraintsV4Fields,
}

func constraintsV1Fields() (schema.Fields, schema.Defaults) {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
65 changes: 53 additions & 12 deletions constraints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
}

Expand All @@ -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.
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6a522a3

Please sign in to comment.