jclouds with OpenStack => java.util.NoSuchElementException: apiType compute not found in catalog []

孤街浪徒 提交于 2019-12-24 15:23:07

问题


Java jclouds API fails to connect to an OpenStack provider.

Exception is thrown with the following message: java.util.NoSuchElementException: apiType compute not found in catalog [].

Other APIs (python-novaclient, ruby-fog) work just fine, so the problem looks language (API)-specific.

import static com.google.common.io.Closeables.closeQuietly;

import java.io.Closeable;
import java.util.Set;

import org.jclouds.ContextBuilder;
import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.domain.ComputeMetadata;
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
import org.jclouds.openstack.nova.v2_0.NovaApi;
import org.jclouds.openstack.nova.v2_0.NovaAsyncApi;
import org.jclouds.openstack.nova.v2_0.domain.Server;
import org.jclouds.openstack.nova.v2_0.features.ServerApi;
import org.jclouds.rest.RestContext;

import com.google.common.collect.ImmutableSet;
import com.google.inject.Module;

public class jcloudsOpenStack implements Closeable {
   private ComputeService compute;
   private RestContext nova;

   public static void main(String[] args) {
      jcloudsOpenStack jcloudOpenStack = new jcloudsOpenStack();

      try {
         jcloudOpenStack.init();
         jcloudOpenStack.listServers();
         jcloudOpenStack.close();
      }
      catch (Exception e) {
         e.printStackTrace();
      }
      finally {
         jcloudOpenStack.close();
      }
   }

   private void init() {
      Iterable modules = ImmutableSet. of(new SLF4JLoggingModule());

      String provider = "openstack-nova";
      String identity = "...";   // login name
      String password = "...";   // password

      ComputeServiceContext context = ContextBuilder.newBuilder(provider)
            .endpoint("https://UltiCloud.com:5000/v2.0/")
            .credentials(identity, password)
            .modules(modules)
            .buildView(ComputeServiceContext.class);
      compute = context.getComputeService();
      nova = context.unwrap();
   }

   private void listServers() {
      Set<? extends ComputeMetadata> nodes = compute.listNodes();
      System.out.println(nodes.size());
   }

   public void close() {
      closeQuietly(compute.getContext());
   }
}

Any help or a hint is greatly appreciated


回答1:


I got the same problem in last few days, and finally i got this resolved. you should use"username:tenantname" for parameters 'identity' of 'credentials' function. if you use "username" instead of "tenantname:username", jclouds would query tokens only instead of querying endpoint-list after token-querying, and the exception been thrown.

looks like this:

  ComputeServiceContext context = ContextBuilder.newBuilder(provider)
        .endpoint("https://UltiCloud.com:5000/v2.0/")
        .credentials("admin:admin", "123456")
        .modules(modules)
        .buildView(ComputeServiceContext.class);

not like this:

  ComputeServiceContext context = ContextBuilder.newBuilder(provider)
        .endpoint("https://UltiCloud.com:5000/v2.0/")
        .credentials("admin", "123456")
        .modules(modules)
        .buildView(ComputeServiceContext.class);

Hope this could help you guys




回答2:


You're missing the proper dependencies to run this. It's easiest to use Maven to get them.

The bare minimum you need for the example above is the openstack-nova dependency. To get it make a file called pom.xml and copy in this code.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <properties>
    <jclouds.version>1.7.0</jclouds.version>
  </properties>
  <groupId>org.apache.jclouds.examples</groupId>
  <artifactId>openstack-examples</artifactId>
  <version>1.0</version>
  <dependencies>
    <dependency>
      <groupId>org.apache.jclouds.api</groupId>
      <artifactId>openstack-nova</artifactId>
      <version>${jclouds.version}</version>
    </dependency>
  </dependencies>
</project>

Then run mvn dependency:copy-dependencies "-DoutputDirectory=./lib" that will download all of the dependencies you need to run your example.

Don't forget to include the lib dir in your path when you compile or run it like java -classpath ".:lib/*" jcloudsOpenStack

A more complete pom.xml file for working with all of the OpenStack services supported in jclouds follows.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven 4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <properties>
    <jclouds.version>1.7.0</jclouds.version>
  </properties>
  <groupId>org.apache.jclouds.examples</groupId>
  <artifactId>openstack-examples</artifactId>
  <version>1.0</version>
  <dependencies>
    <!-- jclouds dependencies -->
    <dependency>
      <groupId>org.apache.jclouds.driver</groupId>
      <artifactId>jclouds-slf4j</artifactId>
      <version>${jclouds.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.jclouds.driver</groupId>
      <artifactId>jclouds-sshj</artifactId>
      <version>${jclouds.version}</version>
    </dependency>
    <!-- OpenStack dependencies -->
    <dependency>
      <groupId>org.apache.jclouds.api</groupId>
      <artifactId>openstack-keystone</artifactId>
      <version>${jclouds.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.jclouds.api</groupId>
      <artifactId>openstack-nova</artifactId>
      <version>${jclouds.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.jclouds.api</groupId>
      <artifactId>swift</artifactId>
      <version>${jclouds.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.jclouds.api</groupId>
      <artifactId>openstack-cinder</artifactId>
      <version>${jclouds.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.jclouds.api</groupId>
      <artifactId>openstack-trove</artifactId>
      <version>${jclouds.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.jclouds.labs</groupId>
      <artifactId>openstack-glance</artifactId>
      <version>${jclouds.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.jclouds.labs</groupId>
      <artifactId>openstack-marconi</artifactId>
      <version>${jclouds.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.jclouds.labs</groupId>
      <artifactId>openstack-neutron</artifactId>
      <version>${jclouds.version}</version>
    </dependency>
    <!-- 3rd party dependencies -->
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.0.13</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.25</version>
    </dependency>
  </dependencies>
</project>


来源:https://stackoverflow.com/questions/21171261/jclouds-with-openstack-java-util-nosuchelementexception-apitype-compute-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!