Search in sources :

Example 11 with UserDataServiceProvider

use of com.cloud.network.element.UserDataServiceProvider in project cloudstack by apache.

the class UserVmManagerTest method testApplyUserDataSuccessful.

@Test
public void testApplyUserDataSuccessful() throws Exception {
    UserVm userVm = mock(UserVm.class);
    lenient().when(userVm.getId()).thenReturn(1L);
    when(_nicMock.getNetworkId()).thenReturn(2L);
    when(_networkMock.getNetworkOfferingId()).thenReturn(3L);
    when(_networkDao.findById(2L)).thenReturn(_networkMock);
    UserDataServiceProvider userDataServiceProvider = mock(UserDataServiceProvider.class);
    when(userDataServiceProvider.saveUserData(any(Network.class), any(NicProfile.class), any(VirtualMachineProfile.class))).thenReturn(true);
    // Userdata support with implementing element
    when(_networkModel.areServicesSupportedByNetworkOffering(3L, Service.UserData)).thenReturn(true);
    when(_networkModel.getUserDataUpdateProvider(_networkMock)).thenReturn(userDataServiceProvider);
    assertTrue(_userVmMgr.applyUserData(HypervisorType.KVM, userVm, _nicMock));
}
Also used : UserVm(com.cloud.uservm.UserVm) Network(com.cloud.network.Network) UserDataServiceProvider(com.cloud.network.element.UserDataServiceProvider) Test(org.junit.Test)

Example 12 with UserDataServiceProvider

use of com.cloud.network.element.UserDataServiceProvider in project cloudstack by apache.

the class RulesManagerImpl method applyUserDataIfNeeded.

protected void applyUserDataIfNeeded(long vmId, Network network, Nic guestNic) throws ResourceUnavailableException {
    UserDataServiceProvider element = null;
    try {
        element = _networkModel.getUserDataUpdateProvider(network);
    } catch (UnsupportedServiceException ex) {
        s_logger.info(String.format("%s is not supported by network %s, skipping.", Service.UserData.getName(), network));
        return;
    }
    if (element == null) {
        s_logger.error("Can't find network element for " + Service.UserData.getName() + " provider needed for UserData update");
    } else {
        UserVmVO vm = _vmDao.findById(vmId);
        try {
            VMTemplateVO template = _templateDao.findByIdIncludingRemoved(vm.getTemplateId());
            NicProfile nicProfile = new NicProfile(guestNic, network, null, null, null, _networkModel.isSecurityGroupSupportedInNetwork(network), _networkModel.getNetworkTag(template.getHypervisorType(), network));
            VirtualMachineProfile vmProfile = new VirtualMachineProfileImpl(vm);
            if (!element.saveUserData(network, nicProfile, vmProfile)) {
                s_logger.error("Failed to update userdata for vm " + vm + " and nic " + guestNic);
            }
        } catch (Exception e) {
            s_logger.error("Failed to update userdata for vm " + vm + " and nic " + guestNic + " due to " + e.getMessage(), e);
        }
    }
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) VirtualMachineProfileImpl(com.cloud.vm.VirtualMachineProfileImpl) UnsupportedServiceException(com.cloud.exception.UnsupportedServiceException) VMTemplateVO(com.cloud.storage.VMTemplateVO) NicProfile(com.cloud.vm.NicProfile) VirtualMachineProfile(com.cloud.vm.VirtualMachineProfile) UserDataServiceProvider(com.cloud.network.element.UserDataServiceProvider) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) NetworkRuleConflictException(com.cloud.exception.NetworkRuleConflictException) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) UnsupportedServiceException(com.cloud.exception.UnsupportedServiceException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Aggregations

UserDataServiceProvider (com.cloud.network.element.UserDataServiceProvider)12 Network (com.cloud.network.Network)8 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)8 PhysicalNetwork (com.cloud.network.PhysicalNetwork)6 VMTemplateVO (com.cloud.storage.VMTemplateVO)6 Capability (com.cloud.network.Network.Capability)2 DhcpServiceProvider (com.cloud.network.element.DhcpServiceProvider)2 UserVm (com.cloud.uservm.UserVm)2 NicProfile (com.cloud.vm.NicProfile)2 Test (org.junit.Test)2 IllegalVirtualMachineException (com.cloud.exception.IllegalVirtualMachineException)1 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)1 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 NetworkRuleConflictException (com.cloud.exception.NetworkRuleConflictException)1 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)1 UnsupportedServiceException (com.cloud.exception.UnsupportedServiceException)1 NetworkVO (com.cloud.network.dao.NetworkVO)1 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)1 ConfigDriveNetworkElement (com.cloud.network.element.ConfigDriveNetworkElement)1 DnsServiceProvider (com.cloud.network.element.DnsServiceProvider)1