不正狼 发表于 2015-10-11 07:19:38

openstack之tenant篇

  一、tenant简介
         这里我们介绍openstack的另一个重要的概念,tenant,在openstack的早期版本里,tenant又称作project,所以在平时的应用中,我们可以将tenant和project看作是相同的。
         在openstack.org的定义里,二者的定义是:
        project: A logical grouping of users within Nova, used to define quotas and access to virtual machine images.
        tenant: A group of users, used to isolate access to Nova resources, an alternative term for a Nova project.
     综合而言,一个tenant/project是多个用户的集合,openstack用它来定义这个组内用户的访问权限,包括能够访问的虚拟机镜像以及相关资源的使用限制(quota),例如能创建多少个instances,ram限制等。
        关于一个tenent的quotas,可以用以下命令取得(admin用户或属于该tenant的普通用户):
        # nova quota-show<tenant-id>

        至于某个tenant的id,又可以通过一下命令取得(admin用户):
         # keystone tenant-list
  
  二、tenant命令
        tenant命令需要admin权限才能够得以执行,同样执行这个命令我们可以仿照user篇的说明,定义环境变量OS_USERNAME,OS_PASSWORD,OS_TENANT_NAME,OS_AUTH_URL以简化命令行的输入。

            tenant命令列表可以通过keystone help取得,包括tenent-list, tenant-create, tenant-update, tenant-delete, tenant-get等,也就是我们常见的创建,更新,插入,删除等。
        新建:
        # keystone tenant-create --name <tenant-name> --description&quot;optionanl, description of the tenant &quot; --enabled true

     其中enabled选项可以是true或false,如果是false,则该tenant内所有用户将不可使用,无法通过keystone验证,这在管理员想要将一组用户同时禁用的情况下特别有用。
        更新:
        # keystone tenant-update --name <tenant_name>--description <tenant-description>--enabled <true|false>   <tenant-id>
        如命令所示,只要拿到一个tenant的id,就可以将该tenant的所有字段就行更新。
        删除:
        # keystone tenant-delete <tenant-id>
        详细信息:

        # keystone tenant-get <tenant-id>
        列出所有tenant:
        # keystone tenant-list

  

  三、tenant api
        这一节介绍编程方式实现tenant操作,以tenant-update为例。
        在openstack.org关于tenant update api的定义:

              
        也就是说,要用post 方式向 v2.0/tenants/{tenantId} 发送update tenant请求。
        tenant属于identity service的操作,也就是说endpoint要用keystone段的adminURL:
        而通过nove endpoints命令可以看到keystone的adminURL是:
        http://localhost:35357/v2.0
        因而,tenant update 操作相应的URL就是: http://localhost:35357/v2.0/tenants/{teant_id}, tenant_id是你想要update的tenant的id,可以通过keystone tenant-list取得。
        而post给URL的数据是:
        
1      
2345678{ &quot;tenant&quot;: { &quot;id&quot;:<tenant id>, &quot;name&quot;:&quot;new tenant name&quot;, &quot;description&quot;:&quot;new description ...&quot;, &quot;enabled&quot;: }}  
  
        这是一个json格式的数据,其中id即是上文中的tenant_id, 可以忽略,而其他字段可以根据需要修改。
        用代码实现:
         http_obj = httplib2.Http()
headers = {'X-Auth-Token':febd28f8277d48bbbd5c58e499d37fde,'
                  'Content-Type': 'application/json',
                     'Accept':'application/json}
headers['X-Auth-Token'] = 'febd28f8277d48bbbd5c58e499d37fde'

body = {
                &quot;tenant&quot;: {
                  &quot;id&quot;: '753ddbd344ce411aa8c53f9ae88d4284',
                  &quot;enabled&quot;: False,
                },
    }

req_url = &quot;http://localhost:35357/v2.0/tenants/753ddbd344ce411aa8c53f9ae88d4284&quot;
method = &quot;POST&quot;

headers['Content-Type'] = 'application/json'
headers['Accept'] = 'application/json'

resp, resp_body = http_obj.request(req_url, method,
                                 headers=headers, body=json.dumps(body))

        这个例子将把这个tenent disable,进而使这个tenant里的所有用户变为不可使用状态(仅限于改tenant里的user,换言之,如果一个用户同时又属于另一个tenant,该用户在另一个tenant里还是能够登录的)。这里需要用POST方式进行http请求,相对于PUT访问,需要定义http请求的body,把请求数据发送给需要访问的URL.
  

         版权声明:本文为博主原创文章,未经博主允许不得转载。
页: [1]
查看完整版本: openstack之tenant篇