Java jclouds API无法连接到OpenStack提供程序。
抛出异常,并显示以下消息:java.util.NoSuchElementException:在catalog []中找不到apiType compute。
其他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());
}
}
非常感谢任何帮助或提示
答案 0 :(得分:3)
我在过去几天遇到了同样的问题,最后我解决了这个问题。 你应该使用&#34;用户名:tenantname&#34;参数&#39;身份&#39; &#39;凭证&#39;功能。 如果您使用&#34;用户名&#34;而不是&#34; tenantname:username&#34;,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);
希望这可以帮助你们
答案 1 :(得分: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>