Created
March 20, 2014 01:35
-
-
Save vksuktha/9655554 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class JCloudsManualAuthentication { | |
private ComputeService compute; | |
private static RestContext<NovaApi, NovaAsyncApi> nova; | |
private static Set<String> zones; | |
private String selected_image_id; | |
private String selected_flavor_id; | |
public static void main(String[] args) { | |
JCloudsManualAuthentication authentication = new JCloudsManualAuthentication(); | |
try { | |
authentication.init(args); | |
authentication.ListImages(); | |
authentication.Listflavors(); | |
authentication.CreateServer(); | |
} | |
finally { | |
//authentication.Listserver(); | |
authentication.close(); | |
} | |
} | |
private void init(String[] args) { | |
String provider = "openstack-nova"; | |
String username = "admin:admin"; | |
String credential = "xxxxxxxxx"; | |
ContextBuilder contextBuilder = ContextBuilder.newBuilder(provider) | |
.credentials(username, credential); | |
if (provider.startsWith("openstack")) { | |
String endpoint = "http://10.0.2.15:5000/v2.0"; | |
contextBuilder.endpoint(endpoint); | |
} | |
ComputeServiceContext context = contextBuilder.buildView(ComputeServiceContext.class); | |
Function<Credentials, Access> auth = context.utils().injector().getInstance(Key.get(new TypeLiteral<Function<Credentials, Access>>(){})); | |
Access access = auth.apply(new Credentials.Builder<Credentials>().identity(username).credential(credential).build()); | |
System.out.println(access); | |
System.out.println(" User Name = " + access.getUser().getName()); | |
System.out.println(" User ID = " + access.getUser().getId()); | |
System.out.println(" Tenant Name = " + access.getToken().getTenant().get().getName()); | |
System.out.println(" Tenant ID = " + access.getToken().getTenant().get().getId()); | |
System.out.println(" Token ID = " + access.getToken().getId()); | |
System.out.println(" Token Expires = " + access.getToken().getExpires()); | |
System.out.println("-------------------------------"); | |
for (Service service: access) { | |
System.out.println(" Service = " + service.getName()); | |
for (Endpoint endpoint: service) { | |
System.out.println(" Endpoint = " + endpoint.getPublicURL()); | |
} | |
System.out.println("================="); | |
} | |
compute = context.getComputeService(); | |
nova = context.unwrap(); | |
zones = nova.getApi().getConfiguredZones(); | |
} | |
/** | |
* Always close your service when you're done with it. | |
*/ | |
private static void Listserver(){ | |
for (String zone : zones){ | |
//System.out.println("zone" +zone); | |
ServerApi serverApi = nova.getApi().getServerApiForZone("RegionOne"); | |
System.out.println("Servers in " +zone); | |
for (Server server: serverApi.listInDetail().concat()){ | |
System.out.println(" " +server); | |
} | |
} | |
} | |
private void ListImages(){ | |
for (String zone : zones){ | |
ImageApi imageApi = nova.getApi().getImageApiForZone(zone); | |
System.out.println("List of Images in " + zone + ":" ); | |
FluentIterable<? extends Image> images = imageApi.listInDetail().concat(); | |
for (Image image : images){ | |
System.out.println("" +image); | |
if (image.equals("cirrus-0.3.1-x86_64-usec")){ | |
selected_image_id = image.getId(); | |
} | |
} | |
} | |
} | |
private void Listflavors(){ | |
for (String zone : zones){ | |
FlavorApi flavorapi = nova.getApi().getFlavorApiForZone(zone); | |
System.out.println("List of flavors in " +zone + ":"); | |
Set<? extends Resource> flavors = flavorapi.list().concat().toSet(); | |
for (Resource flavor : flavors){ | |
System.out.println("" +flavor.getName()); | |
} | |
} | |
} | |
private void CreateServer(){ | |
CreateServerOptions n = new CreateServerOptions(); | |
ServerApi serverApi = nova.getApi().getServerApiForZone("RegionOne"); | |
System.out.println("create server"); | |
ServerCreated servercreated = serverApi.create("jclouds-server","cirrus-0.3.1-x86_64-usec","m1-tiny",n); | |
} | |
public void close() { | |
if (compute != null) { | |
compute.getContext(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment