protected ComputeService getComputeService()

in commands/src/main/java/org/jclouds/karaf/commands/compute/ComputeCommandWithOptions.java [77:151]


   protected ComputeService getComputeService() throws IOException {
      if ((name == null && provider == null && api == null) &&(computeServices != null && computeServices.size() == 1)) {
         return computeServices.get(0);
      }

      ComputeService computeService = null;
      if (propertiesFile != null) {
         EnvHelper.loadProperties(new File(propertiesFile));
      }
      String providerValue = EnvHelper.getComputeProvider(provider);
      String apiValue = EnvHelper.getComputeApi(api);
      String identityValue = EnvHelper.getComputeIdentity(identity);
      String credentialValue = EnvHelper.getComputeCredential(providerValue, credential);
      String endpointValue = EnvHelper.getComputeEndpoint(endpoint);
      boolean contextNameProvided = !Strings.isNullOrEmpty(name);
      boolean canCreateService = (!Strings.isNullOrEmpty(providerValue) || !Strings.isNullOrEmpty(apiValue))
               && !Strings.isNullOrEmpty(identityValue) && !Strings.isNullOrEmpty(credentialValue);

      String providerOrApiValue = !Strings.isNullOrEmpty(providerValue) ? providerValue : apiValue;

      try {
         computeService = ServiceHelper.getService(name, providerOrApiValue, computeServices);
      } catch (Throwable t) {
         if (contextNameProvided) {
           throw new RuntimeException("Could not find compute service with id:" + name);
         } else if (!canCreateService) {
            StringBuilder sb = new StringBuilder();
            sb.append("Insufficient information to create compute service:\n");
            if (providerOrApiValue == null) {
               sb.append("Missing provider or api." +
                     " Please specify the --provider / --api" +
                     " options, set the " + Constants.PROPERTY_PROVIDER +
                     " / " + Constants.PROPERTY_API + " properties" +
                     ", or set the JCLOUDS_COMPUTE_PROVIDER /" +
                     " JCLOUDS_COMPUTE_API environment variables.\n");
            }
            if (identityValue == null) {
               sb.append("Missing identity." +
                     " Please specify the --identity option" +
                     ", set the " + Constants.PROPERTY_IDENTITY +
                     " property, or set the " +
                     EnvHelper.JCLOUDS_COMPUTE_IDENTITY +
                     " environment variable.\n");
            }
            if (credentialValue == null) {
               sb.append("Missing credential." +
                     " Please specify the --credential option" +
                     ", set the " + Constants.PROPERTY_CREDENTIAL +
                     " property, or set the " +
                     EnvHelper.JCLOUDS_COMPUTE_CREDENTIAL +
                     " environment variable.\n");
            }
            throw new RuntimeException(sb.toString());
         }
      }

      if (computeService == null && canCreateService) {
         try {
            // This may run in or inside OSGi, so we choose explicitly set a credential store which
            // should be compatible with both.
            ContextBuilder builder = ContextBuilder
                     .newBuilder(providerOrApiValue)
                     .credentials(identityValue, credentialValue)
                     .modules(ImmutableSet.<Module> of(new JschSshClientModule(), new Log4JLoggingModule(),
                              new PropertiesCredentialStore()));
            if (!Strings.isNullOrEmpty(endpointValue)) {
               builder = builder.endpoint(endpointValue);
            }
            computeService = builder.build(ComputeServiceContext.class).getComputeService();
         } catch (Exception ex) {
            throw new RuntimeException("Failed to create service:" + ex.getMessage());
         }
      }
      return computeService;
   }