与开栈=> java.util.NoSuchElementException jclouds:

2019-10-19 05:42发布

Java的API jclouds无法连接到OpenStack的供应商。

抛出异常以下消息:在产品目录中没有发现apiType计算[]:java.util.NoSuchElementException。

其他API(蟒蛇-novaclient,红宝石雾)工作得很好,所以这个问题看起来语言(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());
   }
}

任何帮助或提示是极大的赞赏

Answer 1:

我在最近几天得到了同样的问题,最后我得到这个解决。 你应该使用“用户名:tenantname”为“凭据”函数参数“身份”。 如果你使用的不是“tenantname:用户名”,“用户名”,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);

希望能帮助你们



Answer 2:

你错过了正确的依赖性运行此。 这是最容易使用的Maven得到它们。

您需要为上述例子中的最低限度是OpenStack的新星依赖。 为了得到它让一个名为的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

与所有的jclouds支持OpenStack的服务工作更完整的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>
    <!-- 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>


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