├── .gitignore ├── README.md ├── common.py ├── cinder_example.py ├── glance_example.py ├── ceilometer_example.py ├── nova_example.py ├── neutron_example.py ├── keystone_example.py └── allinone.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | OpenStack API Examples 2 | ====================== 3 | 4 | This is a collection of minimal examples for the various OpenStack 5 | Python modules. 6 | 7 | -------------------------------------------------------------------------------- /common.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import os 4 | import sys 5 | import argparse 6 | 7 | def create_parser(): 8 | '''This creates a set of common command-line options that we will use 9 | in all of our examples.''' 10 | 11 | p = argparse.ArgumentParser() 12 | 13 | p.add_argument('--os-username', 14 | default=os.environ.get('OS_USERNAME')) 15 | p.add_argument('--os-password', 16 | default=os.environ.get('OS_PASSWORD')) 17 | p.add_argument('--os-tenant-name', 18 | default=os.environ.get('OS_TENANT_NAME')) 19 | p.add_argument('--os-tenant-id', 20 | default=os.environ.get('OS_TENANT_ID')) 21 | p.add_argument('--os-region-name', 22 | default=os.environ.get('OS_REGION_NAME')) 23 | p.add_argument('--os-auth-url', 24 | default=os.environ.get('OS_AUTH_URL')) 25 | 26 | return p 27 | 28 | -------------------------------------------------------------------------------- /cinder_example.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import os 4 | import sys 5 | import argparse 6 | 7 | from cinderclient.v2 import client as cinderclient 8 | 9 | # Import code common to all of the examples. 10 | import common 11 | import keystone_example 12 | 13 | def parse_args(): 14 | p = common.create_parser() 15 | return p.parse_args() 16 | 17 | def main(): 18 | args = parse_args() 19 | kc = keystone_example.get_keystone_client(args) 20 | 21 | # Find an endpoint for the 'image' service. 22 | endpoint = kc.service_catalog.url_for( 23 | service_type='volume', 24 | endpoint_type='publicURL') 25 | 26 | # The cinder library does not appear to know how to make use 27 | # of an existing Keystone auth_token, so we need to provide it with a 28 | # username and password. 29 | cc = cinderclient.Client( 30 | args.os_username, 31 | args.os_password, 32 | None, 33 | auth_url=args.os_auth_url, 34 | tenant_id=args.os_tenant_id) 35 | 36 | # Print information about available volumes. 37 | for volume in cc.volumes.list(): 38 | print volume.id, volume.name, volume.size 39 | 40 | if __name__ == '__main__': 41 | main() 42 | 43 | -------------------------------------------------------------------------------- /glance_example.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import os 4 | import sys 5 | import argparse 6 | 7 | from glanceclient.v2 import client as glanceclient 8 | 9 | # Import code common to all of the examples. 10 | import common 11 | import keystone_example 12 | 13 | def parse_args(): 14 | p = common.create_parser() 15 | return p.parse_args() 16 | 17 | def main(): 18 | args = parse_args() 19 | kc = keystone_example.get_keystone_client( 20 | os_username=args.os_username, 21 | os_password=args.os_password, 22 | os_tenant_name=args.os_tenant_name, 23 | os_tenant_id=args.os_tenant_id, 24 | os_auth_url=args.os_auth_url 25 | ) 26 | 27 | # Find an endpoint for the 'image' service. 28 | endpoint = kc.service_catalog.url_for( 29 | service_type='image', 30 | endpoint_type='publicURL') 31 | 32 | # Authenticate to glance using our Keystone token. 33 | gc = glanceclient.Client( 34 | endpoint=endpoint, 35 | token=kc.auth_token) 36 | 37 | # Print information about available images. 38 | for image in gc.images.list(): 39 | print image['id'], image['name'], image['disk_format'] 40 | 41 | if __name__ == '__main__': 42 | main() 43 | 44 | -------------------------------------------------------------------------------- /ceilometer_example.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import os 4 | import sys 5 | import argparse 6 | import logging 7 | 8 | from ceilometerclient.client import get_client 9 | 10 | # Import code common to all of the examples. 11 | import common 12 | import keystone_example 13 | 14 | 15 | def get_ceilometer_client(keystone_client): 16 | # Find an endpoint for the 'image' service. 17 | endpoint = keystone_client.service_catalog.url_for( 18 | service_type='metering', 19 | endpoint_type='publicURL') 20 | 21 | # Authenticate to neutron using our Keystone token. 22 | cc = get_client(2, 23 | ceilometer_url=endpoint, 24 | os_auth_token=keystone_client.auth_token, 25 | tenant_id=keystone_client.tenant_id, 26 | auth_url=keystone_client.auth_url) 27 | 28 | return cc 29 | 30 | def parse_args(): 31 | p = common.create_parser() 32 | return p.parse_args() 33 | 34 | def main(): 35 | logging.basicConfig(level=logging.DEBUG) 36 | 37 | args = parse_args() 38 | kc = keystone_example.get_keystone_client( 39 | os_username=args.os_username, 40 | os_password=args.os_password, 41 | os_tenant_name=args.os_tenant_name, 42 | os_tenant_id=args.os_tenant_id, 43 | os_auth_url=args.os_auth_url 44 | ) 45 | 46 | # Authenticate to neutron using our Keystone token. 47 | cc = get_ceilometer_client(kc) 48 | import pprint 49 | for sample in cc.samples.list(meter_name='cpu_util', limit=10): 50 | print sample.timestamp, sample.counter_name, sample.counter_volume 51 | 52 | if __name__ == '__main__': 53 | main() 54 | 55 | -------------------------------------------------------------------------------- /nova_example.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import os 4 | import sys 5 | import argparse 6 | 7 | from novaclient.v1_1 import client as novaclient 8 | 9 | # Import code common to all of the examples. 10 | import common 11 | import keystone_example 12 | 13 | def get_nova_client(keystone_client): 14 | # We pass username, password, and project_id as None 15 | # because we want to use our existing Keystone token rather 16 | # than having Nova acquire a new one. 17 | return novaclient.Client( 18 | None, 19 | None, 20 | None, 21 | auth_url=keystone_client.auth_url, 22 | tenant_id=keystone_client.tenant_id, 23 | auth_token=keystone_client.auth_token) 24 | 25 | 26 | def parse_args(): 27 | p = common.create_parser() 28 | p.add_argument('--all-tenants', 29 | action='store_true') 30 | return p.parse_args() 31 | 32 | def main(): 33 | args = parse_args() 34 | 35 | kc = keystone_example.get_keystone_client( 36 | os_username=args.os_username, 37 | os_password=args.os_password, 38 | os_tenant_name=args.os_tenant_name, 39 | os_tenant_id=args.os_tenant_id, 40 | os_auth_url=args.os_auth_url 41 | ) 42 | nc = get_nova_client(kc) 43 | 44 | # Print a list of running servers. 45 | for server in nc.servers.list(search_opts={'all_tenants': 46 | args.all_tenants}): 47 | print server.id, server.name 48 | for network_name, network in server.networks.items(): 49 | print ' ', network_name, ', '.join(network) 50 | 51 | if __name__ == '__main__': 52 | main() 53 | 54 | -------------------------------------------------------------------------------- /neutron_example.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import os 4 | import sys 5 | import argparse 6 | 7 | from neutronclient.common.exceptions import * 8 | import neutronclient.v2_0.client as neutronclient 9 | 10 | # Import code common to all of the examples. 11 | import common 12 | import keystone_example 13 | 14 | 15 | def get_neutron_client(keystone_client, args): 16 | # Find an endpoint for the 'image' service. 17 | endpoint = keystone_client.service_catalog.url_for( 18 | service_type='network', 19 | endpoint_type='publicURL') 20 | 21 | # Authenticate to neutron using our Keystone token. 22 | nc = neutronclient.Client( 23 | endpoint_url=endpoint, 24 | token=keystone_client.auth_token, 25 | tenant_id=args.os_tenant_id, 26 | auth_url=args.os_auth_url) 27 | 28 | return nc 29 | 30 | def parse_args(): 31 | p = common.create_parser() 32 | return p.parse_args() 33 | 34 | def main(): 35 | args = parse_args() 36 | kc = keystone_example.get_keystone_client( 37 | os_username=args.os_username, 38 | os_password=args.os_password, 39 | os_tenant_name=args.os_tenant_name, 40 | os_tenant_id=args.os_tenant_id, 41 | os_auth_url=args.os_auth_url 42 | ) 43 | 44 | # Find an endpoint for the 'image' service. 45 | endpoint = kc.service_catalog.url_for( 46 | service_type='network', 47 | endpoint_type='publicURL') 48 | 49 | # Authenticate to neutron using our Keystone token. 50 | nc = get_neutron_client(kc, args) 51 | 52 | networks = nc.list_networks() 53 | for network in networks.get('networks', []): 54 | print network['id'], network['name'] 55 | for subnet in network['subnets']: 56 | try: 57 | data = nc.show_subnet(subnet) 58 | print ' ', subnet, data['subnet']['cidr'] 59 | except NeutronClientException: 60 | print ' ', subnet, 'not found' 61 | 62 | 63 | if __name__ == '__main__': 64 | main() 65 | 66 | -------------------------------------------------------------------------------- /keystone_example.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import os 4 | import sys 5 | import argparse 6 | 7 | import keystoneclient.v2_0.client as ksclient 8 | 9 | # Import code common to all of the examples. 10 | import common 11 | 12 | def get_keystone_client(os_username=None, 13 | os_password=None, 14 | os_tenant_name=None, 15 | os_tenant_id=None, 16 | os_auth_url=None 17 | ): 18 | '''Returns a keystone client. Other examples that need an 19 | authenticated keystone client can simply: 20 | 21 | import common 22 | import keystone_example 23 | parser = common.create_parser() 24 | args = parser.parse_args() 25 | client = get_keystone_client( 26 | os_username=args.os_username, 27 | os_password=args.os_password, 28 | os_tenant_name=args.os_tenant_name, 29 | os_tenant_id=args.os_tenant_id, 30 | os_auth_url=args.os_auth_url 31 | ) 32 | ''' 33 | 34 | return ksclient.Client(username=os_username, 35 | password=os_password, 36 | tenant_name=os_tenant_name, 37 | tenant_id=os_tenant_id, 38 | auth_url=os_auth_url) 39 | 40 | 41 | 42 | def parse_args(): 43 | p = common.create_parser() 44 | return p.parse_args() 45 | 46 | def main(): 47 | args = parse_args() 48 | client = get_keystone_client( 49 | os_username=args.os_username, 50 | os_password=args.os_password, 51 | os_tenant_name=args.os_tenant_name, 52 | os_tenant_id=args.os_tenant_id, 53 | os_auth_url=args.os_auth_url 54 | ) 55 | 56 | # Iterate through the service catalog and display all endpoint 57 | # URLs for all services. 58 | for endpoint_type, endpoints in client.service_catalog.get_endpoints().items(): 59 | print endpoint_type 60 | for i, endpoint in enumerate(endpoints): 61 | for url in ['adminURL', 'internalURL', 'publicURL']: 62 | print ' ', i, url, endpoint[url] 63 | 64 | 65 | if __name__ == '__main__': 66 | main() 67 | 68 | -------------------------------------------------------------------------------- /allinone.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import logging 4 | import os 5 | 6 | import keystoneclient.auth as keystone_auth 7 | import keystoneclient.auth.identity as keystone_identity 8 | import keystoneclient.session as keystone_session 9 | import keystoneclient.client as keystone_client 10 | import novaclient.client as nova_client 11 | import glanceclient.client as glance_client 12 | import cinderclient.client as cinder_client 13 | import neutronclient.neutron.client as neutron_client 14 | import heatclient.client as heat_client 15 | 16 | LOG = logging.getLogger(__name__) 17 | 18 | 19 | class OpenStack(object): 20 | def __init__(self, 21 | sess=None, 22 | identity_api_version=None, 23 | username=None, 24 | password=None, 25 | tenant_name=None, 26 | tenant_id=None, 27 | user_domain_id=None, 28 | project_domain_id=None, 29 | auth_url=None): 30 | 31 | self.username = username or os.environ.get('OS_USERNAME') 32 | self.password = password or os.environ.get('OS_PASSWORD') 33 | self.tenant_name = tenant_name or os.environ.get('OS_TENANT_NAME') 34 | self.tenant_id = tenant_id or os.environ.get('OS_TENANT_ID') 35 | self.user_domain_id = ( 36 | user_domain_id or os.environ.get('OS_USER_DOMAIN_ID', 37 | 'default')) 38 | self.project_domain_id = ( 39 | project_domain_id or os.environ.get('OS_PROJECT_DOMAIN_ID', 40 | 'default')) 41 | self.auth_url = ( 42 | auth_url if auth_url else os.environ.get('OS_AUTH_URL')) 43 | 44 | self.identity_api_version = ( 45 | identity_api_version if identity_api_version 46 | else os.environ.get('OS_IDENTITY_API_VERSION')) 47 | 48 | self.sess = sess or self.get_session() 49 | 50 | def get_session(self): 51 | if self.auth_url is None: 52 | raise ValueError('Missing auth_url') 53 | 54 | if self.identity_api_version is None: 55 | self.identity_api_version = ( 56 | '2' if 'v2.0' in self.auth_url 57 | else '3') 58 | 59 | if self.identity_api_version == '3': 60 | LOG.info('using keystone v3 api') 61 | auth = self.get_keystone_v3_auth() 62 | else: 63 | LOG.info('using keystone v2 api') 64 | auth = self.get_keystone_v2_auth() 65 | 66 | # establish a keystone session 67 | sess = keystone_session.Session(auth=auth) 68 | 69 | return sess 70 | 71 | def get_keystone_v2_auth(self): 72 | return keystone_identity.v2.Password( 73 | auth_url=self.auth_url, 74 | username=self.username, 75 | password=self.password, 76 | tenant_name=self.tenant_name, 77 | tenant_id=self.tenant_id) 78 | 79 | def get_keystone_v3_auth(self): 80 | return keystone_identity.v3.Password( 81 | auth_url=self.auth_url, 82 | username=self.username, 83 | password=self.password, 84 | user_domain_id=self.user_domain_id, 85 | project_name=self.tenant_name, 86 | project_id=self.tenant_id, 87 | project_domain_id=self.project_domain_id) 88 | 89 | def get_keystone_client(self): 90 | kc = keystone_client.Client( 91 | self.identity_api_version, 92 | session=self.sess, 93 | project_id=self.sess.get_project_id(), 94 | tenant_id=self.sess.get_project_id(), 95 | auth_url=self.sess.get_endpoint( 96 | interface=keystone_auth.AUTH_INTERFACE)) 97 | 98 | kc.authenticate(token=self.sess.get_token()) 99 | 100 | return kc 101 | 102 | def get_nova_client(self): 103 | return nova_client.Client('2', session=self.sess) 104 | 105 | def get_glance_client(self): 106 | return glance_client.Client( 107 | '2', endpoint=self.keystone.service_catalog.url_for( 108 | service_type='image', 109 | endpoint_type='publicURL'), 110 | token=self.sess.get_token()) 111 | 112 | def get_cinder_client(self): 113 | return cinder_client.Client('2', session=self.sess) 114 | 115 | def get_neutron_client(self): 116 | return neutron_client.Client('2.0', session=self.sess) 117 | 118 | def get_heat_client(self): 119 | # XXX (Lars Kellogg-Stedman): Why is it necessary to specify 120 | # service_type here? Heat should already know it is the 121 | # orchestration service. 122 | return heat_client.Client( 123 | '1', service_type='orchestration', 124 | session=self.sess, 125 | endpoint=self.keystone.service_catalog.url_for( 126 | service_type='orchestration', 127 | endpoint_type='publicURL')) 128 | 129 | @property 130 | def token(self): 131 | return self.sess.get_token() 132 | 133 | @property 134 | def keystone(self): 135 | try: 136 | return self._keystone 137 | except AttributeError: 138 | self._keystone = self.get_keystone_client() 139 | return self._keystone 140 | 141 | @property 142 | def nova(self): 143 | try: 144 | return self._nova 145 | except AttributeError: 146 | self._nova = self.get_nova_client() 147 | return self._nova 148 | 149 | @property 150 | def glance(self): 151 | try: 152 | return self._glance 153 | except AttributeError: 154 | self._glance = self.get_glance_client() 155 | return self._glance 156 | 157 | @property 158 | def cinder(self): 159 | try: 160 | return self._cinder 161 | except AttributeError: 162 | self._cinder = self.get_cinder_client() 163 | return self._cinder 164 | 165 | @property 166 | def neutron(self): 167 | try: 168 | return self._neutron 169 | except AttributeError: 170 | self._neutron = self.get_neutron_client() 171 | return self._neutron 172 | 173 | @property 174 | def heat(self): 175 | try: 176 | return self._heat 177 | except AttributeError: 178 | self._heat = self.get_heat_client() 179 | return self._heat 180 | 181 | 182 | if __name__ == '__main__': 183 | logging.basicConfig(level='DEBUG') 184 | clients = OpenStack() 185 | --------------------------------------------------------------------------------