Skip to content

Commit

Permalink
Adding virttype to constraints.
Browse files Browse the repository at this point in the history
  • Loading branch information
anastasiamac committed Mar 16, 2016
1 parent 29c542b commit a23d460
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 2 deletions.
30 changes: 30 additions & 0 deletions constraints/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
InstanceType = "instance-type"
Networks = "networks"
Spaces = "spaces"
VirtType = "virt-type"
)

// Value describes a user's requirements of the hardware on which units
Expand Down Expand Up @@ -88,6 +89,10 @@ type Value struct {
// TODO(dimitern): Drop this as soon as spaces can be used for
// deployments instead.
Networks *[]string `json:"networks,omitempty" yaml:"networks,omitempty"`

// VirtType, if not nil or empty, indicates that a machine must run the named
// virtual type. Only valid for clouds with multi-hypervisor support.
VirtType *string `json:"virt-type,omitempty" yaml:"virt-type,omitempty"`
}

// fieldNames records a mapping from the constraint tag to struct field name.
Expand Down Expand Up @@ -199,6 +204,11 @@ func (v *Value) HaveNetworks() bool {
return v.Networks != nil && len(*v.Networks) > 0
}

// HasVirtType returns true if the constraints.Value specifies an virtual type.
func (v *Value) HasVirtType() bool {
return v.VirtType != nil && *v.VirtType != ""
}

// String expresses a constraints.Value in the language in which it was specified.
func (v Value) String() string {
var strs []string
Expand Down Expand Up @@ -243,6 +253,9 @@ func (v Value) String() string {
s := strings.Join(*v.Networks, ",")
strs = append(strs, "networks="+s)
}
if v.VirtType != nil {
strs = append(strs, "virt-type="+string(*v.VirtType))
}
return strings.Join(strs, " ")
}

Expand Down Expand Up @@ -286,6 +299,9 @@ func (v Value) GoString() string {
} else if v.Networks != nil {
values = append(values, "Networks: (*[]string)(nil)")
}
if v.VirtType != nil {
values = append(values, fmt.Sprintf("VirtType: %q", *v.VirtType))
}
return fmt.Sprintf("{%s}", strings.Join(values, ", "))
}

Expand Down Expand Up @@ -427,6 +443,8 @@ func (v *Value) setRaw(raw string) error {
err = v.setSpaces(str)
case Networks:
err = v.setNetworks(str)
case VirtType:
err = v.setVirtType(str)
default:
return errors.Errorf("unknown constraint %q", name)
}
Expand Down Expand Up @@ -487,6 +505,8 @@ func (v *Value) UnmarshalYAML(unmarshal func(interface{}) error) error {
if err == nil {
v.Networks = networks
}
case VirtType:
v.VirtType = &vstr
default:
return errors.Errorf("unknown constraint value: %v", k)
}
Expand Down Expand Up @@ -628,6 +648,16 @@ func (v *Value) validateNetworks(networks *[]string) error {
return nil
}

func (v *Value) setVirtType(str string) error {
if v.VirtType != nil {
return errors.Errorf("already set")
}
// TODO (anastasiamac 2016-03-15)
// Do I need to validate that this is a supported virt type?
v.VirtType = &str
return nil
}

func parseUint64(str string) (*uint64, error) {
var value uint64
if str != "" {
Expand Down
25 changes: 23 additions & 2 deletions constraints/constraints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,18 +310,39 @@ var parseConstraintsTests = []struct {
args: []string{"instance-type="},
},

// "virt-type" in detail.
{
summary: "set virt-type empty",
args: []string{"virt-type="},
}, {
summary: "set virt-type kvm",
args: []string{"virt-type=kvm"},
}, {
summary: "set virt-type lxd",
args: []string{"virt-type=lxd"},
}, {
summary: "double set virt-type together",
args: []string{"virt-type=kvm virt-type=kvm"},
err: `bad "virt-type" constraint: already set`,
}, {
summary: "double set virt-type separately",
args: []string{"virt-type=kvm", "virt-type="},
err: `bad "virt-type" constraint: already set`,
},

// Everything at once.
{
summary: "kitchen sink together",
args: []string{
"root-disk=8G mem=2T arch=i386 cpu-cores=4096 cpu-power=9001 container=lxc " +
"tags=foo,bar spaces=space1,^space2 networks=net,^net2 instance-type=foo"},
"tags=foo,bar spaces=space1,^space2 networks=net,^net2 instance-type=foo",
"virt-type=kvm"},
}, {
summary: "kitchen sink separately",
args: []string{
"root-disk=8G", "mem=2T", "cpu-cores=4096", "cpu-power=9001", "arch=armhf",
"container=lxc", "tags=foo,bar", "spaces=space1,^space2", "networks=net1,^net2",
"instance-type=foo"},
"instance-type=foo", "virt-type=kvm"},
},
}

Expand Down
4 changes: 4 additions & 0 deletions constraints/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ var validationTests = []struct {
"instance-type": {"foo", "bar"},
"arch": {"amd64", "i386"}},
},
{
cons: "virt-type=bar",
vocab: map[string][]interface{}{"virt-type": {"bar"}},
},
}

func (s *validationSuite) TestValidation(c *gc.C) {
Expand Down
3 changes: 3 additions & 0 deletions environs/instances/instancetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ func (itype InstanceType) match(cons constraints.Value) (InstanceType, bool) {
if cons.Tags != nil && len(*cons.Tags) > 0 && !tagsMatch(*cons.Tags, itype.Tags) {
return nothing, false
}
if cons.HasVirtType() && (itype.VirtType == nil || *itype.VirtType != *cons.VirtType) {
return nothing, false
}
return itype, true
}

Expand Down
5 changes: 5 additions & 0 deletions environs/instances/instancetype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ var getInstanceTypesTest = []struct {
{Id: "1", Name: "it-1", Arches: []string{"amd64"}, Mem: 512, CpuCores: 4, Cost: 100},
},
expectedItypes: []string{"it-2"},
}, {
about: "virt-type filtered by constraint",
cons: "virt-type=hvm",
expectedItypes: []string{"cc1.4xlarge", "cc2.8xlarge"},
itypesToUse: nil,
}, {
about: "deprecated image type requested by name",
cons: "instance-type=dep.small",
Expand Down

0 comments on commit a23d460

Please sign in to comment.