2

I want to add and list all the instances/VM under my project in google cloud using Jcloud api. In this code, I am assuming a node to be an instance. I have set all the variables as required and extracted the private key from the json file. The context build takes place successfully.
images = compute.listImages() => lists all the images provided by google.
nodes = compute.listNodes() =>should list nodes but instead give null pointer exception.

Output=>

No of images 246

Exception in thread "main" java.lang.NullPointerException: group at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:229) at org.jclouds.compute.internal.FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat.checkGroup(FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat.java:124) at org.jclouds.compute.internal.FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat.sharedNameForGroup(FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat.java:120) at org.jclouds.googlecomputeengine.compute.functions.FirewallTagNamingConvention$Factory.get(FirewallTagNamingConvention.java:39) at org.jclouds.googlecomputeengine.compute.functions.InstanceToNodeMetadata.apply(InstanceToNodeMetadata.java:68) at org.jclouds.googlecomputeengine.compute.functions.InstanceToNodeMetadata.apply(InstanceToNodeMetadata.java:43) at com.google.common.base.Functions$FunctionComposition.apply(Functions.java:211) at com.google.common.collect.Iterators$8.transform(Iterators.java:794) at com.google.common.collect.TransformedIterator.next(TransformedIterator.java:48) at com.google.common.collect.Iterators$7.computeNext(Iterators.java:646) at com.google.common.collect.AbstractIterator.tryToComputeNext(AbstractIterator.java:143) at com.google.common.collect.AbstractIterator.hasNext(AbstractIterator.java:138) at com.google.common.collect.Iterators.addAll(Iterators.java:356) at com.google.common.collect.Iterables.addAll(Iterables.java:350) at com.google.common.collect.Sets.newLinkedHashSet(Sets.java:328) at org.jclouds.compute.internal.BaseComputeService.listNodes(BaseComputeService.java:335) at org.jclouds.examples.compute.basics.Example.main(Example.java:54)

    package org.jclouds.examples.compute.basics;
    import static com.google.common.base.Charsets.UTF_8;
    import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_SCRIPT_COMPLETE;

    import java.io.File;
    import java.io.IOException;
    import java.util.Properties;
    import java.util.Set;
    import java.util.concurrent.TimeUnit;

    import org.jclouds.ContextBuilder;
    import org.jclouds.compute.ComputeService;
    import org.jclouds.compute.ComputeServiceContext;
    import org.jclouds.compute.domain.ComputeMetadata;
    import org.jclouds.compute.domain.Image;
    import org.jclouds.domain.Credentials;
    import org.jclouds.enterprise.config.EnterpriseConfigurationModule;
    import org.jclouds.googlecloud.GoogleCredentialsFromJson;
    import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
    import org.jclouds.sshj.config.SshjSshClientModule;

    import com.google.common.base.Supplier;
    import com.google.common.collect.ImmutableSet;
    import com.google.common.io.Files;
    import com.google.inject.Module;

    public class Example {

     public static void main(String[] args) 
     {
      String provider = "google-compute-engine";
      String identity = "***@developer.gserviceaccount.com";
      String credential = "path to private key file ";
      credential = getCredentialFromJsonKeyFile(credential);

      Properties properties = new Properties();
      long scriptTimeout = TimeUnit.MILLISECONDS.convert(20, TimeUnit.MINUTES);
      properties.setProperty(TIMEOUT_SCRIPT_COMPLETE, scriptTimeout + "");
      Iterable<Module> modules = ImmutableSet.<Module> of(
            new SshjSshClientModule(),new SLF4JLoggingModule(),
            new EnterpriseConfigurationModule());

      ContextBuilder builder = ContextBuilder.newBuilder(provider)
                                             .credentials(identity, credential)
                                             .modules(modules)
                                             .overrides(properties);
      ComputeService compute=builder.buildView(ComputeServiceContext.class).getComputeService();

      Set<? extends Image> images = compute.listImages();
      System.out.printf(">> No of images %d%n", images.size());

      Set<? extends ComputeMetadata> nodes = compute.listNodes();
      System.out.printf(">> No of nodes/instances %d%n", nodes.size());

      compute.getContext().close();     
    }

    private static String getCredentialFromJsonKeyFile(String filename) {
      try {
         String fileContents = Files.toString(new File(filename), UTF_8);
         Supplier<Credentials> credentialSupplier = new GoogleCredentialsFromJson(fileContents);
         String credential = credentialSupplier.get().credential;
         return credential;
      } catch (IOException e) {
         System.err.println("Exception reading private key from '%s': " + filename);
         e.printStackTrace();
         System.exit(1);
         return null;
      }
    }
    }
  • This looks like an issue related to the naming jclouds assumes for nodes. Could you please share the entire stacktrace of the failure? Could this be the same issue as [JCLOUDS-908](https://issues.apache.org/jira/browse/JCLOUDS-908)? – Ignasi Barrera May 20 '15 at 08:35
  • i have added the stack trace. – Devansh Agarwal May 20 '15 at 11:21
  • It seems the same naming convention issue. Just to have the whole picture, can you also share the names of the nodes you have? – Ignasi Barrera May 20 '15 at 11:24
  • I am assuming nodes to be instances. Names of instances are => example-instance, instance-group-1-4bui, instance-group-1-wxml and test. – Devansh Agarwal May 20 '15 at 11:50
  • 1
    Yes. The issue will be with existing instances that weren't created by jclouds. Having their names will help confirm it is the same mentioned issue. – Ignasi Barrera May 20 '15 at 11:52
  • So what's the solution to it? Even createNodesInGroup(groupName, 1, template) gives same null pointer exception. – Devansh Agarwal May 20 '15 at 12:27
  • Which version of jclouds are you using? Unfortunately there may not be something that you can workaround, except by assigning the firewall tag names and instance names following the pattern that jclouds uses. – Ignasi Barrera May 20 '15 at 12:36
  • 1
    This is something to be fixed in JCLOUDS-908, so please, vote for the issue so it is properly fixed and allows working with existing tags and instances. – Ignasi Barrera May 20 '15 at 12:36
  • working with 1.9.0 version. – Devansh Agarwal May 20 '15 at 12:49
  • Could you add a comment in [JCLOUDS-908](https://issues.apache.org/jira/browse/JCLOUDS-908) with the names of the nodes you have and the firewall tags? That would help us reproduce the issue and create a patch sooner. Thanks! – Ignasi Barrera May 20 '15 at 20:31
  • Just FYI, there is a [patch in progress](https://github.com/jclouds/jclouds-labs-google/pull/139). Keep an eye on it and try the 1.9.1-SNAPSHOT version once the patch is merged. – Ignasi Barrera May 20 '15 at 21:11
  • "except by assigning the firewall tag names and instance names following the pattern that jclouds uses" - can you tell me about naming pattern jcloud uses and where to assign firewall tag names? – Devansh Agarwal May 21 '15 at 08:36
  • The problem was with naming of google cloud instance. The instance named - "test" was not according to the naming pattern of jcloud. – Devansh Agarwal May 21 '15 at 13:07
  • I'll try with instances named like that to verify the patch. Meanwhile, if you can build the linked patch branch and check if ti fixes your issue, the better! – Ignasi Barrera May 21 '15 at 15:40

0 Answers0