instance.setName( n.textValue());
instance.setName( ModelUtils.getPropertyValue( currentBlock, ParsingConstants.PROPERTY_INSTANCE_NAME )); instance.channels.addAll( ModelUtils.getPropertyValues( currentBlock, ParsingConstants.PROPERTY_INSTANCE_CHANNELS ));
@Override public void execute() throws CommandException { Instance instance = resolveInstance( this.instr, this.instr.getInstancePath(), false ); Instance scopedInstance = InstanceHelpers.findScopedInstance( instance ); if( scopedInstance.getStatus() != InstanceStatus.NOT_DEPLOYED ) throw new CommandException( "Only instances that are not yet managed by an agent can be renamed." ); instance.setName( this.instr.getNewInstanceName()); } }
@Test public void testInstance() { Instance inst = new Instance(); Iterator<ModelError> iterator = RuntimeModelValidator.validate( inst ).iterator(); Assert.assertEquals( ErrorCode.RM_EMPTY_INSTANCE_NAME, iterator.next().getErrorCode()); Assert.assertEquals( ErrorCode.RM_EMPTY_INSTANCE_COMPONENT, iterator.next().getErrorCode()); Assert.assertFalse( iterator.hasNext()); inst.setName( "?my instance?" ); iterator = RuntimeModelValidator.validate( inst ).iterator(); Assert.assertEquals( ErrorCode.RM_INVALID_INSTANCE_NAME, iterator.next().getErrorCode()); Assert.assertEquals( ErrorCode.RM_EMPTY_INSTANCE_COMPONENT, iterator.next().getErrorCode()); Assert.assertFalse( iterator.hasNext()); inst.setName( "my-instance" ); iterator = RuntimeModelValidator.validate( inst ).iterator(); Assert.assertEquals( ErrorCode.RM_EMPTY_INSTANCE_COMPONENT, iterator.next().getErrorCode()); Assert.assertFalse( iterator.hasNext()); inst.setComponent( new Component( "comp" )); Assert.assertEquals( 0, RuntimeModelValidator.validate( inst ).size()); inst.setName( "my instance" ); Assert.assertEquals( 0, RuntimeModelValidator.validate( inst ).size()); inst.overriddenExports.put( "inst.value", "whatever" ); iterator = RuntimeModelValidator.validate( inst ).iterator(); Assert.assertEquals( ErrorCode.RM_MAGIC_INSTANCE_VARIABLE, iterator.next().getErrorCode()); Assert.assertFalse( iterator.hasNext()); }
@Test public void testHaveSamePath() { Instance instance1 = new Instance( "inst1" ); Assert.assertTrue( InstanceHelpers.haveSamePath( instance1, instance1 )); Instance instance2 = new Instance( "inst2" ); Assert.assertFalse( InstanceHelpers.haveSamePath( instance1, instance2 )); instance2.setName( "inst1" ); Assert.assertTrue( InstanceHelpers.haveSamePath( instance1, instance2 )); }
@Test public void testComputeInstancePath() { Instance instance1 = new Instance(); Assert.assertEquals( "/", InstanceHelpers.computeInstancePath( instance1 )); instance1.setName( "inst1" ); Assert.assertEquals( "/inst1", InstanceHelpers.computeInstancePath( instance1 )); Instance instance2 = new Instance( "inst2" ); Assert.assertEquals( "/inst2", InstanceHelpers.computeInstancePath( instance2 )); instance1.getChildren().add( instance2 ); instance2.setParent( instance1 ); Assert.assertEquals( "/inst1", InstanceHelpers.computeInstancePath( instance1 )); Assert.assertEquals( "/inst1/inst2", InstanceHelpers.computeInstancePath( instance2 )); }
@Override public void execute() throws CommandException { // Resolve runtime structure Instance rootInstance = resolveInstance( this.instr, this.instr.getReplicatedInstancePath(), true ); ManagedApplication ma = resolveManagedApplication( this.manager, this.instr ); // Verify we can create new VMs in the model CreateInstanceCommandExecution.verify( this.executionContext, rootInstance.getComponent()); try { // Copy the instance Instance copy = InstanceHelpers.replicateInstance( rootInstance ); copy.setName( this.instr.getNewInstanceName()); this.manager.instancesMngr().addInstance( ma, null, copy ); // Associate this new instance with the same target, if it has one String targetId = this.manager.targetsMngr().findTargetId( ma.getApplication(), "/" + rootInstance.getName(), true ); if( targetId != null ) this.manager.targetsMngr().associateTargetWith( targetId, ma.getApplication(), "/" + copy.getName()); // Register meta-data CreateInstanceCommandExecution.update( this.executionContext, copy ); } catch( Exception e ) { throw new CommandException( e ); } } }
Assert.assertEquals( 3, InstanceHelpers.getAllInstances( app ).size()); instWithInvalidName.setName( "whatever" ); Assert.assertTrue( InstanceHelpers.tryToInsertChildInstance( app, vmInstance, instWithInvalidName )); Assert.assertEquals( 4, InstanceHelpers.getAllInstances( app ).size());