func getVirtualDiskUUID(newDevice types.BaseVirtualDevice) (string, error) { vd := newDevice.GetVirtualDevice() if b, ok := vd.Backing.(*types.VirtualDiskFlatVer2BackingInfo); ok { uuidWithNoHypens := strings.Replace(b.Uuid, "-", "", -1) return strings.ToLower(uuidWithNoHypens), nil } return "", ErrNoDiskUUIDFound }
// Returns formatted UUID for a virtual disk device. func getVirtualDiskUUID(newDevice types.BaseVirtualDevice) (string, error) { vd := newDevice.GetVirtualDevice() if b, ok := vd.Backing.(*types.VirtualDiskFlatVer2BackingInfo); ok { uuid := formatVirtualDiskUUID(b.Uuid) return uuid, nil } return "", ErrNoDiskUUIDFound }
// AssignController assigns a device to a controller. func (l VirtualDeviceList) AssignController(device types.BaseVirtualDevice, c types.BaseVirtualController) { d := device.GetVirtualDevice() d.ControllerKey = c.GetVirtualController().Key d.UnitNumber = new(int32) *d.UnitNumber = l.newUnitNumber(c) if d.Key == 0 { d.Key = -1 } }
func (l VirtualDeviceList) connectivity(device types.BaseVirtualDevice, v bool) error { c := device.GetVirtualDevice().Connectable if c == nil { return fmt.Errorf("%s is not connectable", l.Name(device)) } c.Connected = v c.StartConnected = v return nil }
// AssignController assigns a device to a controller. func (l VirtualDeviceList) AssignController(device types.BaseVirtualDevice, c types.BaseVirtualController) { d := device.GetVirtualDevice() d.ControllerKey = c.GetVirtualController().Key d.UnitNumber = l.newUnitNumber(c) if d.UnitNumber == 0 { d.UnitNumber = -1 // TODO: this field is annotated as omitempty } if d.Key == 0 { d.Key = -1 } }
// VirtualDeviceSlotNumber returns the PCI slot number of a device func VirtualDeviceSlotNumber(d types.BaseVirtualDevice) int32 { s := d.GetVirtualDevice().SlotInfo if s == nil { return NilSlot } if i, ok := s.(*types.VirtualDevicePciBusSlotInfo); ok { return i.PciSlotNumber } return NilSlot }
// AssignSlotNumber assigns a specific PCI slot number to the specified device. This ensures that // the slot is valid and not in use by anything else in the spec func (s *VirtualMachineConfigSpec) AssignSlotNumber(dev types.BaseVirtualDevice, known map[int32]bool) int32 { slot := VirtualDeviceSlotNumber(dev) if slot != NilSlot { return slot } // build the slots in use from the spec slots := s.CollectSlotNumbers(known) slot = findSlotNumber(slots) if slot != NilSlot { dev.GetVirtualDevice().SlotInfo = &types.VirtualDevicePciBusSlotInfo{PciSlotNumber: slot} } return slot }
// Name returns a stable, human-readable name for the given device func (l VirtualDeviceList) Name(device types.BaseVirtualDevice) string { var key string d := device.GetVirtualDevice() dtype := l.Type(device) switch dtype { case DeviceTypeEthernet: key = fmt.Sprintf("%d", d.UnitNumber-7) case DeviceTypeDisk: key = fmt.Sprintf("%d-%d", d.ControllerKey, d.UnitNumber) default: key = fmt.Sprintf("%d", d.Key) } return fmt.Sprintf("%s-%s", dtype, key) }
delete(c.containers, a) } // long id delete(c.containers, con.ID().String()) // short id delete(c.containers, con.ID().Truncate().String()) // name delete(c.containers, con.Name()) return endpoints, nil } var addEthernetCard = func(h *exec.Handle, s *Scope) (types.BaseVirtualDevice, error) { var devices object.VirtualDeviceList var d types.BaseVirtualDevice var dc types.BaseVirtualDeviceConfigSpec ctx := context.Background() dcs, err := h.Spec.FindNICs(ctx, s.Network()) if err != nil { return nil, err } for _, ds := range dcs { if ds.GetVirtualDeviceConfigSpec().Operation == types.VirtualDeviceConfigSpecOperationAdd { d = ds.GetVirtualDeviceConfigSpec().Device dc = ds break } }
func (s *VirtualMachineConfigSpec) addVirtualNIC(device types.BaseVirtualDevice) *VirtualMachineConfigSpec { device.GetVirtualDevice().Key = s.generateNextKey() return s.AddVirtualDevice(device) }
func TestContextAddContainer(t *testing.T) { ctx, err := NewContext(net.IPNet{IP: net.IPv4(172, 16, 0, 0), Mask: net.CIDRMask(12, 32)}, net.CIDRMask(16, 32)) if err != nil { t.Fatalf("NewContext() => (nil, %s), want (ctx, nil)", err) return } h := exec.NewContainer("foo") var devices object.VirtualDeviceList backing, _ := ctx.DefaultScope().Network().EthernetCardBackingInfo(context.TODO()) specWithEthCard := &spec.VirtualMachineConfigSpec{ VirtualMachineConfigSpec: &types.VirtualMachineConfigSpec{}, } var d types.BaseVirtualDevice if d, err = devices.CreateEthernetCard("vmxnet3", backing); err == nil { d.GetVirtualDevice().SlotInfo = &types.VirtualDevicePciBusSlotInfo{ PciSlotNumber: 1111, } devices = append(devices, d) var cs []types.BaseVirtualDeviceConfigSpec if cs, err = devices.ConfigSpec(types.VirtualDeviceConfigSpecOperationAdd); err == nil { specWithEthCard.DeviceChange = cs } } if err != nil { t.Fatalf(err.Error()) } aecErr := func(_ *exec.Handle, _ *Scope) (types.BaseVirtualDevice, error) { return nil, fmt.Errorf("error") } otherScope, err := ctx.NewScope(BridgeScopeType, "other", nil, net.IPv4(0, 0, 0, 0), nil, nil) if err != nil { t.Fatalf("failed to add scope") } hBar := exec.NewContainer(uid.New()) var tests = []struct { aec func(h *exec.Handle, s *Scope) (types.BaseVirtualDevice, error) h *exec.Handle s *spec.VirtualMachineConfigSpec scope string ip *net.IP err error }{ // nil handle {nil, nil, nil, "", nil, fmt.Errorf("")}, // scope not found {nil, h, nil, "foo", nil, ResourceNotFoundError{}}, // addEthernetCard returns error {aecErr, h, nil, "default", nil, fmt.Errorf("")}, // add a container {nil, h, nil, "default", nil, nil}, // container already added {nil, h, nil, "default", nil, nil}, {nil, hBar, specWithEthCard, "default", nil, nil}, {nil, hBar, nil, otherScope.Name(), nil, nil}, } origAEC := addEthernetCard defer func() { addEthernetCard = origAEC }() for i, te := range tests { // setup addEthernetCard = origAEC scopy := &spec.VirtualMachineConfigSpec{} if te.h != nil { te.h.SetSpec(te.s) if te.h.Spec != nil { *scopy = *te.h.Spec } } if te.aec != nil { addEthernetCard = te.aec } options := &AddContainerOptions{ Scope: te.scope, IP: te.ip, } err := ctx.AddContainer(te.h, options) if te.err != nil { // expect an error if err == nil { t.Fatalf("case %d: ctx.AddContainer(%v, %s, %s) => nil want err", i, te.h, te.scope, te.ip) } if reflect.TypeOf(err) != reflect.TypeOf(te.err) { t.Fatalf("case %d: ctx.AddContainer(%v, %s, %s) => (%v, %v) want (%v, %v)", i, te.h, te.scope, te.ip, err, te.err, err, te.err) } if _, ok := te.err.(DuplicateResourceError); ok { continue } // verify no device changes in the spec if te.s != nil { if len(scopy.DeviceChange) != len(h.Spec.DeviceChange) { t.Fatalf("case %d: ctx.AddContainer(%v, %s, %s) added device", i, te.h, te.scope, te.ip) } } continue } if err != nil { t.Fatalf("case %d: ctx.AddContainer(%v, %s, %s) => %s want nil", i, te.h, te.scope, te.ip, err) } // verify the container was not added to the scope s, _ := ctx.resolveScope(te.scope) if s != nil && te.h != nil { c := s.Container(uid.Parse(te.h.Container.ExecConfig.ID)) if c != nil { t.Fatalf("case %d: ctx.AddContainer(%v, %s, %s) added container", i, te.h, te.scope, te.ip) } } // spec should have a nic attached to the scope's network var dev types.BaseVirtualDevice dcs, err := te.h.Spec.FindNICs(context.TODO(), s.Network()) if len(dcs) != 1 { t.Fatalf("case %d: ctx.AddContainer(%v, %s, %s) more than one NIC added for scope %s", i, te.h, te.scope, te.ip, s.Network()) } dev = dcs[0].GetVirtualDeviceConfigSpec().Device if spec.VirtualDeviceSlotNumber(dev) == spec.NilSlot { t.Fatalf("case %d: ctx.AddContainer(%v, %s, %s) NIC added has nil pci slot", i, te.h, te.scope, te.ip) } // spec metadata should be updated with endpoint info ne, ok := te.h.ExecConfig.Networks[s.Name()] if !ok { t.Fatalf("case %d: ctx.AddContainer(%v, %s, %s) no network endpoint info added", i, te.h, te.scope, te.ip) } if spec.VirtualDeviceSlotNumber(dev) != atoiOrZero(ne.ID) { t.Fatalf("case %d; ctx.AddContainer(%v, %s, %s) => ne.ID == %d, want %d", i, te.h, te.scope, te.ip, atoiOrZero(ne.ID), spec.VirtualDeviceSlotNumber(dev)) } if ne.Network.Name != s.Name() { t.Fatalf("case %d; ctx.AddContainer(%v, %s, %s) => ne.NetworkName == %s, want %s", i, te.h, te.scope, te.ip, ne.Network.Name, s.Name()) } if te.ip != nil && !te.ip.Equal(ne.Static.IP) { t.Fatalf("case %d; ctx.AddContainer(%v, %s, %s) => ne.Static.IP == %s, want %s", i, te.h, te.scope, te.ip, ne.Static.IP, te.ip) } if te.ip == nil && ne.Static != nil { t.Fatalf("case %d; ctx.AddContainer(%v, %s, %s) => ne.Static.IP == %s, want %s", i, te.h, te.scope, te.ip, ne.Static.IP, net.IPv4zero) } } }