• CLI

sdk
  1. Help Center
  2. CLI
  3. Developer Guide
  4. SDK
  5. JAVA
  6. AS Java SDK Demo

AS Java SDK Demo

Creating an AS Group

An Auto Scaling (AS) group is a set of ECSs with the same application scenario configurations. An AS group defines the minimum and maximum numbers of ECSs.

You can create an AS group using OpenStack4j based on the following code, where the network, security group, and VPC are mandatory parameters. Before creating an AS group, you must create a VPC as well as networks and security groups in this VPC.

private String createScalingGroup() {  
//network
     IdResourceEntity network = new IdResourceEntity();
     network.setId("fd329aab-d33a-436c-abcf-9ccd4082b2e3");  
//securityGroup
     IdResourceEntity securityGroup = new IdResourceEntity();
     securityGroup.setId("57f0a6cd-c427-4e40-a9a2-301ca90893fd");  
//az
     String availabilityZone = "eu-de-01";  
//group
     ASAutoScalingGroupCreate group = ASAutoScalingGroupCreate.builder().groupName("test-4-bill")
           .vpcId("0bbc9614-1209-438f-83bb-572b3ad475ea").networks(Lists.newArrayList(network))
           .configId("834b1d4a-36a7-4713-8ec3-41da28f12957").securityGroups(Lists.newArrayList(securityGroup))
           .maxInstanceNumber(2).minInstanceNumber(1).desireInstanceNumber(1).coolDownTime(800)
           .lbListenerId("4e4f42f1ff004cbdac61f034c9cdfde8")           .lbListenerId("9ece3b458dd14ce6a15b09073855402e")
           .availabilityZones(Lists.newArrayList(availabilityZone))
           .healthPeriodicAuditMethod(HealthPeriodicAuditMethod.NOVA_AUDIT)
           .healthPeriodicAuditTime(15)
           .instanceTerminatePolicy(InstanceTerminatePolicy.OLD_INSTANCE)
           .deletePublicip(true)
           .build();  
//creat group
     ScalingGroupCreate result = osclient.autoScaling().groups().create(group);
     Assert.assertNotNull(result.getGroupId());
     return result.getGroupId();  }

Creating an AS Configuration

An AS configuration defines the configurations for creating instances in an AS group. The AS service automatically adds instances to an AS group based on the AS configuration.

You can create an AS configuration based on the following code. When using an existing ECS flavor as the template to create the AS configuration, specify parameter instance_id. In this case, parameter flavorRefimageRef, and disks do not take effect. If instance_id is not specified, flavorRefimageRef, and disks are mandatory.

private String createScalingConfig () {
        String keyname = "KeyPair-0406-as";
        Map<String, String> metaData = Maps.newHashMap();
        metaData.put("key1", "val1");
        metaData.put("key2", "val2");
        Disk disk = Disk.builder().size(40).volumeType(VolumeType.SATA).diskType(DiskType.SYS).build();
//eip
        Bandwidth build = Bandwidth.builder().chargingMode(Bandwidth.ChargingMode.TRAFFIC).shareType(Bandwidth.ShareType.PER).size("100").build();
        Eip eip = Eip.builder().ipType("BGP5").bandwidth(build).build();
        PublicIp publicIp = PublicIp.builder().eip(eip).build();
//instanceConfig
        InstanceConfig instanceConfig = InstanceConfig.builder().instanceId("e926dffb-6fc5-4f8e-b2ff-b2bffe82efb9")
              .flavorRef("c2.medium").imageRef("e215580f-73ad-429d-b6f2-5433947433b0").disks(Lists.newArrayList(disk))
              .keyName(keyname)
              .metadata(metaData)
              .publicIp(publicIp)
              .userData("fegrhtht").build();
//createScalingConfig
        ScalingConfigCreate config = ASAutoScalingConfigCreate.builder().configName("test-config-name")
              .instanceConfig(instanceConfig).build();
        ScalingConfigCreate result = osclient.autoScaling().configs().create(config);
        assertNotNull(result.getConfigId());
        return result.getConfigId();  }

Creating an AS Policy

The AS service supports the periodic, scheduled, and alarm policies. If you configure the alarm policy, the selected or created alarm policies can be associated with only one AS group.

You can create periodic, scheduled, and alarm policies based on the following code. The periodic policy can be configured as daily, weekly, or monthly.

private void testCreateAutoScalingPolicy() {
     String groupId = "3545d5a1-2d8c-4370-8b95-36f2e8133c24";
//RECURRENCE Daily
   ScheduledPolicy scheduledPolicyDaily = ScheduledPolicy.builder().launchTime("01:21")
           .recurrenceType(RecurrenceType.DAILY).endTime(getEndTime()).recurrenceValue(null).build();
     ScalingPolicyCreateUpdate policyDaily = ASAutoScalingPolicyCreateUpdate.builder().policyName("SDK-policyName")
           .groupId(groupId).policyType(ScalingPolicyType.RECURRENCE).scheduledPolicy(scheduledPolicyDaily).coolDownTime(800)
           .scalingPolicyAction(ScalingPolicyAction.builder().operation(Operation.ADD).build())
           .build();
     ScalingPolicyCreateUpdate createDaily = osclient.autoScaling().policies().create(policyDaily);
     assertTrue(createDaily != null && !Strings.isNullOrEmpty(createDaily.getPolicyId()));
//RECURRENCE1 Weekly
    ScheduledPolicy scheduledPolicyWeekly = ScheduledPolicy.builder().launchTime("01:21")
           .recurrenceType(RecurrenceType.WEEKLY).startTime(getStartTime()).endTime(getEndTime()).recurrenceValue("1,2,3").build();
     ScalingPolicyCreateUpdate policyWeekly = ASAutoScalingPolicyCreateUpdate.builder().policyName("SDK-policyName")
           .groupId(groupId).policyType(ScalingPolicyType.RECURRENCE).scheduledPolicy(scheduledPolicyWeekly).coolDownTime(800)
           .scalingPolicyAction(ScalingPolicyAction.builder().operation(Operation.ADD).instanceNumber(1).build())
           .build();
     ScalingPolicyCreateUpdate createWeekly = osclient.autoScaling().policies().create(policyWeekly);
     assertTrue(createWeekly != null && !Strings.isNullOrEmpty(createWeekly.getPolicyId()));
//RECURRENCE1 Monthly
     ScheduledPolicy scheduledPolicyMonthly = ScheduledPolicy.builder().launchTime("01:21")
           .recurrenceType(RecurrenceType.MONTHLY).startTime(getStartTime()).endTime(getEndTime()).recurrenceValue("1,2,3,10").build();
     ScalingPolicyCreateUpdate policyMonthly = ASAutoScalingPolicyCreateUpdate.builder().policyName("SDK-policyName")
           .groupId(groupId).policyType(ScalingPolicyType.RECURRENCE).scheduledPolicy(scheduledPolicyMonthly).coolDownTime(800)
           .scalingPolicyAction(ScalingPolicyAction.builder().operation(Operation.ADD).instanceNumber(1).build())
           .build();
     ScalingPolicyCreateUpdate createMonthly = osclient.autoScaling().policies().create(policyWeekly0);
     assertTrue(createMonthly != null && !Strings.isNullOrEmpty(createMonthly.getPolicyId()));
//SCHEDULED
      ScheduledPolicy scheduledPolicyScheduled = ScheduledPolicy.builder().launchTime("2017-07-24T01:21Z").build();
     ScalingPolicyCreateUpdate policyScheduled = ASAutoScalingPolicyCreateUpdate.builder().policyName("policyTestName")
           .groupId(groupId).policyType(ScalingPolicyType.SCHEDULED).scheduledPolicy(scheduledPolicy1).coolDownTime(800)
     .scalingPolicyAction(ScalingPolicyAction.builder().operation(Operation.ADD).instanceNumber(1).build())
     .build();
     ScalingPolicyCreateUpdate createScheduled = osclient.autoScaling().policies().create(policyScheduled);
     assertTrue(createScheduled != null && !Strings.isNullOrEmpty(createScheduled.getPolicyId()));
//ALARM
      ScalingPolicyCreateUpdate policyAlarm = ASAutoScalingPolicyCreateUpdate.builder().policyName("policyTestName")
           .groupId(groupId).policyType(ScalingPolicyType.ALARM).alarmId("al1499772396965q7BBl9MpR").coolDownTime(800)
           .scalingPolicyAction(ScalingPolicyAction.builder().operation(Operation.REMOVE).instanceNumber(1).build())
           .build();
     ScalingPolicyCreateUpdate createAlarm = osclient.autoScaling().policies().create(policyAlarm);
     assertTrue(createAlarm!= null && !Strings.isNullOrEmpty(createAlarm.getPolicyId()));  }

Creating a Lifecycle Hook

The purpose of adding a lifecycle hook to the AS group is to suspend the instance status to Wait (Adding to AS group) or Wait (Removing from AS group) during a scaling action. This status retains until the suspension times out or you manually call back the action.

Code reference:

import com.huawei.openstack4j.openstack.scaling.domain.ASAutoScalingLifecycleHook;
import com.huawei.openstack4j.openstack.scaling.domain.ASAutoScalingLifecycleHookType;

//create lifecycle_hook
public ASAutoScalingLifecycleHook creatLifeHook() {

  String groupId = "36f3ac20-e8cf-4c0c-ab4f-78b092b74192";
  String lifecycleHookName = "test-hook";?
  ASAutoScalingLifecycleHookType lifecycleHookType = ASAutoScalingLifecycleHookType.INSTANCE_LAUNCHING;
  ASAutoScalingDefaultResult defaultResult = ASAutoScalingDefaultResult.ABANDON;
  String notificationTopicUrn = "urn:smn:cn-suzhou2-1:ebac0c927c104c4587687ce375d0b656:as_test";
  String notificationMetadata = "xxxxxxxx";

  ASAutoScalingLifecycleHook lifecycleHook = ASAutoScalingLifecycleHook.builder().lifecycleHookName(lifecycleHookName).lifecycleHookType(lifecycleHookType.defaultResult(defaultResult).defaultTimeout("86400").notificationTopicUrn(notificationTopicUrn).notificationMetadata(notificationMetadata).build();

  ASAutoScalingLifecycleHook hook = osclient.autoScaling().lifecycleHook().create(lifecycleHook, groupId);
  return hook;
 }