@Override
	public String createMachine( TargetHandlerParameters parameters ) throws TargetException {

		this.logger.fine( "Creating a new machine." );

		// For IaaS, we only expect root instance names to be passed
		if( InstanceHelpers.countInstances( parameters.getScopedInstancePath()) > 1 )
			throw new TargetException( "Only root instances can be passed in arguments." );

		// Validate all the properties here
		String rootInstanceName = InstanceHelpers.findRootInstancePath( parameters.getScopedInstancePath());
		Map<String,String> targetProperties = parameters.getTargetProperties();
		validateAll( targetProperties, parameters.getApplicationName(), rootInstanceName );

		// Prepare the work
		NovaApi novaApi = OpenstackIaasHandler.novaApi( targetProperties );
		String zoneName = findZoneName( novaApi, targetProperties );
		String vmName = parameters.getApplicationName() + "." + rootInstanceName;

		// Find flavor and image IDs
		String flavorId = null;
		String flavorName = targetProperties.get( OpenstackIaasHandler.FLAVOR_NAME );
		for( Resource res : novaApi.getFlavorApiForZone( zoneName ).list().concat()) {
			if( res.getName().equalsIgnoreCase( flavorName )) {
				flavorId = res.getId();
				break;
			}
		}

		if( flavorId == null )
			throw new TargetException( "No flavor named '" + flavorName + "' was found." );

		String imageId = null;
		String imageName = targetProperties.get( OpenstackIaasHandler.IMAGE_NAME );
		for( Resource res : novaApi.getImageApiForZone( zoneName ).list().concat()) {
			if( res.getName().equalsIgnoreCase( imageName )) {
				imageId = res.getId();
				break;
			}
		}

		if( imageId == null )
			throw new TargetException( "No image named '" + imageName + "' was found." );

		// Prepare the server creation
		Map<String,String> metadata = new HashMap<>(3);
		metadata.put( "Application Name", parameters.getApplicationName());
		metadata.put( "Root Instance Name", rootInstanceName );
		metadata.put( "Created by", "Roboconf" );

		try {
			String userData = UserDataHelpers.writeUserDataAsString(
					parameters.getMessagingProperties(),
					parameters.getDomain(),
					parameters.getApplicationName(),
					rootInstanceName );

			CreateServerOptions options = CreateServerOptions.Builder
					.keyPairName( targetProperties.get( OpenstackIaasHandler.KEY_PAIR ))
					.securityGroupNames( targetProperties.get( OpenstackIaasHandler.SECURITY_GROUP ))
					.userData( userData.getBytes( StandardCharsets.UTF_8 ))
					.metadata( metadata );

			String networkId = targetProperties.get( OpenstackIaasHandler.NETWORK_ID );
			if( ! Utils.isEmptyOrWhitespaces( networkId ))
				options = options.networks( networkId );

			ServerCreated server = novaApi.getServerApiForZone( zoneName ).create( vmName, imageId, flavorId, options);
			String machineId = server.getId();
			novaApi.close();

			return machineId;

		} catch( Exception e ) {
			throw new TargetException( e );
		}
	}
@ Override public String createMachine ( TargetHandlerParameters parameters ) throws TargetException { this . logger . fine ( "Creating a new machine." ) ; if ( InstanceHelpers . countInstances ( parameters . getScopedInstancePath ( ) ) > 1 ) throw new TargetException ( "Only root instances can be passed in arguments." ) ; String rootInstanceName = InstanceHelpers . findRootInstancePath ( parameters . getScopedInstancePath ( ) ) ; Map < String , String > targetProperties = parameters . getTargetProperties ( ) ; validateAll ( targetProperties , parameters . getApplicationName ( ) , rootInstanceName ) ; NovaApi novaApi = OpenstackIaasHandler . novaApi ( targetProperties ) ; String zoneName = findZoneName ( novaApi , targetProperties ) ; String vmName = parameters . getApplicationName ( ) + "." + rootInstanceName ; String flavorId = null ; String flavorName = targetProperties . get ( OpenstackIaasHandler . FLAVOR_NAME ) ; for ( Resource res : novaApi . getFlavorApiForZone ( zoneName ) . list ( ) . concat ( ) ) { if ( res . getName ( ) . equalsIgnoreCase ( flavorName ) ) { flavorId = res . getId ( ) ; break ; } } if ( flavorId == null ) throw new TargetException ( "No flavor named '" + flavorName + "' was found." ) ; String imageId = null ; String imageName = targetProperties . get ( OpenstackIaasHandler . IMAGE_NAME ) ; for ( Resource res : novaApi . getImageApiForZone ( zoneName ) . list ( ) . concat ( ) ) { if ( res . getName ( ) . equalsIgnoreCase ( imageName ) ) { imageId = res . getId ( ) ; break ; } } if ( imageId == null ) throw new TargetException ( "No image named '" + imageName + "' was found." ) ; Map < String , String > metadata = new HashMap < > ( 3 ) ; metadata . put ( "Application Name" , parameters . getApplicationName ( ) ) ; metadata . put ( "Root Instance Name" , rootInstanceName ) ; metadata . put ( "Created by" , "Roboconf" ) ; try { String userData = UserDataHelpers . writeUserDataAsString ( parameters . getMessagingProperties ( ) , parameters . getDomain ( ) , parameters . getApplicationName ( ) , rootInstanceName ) ; CreateServerOptions options = CreateServerOptions . Builder . keyPairName ( targetProperties . get ( OpenstackIaasHandler . KEY_PAIR ) ) . securityGroupNames ( targetProperties . get ( OpenstackIaasHandler . SECURITY_GROUP ) ) . userData ( userData . getBytes ( StandardCharsets . UTF_8 ) ) . metadata ( metadata ) ; String networkId = targetProperties . get ( OpenstackIaasHandler . NETWORK_ID ) ; if ( ! Utils . isEmptyOrWhitespaces ( networkId ) ) options = options . networks ( networkId ) ; ServerCreated server = novaApi . getServerApiForZone ( zoneName ) . create ( vmName , imageId , flavorId , options ) ; String machineId = server . getId ( ) ; novaApi . close ( ) ; return machineId ; } catch ( Exception e ) { throw new TargetException ( e ) ; } }
/*
(non-Javadoc)
@see net.roboconf.target.api.TargetHandler
#createMachine(net.roboconf.target.api.TargetHandlerParameters)