Skip to content

3.5 Policies management

Aliaksandr Krasitski edited this page Aug 28, 2015 · 2 revisions

This section overviews anti-affinity and alert policy management features available in the SDK. Note that in all examples policies refer to the new ClcSdk().policyService() variable.

Anti-affinity policies

Creating an anti-affinity policy

You can asynchronously create a policy, using the AntiAffinityPolicyConfig command. To get the result of the operation, call waitUntilComplete().getResult().

policies.antiAffinity().create(
    new AntiAffinityPolicyConfig()
        .name("My policy")
        .dataCenter(DataCenter.CA_TORONTO_1)
    )
    .waitUntilComplete()
    .getResult();

Modifying an anti-affinity policy

Existing policy names can be asynchronously modified, like so:

policies.antiAffinity()
    .modify(
        AntiAffinityPolicy.refByName(DataCenter.CA_TORONTO_1, "Policy"), //policy reference, list of refs, filter
        new AntiAffinityPolicyConfig().name("New policy name")
    )
    .waitUntilComplete()
    .getResult();

Searching anti-affinity policies

You can find anti-affinity policies by data centers, name, and ID. For instance:

policies
    .antiAffinity()
    .find(
        new AntiAffinityPolicyFilter()
            .dataCenters(DataCenter.DE_FRANKFURT)
            .nameContains("PoLiCy")
    );

Deleting an anti-affinity policy

You may delete a single policy with:

OperationFuture<AntiAffinityPolicy> future = policies.antiAffinity().delete(policyRef);

To delete a set of policies, use:

OperationFuture<List<AntiAffinityPolicy>> future = 
policies.antiAffinity()
    .delete(
        AntiAffinityPolicy.refById("87b67bf7aee948dea939f5de2d9b2dc3"),
        AntiAffinityPolicy.refByName(DataCenter.DE_FRANKFURT, "My Policy")
    );

To delete a set of policies specified by some search criteria, use:

OperationFuture<List<AntiAffinityPolicy>> future = 
policies.antiAffinity()
    .delete(new AntiAffinityPolicyFilter()
        .dataCenters(DataCenter.US_WEST_SEATTLE)
    );
    

To see the result of the operation, call: waitUntilComplete().getResult().

Alert policies

Creating an alert policy

It is possible to asynchronously create a policy, using the AlertPolicyConfig command. To get the result of the operation, call waitUntilComplete().getResult(). See this example:

policies.alert().create(
    new AlertPolicyConfig()
        .name(policyName)
        .action(new AlertAction()
            .settings(new ActionSettingsEmail("[email protected]"))
        )
        .triggers(
            new AlertTrigger().duration(5).metric(AlertTriggerMetric.CPU).threshold(52.0f)
        )
    )

Modifying an alert policy

Existing policies can be asynchronously modified:

policies.alert()
    .modify(AlertPolicy.refByName("Alert policy"),
        new AlertPolicyConfig()
            .name(newPolicyName)
            .action(new AlertAction().settings(new ActionSettingsEmail("[email protected]", "[email protected]")))
            .trigger(new AlertTrigger().duration(10).metric(AlertTriggerMetric.DISK).threshold(40f))
    )
    .waitUntilComplete()
    .getResult();

Searching alert policies

You can find alert policies by name, metrics, and actions. For instance:

policies
    .alert()
    .find(new AlertPolicyFilter()
        .nameContains("PoLiCy")
        .where(ap -> ap.getTriggers().get(0).getMetric().equals(AlertTriggerMetric.CPU) &&
                     ap.getTriggers().get(0).getThreshold() > 20f)
    );
    

Deleting an alert policy

You may delete a single policy with:

OperationFuture<AlertPolicy> future = policies.alert().delete(policyRef);

To delete a set of policies, use:

OperationFuture<List<AlertPolicy>> future = 
policies.alert()
    .delete(
        AlertPolicy.refById("e5e2c4fa9daa460b9bf46a8028b021f9"),
        AlertPolicy.refByName("My Policy")
    );

To delete a set of policies specified by some search criteria, use:

OperationFuture<List<AlertPolicy>> future = 
policies.alert()
    .delete(new AlertPolicyFilter()
        .dataCenters(DataCenter.US_WEST_SEATTLE)
    );
    

To see the result of the operation, call: waitUntilComplete().getResult().