1 [root@controller ~]# cat flavor.py 2 from novaclient import client as nvclient 3 from novaclient import utils 4 from novaclient import base 5 6 from keystoneauth1.identity import v3 7 from keystoneauth1 import session 8 from keystoneclient.v3 import client 9 10 import time 11 import sys 12 import keystoneclient.v3.client as ksclient 13 from neutronclient.v2_0 import client as netclient 14 15 # Keystone get project_id 16 auth = v3.Password(user_domain_name='Default', 17 username='admin', 18 password='openstack', 19 project_domain_name='Default', 20 project_name='admin', 21 auth_url='http://controller:35357/v3') 22 sess = session.Session(auth=auth) 23 keystone = client.Client(session=sess) 24 25 projects_list = keystone.projects.list() 26 for o in projects_list: 27 name = getattr(o, "name", '') 28 if name == "admin": 29 project_id = getattr(o, "id", '') 30 break 31 32 # Nova 33 nova = nvclient.Client('2.42', 'admin', 'openstack', project_id, 'http://controller:5000/v2.0') 34 35 def flavors_list(): 36 flavors_list = nova.flavors.list() 37 print "================ list flavor ==============" 38 for data in flavors_list: 39 print "ID :" + data.id 40 print "Name:" + data.name 41 print "CPU :" + str(data.vcpus) 42 print "MEM :" + str(data.ram) 43 print "Disk:" + str(data.disk) 44 print "===========================================" 45 print "" 46 47 def flavors_create(name, ram, vcpus, disk): 48 print "=============== create flavor =============" 49 flavors_create = nova.flavors.create(name, ram, vcpus, disk, flavorid="auto", 50 ephemeral=0, swap=0, rxtx_factor=1.0, is_public=True) 51 print flavors_create 52 print "=================== end =================" 53 print "" 54 55 def set_key_flavor(name): 56 print "============== set key flavor =============" 57 key = {'hw:cpu_cores':'4', 'hw:cpu_sockets':'1'} 58 flavors_list = nova.flavors.list() 59 for data in flavors_list: 60 if data.name == name: 61 print data 62 data.set_keys(key) 63 print "=================== end =================" 64 print "" 65 66 def flavors_delete(name): 67 print "=============== delete flavor =============" 68 flavors_list = nova.flavors.list() 69 for data in flavors_list: 70 if data.name == name: 71 print data 72 nova.flavors.delete(data) 73 print "=================== end =================" 74 75 flavors_list() 76 flavors_create(name='flavor_demo', ram=4096, vcpus='4', disk=0) 77 set_key_flavor(name='flavor_demo') 78 flavors_list() 79 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 =================