Created
May 13, 2014 08:42
-
-
Save maksimov/380e0fd9c73eeb255d3f to your computer and use it in GitHub Desktop.
Create cloud provider from properties
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 void loadProviderFromProperties() throws Exception { | |
final Properties props = new Properties(); | |
InputStream inputStream = new FileInputStream("/Users/stas/dsn.properties"); | |
try { | |
props.load(inputStream); | |
} catch( IOException e ) { | |
e.printStackTrace(); | |
} | |
Cloud cloud = Cloud.register(props.getProperty("DSN_PROVIDER_NAME"), | |
props.getProperty("DSN_CLOUD_NAME"), | |
props.getProperty("DSN_ENDPOINT"), | |
(Class<? extends CloudProvider>)Class.forName(props.getProperty("DSN_PROVIDER_CLASS"))); | |
// Find what additional fields are necessary to connect to the cloud | |
ContextRequirements requirements = cloud.buildProvider().getContextRequirements(); | |
List<ContextRequirements.Field> fields = requirements.getConfigurableValues(); | |
// Load the values for the required fields from the system properties | |
List<ProviderContext.Value<?>> values = new ArrayList<ProviderContext.Value<?>>(); | |
int i = 0; | |
for(ContextRequirements.Field f : fields ) { | |
System.out.print("Loading '" + f.name + "' from "); | |
if( f.type.equals(ContextRequirements.FieldType.KEYPAIR) ) { | |
System.out.println("'DSN_" + f.name + "_SHARED' and 'DSN_" + f.name + "_SECRET'"); | |
String shared = props.getProperty("DSN_" + f.name + "_SHARED"); | |
String secret = props.getProperty("DSN_" + f.name + "_SECRET"); | |
System.out.println(secret); | |
values.add(ProviderContext.Value.parseValue(f, shared, secret)); | |
} | |
else { | |
System.out.println("'DSN_" + f.name + "'"); | |
String value = props.getProperty("DSN_" + f.name); | |
if( value != null ) { | |
values.add(ProviderContext.Value.parseValue(f, value)); | |
} | |
} | |
i++; | |
} | |
ProviderContext ctx = cloud.createContext(props.getProperty("DSN_ACCOUNT"), props.getProperty("DSN_REGION"), values.toArray(new ProviderContext.Value<?>[0])); | |
CloudProvider configuredProvider = ctx.connect(); | |
MachineImage mi = configuredProvider.getComputeServices().getImageSupport().getImage("bogus"); | |
System.out.println("All done"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment