Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static ArrayList<String> clientWithWaiters(int numberOfInstances) throws Exception {
- long startTime = System.nanoTime();
- System.out.println("-----------------------------------------------------------------------");
- ArrayList<String> instanceIds = new ArrayList<>();
- // Configure the client
- ClientConfiguration clientConfig = ClientConfiguration.builder()
- .connectionTimeoutMillis(3000)
- .readTimeoutMillis(60000)
- .build();
- // Using default profile
- AuthenticationDetailsProvider authenticationDetailsProvider = new ConfigFileAuthenticationDetailsProvider("DEFAULT");
- // Set Region
- IdentityClient identityClient = IdentityClient.builder().build(authenticationDetailsProvider);
- identityClient.setRegion(Region.valueOf(System.getenv("REGION")));
- // Create compute client
- ComputeClient computeClient = ComputeClient.builder()
- .configuration(clientConfig)
- .build(authenticationDetailsProvider);
- computeClient.setRegion(Region.valueOf(System.getenv("REGION")));
- // Creating work request client to be able to wait on it
- WorkRequestClient workRequestClient = WorkRequestClient.builder().build(authenticationDetailsProvider);
- workRequestClient.setRegion(Region.valueOf(System.getenv("REGION")));
- // Creating waiters
- ComputeWaiters computeWaiters = computeClient.newWaiters(workRequestClient);
- // oci compute instance launch --display-name "test-$i"
- // --availability-domain $AD --compartment-id $COMPARTMENT_ID
- // --shape VM.Standard.E2.1 --subnet-id $SUBNET_ID
- // --image-id $IMAGE_ID
- // --raw-output --query 'data.id'
- // lets cheat from cli command
- String availabilityDomain = System.getenv("AD");
- String compartmentId = System.getenv("COMPARTMENT_ID");
- String imageId = System.getenv("IMAGE_ID");
- String subnetId = System.getenv("SUBNET_ID");
- String shape = System.getenv("SHAPE");
- String instanceName = System.getenv("INSTANCE_NAME_PREFIX");
- for (int i = 0; i < numberOfInstances; i++) {
- long startTime1 = System.nanoTime();
- // Set instance details
- LaunchInstanceDetails launchInstanceDetails = LaunchInstanceDetails.builder()
- .availabilityDomain(availabilityDomain)
- .compartmentId(compartmentId)
- .displayName(instanceName + (i+1))
- .imageId(imageId)
- .subnetId(subnetId)
- .shape(shape)
- .build();
- // Actual request
- LaunchInstanceRequest launchInstanceRequest = LaunchInstanceRequest.builder()
- .launchInstanceDetails(launchInstanceDetails)
- .build();
- // Execute waiter callback and wait
- LaunchInstanceResponse launchInstanceResponse = computeWaiters.forLaunchInstance(launchInstanceRequest).execute();
- GetInstanceRequest getInstanceRequest = GetInstanceRequest.builder()
- .instanceId(launchInstanceResponse.getInstance().getId())
- .build();
- String instanceId = getInstanceRequest.getInstanceId();
- // Execute another waiter callback and wait until compute instance is in RUNNING state
- GetInstanceResponse getInstanceResponse = computeWaiters.forInstance(getInstanceRequest, Instance.LifecycleState.Running).execute();
- Instance instance = getInstanceResponse.getInstance();
- instanceId = instance.getId();
- instanceIds.add(instanceId);
- // How long did it take
- long elapsedTime1 = System.nanoTime() - startTime1;
- System.out.println(instanceId + " created in " + (elapsedTime1 / 1000000) + " ms");
- }
- // Display total duration
- long elapsedTime = System.nanoTime() - startTime;
- System.out.println("Total execution time in seconds: " + TimeUnit.MILLISECONDS.toSeconds(elapsedTime / 1000000));
- System.out.println("-----------------------------------------------------------------------");
- return instanceIds;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement