Skip to content

Commit

Permalink
Merge pull request #121 from SimonRichardson/cloud-instance-virt-type-v4
Browse files Browse the repository at this point in the history
#121

Allow the setting and getting of the virt-type for a cloud instance. We now require that a virt-type is set in the hardware characteristics of an instance. This means that for migration import and export we also need to send that information over the wire.

This PR exposes the virt-type for that to work.

This is backward compatible with v4, so we should be able to just create a new tag - v4.0.4
  • Loading branch information
jujubot authored Jan 3, 2023
2 parents 88a4608 + fce5b8e commit 8e38e6c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
23 changes: 22 additions & 1 deletion cloudinstance.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type CloudInstance interface {
CpuPower() uint64
Tags() []string
AvailabilityZone() string
VirtType() string
CharmProfiles() []string

Validate() error
Expand All @@ -43,6 +44,7 @@ type CloudInstanceArgs struct {
CpuPower uint64
Tags []string
AvailabilityZone string
VirtType string
CharmProfiles []string
}

Expand All @@ -52,7 +54,7 @@ func newCloudInstance(args CloudInstanceArgs) *cloudInstance {
profiles := make([]string, len(args.CharmProfiles))
copy(profiles, args.CharmProfiles)
return &cloudInstance{
Version: 5,
Version: 6,
InstanceId_: args.InstanceId,
DisplayName_: args.DisplayName,
Architecture_: args.Architecture,
Expand All @@ -63,6 +65,7 @@ func newCloudInstance(args CloudInstanceArgs) *cloudInstance {
CpuPower_: args.CpuPower,
Tags_: tags,
AvailabilityZone_: args.AvailabilityZone,
VirtType_: args.VirtType,
CharmProfiles_: profiles,
StatusHistory_: newStatusHistory(),
}
Expand Down Expand Up @@ -95,6 +98,7 @@ type cloudInstance struct {
CpuPower_ uint64 `yaml:"cpu-power,omitempty"`
Tags_ []string `yaml:"tags,omitempty"`
AvailabilityZone_ string `yaml:"availability-zone,omitempty"`
VirtType_ string `yaml:"virt-type,omitempty"`
CharmProfiles_ []string `yaml:"charm-profiles,omitempty"`
}

Expand Down Expand Up @@ -178,6 +182,11 @@ func (c *cloudInstance) AvailabilityZone() string {
return c.AvailabilityZone_
}

// VirtType implements CloudInstance.
func (c *cloudInstance) VirtType() string {
return c.VirtType_
}

// CharmProfiles implements CloudInstance.
func (c *cloudInstance) CharmProfiles() []string {
profiles := make([]string, len(c.CharmProfiles_))
Expand Down Expand Up @@ -216,6 +225,7 @@ var cloudInstanceFieldsFuncs = map[int]fieldsFunc{
3: cloudInstanceV3Fields,
4: cloudInstanceV4Fields,
5: cloudInstanceV5Fields,
6: cloudInstanceV6Fields,
}

func cloudInstanceV1Fields() (schema.Fields, schema.Defaults) {
Expand Down Expand Up @@ -273,6 +283,13 @@ func cloudInstanceV5Fields() (schema.Fields, schema.Defaults) {
return fields, defaults
}

func cloudInstanceV6Fields() (schema.Fields, schema.Defaults) {
fields, defaults := cloudInstanceV5Fields()
fields["virt-type"] = schema.String()
defaults["virt-type"] = ""
return fields, defaults
}

func importCloudInstanceVx(source map[string]interface{}, version int, fieldFunc func() (schema.Fields, schema.Defaults)) (*cloudInstance, error) {
fields, defaults := fieldFunc()
checker := schema.FieldMap(fields, defaults)
Expand Down Expand Up @@ -334,6 +351,10 @@ func newCloudInstanceFromValid(valid map[string]interface{}, importVersion int)
if importVersion > 4 {
instance.RootDiskSource_ = valid["root-disk-source"].(string)
}

if importVersion > 5 {
instance.VirtType_ = valid["virt-type"].(string)
}
default:
return nil, errors.NotValidf("unexpected version: %d", importVersion)
}
Expand Down
51 changes: 50 additions & 1 deletion cloudinstance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (s *CloudInstanceSerializationSuite) SetUpTest(c *gc.C) {

func minimalCloudInstanceMap() map[interface{}]interface{} {
return map[interface{}]interface{}{
"version": 5,
"version": 6,
"instance-id": "instance id",
"status": minimalStatusMap(),
"status-history": emptyStatusHistoryMap(),
Expand Down Expand Up @@ -66,6 +66,7 @@ func (s *CloudInstanceSerializationSuite) TestNewCloudInstance(c *gc.C) {
c.Check(instance.RootDiskSource(), gc.Equals, args.RootDiskSource)
c.Check(instance.CpuCores(), gc.Equals, args.CpuCores)
c.Check(instance.CpuPower(), gc.Equals, args.CpuPower)
c.Check(instance.VirtType(), gc.Equals, args.VirtType)
c.Check(instance.AvailabilityZone(), gc.Equals, args.AvailabilityZone)

// Before we check tags, modify args to make sure that the instance ones
Expand Down Expand Up @@ -171,6 +172,7 @@ func (s *CloudInstanceSerializationSuite) testArgs() CloudInstanceArgs {
CpuPower: 4000,
Tags: []string{"much", "strong"},
AvailabilityZone: "everywhere",
VirtType: "container",
CharmProfiles: []string{"much", "strong"},
}
}
Expand Down Expand Up @@ -206,6 +208,7 @@ func (s *CloudInstanceSerializationSuite) TestParsingV4Full(c *gc.C) {
imported := s.importCloudInstance(c, original)
expected := s.testCloudInstance()
expected.RootDiskSource_ = ""
expected.VirtType_ = ""
expected.Version = 4
c.Assert(imported, jc.DeepEquals, expected)
}
Expand Down Expand Up @@ -257,6 +260,8 @@ func (s *CloudInstanceSerializationSuite) TestParsingV5Full(c *gc.C) {
original := s.allV5Map()
imported := s.importCloudInstance(c, original)
expected := s.testCloudInstance()
expected.VirtType_ = ""
expected.Version = 5
c.Assert(imported, jc.DeepEquals, expected)
}

Expand All @@ -272,5 +277,49 @@ func (s *CloudInstanceSerializationSuite) TestParsingV5Minimal(c *gc.C) {
expected := newCloudInstance(minimalCloudInstanceArgs())
expected.SetStatus(minimalStatusArgs())
expected.SetModificationStatus(minimalStatusArgs())
expected.Version = 5
c.Assert(imported, jc.DeepEquals, expected)
}

func (s *CloudInstanceSerializationSuite) allV6Map() map[string]interface{} {
return map[string]interface{}{
"version": 6,
"instance-id": "instance id",
"display-name": "foo",
"status": minimalStatusMap(),
"status-history": emptyStatusHistoryMap(),
"modification-status": minimalStatusMap(),
"architecture": "amd64",
"memory": 16 * gig,
"root-disk": 200 * gig,
"root-disk-source": "my-house",
"cores": 8,
"cpu-power": 4000,
"tags": []string{"much", "strong"},
"availability-zone": "everywhere",
"virt-type": "container",
"charm-profiles": []string{"much", "strong"},
}
}

func (s *CloudInstanceSerializationSuite) TestParsingV6Full(c *gc.C) {
original := s.allV6Map()
imported := s.importCloudInstance(c, original)
expected := s.testCloudInstance()
c.Assert(imported, jc.DeepEquals, expected)
}

func (s *CloudInstanceSerializationSuite) TestParsingV6Minimal(c *gc.C) {
original := map[string]interface{}{
"version": 6,
"instance-id": "instance id",
"status": minimalStatusMap(),
"status-history": emptyStatusHistoryMap(),
"modification-status": minimalStatusMap(),
}
imported := s.importCloudInstance(c, original)
expected := newCloudInstance(minimalCloudInstanceArgs())
expected.SetStatus(minimalStatusArgs())
expected.SetModificationStatus(minimalStatusArgs())
c.Assert(imported, jc.DeepEquals, expected)
}

0 comments on commit 8e38e6c

Please sign in to comment.