Java Code Examples for org.apache.brooklyn.api.entity.EntitySpec#configure()

The following examples show how to use org.apache.brooklyn.api.entity.EntitySpec#configure() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InfrastructureDeploymentTestCaseTest.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoEntitySpec() {
    EntitySpec<TestInfrastructure> infrastructureSpec = EntitySpec.create(TestInfrastructure.class);
    infrastructureSpec.configure(DEPLOYMENT_LOCATION_SENSOR, infrastructureLoc);

    InfrastructureDeploymentTestCase infrastructureDeploymentTestCase = app.createAndManageChild(EntitySpec.create(InfrastructureDeploymentTestCase.class));
    infrastructureDeploymentTestCase.config().set(InfrastructureDeploymentTestCase.INFRASTRUCTURE_SPEC, infrastructureSpec);
    infrastructureDeploymentTestCase.config().set(InfrastructureDeploymentTestCase.DEPLOYMENT_LOCATION_SENSOR_NAME, DEPLOYMENT_LOCATION_SENSOR.getName());

    try {
        app.start(ImmutableList.of(app.newSimulatedLocation()));
        Asserts.shouldHaveFailedPreviously();
    } catch (Throwable throwable) {
        Asserts.expectedFailureContains(throwable, "entity.specs", "List", "not configured");
    }

    assertThat(infrastructureDeploymentTestCase.sensors().get(SERVICE_UP)).isFalse();
}
 
Example 2
Source File: DynamicToyMySqlEntityChef.java    From brooklyn-library with Apache License 2.0 6 votes vote down vote up
protected static EntitySpec<? extends Entity> specBase() {
        EntitySpec<ChefEntity> spec = EntitySpec.create(ChefEntity.class);
        
        ChefConfigs.addToLaunchRunList(spec, "mysql::server");
        spec.configure(PID_FILE, "/var/run/mysqld/mysql*.pid");
        // init.d service name is sometimes mysql, sometimes mysqld, depending ubuntu/centos
        // we use pid file above instead, but this (with the right name) could be used:
//        spec.configure(SERVICE_NAME, "mysql");
        
        // chef mysql fails on first run but works on second if switching between server and solo modes
        spec.configure(ChefConfig.CHEF_RUN_CONVERGE_TWICE, true);

        // only used for solo, but safely ignored for knife
        ChefConfigs.addToCookbooksFromGithub(spec, "mysql", "build-essential", "openssl");
        // we always need dependent cookbooks set, and mysql requires password set
        // (TODO for knife we might wish to prefer things from the server)
        ChefConfigs.addLaunchAttributes(spec, MutableMap.of("mysql",  
                MutableMap.of()
                .add("server_root_password", "MyPassword")
                .add("server_debian_password", "MyPassword")
                .add("server_repl_password", "MyPassword")
            ));
        
        return spec;
    }
 
Example 3
Source File: InfrastructureDeploymentTestCaseTest.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoDeploymentLocation() {
    EntitySpec<TestInfrastructure> infrastructureSpec = EntitySpec.create(TestInfrastructure.class);
    infrastructureSpec.configure(DEPLOYMENT_LOCATION_SENSOR, infrastructureLoc);

    List<EntitySpec<? extends Startable>> testSpecs = ImmutableList.<EntitySpec<? extends Startable>>of(EntitySpec.create(BasicApplication.class));

    InfrastructureDeploymentTestCase infrastructureDeploymentTestCase = app.createAndManageChild(EntitySpec.create(InfrastructureDeploymentTestCase.class));
    infrastructureDeploymentTestCase.config().set(InfrastructureDeploymentTestCase.INFRASTRUCTURE_SPEC, infrastructureSpec);
    infrastructureDeploymentTestCase.config().set(InfrastructureDeploymentTestCase.ENTITY_SPEC_TO_DEPLOY, testSpecs);

    try {
        app.start(ImmutableList.of(app.newSimulatedLocation()));
        Asserts.shouldHaveFailedPreviously();
    } catch (Throwable throwable) {
        Asserts.expectedFailure(throwable);
    }

    assertThat(infrastructureDeploymentTestCase.sensors().get(SERVICE_UP)).isFalse();
}
 
Example 4
Source File: MongoDBShardClusterImpl.java    From brooklyn-library with Apache License 2.0 5 votes vote down vote up
@Override
protected EntitySpec<?> getMemberSpec() {
    EntitySpec<?> result = super.getMemberSpec();
    if (result == null)
        result = EntitySpec.create(MongoDBReplicaSet.class);
    result.configure(DynamicClusterImpl.INITIAL_SIZE, getConfig(MongoDBShardedDeployment.SHARD_REPLICASET_SIZE));
    return result;
}
 
Example 5
Source File: BrooklynAppUnitTestSupport.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
protected void setUpApp() {
    EntitySpec<TestApplication> appSpec = EntitySpec.create(TestApplication.class);
    if (shouldSkipOnBoxBaseDirResolution()!=null)
        appSpec.configure(BrooklynConfigKeys.SKIP_ON_BOX_BASE_DIR_RESOLUTION, shouldSkipOnBoxBaseDirResolution());
    
    app = mgmt.getEntityManager().createEntity(appSpec);
}
 
Example 6
Source File: InfrastructureDeploymentTestCaseTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultipleSpec() {
    EntitySpec<TestInfrastructure> infrastructureSpec = EntitySpec.create(TestInfrastructure.class);
    infrastructureSpec.configure(DEPLOYMENT_LOCATION_SENSOR, infrastructureLoc);

    List<EntitySpec<? extends Startable>> testSpecs = ImmutableList.<EntitySpec<? extends Startable>>of
            (EntitySpec.create(BasicApplication.class),
                    (EntitySpec.create(BasicApplication.class)));

    InfrastructureDeploymentTestCase infrastructureDeploymentTestCase = app.createAndManageChild(EntitySpec.create(InfrastructureDeploymentTestCase.class));
    infrastructureDeploymentTestCase.config().set(InfrastructureDeploymentTestCase.INFRASTRUCTURE_SPEC, infrastructureSpec);
    infrastructureDeploymentTestCase.config().set(InfrastructureDeploymentTestCase.ENTITY_SPEC_TO_DEPLOY, testSpecs);
    infrastructureDeploymentTestCase.config().set(InfrastructureDeploymentTestCase.DEPLOYMENT_LOCATION_SENSOR_NAME, DEPLOYMENT_LOCATION_SENSOR.getName());

    app.start(ImmutableList.of(loc));

    assertThat(infrastructureDeploymentTestCase.sensors().get(SERVICE_UP)).isTrue();
    assertThat(infrastructureDeploymentTestCase.getChildren().size()).isEqualTo(3);

    boolean seenInfrastructure = false;
    int entitiesSeen = 0;

    for (Entity entity : infrastructureDeploymentTestCase.getChildren()) {
        assertThat(entity.sensors().get(SERVICE_UP)).isTrue();
        assertThat(entity.getLocations().size()).isEqualTo(1);

        if (entity instanceof TestInfrastructure && !seenInfrastructure) {
            assertThat(entity.getLocations().iterator().next().getDisplayName()).isEqualTo(LOC_NAME);
            seenInfrastructure = true;
        } else if (entity instanceof BasicApplication) {
            assertThat(entity.getLocations().iterator().next().getDisplayName()).isEqualTo(INFRASTRUCTURE_LOC_NAME);
            entitiesSeen++;
        } else {
            fail("Unknown child of InfrastructureDeploymentTestCase");
        }
    }

    assertThat(seenInfrastructure).isTrue();
    assertThat(entitiesSeen).isEqualTo(2);
}
 
Example 7
Source File: EntityManagementUtils.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/** Modifies the child so it includes the inessential setup of its parent,
 * for use when unwrapping specific children, but a name or other item may have been set on the parent.
 * See {@link #WRAPPER_APP_MARKER}. */
private static void mergeWrapperParentSpecToChildEntity(EntitySpec<? extends Application> wrapperParent, EntitySpec<?> wrappedChild) {
    if (Strings.isNonEmpty(wrapperParent.getDisplayName())) {
        wrappedChild.displayName(wrapperParent.getDisplayName());
    }
    
    wrappedChild.locationSpecs(wrapperParent.getLocationSpecs());
    wrappedChild.locations(wrapperParent.getLocations());
    
    if (!wrapperParent.getParameters().isEmpty()) {
        wrappedChild.parametersAdd(wrapperParent.getParameters());
    }

    wrappedChild.catalogItemIdAndSearchPath(wrapperParent.getCatalogItemId(), wrapperParent.getCatalogItemIdSearchPath());

    // NB: this clobber's child config wherever they conflict; might prefer to deeply merge maps etc
    // (or maybe even prevent the merge in these cases; 
    // not sure there is a compelling reason to have config on a pure-wrapper parent)
    Map<ConfigKey<?>, Object> configWithoutWrapperMarker =
        Maps.filterKeys(wrapperParent.getConfig(),
            Predicates.not(Predicates.<ConfigKey<?>>equalTo(EntityManagementUtils.WRAPPER_APP_MARKER)));
    wrappedChild.configure(configWithoutWrapperMarker);
    wrappedChild.configure(wrapperParent.getFlags());
    
    // copying tags to all entities may be something the caller wants to control,
    // e.g. if we're adding multiple, the caller might not want to copy the parent
    // (the BrooklynTags.YAML_SPEC tag will include the parents source including siblings),
    // but OTOH they might because otherwise the parent's tags might get lost.
    // also if we are unwrapping multiple registry references we will get the YAML_SPEC for each;
    // putting the parent's tags first however causes the preferred (outer) one to be retrieved first.
    wrappedChild.tagsReplace(MutableList.copyOf(wrapperParent.getTags()).appendAll(wrappedChild.getTags()));
}
 
Example 8
Source File: EntityNameTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void testDefaultDisplayNameUsesDeferredSupplier() {
    EntitySpec<TestEntity> spec = EntitySpec.create(TestEntity.class);
    spec.configure((ConfigKey)AbstractEntity.DEFAULT_DISPLAY_NAME, new DeferredSupplier<String>() {
        @Override public String get() {
            return "myDefaultName";
        }});
    TestEntity entity = app.addChild(spec);
    assertEquals(entity.getDisplayName(), "myDefaultName");
}
 
Example 9
Source File: RebindTestFixtureWithApp.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Override
protected TestApplication createApp() {
    EntitySpec<TestApplication> spec = EntitySpec.create(TestApplication.class, TestApplicationNoEnrichersImpl.class);
    if (shouldSkipOnBoxBaseDirResolution()!=null) {
        spec.configure(BrooklynConfigKeys.SKIP_ON_BOX_BASE_DIR_RESOLUTION, shouldSkipOnBoxBaseDirResolution());
    }
    return origManagementContext.getEntityManager().createEntity(spec);
}
 
Example 10
Source File: ZooKeeperEnsembleImpl.java    From brooklyn-library with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the default {@link #MEMBER_SPEC} to describe the ZooKeeper nodes.
 * Overwrites any value configured for {@link ZooKeeperNode#MY_ID} to use
 * the value given by {@link ZooKeeperEnsemble#NODE_ID_SUPPLIER}.
 */
@Override
protected EntitySpec<?> getMemberSpec() {
    EntitySpec<?> spec = getConfig(MEMBER_SPEC, EntitySpec.create(ZooKeeperNode.class));
    spec.configure(ZooKeeperNode.MY_ID, config().get(ZooKeeperEnsemble.NODE_ID_SUPPLIER).get());
    return spec;
}
 
Example 11
Source File: MongoDBAuthenticationUtils.java    From brooklyn-library with Apache License 2.0 5 votes vote down vote up
/**
 * Configures the {@code spec} with authentication configuration from {@code source}
 */
public static void setAuthenticationConfig(EntitySpec<?> spec, Entity source) {
    if (MongoDBAuthenticationUtils.usesAuthentication(source)) {
        spec.configure(MongoDBAuthenticationMixins.MONGODB_KEYFILE_CONTENTS, source.config().get(MongoDBAuthenticationMixins.MONGODB_KEYFILE_CONTENTS));
        spec.configure(MongoDBAuthenticationMixins.MONGODB_KEYFILE_URL, source.config().get(MongoDBAuthenticationMixins.MONGODB_KEYFILE_URL));
        spec.configure(MongoDBAuthenticationMixins.ROOT_USERNAME, source.config().get(MongoDBAuthenticationMixins.ROOT_USERNAME));
        spec.configure(MongoDBAuthenticationMixins.ROOT_PASSWORD, getRootPassword(source));
    }
}
 
Example 12
Source File: ChefConfigs.java    From brooklyn-library with Apache License 2.0 4 votes vote down vote up
/** replaces the attributes underneath the rootAttribute parameter with the given value;
 * see {@link #addLaunchAttributesMap(EntitySpec, Map)} for richer functionality */
public static void setLaunchAttribute(EntitySpec<?> entity, String rootAttribute, Object value) {
    entity.configure(ChefConfig.CHEF_LAUNCH_ATTRIBUTES.subKey(rootAttribute), value);
}
 
Example 13
Source File: PostgreSqlSpecs.java    From brooklyn-library with Apache License 2.0 4 votes vote down vote up
/** Requires {@code knife}. */
public static EntitySpec<PostgreSqlNode> specChef() {
    EntitySpec<PostgreSqlNode> spec = EntitySpec.create(PostgreSqlNode.class, PostgreSqlNodeChefImplFromScratch.class);
    spec.configure(ChefConfig.CHEF_MODE, ChefModes.KNIFE);
    return spec;
}
 
Example 14
Source File: DynamicToyMySqlEntityChef.java    From brooklyn-library with Apache License 2.0 4 votes vote down vote up
public static EntitySpec<? extends Entity> specSolo() {
    EntitySpec<? extends Entity> spec = specBase();
    spec.configure(ChefConfig.CHEF_MODE, ChefConfig.ChefModes.SOLO);
    log.debug("Created entity spec for MySql: "+spec);
    return spec;
}
 
Example 15
Source File: DynamicToyMySqlEntityChef.java    From brooklyn-library with Apache License 2.0 4 votes vote down vote up
public static EntitySpec<? extends Entity> specKnife() {
    EntitySpec<? extends Entity> spec = specBase();
    spec.configure(ChefConfig.CHEF_MODE, ChefConfig.ChefModes.KNIFE);
    log.debug("Created entity spec for MySql: "+spec);
    return spec;
}
 
Example 16
Source File: SaltConfigs.java    From brooklyn-library with Apache License 2.0 4 votes vote down vote up
public static void addToRunList(EntitySpec<?> entity, String...states) {
    for (String state : states) {
        entity.configure(SaltConfig.START_STATES, SetModifications.addItem(state));
    }
}
 
Example 17
Source File: SaltConfigs.java    From brooklyn-library with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void addLaunchAttributes(EntitySpec<?> entity, Map<?,?> attributesMap) {
    entity.configure(SaltConfig.SALT_SSH_LAUNCH_ATTRIBUTES, MapModifications.add((Map)attributesMap));
}
 
Example 18
Source File: SaltConfigs.java    From brooklyn-library with Apache License 2.0 4 votes vote down vote up
public static void addToFormulas(EntitySpec<?> entity, String formulaUrl) {
    entity.configure(SaltConfig.SALT_FORMULAS, SetModifications.addItem(formulaUrl));
}
 
Example 19
Source File: BrooklynComponentTemplateResolver.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private <T extends Entity> void populateSpec(EntitySpec<T> spec, Set<String> encounteredRegisteredTypeIds) {
    String name, source=null, templateId=null, planId=null;
    if (template.isPresent()) {
        name = template.get().getName();
        templateId = template.get().getId();
        source = template.get().getSourceCode();
    } else {
        name = (String)attrs.getStringKey("name");
    }
    planId = (String)attrs.getStringKey("id");
    if (planId==null)
        planId = (String) attrs.getStringKey(BrooklynCampConstants.PLAN_ID_FLAG);

    Object childrenObj = attrs.getStringKey(BrooklynCampReservedKeys.BROOKLYN_CHILDREN);
    if (childrenObj != null) {
        Iterable<Map<String,?>> children = (Iterable<Map<String,?>>)childrenObj;
        for (Map<String,?> childAttrs : children) {
            BrooklynComponentTemplateResolver entityResolver = BrooklynComponentTemplateResolver.Factory.newInstance(loader, childAttrs);
            // encounteredRegisteredTypeIds must contain the items currently being loaded (the dependency chain),
            // but not parent items in this type already resolved (this is because an item's definition should
            // not include itself here, as a defined child, as that would create an endless loop; 
            // use in a member spec is fine)
            EntitySpec<? extends Entity> childSpec = entityResolver.resolveSpec(encounteredRegisteredTypeIds);
            spec.child(EntityManagementUtils.unwrapEntity(childSpec));
        }
    }

    if (source!=null) {
        spec.tag(BrooklynTags.newYamlSpecTag(source));
    }

    if (!Strings.isBlank(name))
        spec.displayName(name);
    if (templateId != null)
        spec.configure(BrooklynCampConstants.TEMPLATE_ID, templateId);
    if (planId != null)
        spec.configure(BrooklynCampConstants.PLAN_ID, planId);

    List<LocationSpec<?>> locations = new BrooklynYamlLocationResolver(mgmt).resolveLocations(attrs.getAllConfig(), true);
    if (locations != null) {
        // override locations defined in the type if locations are specified here
        // empty list can be used by caller to clear, so they are inherited
        spec.clearLocations();
        spec.locationSpecs(locations);
    }

    decorateSpec(spec, encounteredRegisteredTypeIds);
}
 
Example 20
Source File: ControlledDynamicWebAppClusterImpl.java    From brooklyn-library with Apache License 2.0 4 votes vote down vote up
@Override
public void init() {
    super.init();
    
    ConfigToAttributes.apply(this, MEMBER_SPEC);
    ConfigToAttributes.apply(this, CONTROLLER);
    ConfigToAttributes.apply(this, CONTROLLER_SPEC);
    ConfigToAttributes.apply(this, WEB_CLUSTER_SPEC);
    ConfigToAttributes.apply(this, CONTROLLED_GROUP);
    
    EntitySpec<? extends WebAppService> webServerSpec = getAttribute(MEMBER_SPEC);
    if (webServerSpec == null) {
        log.debug("creating default web server spec for {}", this);
        webServerSpec = EntitySpec.create(TomcatServer.class);
        sensors().set(MEMBER_SPEC, webServerSpec);
    }
    
    log.debug("creating cluster child for {}", this);
    // Note relies on initial_size being inherited by DynamicWebAppCluster, because key id is identical
    EntitySpec<? extends DynamicWebAppCluster> webClusterSpec = getAttribute(WEB_CLUSTER_SPEC);
    Map<String,Object> webClusterFlags = MutableMap.<String,Object>of("memberSpec", webServerSpec);

    if (webClusterSpec == null) {
        log.debug("creating default web cluster spec for {}", this);
        webClusterSpec = EntitySpec.create(DynamicWebAppCluster.class);
    }
    boolean hasMemberSpec = webClusterSpec.getConfig().containsKey(DynamicWebAppCluster.MEMBER_SPEC) || webClusterSpec.getFlags().containsKey("memberSpec");
    if (!hasMemberSpec) {
        webClusterSpec.configure(webClusterFlags);
    } else {
        log.warn("In {}, not setting cluster's {} because already set on webClusterSpec", new Object[] {this, webClusterFlags.keySet()});
    }
    sensors().set(WEB_CLUSTER_SPEC, webClusterSpec);
    
    DynamicWebAppCluster cluster = addChild(webClusterSpec);
    sensors().set(CLUSTER, cluster);
    setEntityFilter(EntityPredicates.isMemberOf(cluster));
    
    LoadBalancer controller = getAttribute(CONTROLLER);
    if (controller == null) {
        EntitySpec<? extends LoadBalancer> controllerSpec = getAttribute(CONTROLLER_SPEC);
        if (controllerSpec == null) {
            log.debug("creating controller using default spec for {}", this);
            controllerSpec = EntitySpec.create(NginxController.class);
            sensors().set(CONTROLLER_SPEC, controllerSpec);
        } else {
            log.debug("creating controller using custom spec for {}", this);
        }
        controller = addChild(controllerSpec);
        enrichers().add(Enrichers.builder()
                .propagating(LoadBalancer.PROXY_HTTP_PORT, LoadBalancer.PROXY_HTTPS_PORT)
                .from(controller)
                .build());
        sensors().set(CONTROLLER, controller);
    }
    
    Group controlledGroup = getAttribute(CONTROLLED_GROUP);
    if (controlledGroup == null) {
        log.debug("using cluster as controlledGroup for {}", this);
        controlledGroup = cluster;
        sensors().set(CONTROLLED_GROUP, cluster);
    } else {
        log.debug("using custom controlledGroup {} for {}", controlledGroup, this);
    }
    
    doBind();
}