Esempio n. 1
0
func (m *InstanceManager) LocateInstanceIp(
	network *types.VirtualNetwork, instanceUID string, nic *types.VirtualMachineInterface) *types.InstanceIp {
	tenant := nic.GetFQName()[len(nic.GetFQName())-2]
	name := makeInstanceIpName(tenant, nic.GetName())
	obj, err := m.client.FindByName("instance-ip", name)
	if err == nil {
		// TODO(prm): ensure that attributes are as expected
		return obj.(*types.InstanceIp)
	}

	address, err := m.allocator.LocateIpAddress(instanceUID)
	if err != nil {
		return nil
	}

	// Create InstanceIp
	ipObj := new(types.InstanceIp)
	ipObj.SetName(name)
	ipObj.AddVirtualNetwork(network)
	ipObj.AddVirtualMachineInterface(nic)
	ipObj.SetInstanceIpAddress(address)
	ipObj.SetInstanceIpMode("active-active")
	err = m.client.Create(ipObj)
	if err != nil {
		glog.Errorf("Create instance-ip %s for %s: %v", address, nic.GetName(), err)
		return nil
	}
	obj, err = m.client.FindByUuid(ipObj.GetType(), ipObj.GetUuid())
	if err != nil {
		glog.Errorf("Get instance-ip %s for %s: %v", address, ipObj.GetUuid(), err)
		return nil
	}
	return ipObj
}
func (a *AddressAllocatorImpl) allocateIpAddress(uid string) (contrail.IObject, error) {
	ipObj := new(types.InstanceIp)
	ipObj.SetName(uid)
	ipObj.AddVirtualNetwork(a.network)
	err := a.client.Create(ipObj)
	if err != nil {
		glog.Errorf("Create InstanceIp %s: %v", uid, err)
		return nil, err
	}
	obj, err := a.client.FindByUuid("instance-ip", ipObj.GetUuid())
	if err != nil {
		glog.Errorf("Get InstanceIp %s: %v", uid, err)
		return nil, err
	}
	return obj, err
}