文档章节

openstack neutronclient接口学习

alazyer
 alazyer
发布于 2016/08/25 19:24
字数 611
阅读 84
收藏 0

实例化neutronclient

from os import environ as env

# 方式一
from neutronclient.v2_0 import client
neutron = client.Client(username=os.environ['OS_USERNAME']
                        password=os.environ['OS_PASSWORD']
                        auth_url=os.environ['OS_AUTH_URL']
                        tenant_name=os.environ['OS_TENANT_NAME'])

# 方式二
from neutronclient.neutron import client as client
neutron = client.Client('2.0', 
                        username=os.environ['OS_USERNAME']
                        password=os.environ['OS_PASSWORD']
                        auth_url=os.environ['OS_AUTH_URL']
                        tenant_name=os.environ['OS_TENANT_NAME'])

api调用实例

neutron中有一下几种objects:

  • network
  • subnet
  • port
  • router
  • floatingip
  • security_group
  • vpnservices
  • 等等

所有的objects都具有的动作包含:

  • show
    • 查看详情
  • list
    • 查看全部
  • create
    • 新建
  • update
    • 更新

获取所有network信息

# 获取所有network实例
networks = neutron.list_networks()
"""
{
    'networks': [
        {
            u'admin_state_up': True,
            u'id': u'd6e83c1b-1b16-470d-8006-18ee19f6dafe',
            u'mtu': 0,
            u'name': u'ext-net',
            u'provider:network_type': u'flat',
            u'provider:physical_network': u'external',
            u'provider:segmentation_id': None,
            u'router:external': True,
            u'shared': True,
            u'status': u'ACTIVE',
            u'subnets': [u'3ee3f602-86cc-44e9-8fd3-1ecd454760da'],
            u'tenant_id': u'65f273a79a834498a87f3c51bd35bebf'
        },
        {
            u'admin_state_up': True,
            u'id': u'd6e83c1b-1b16-470d-8006-18ee19f6dafe',
            u'mtu': 0,
            u'name': u'ext-net',
            u'provider:network_type': u'flat',
            u'provider:physical_network': u'external',
            u'provider:segmentation_id': None,
            u'router:external': True,
            u'shared': True,
            u'status': u'ACTIVE',
            u'subnets': [u'3ee3f602-86cc-44e9-8fd3-1ecd454760da'],
            u'tenant_id': u'65f273a79a834498a87f3c51bd35bebf'
        },
    ]
}
"""

# 依据条件过滤,条件是返回结果中network实例的属性
# 例如,依据provider:physical_network属性来过滤所有可以通外网的network
filters = {}
filters["provider:physical_network"] = "external"
networks = neutron.list_networks(**filters)

创建network

body_data = {
    'network': {
        "name": "test_creating_network",
        "admin_state_up": True
        "router:external": True,
        "provider:physical_network": "external",
        "provider:network_type": "flat",
    }
}

netw = neutron.create_network(body=body_data)
"""
{
    "network": {
        "status": "ACTIVE",
        "subnets": [],
        "name": "net1",
        "admin_state_up": true,
        "tenant_id": "9bacb3c5d39d41a79512987f338cf177",
        "router:external": True,
        "provider:physical_network": "external",
        "provider:network_type": "flat",
        "segments": [
            {
                "provider:segmentation_id": 2,
                "provider:physical_network": "8bab8453-1bc9-45af-8c70-f83aa9b50453",
                "provider:network_type": "vlan"
            },
            {
                "provider:segmentation_id": null,
                "provider:physical_network": "8bab8453-1bc9-45af-8c70-f83aa9b50453",
                "provider:network_type": "stt"
            }
        ],
        "shared": false,
        "id": "4e8e5957-649f-477b-9e5b-f1f75b21c03c"
    }
}
"""

删除network

body_data = {
    'network': {
        "name": "test_creating_network",
        "admin_state_up": True
        "router:external": True,
        "provider:physical_network": "external",
        "provider:network_type": "flat",
    }
}

netw = neutron.create_network(body=body_data)
net_dict = netw['network']
network_id = net_dict['id']
neutron.delete_network(network_id)

更新network

body_data = {
    'network': {
        "name": "test_creating_network_renew",
    }
}

netw = neutron.update_network(network_id, body=body_data)

查看所有的subnet

subnets = neutron.list_subnets()
"""
{
    "subnets": [
        {
            "name": "private-subnet",
            "enable_dhcp": true,
            "network_id": "db193ab3-96e3-4cb3-8fc5-05f4296d0324",
            "tenant_id": "26a7980765d0414dbc1fc1f88cdb7e6e",
            "dns_nameservers": [],
            "allocation_pools": [
                {
                    "start": "10.0.0.2",
                    "end": "10.0.0.254"
                }
            ],
            "host_routes": [],
            "ip_version": 4,
            "gateway_ip": "10.0.0.1",
            "cidr": "10.0.0.0/24",
            "id": "08eae331-0402-425a-923c-34f7cfe39c1b"
        },
        {
            "name": "my_subnet",
            "enable_dhcp": true,
            "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
            "tenant_id": "4fd44f30292945e481c7b8a0c8908869",
            "dns_nameservers": [],
            "allocation_pools": [
                {
                    "start": "192.0.0.2",
                    "end": "192.255.255.254"
                }
            ],
            "host_routes": [],
            "ip_version": 4,
            "gateway_ip": "192.0.0.1",
            "cidr": "192.0.0.0/8",
            "id": "54d6f61d-db07-451c-9ab3-b9609b6b6f0b"
        }
    ]
}
"""

创建subnet

start = '192.168.199.2'
end = '192.168.199.254'
allocation_pools = {'start': start, 'end': end}
gateway_ip = '192.168.199.1'

body_data = {
    'subnets':{
        'network_id': network_id,
        'cidr': '192.168.199.0/24',
        'ip_version': 4,
        "enable_dhcp": False,
        "allocation_pools": allocation_pools,
        "gateway_ip": gateway_ip,
    }
}

updated = neutron.update_subnet(subnet_id, body=body_data)

ret = neutron.create_subnet(body=body_data)
"""
{
    "subnet": {
        "name": "",
        "enable_dhcp": false,
        "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
        "tenant_id": "4fd44f30292945e481c7b8a0c8908869",
        "dns_nameservers": [],
        "allocation_pools": [
            {
                "start": "192.168.199.2",
                "end": "192.168.199.254"
            }
        ],
        "host_routes": [],
        "ip_version": 4,
        "gateway_ip": "192.168.199.1",
        "cidr": "192.168.199.0/24",
        "id": "3b80198d-4f7b-4f77-9ef5-774d54e17126"
    }
}
"""

查看所有的port

neutron.list_port()
"""
{
    "ports": [
        {
            "status": "ACTIVE",
            "name": "",
            "allowed_address_pairs": [],
            "admin_state_up": true,
            "network_id": "70c1db1f-b701-45bd-96e0-a313ee3430b3",
            "tenant_id": "",
            "extra_dhcp_opts": [],
            "device_owner": "network:router_gateway",
            "mac_address": "fa:16:3e:58:42:ed",
            "fixed_ips": [
                {
                    "subnet_id": "008ba151-0b8c-4a67-98b5-0d2b87666062",
                    "ip_address": "172.24.4.2"
                }
            ],
            "id": "d80b1a3b-4fc1-49f3-952e-1e2ab7081d8b",
            "security_groups": [],
            "device_id": "9ae135f4-b6e0-4dad-9e91-3c223e385824"
        },
        {
            "status": "ACTIVE",
            "name": "",
            "allowed_address_pairs": [],
            "admin_state_up": true,
            "network_id": "f27aa545-cbdd-4907-b0c6-c9e8b039dcc2",
            "tenant_id": "d397de8a63f341818f198abb0966f6f3",
            "extra_dhcp_opts": [],
            "device_owner": "network:router_interface",
            "mac_address": "fa:16:3e:bb:3c:e4",
            "fixed_ips": [
                {
                    "subnet_id": "288bf4a1-51ba-43b6-9d0a-520e9005db17",
                    "ip_address": "10.0.0.1"
                }
            ],
            "id": "f71a6703-d6de-4be1-a91a-a570ede1d159",
            "security_groups": [],
            "device_id": "9ae135f4-b6e0-4dad-9e91-3c223e385824"
        }
    ]
}
"""

© 著作权归作者所有

共有 人打赏支持
alazyer
粉丝 5
博文 68
码字总数 35105
作品 0
程序员
私信 提问
如何通过OpenStack命令行管理VMs

I assume that there is an OpenStack deployment already up and running somewhere. I am going to use OpenStack CLI clients to manage VMs on the existing OpenStack setup. Install O......

yuyanghuoming
2014/04/20
0
1
openstack neutron-server.service启动出错,求大神帮助,小白跪谢。

系统: RHEL7.1 ip:192.168.10.101 rpm安装openstack k版,mysql、rabbit、keystone、glance、nova均已正常,但neutron.server无法启动,数据库已同步。 neutron安装过程: yum install -y o...

小白的落寞
2017/02/24
925
2
openstack学习之路-level1-1

Openstack各组件介绍 Dashboard 项目名horizon 提供openstack的交互界面,可以在交互界面中管理openstack,比如启动一个实例,配置IP地址,配置用户认证等 Compute 项目名nova 负责openstack...

gyj0825
2018/06/26
0
0
OSC 第 137 期高手问答 — 关于 OpenStack 运维部署

OSCHINA 本期高手问答(2016 年 12 月 15 日 — 12 月 21 日)我们请来了@Jeffrey4l(张雷)为大家解答关于 OpenStack 运维部署相关的问题。 张雷,九州云 OpenStack 社区总监、OpenStack K...

局长
2016/12/14
2.6K
10
openstack学习之路-level1-1

Openstack各组件介绍 Dashboard 项目名horizon 提供openstack的交互界面,可以在交互界面中管理openstack,比如启动一个实例,配置IP地址,配置用户认证等 Compute 项目名nova 负责openstack...

gyj0825
2018/06/26
0
0

没有更多内容

加载失败,请刷新页面

加载更多

想问一下C++里queue要怎么遍历

如题,想知道怎么遍历<queue>对象的元素? 貌似不能遍历。要么全部pop push一遍,要么换个容器呗。 queue是先进后出的数据类型,只能不断读top()然后再pop()掉。故意把遍历操作隐藏掉了,...

shzwork
昨天
2
0
Ubuntu 18.04.2 LTS nvidia-docker2 : 依赖: docker-ce (= 5:18.09.0~3-0~ubuntu-bionic)

平台:Ubuntu 18.04.2 LTS nvidia-docker2 版本:2.0.3 错误描述:在安装nvidia-docker2的时候报dpkg依赖错误 nvidia-docker2 : 依赖: docker-ce (= 5:18.09.0~3-0~ubuntu-bionic) 先看一下依......

Pulsar-V
昨天
2
0
学习笔记1-goland结构体(struct)

写在前面:若有侵权,请发邮件by.su@qq.com告知。 转载者告知:如果本文被转载,但凡涉及到侵权相关事宜,转载者需负责。请知悉! 本文永久更新地址:https://my.oschina.net/bysu/blog/3036...

不最醉不龟归
昨天
3
0
【转】go get命令使用socket代理

由于某些不可描述的原因,国内使用go get命令安装某些包的时候会超时导致失败,比如net包、sys包、tools包等。第一种解决办法就是自己从git上下载后添加链接到GOPATH中,比如: 1234...

yiduwangkai
昨天
6
0
从上往下打印出二叉树的每个节点,同层节点从左至右打印。

//第一种做法 public class Solution { public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) { ArrayList <Integer> li=new ArrayList<Integer>(); ArrayList <TreeN......

南桥北木
昨天
3
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部