public static PolicySummary policySummary(Entity entity, Policy policy) { String applicationUri = "/v1/applications/" + entity.getApplicationId(); String entityUri = applicationUri + "/entities/" + entity.getId(); Map<String, URI> links = ImmutableMap.<String, URI>builder() .put("self", URI.create(entityUri + "/policies/" + policy.getId())) .put("config", URI.create(entityUri + "/policies/" + policy.getId() + "/config")) .put("start", URI.create(entityUri + "/policies/" + policy.getId() + "/start")) .put("stop", URI.create(entityUri + "/policies/" + policy.getId() + "/stop")) .put("destroy", URI.create(entityUri + "/policies/" + policy.getId() + "/destroy")) .put("application", URI.create(applicationUri)) .put("entity", URI.create(entityUri)) .build(); return new PolicySummary(policy.getId(), policy.getName(), ApplicationTransformer.statusFromLifecycle(Policies.getPolicyStatus(policy)), links); }
public PolicyDescriptor(Policy policy) { this.id = policy.getId(); this.type = policy.getPolicyType().getName(); this.name = policy.getName(); } public String getId() {
@Override public Map<String, Boolean> batchConfigRead( String application, String entityToken) { // TODO: add test EntityLocal entity = brooklyn().getEntity(application, entityToken); Map<String, Boolean> result = Maps.newLinkedHashMap(); for (Policy p : entity.getPolicies()) { result.put(p.getId(), !p.isSuspended()); } return result; }
public static Lifecycle getPolicyStatus(Policy p) { if (p.isRunning()) return Lifecycle.RUNNING; if (p.isDestroyed()) return Lifecycle.DESTROYED; if (p.isSuspended()) return Lifecycle.STOPPED; // TODO could policy be in an error state? return Lifecycle.CREATED; }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Override public Response set( String application, String entityToken, String policyToken, String configKeyName, String value ) { Policy policy = brooklyn().getPolicy(application, entityToken, policyToken); ConfigKey<?> ck = policy.getPolicyType().getConfigKey(configKeyName); if (ck == null) throw WebResourceUtils.notFound("Cannot find config key '%s' in policy '%s' of entity '%s'", configKeyName, policy, entityToken); policy.setConfig((ConfigKey)ck, TypeCoercions.coerce(value, ck.getType())); return Response.status(Response.Status.OK).build(); }
@Override public String get(String application, String entityToken, String policyToken, String configKeyName) { Policy policy = brooklyn().getPolicy(application, entityToken, policyToken); ConfigKey<?> ck = policy.getPolicyType().getConfigKey(configKeyName); if (ck == null) throw WebResourceUtils.notFound("Cannot find config key '%s' in policy '%s' of entity '%s'", configKeyName, policy, entityToken); return getStringValueForDisplay(brooklyn(), policy, policy.getConfig(ck)); }
@Override public Response stop(String application, String entityToken, String policyId) { Policy policy = brooklyn().getPolicy(application, entityToken, policyId); policy.suspend(); return Response.status(Response.Status.OK).build(); }
public PolicyConfigSummary(EntityLocal entity, Policy policy, ConfigKey<?> config) { super(config); String applicationUri = "/v1/applications/" + entity.getApplicationId(); String entityUri = applicationUri + "/entities/" + entity.getId(); String policyUri = entityUri + "/policies/" + policy.getId(); this.links = ImmutableMap.<String, URI>builder() .put("self", URI.create(policyUri + "/config/" + config.getName())) .put("application", URI.create(applicationUri)) .put("entity", URI.create(entityUri)) .put("policy", URI.create(policyUri)) .build(); }
@Override public Response start( String application, String entityToken, String policyId) { Policy policy = brooklyn().getPolicy(application, entityToken, policyId); policy.resume(); return Response.status(Response.Status.OK).build(); }
public static void dumpInfo(Policy pol, Writer out, String currentIndentation, String tab) throws IOException { out.append(currentIndentation+pol.toString()+"\n"); for (ConfigKey<?> key : sortConfigKeys(pol.getPolicyType().getConfigKeys())) { Object val = ((AbstractPolicy)pol).getConfigMap().getRawConfig(key); if (!isTrivial(val)) { out.append(currentIndentation+tab+tab+key); out.append(" = "); if (isSecret(key.getName())) out.append("xxxxxxxx"); else out.append(""+val); out.append("\n"); } } out.flush(); }
@Override public Map<String, Object> batchConfigRead(String application, String entityToken, String policyToken) { // TODO: add test Policy policy = brooklyn().getPolicy(application, entityToken, policyToken); Map<ConfigKey<?>, Object> source = policy.getAllConfig(); Map<String, Object> result = Maps.newLinkedHashMap(); for (Map.Entry<ConfigKey<?>, Object> ek: source.entrySet()) { result.put(ek.getKey().getName(), getStringValueForDisplay(brooklyn(), policy, ek.getValue())); } return result; }
@SuppressWarnings({ "unchecked", "rawtypes" }) @POST @Path("/{config}/set") @ApiOperation(value = "Sets the given config on this policy") @ApiErrors(value = { @ApiError(code = 404, reason = "Could not find application, entity, policy or config key") }) public Response set( @ApiParam(value = "Application ID or name", required = true) @PathParam("application") String application, @ApiParam(value = "Entity ID or name", required = true) @PathParam("entity") String entityToken, @ApiParam(value = "Policy ID or name", required = true) @PathParam("policy") String policyToken, @ApiParam(value = "Config key ID", required = true) @PathParam("config") String configKeyName, @ApiParam(name = "value", value = "New value for the configuration", required = true) @QueryParam("value") String value ) { Policy policy = brooklyn().getPolicy(application, entityToken, policyToken); ConfigKey<?> ck = policy.getPolicyType().getConfigKey(configKeyName); if (ck == null) throw WebResourceUtils.notFound("Cannot find config key '%s' in policy '%s' of entity '%s'", configKeyName, policy, entityToken); policy.setConfig((ConfigKey)ck, TypeCoercions.coerce(value, ck.getType())); return Response.status(Response.Status.OK).build(); }
@GET @Path("/{config}") @ApiOperation(value = "Fetch config value", responseClass = "Object") @ApiErrors(value = { @ApiError(code = 404, reason = "Could not find application, entity, policy or config key") }) public String get( @ApiParam(value = "Application ID or name", required = true) @PathParam("application") String application, @ApiParam(value = "Entity ID or name", required = true) @PathParam("entity") String entityToken, @ApiParam(value = "Policy ID or name", required = true) @PathParam("policy") String policyToken, @ApiParam(value = "Config key ID", required = true) @PathParam("config") String configKeyName ) { Policy policy = brooklyn().getPolicy(application, entityToken, policyToken); ConfigKey<?> ck = policy.getPolicyType().getConfigKey(configKeyName); if (ck == null) throw WebResourceUtils.notFound("Cannot find config key '%s' in policy '%s' of entity '%s'", configKeyName, policy, entityToken); return getValueForDisplay(policy, policy.getConfig(ck)); }
@Override public Response destroy(String application, String entityToken, String policyToken) { EntityLocal entity = brooklyn().getEntity(application, entityToken); Policy policy = brooklyn().getPolicy(entity, policyToken); policy.suspend(); entity.removePolicy(policy); return Response.status(Response.Status.OK).build(); } }
@Override public String addPolicy( String application,String entityToken, String policyTypeName, // TODO would like to make this optional but jersey complains if we do Map<String, String> config ) { EntityLocal entity = brooklyn().getEntity(application, entityToken); try { Class<?> policyType = Class.forName(policyTypeName); Policy policy = (Policy) policyType.newInstance(); if (config != null && !config.isEmpty()) { // TODO support this: //policy.setConfig(config); policyType.getMethod("setConfig", Map.class).invoke(policy, config); } log.debug("REST API adding policy " + policy + " to " + entity); entity.addPolicy(policy); return policy.getId(); } catch (Exception e) { throw Exceptions.propagate(e); } }
@POST @Path("/{policy}/start") @ApiOperation(value = "Start or resume a policy") @ApiErrors(value = { @ApiError(code = 404, reason = "Could not find application, entity or policy") }) public Response start( @ApiParam(name = "application", value = "Application ID or name", required = true) @PathParam("application") String application, @ApiParam(name = "entity", value = "Entity ID or name", required = true) @PathParam("entity") String entityToken, @ApiParam(name = "policy", value = "Policy ID or name", required = true) @PathParam("policy") String policyId ) { Policy policy = brooklyn().getPolicy(application, entityToken, policyId); policy.resume(); return Response.status(Response.Status.OK).build(); }
@Override public List<PolicyConfigSummary> list( final String application, final String entityToken, final String policyToken ) { EntityLocal entity = brooklyn().getEntity(application, entityToken); Policy policy = brooklyn().getPolicy(entity, policyToken); List<PolicyConfigSummary> result = Lists.newArrayList(); for (ConfigKey<?> key : policy.getPolicyType().getConfigKeys()) { result.add(PolicyTransformer.policyConfigSummary(brooklyn(), entity, policy, key)); } return result; }
@GET @Path("/current-state") @ApiOperation(value = "Fetch config key values in batch", notes="Returns a map of config name to value") public Map<String, Object> batchConfigRead( @ApiParam(value = "Application ID or name", required = true) @PathParam("application") String application, @ApiParam(value = "Entity ID or name", required = true) @PathParam("entity") String entityToken, @ApiParam(value = "Policy ID or name", required = true) @PathParam("policy") String policyToken) { // TODO: add test Policy policy = brooklyn().getPolicy(application, entityToken, policyToken); Map<ConfigKey<?>, Object> source = policy.getAllConfig(); Map<String, Object> result = Maps.newLinkedHashMap(); for (Map.Entry<ConfigKey<?>, Object> ek: source.entrySet()) { result.put(ek.getKey().getName(), getValueForDisplay(policy, ek.getValue())); } return result; }
public static BasicPolicyMemento.Builder newPolicyMementoBuilder(Policy policy) { BasicPolicyMemento.Builder builder = BasicPolicyMemento.builder(); builder.type = policy.getClass().getName(); builder.id = policy.getId(); builder.displayName = policy.getName(); builder.flags.putAll(FlagUtils.getFieldsWithFlagsExcludingModifiers(policy, Modifier.STATIC ^ Modifier.TRANSIENT)); return builder; } }
@GET @Path("/current-state") @ApiOperation(value = "Fetch policy states in batch", notes="Returns a map of policy ID to whether it is active") public Map<String, Boolean> batchConfigRead( @ApiParam(value = "Application ID or name", required = true) @PathParam("application") String application, @ApiParam(value = "Entity ID or name", required = true) @PathParam("entity") String entityToken) { // TODO: add test EntityLocal entity = brooklyn().getEntity(application, entityToken); Map<String, Boolean> result = Maps.newLinkedHashMap(); for (Policy p: entity.getPolicies()) { result.put(p.getId(), !p.isSuspended()); } return result; }