hello-world/java-cli-app/src/main/java/com/oracle/oci/sdk/examples/compute/ListInstances.java
package com.oracle.oci.sdk.examples.compute;

import java.util.List;

import com.oracle.bmc.Region;
import com.oracle.bmc.auth.AuthenticationDetailsProvider;
import com.oracle.bmc.core.ComputeClient;
import com.oracle.bmc.core.model.Instance;
import com.oracle.bmc.core.requests.ListInstancesRequest;
import com.oracle.bmc.core.responses.ListInstancesResponse;
import com.oracle.oci.sdk.OCIConfig;

/*
 * Sample code to get list of instances in particular region, compartment.
 * You may still add more select criteria in ListInstancesRequest like compartmentId.
 * 
 * Sample Output (Masked):
 * Instance: ocid1.instance.oc1.iad.anu...xru2a
 */

public class ListInstances {
    public static void main(String args[]) throws Exception {
    	
    	AuthenticationDetailsProvider adp = OCIConfig.getAuthenticationDetailsProvider();
    	Region region = Region.US_ASHBURN_1;
    	String compartmentId = "ocid1.compartment.oc1.......";  // TODO: Set your compartment ID here.

    	ListInstancesRequest request = ListInstancesRequest.builder()
        		.compartmentId(compartmentId)
        		.build(); 
        
        List<Instance> instances = getInstances(adp, region, request);

        System.out.println("Hello, Here are the list of instances");
        
        instances.stream().forEach((Instance instance) -> {
        	System.out.println("Instance: " + instance.getId());
        });
    }
    
    public static List<Instance> getInstances(AuthenticationDetailsProvider adp, Region region, ListInstancesRequest request) throws Exception {
        ComputeClient computeClient = ComputeClient.builder().build(adp);
        computeClient.setRegion(region);
        ListInstancesResponse listInstancesResponse = computeClient.listInstances(request);
        return listInstancesResponse.getItems();
    }
}
