jclouds在openstack中创建租户

时间:2014-08-01 11:52:48

标签: openstack jclouds

我正在使用jclouds 1.8.0在openstack中创建租户。 " TenantAdminApi"用于创建租户。

以下是我的代码段:

Optional<? extends TenantAdminApi> tennantAdminApiList =
keystoneApi.getTenantAdminApi();
if (tennantAdminApiList.isPresent()){
     System.out.println("tennantAdminApi is present");
    TenantAdminApi tennantAdminApi = tennantAdminApiList.get();
     CreateTenantOptions tenantOptions =
CreateTenantOptions.Builder.enabled(false);
     tennantAdminApi.create(name);
}else{
    System.out.println("tennantAdminApi not present");
}

但我无法创建租户作为&#34; isPresent&#34;旗帜是假的。 如何设置此标志

此致 AKHIL

2 个答案:

答案 0 :(得分:0)

如果我没错,您需要启用OS-KSADM扩展,并且您需要在创建上下文时提供管理端点(仔细检查您正在使用的端口)。

答案 1 :(得分:0)

使用此代码创建租户(在DevStack上使用OpenStack Icehouse进行测试)。

import com.google.common.base.Optional;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.Closeables;
import com.google.inject.Module;
import org.jclouds.ContextBuilder;
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
import org.jclouds.openstack.keystone.v2_0.KeystoneApi;
import org.jclouds.openstack.keystone.v2_0.extensions.TenantAdminApi;
import org.jclouds.openstack.keystone.v2_0.options.CreateTenantOptions;

import java.io.Closeable;
import java.io.IOException;

public class JCloudsKeystone implements Closeable {
    private final KeystoneApi keystoneApi;

    public static void main(String[] args) throws IOException {
        JCloudsKeystone jcloudsKeystone = new JCloudsKeystone();

        try {
            jcloudsKeystone.createTenant();
            jcloudsKeystone.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jcloudsKeystone.close();
        }
    }

    public JCloudsKeystone() {
        Iterable<Module> modules = ImmutableSet.<Module>of(new SLF4JLoggingModule());

        String provider = "openstack-keystone";
        String identity = "admin:admin"; // tenantName:userName
        String password = "devstack";

        keystoneApi = ContextBuilder.newBuilder(provider)
                .endpoint("http://my.ip.4.addr:35357/v2.0/")
                .credentials(identity, password)
                .modules(modules)
                .buildApi(KeystoneApi.class);
    }

    private void createTenant() {
        Optional<? extends TenantAdminApi> tenantAdminApiExtension = keystoneApi.getTenantAdminApi();

        if (tenantAdminApiExtension.isPresent()) {
            System.out.println("TenantAdminApi is present");

            TenantAdminApi tenantAdminApi = tenantAdminApiExtension.get();
            CreateTenantOptions tenantOptions = CreateTenantOptions.Builder.enabled(false);

            tenantAdminApi.create("newTenant", tenantOptions);
        }
        else {
            System.out.println("TenantAdminApi is *not* present");
        }
    }

    public void close() throws IOException {
        Closeables.close(keystoneApi, true);
    }
}

如果这对您不起作用,请确认您可以以管理员身份登录,并且该扩展程序在那里。

TOKEN=`curl -s -X POST http://my.ip.4.addr:5000/v2.0/tokens -d '{"auth": {"passwordCredentials": {"username":"admin", "password":"devstack"}, "tenantName":"admin"}}' -H "Content-type: application/json" |  jq -r .access.token.id`
curl -s -H "X-Auth-Token: $TOKEN" -H "Content-type: application/json" http://my.ip.4.addr:35357/v2.0/extensions | jq .

Here's jq。 OS-KSADM应该在输出中

{
  "description": "OpenStack extensions to Keystone v2.0 API enabling Administrative Operations.",
  "alias": "OS-KSADM",
  "namespace": "http://docs.openstack.org/identity/api/ext/OS-KSADM/v1.0",
  "links": [
    {
      "rel": "describedby",
      "type": "text/html",
      "href": "https://github.com/openstack/identity-api"
    }
  ],
  "name": "OpenStack Keystone Admin",
  "updated": "2013-07-11T17:14:00-00:00"
}