Ask Your Question
0

how to execute python API's for load balancer?

asked 2014-12-29 03:12:50 -0500

amitabh sinha gravatar image

I want to execute python API'S for balancer. please guide and share link or document if anybody have it.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2014-12-30 01:27:07 -0500

Ranjit gravatar image

updated 2014-12-30 01:29:06 -0500

link for all the neutron API: http://developer.openstack.org/api-re...

below are some ways to call API:

  • Using curl

1.Get the keystone token

curl -i 'http://127.0.0.1:5000/v2.0/tokens' -X POST -H "Content-Type: application/json" -H "Accept: application/json"  -d '{"auth": {"tenantName": "admin", "passwordCredentials": {"username": "admin", "password": "admin_pass"}}}'

{"access": {"token": {"issued_at": "2014-12-30T06:56:08.718473", "expires": "2014-12-30T07:56:08Z", "id": "afc877f644b94eed95b71100eab17bb9", "tenant": {"description": "Admin Tenant", "enabled": true, "id": "fd987a8fb1b845fbb04b28e8f680d040", "name": "admin"},

2.Use that token in curl request

    curl -H "Content-Type: application/json" -H "Accept: application/json" -H "X-Auth-Token:afc877f644b94eed95b71100eab17bb9" http://openstackJuno:9696/v2.0/networks

{"networks": [{"status": "ACTIVE", "subnets": ["a285caf5-f032-46e6-b359-34029269671a"], "name": "flatNetwork", "provider:physical_network": "ph-enp4s0f1", "admin_state_up": true, "tenant_id": "fd987a8fb1b845fbb04b28e8f680d040", "provider:network_type": "flat", "router:external": false, "shared": false, "id": "18cb4b31-d131-42d7-bcd2-b2ec92a40e7d", "provider:segmentation_id": null}]}
  • in python code

neutron API will be used by importing neutronclient module.

Interface for all the API (including load-balancer API's) are in the file: (neutronclient Installation dir)/v2_0/client.py

sample code for calling API which will list all the subnets. Before calling this script you need to set the keystone credentials.

#!/usr/bin/env python

import os
from oslo.config import cfg
from neutronclient.v2_0 import client

def get_keystone_creds():
    d = {}
    d['username'] = os.environ['OS_USERNAME']
    d['password'] = os.environ['OS_PASSWORD']
    d['auth_url'] = os.environ['OS_AUTH_URL']
    d['tenant_name'] = os.environ['OS_TENANT_NAME']
    return d

def listNetwork():
    creds = get_keystone_creds()
    myNeutronClient = client.Client(**creds)
    print myNeutronClient.list_subnets()


if __name__ == "__main__":
    listNetwork()



[root@openstackJuno ~]# python networkTest.py 
{'subnets': [{u'name': u'flatSubNet', u'enable_dhcp': True, u'network_id': u'18cb4b31-d131-42d7-bcd2-b2ec92a40e7d', u'tenant_id': u'fd987a8fb1b845fbb04b28e8f680d040', u'dns_nameservers': [], u'ipv6_ra_mode': None, u'allocation_pools': [{u'start': u'192.168.1.10', u'end': u'192.168.1.20'}], u'gateway_ip': u'192.168.1.1', u'ipv6_address_mode': None, u'ip_version': 4, u'host_routes': [], u'cidr': u'192.168.1.0/24', u'id': u'a285caf5-f032-46e6-b359-34029269671a'}]}
edit flag offensive delete link more

Comments

thanks for help! It worked :-)

amitabh sinha gravatar imageamitabh sinha ( 2014-12-30 23:07:23 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Get to know Ask OpenStack

Resources for moderators

Question Tools

1 follower

Stats

Asked: 2014-12-29 03:12:50 -0500

Seen: 993 times

Last updated: Dec 30 '14