2

Java jclouds API 无法连接到 OpenStack 提供程序。

抛出异常并显示以下消息:java.util.NoSuchElementException:在目录 [] 中找不到 apiType 计算。

其他 API(python-novaclient、ruby-fog)工作得很好,所以问题看起来是特定于语言 (API) 的。

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());
   }
}

非常感谢任何帮助或提示

4

2 回答 2

3

这几天我也遇到了同样的问题,终于解决了。您应该使用“用户名:租户名称”作为“凭据”函数的参数“身份”。如果您使用“用户名”而不是“租户名:用户名”,jclouds 将只查询令牌,而不是在令牌查询后查询端点列表,并引发异常。

看起来像这样:

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

不是这样的:

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

希望这可以帮助你们

于 2015-11-20T08:06:32.733 回答
0

您缺少正确的依赖项来运行它。使用Maven获取它们是最简单的。

上述示例所需的最低限度是 openstack-nova 依赖项。要获取它,请创建一个名为 pom.xml 的文件并复制此代码。

<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>

然后运行mvn dependency:copy-dependencies "-DoutputDirectory=./lib"它将下载运行示例所需的所有依赖项。

编译或运行时不要忘记在路径中包含 lib 目录java -classpath ".:lib/*" jcloudsOpenStack

下面是一个更完整的 pom.xml 文件,用于处理 jclouds 支持的所有 OpenStack 服务。

<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>
于 2014-01-17T02:35:32.990 回答