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

 1 [[email protected] ~]# 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‘)

运行效果:

[[email protected] ~]# python flavor.py
================ list flavor ==============

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

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

[[email protected] ~]# vim flavor.py
[[email protected] ~]# 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 =================

原文地址:https://www.cnblogs.com/wangjq19920210/p/9099591.html

时间: 2024-11-05 15:46:54

Nova 通过Python API 查询,创建,删除flavor的相关文章

临时表空间管理(查询-创建-删除等)

临时表空间概念 临时表空间用来管理数据库排序操作以及用于存储临时表.中间排序结果等临时对象,当ORACLE里需要用到SORT的时候,并且当PGA中sort_area_size大小不够时,将会把数据放入临时表空间里进行排序.像数据库中一些操作: CREATE INDEX. ANALYZE.SELECT DISTINCT.ORDER BY.GROUP BY. UNION ALL. INTERSECT.MINUS.SORT-MERGE JOINS.HASH JOIN等都可能会用到临时表空间.当操作完成

Python mysql-表的创建,删除和更新

2017-09-06 20:59:56 数据库的创建 CREATE DATEBASE <数据库的名称> 表的创建 CREATE TABLE <表名> (<列名1> <数据类型> <该列所需的约束>, <列名2> <数据类型> <该列所需的约束>, . <该表的约束1>,<该表的约束2>,......); import pymysql db = pymysql.connect("

elasticsearch 中文API 基于查询的删除(九)

基于查询的删除API 基于查询的删除API允许开发者基于查询删除一个或者多个索引.一个或者多个类型.下面是一个例子. import static org.elasticsearch.index.query.FilterBuilders.*; import static org.elasticsearch.index.query.QueryBuilders.*; DeleteByQueryResponse response = client.prepareDeleteByQuery("test&q

Elasticsearch Java API (二): index创建删除 cluster管理

Elasticsearch Java API (二): index创建删除 cluster管理  elastic官网有权威的java api 英文的 需要耐心看 这里整理下基本操作 创建maven工程添加依赖 <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>5.2.2</ver

使用python调用和风天气API查询当前天气信息

和风天气网址:https://www.heweather.com/ 注册后在控制台会有个人认证key API帮助文档:https://www.heweather.com/documents/api/v5 #!/usr/bin/python #-*-coding:utf-8-*- #调用和风天气API查询当前天气信息 #2017/07/12 import json import urllib2 import os city='北京' #在和风天气注册后获得的key key='your key' c

《Spark Python API 官方文档中文版》 之 pyspark.sql (一)

摘要:在Spark开发中,由于需要用Python实现,发现API与Scala的略有不同,而Python API的中文资料相对很少.每次去查英文版API的说明相对比较慢,还是中文版比较容易get到所需,所以利用闲暇之余将官方文档翻译为中文版,并亲测Demo的代码.在此记录一下,希望对那些对Spark感兴趣和从事大数据开发的人员提供有价值的中文资料,对PySpark开发人员的工作和学习有所帮助. 官网地址:http://spark.apache.org/docs/1.6.2/api/python/p

初识Django —Python API接口编程入门

初识Django -Python API接口编程入门 一.WEB架构的简单介绍 Django是什么? Django是一个开放源代码的Web应用框架,由Python写成.我们的目标是用Python语言,基于Django框架,利用MVC模型,实现后台方面的针对数据库的API开发.先了解一下互联网的WEB架构, 如上图: 互联网的WEB架构大致分为三层,web层.app层和数据库层.Web层:如apache网站服务器:app层主要是应用业务:DB指后台数据库.随着互联网的高速发展,网站访问量的增长.数

The novaclient Python API

The novaclient Python API Usage First create a client instance with your credentials: >>> from novaclient.client import Client >>> nova = Client(VERSION, USERNAME, PASSWORD, PROJECT_ID, AUTH_URL) Here VERSION can be: 1.1, 2 and 3. Altern

Openstack python api 学习文档

Openstack python api 学习文档 转载请注明http://www.cnblogs.com/juandx/p/4953191.html 因为需要学习使用api接口调用openstack,所以上一篇写了一些使用openstack的纯api调用的方法, 但是openstack还提供了更好的python的api,只需要python的包即可,感觉更好使用. 对于compute的api,包是放在了/usr/lib/python2.7/site-packages/novaclient/目录,