Nova 通过Python API 查询,创建,删除flavor

 [root@controller ~]# cat flavor.py
from novaclient import client as nvclient
from novaclient import utils
from novaclient import base from keystoneauth1.identity import v3
from keystoneauth1 import session
from keystoneclient.v3 import client import time
import sys
import keystoneclient.v3.client as ksclient
from neutronclient.v2_0 import client as netclient # Keystone get project_id
auth = v3.Password(user_domain_name='Default',
username='admin',
password='openstack',
project_domain_name='Default',
project_name='admin',
auth_url='http://controller:35357/v3')
sess = session.Session(auth=auth)
keystone = client.Client(session=sess) projects_list = keystone.projects.list()
for o in projects_list:
name = getattr(o, "name", '')
if name == "admin":
project_id = getattr(o, "id", '')
break # Nova
nova = nvclient.Client('2.42', 'admin', 'openstack', project_id, 'http://controller:5000/v2.0') def flavors_list():
flavors_list = nova.flavors.list()
print "================ list flavor =============="
for data in flavors_list:
print "ID :" + data.id
print "Name:" + data.name
print "CPU :" + str(data.vcpus)
print "MEM :" + str(data.ram)
print "Disk:" + str(data.disk)
print "==========================================="
print "" def flavors_create(name, ram, vcpus, disk):
print "=============== create flavor ============="
flavors_create = nova.flavors.create(name, ram, vcpus, disk, flavorid="auto",
ephemeral=0, swap=0, rxtx_factor=1.0, is_public=True)
print flavors_create
print "=================== end ================="
print "" def set_key_flavor(name):
print "============== set key flavor ============="
key = {'hw:cpu_cores':'', 'hw:cpu_sockets':''}
flavors_list = nova.flavors.list()
for data in flavors_list:
if data.name == name:
print data
data.set_keys(key)
print "=================== end ================="
print "" def flavors_delete(name):
print "=============== delete flavor ============="
flavors_list = nova.flavors.list()
for data in flavors_list:
if data.name == name:
print data
nova.flavors.delete(data)
print "=================== end =================" flavors_list()
flavors_create(name='flavor_demo', ram=4096, vcpus='', disk=0)
set_key_flavor(name='flavor_demo')
flavors_list()
flavors_delete(name='flavor_demo')

运行效果:

[root@controller ~]# python flavor.py
================ list flavor ==============

=============== create flavor =============
<Flavor: flavor_demo>
=================== end =================

============== set key flavor =============
<Flavor: flavor_demo>
=================== end =================

[root@controller ~]# vim flavor.py
[root@controller ~]# python flavor.py
================ list flavor ==============

=============== create flavor =============
<Flavor: flavor_demo>
=================== end =================

============== set key flavor =============
<Flavor: flavor_demo>
=================== end =================

================ list flavor ==============
ID :c2c99abd-82d2-4ef5-9656-10bfaca5b8e8
Name:flavor_demo
CPU :4
MEM :4096
Disk:0
===========================================

=============== delete flavor =============
<Flavor: flavor_demo>
=================== end =================

上一篇:海康相机之onvif测试工具使用


下一篇:Delphi安装/卸载OCX控件的方法