Advertisement
fkeles

clientWithWaiters.java

Oct 10th, 2023
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.28 KB | Source Code | 0 0
  1.     public static ArrayList<String> clientWithWaiters(int numberOfInstances) throws Exception {
  2.         long startTime = System.nanoTime();
  3.         System.out.println("-----------------------------------------------------------------------");
  4.         ArrayList<String> instanceIds = new ArrayList<>();
  5.         // Configure the client
  6.         ClientConfiguration clientConfig = ClientConfiguration.builder()
  7.                 .connectionTimeoutMillis(3000)
  8.                 .readTimeoutMillis(60000)
  9.                 .build();
  10.         // Using default profile
  11.         AuthenticationDetailsProvider authenticationDetailsProvider = new ConfigFileAuthenticationDetailsProvider("DEFAULT");
  12.         // Set Region
  13.         IdentityClient identityClient = IdentityClient.builder().build(authenticationDetailsProvider);
  14.         identityClient.setRegion(Region.valueOf(System.getenv("REGION")));
  15.         // Create compute client
  16.         ComputeClient computeClient = ComputeClient.builder()
  17.                 .configuration(clientConfig)
  18.                 .build(authenticationDetailsProvider);
  19.         computeClient.setRegion(Region.valueOf(System.getenv("REGION")));
  20.         // Creating work request client to be able to wait on it
  21.         WorkRequestClient workRequestClient = WorkRequestClient.builder().build(authenticationDetailsProvider);
  22.         workRequestClient.setRegion(Region.valueOf(System.getenv("REGION")));
  23.         // Creating waiters
  24.         ComputeWaiters computeWaiters = computeClient.newWaiters(workRequestClient);
  25.  
  26.         // oci compute instance launch --display-name "test-$i"
  27.         // --availability-domain $AD --compartment-id $COMPARTMENT_ID
  28.         // --shape VM.Standard.E2.1 --subnet-id $SUBNET_ID
  29.         // --image-id $IMAGE_ID
  30.         // --raw-output --query 'data.id'
  31.         // lets cheat from cli command
  32.         String availabilityDomain = System.getenv("AD");
  33.         String compartmentId = System.getenv("COMPARTMENT_ID");
  34.         String imageId = System.getenv("IMAGE_ID");
  35.         String subnetId = System.getenv("SUBNET_ID");
  36.         String shape = System.getenv("SHAPE");
  37.         String instanceName = System.getenv("INSTANCE_NAME_PREFIX");
  38.  
  39.         for (int i = 0; i < numberOfInstances; i++) {
  40.             long startTime1 = System.nanoTime();
  41.             // Set instance details
  42.             LaunchInstanceDetails launchInstanceDetails = LaunchInstanceDetails.builder()
  43.                     .availabilityDomain(availabilityDomain)
  44.                     .compartmentId(compartmentId)
  45.                     .displayName(instanceName + (i+1))
  46.                     .imageId(imageId)
  47.                     .subnetId(subnetId)
  48.                     .shape(shape)
  49.                     .build();
  50.             // Actual request
  51.             LaunchInstanceRequest launchInstanceRequest = LaunchInstanceRequest.builder()
  52.                     .launchInstanceDetails(launchInstanceDetails)
  53.                     .build();
  54.             // Execute waiter callback and wait
  55.             LaunchInstanceResponse launchInstanceResponse = computeWaiters.forLaunchInstance(launchInstanceRequest).execute();
  56.  
  57.             GetInstanceRequest getInstanceRequest = GetInstanceRequest.builder()
  58.                     .instanceId(launchInstanceResponse.getInstance().getId())
  59.                     .build();
  60.             String instanceId = getInstanceRequest.getInstanceId();
  61.             // Execute another waiter callback and wait until compute instance is in RUNNING state
  62.             GetInstanceResponse getInstanceResponse = computeWaiters.forInstance(getInstanceRequest, Instance.LifecycleState.Running).execute();
  63.             Instance instance = getInstanceResponse.getInstance();
  64.             instanceId = instance.getId();
  65.             instanceIds.add(instanceId);
  66.             // How long did it take
  67.             long elapsedTime1 = System.nanoTime() - startTime1;
  68.             System.out.println(instanceId + " created in " + (elapsedTime1 / 1000000) + " ms");
  69.         }
  70.         // Display total duration
  71.         long elapsedTime = System.nanoTime() - startTime;
  72.         System.out.println("Total execution time in seconds: " + TimeUnit.MILLISECONDS.toSeconds(elapsedTime / 1000000));
  73.         System.out.println("-----------------------------------------------------------------------");
  74.         return instanceIds;
  75.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement