Skip to content

Homework 4. Neutron API CURD

life1347 edited this page Jan 17, 2014 · 3 revisions

curd.py

import logging
import pprint
import time
import os
from time import sleep, asctime
from neutronclient.v2_0 import client as neclient
from novaclient.v1_1 import client as nclient

logging.basicConfig(level=logging.INFO)
logging.basicConfig(level=logging.DEBUG)

KEYSTONE_URL='http://openstack.nctu.edu.tw:5000/v2.0'

class CURD():
    def getCreds(self, user, pwd, tName):
        global KEYSTONE_URL
        creds = []
        creds.append(neclient.Client(auth_url=KEYSTONE_URL, username = user, tenant_name = tName, password = pwd))
        creds.append(nclient.Client(auth_url=KEYSTONE_URL, username = user, api_key = pwd, project_id = tName,service_type = 'compute'))
        return creds

    def createNetwork(self, neutron):
        router_name = '0256081_router'
        network_name = '0256081_network'
        subnet_name = '0256081_subnet'
        net = neutron.create_network({'network':{'name': network_name, 'admin_state_up': True, }})['network']['id']
        subnet = neutron.create_subnet({'subnet':{'name':subnet_name, 'network_id': net, 'ip_version': 4, 'cidr': '192.168.18.0/24', 'dns_nameservers': ['8.8.8.8']}})['subnet']['id']
        neutron.create_router({'router':{ 'name': router_name, 'admin_state_up' : True }})
        router = neutron.list_routers(name = router_name)['routers'][0]['id']
        ext_net = neutron.list_networks(name = 'ext-net')['networks'][0]['id']
        neutron.add_gateway_router(router,{'network_id': ext_net})
        neutron.add_interface_router(router,{'subnet_id': subnet})

        return {'net-id': net, 'r-id': router, 'subnet-id': subnet}

    def updateNetwork(self, neutron, netinfo):
        neutron.remove_gateway_router(netinfo['r-id'])
        ext_net = neutron.list_networks(name = 'CloudOS_2013')['networks'][0]['id']
        neutron.add_gateway_router(netinfo['r-id'],{'network_id': ext_net})

    def attatchNetwork(self, neutron, nova, netinfo):
        server = nova.servers.create(
                    name = 'newInstance', 
                    image = nova.images.find(name = "nginx-proxy-server-cloudOSproject").id,
                    flavor = nova.flavors.find(name = "cloudos_flavor").id, 
                    nics = [{'net-id' : netinfo['net-id']}])

        while server.status != 'ACTIVE':
                time.sleep(3)
                server = nova.servers.get(server.id)

    def deleteNetwork(self, neutron, nova, netinfo):
        nova.servers.find(name = 'newInstance').delete()
        neutron.remove_interface_router(netinfo['r-id'],{'subnet_id': netinfo['subnet-id']})
        neutron.delete_router(netinfo['r-id'])
        while True:
            try:
                neutron.delete_network(netinfo['net-id'])
                break
            except:
                time.sleep(3)
                pass
def main():
    curd = CURD()
    creds = curd.getCreds('xxx', 'xxx', 'xxx')
    raw_input('Press enter to create network: ')
    netinfo = curd.createNetwork(creds[0])
    raw_input('Press enter to update network: ')
    curd.updateNetwork(creds[0], netinfo)
    raw_input('Press enter to attatch network: ')
    curd.attatchNetwork(creds[0], creds[1], netinfo)
    raw_input('Press enter to delete network: ')
    curd.deleteNetwork(creds[0], creds[1], netinfo)

if __name__ == '__main__': main()

Before start

delete

Create network

delete

Update network external gateway from ext-net to CloudOS_2013

delete

Attach network to instance

delete

Delete network & instance

delete

Console

delete