Adding api code for openstack modules

Change-Id: Icbba398a8ec1fe69fb93b3484a238059363a9f71
This commit is contained in:
Anand Shanmugam 2015-06-22 10:38:37 -07:00
parent 23bf677c0b
commit 544fe8e17c
7 changed files with 398 additions and 0 deletions

View File

View File

@ -0,0 +1,40 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from cinderclient.client import Client
class CinderHealth(object):
def __init__(self, creds):
self.cinderclient = Client(**creds)
def cinder_list(self):
try:
cinder_list = self.cinderclient.volumes.list()
except Exception as e:
return (404, e.message, [])
return (200, "success", cinder_list)
def cinder_volume_create(self, volume_name, volume_size):
try:
cinder_ret = self.cinderclient.volumes.create(volume_size,
name=volume_name)
except Exception as e:
return (404, e.message, [])
return (200, "success", cinder_ret)
def cinder_volume_delete(self, volume_id):
try:
cinder_ret = self.cinderclient.volumes.delete(volume_id)
except Exception as e:
return (404, e.message, [])
return (200, "success", cinder_ret)

View File

@ -0,0 +1,111 @@
# Copyright 2014 Cisco Systems, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# Module for credentials in Openstack
import getpass
import os
import re
class Credentials(object):
def get_credentials(self):
dct = {}
dct['username'] = self.rc_username
dct['password'] = self.rc_password
dct['auth_url'] = self.rc_auth_url
dct['tenant_name'] = self.rc_tenant_name
return dct
def get_nova_credentials(self):
dct = {}
dct['username'] = self.rc_username
dct['api_key'] = self.rc_password
dct['auth_url'] = self.rc_auth_url
dct['project_id'] = self.rc_tenant_name
return dct
def get_nova_credentials_v2(self):
dct = self.get_nova_credentials()
dct['version'] = 2
return dct
#
# Read a openrc file and take care of the password
# The 2 args are passed from the command line and can be None
#
def __init__(self, openrc_file, pwd, no_env):
self.rc_password = None
self.rc_username = None
self.rc_tenant_name = None
self.rc_auth_url = None
success = True
if openrc_file:
if os.path.exists(openrc_file):
export_re = re.compile('export OS_([A-Z_]*)="?(.*)')
for line in open(openrc_file):
line = line.strip()
mstr = export_re.match(line)
if mstr:
# get rif of posible trailing double quote
# the first one was removed by the re
name = mstr.group(1)
value = mstr.group(2)
if value.endswith('"'):
value = value[:-1]
# get rid of password assignment
# echo "Please enter your OpenStack Password: "
# read -sr OS_PASSWORD_INPUT
# export OS_PASSWORD=$OS_PASSWORD_INPUT
if value.startswith('$'):
continue
# now match against wanted variable names
if name == 'USERNAME':
self.rc_username = value
elif name == 'AUTH_URL':
self.rc_auth_url = value
elif name == 'TENANT_NAME':
self.rc_tenant_name = value
else:
print('Error: rc file does not exist %s' % (openrc_file))
success = False
elif not no_env:
# no openrc file passed - we assume the variables have been
# sourced by the calling shell
# just check that they are present
for varname in ['OS_USERNAME', 'OS_AUTH_URL', 'OS_TENANT_NAME']:
if varname not in os.environ:
print('Warning: %s is missing' % (varname))
success = False
if success:
self.rc_username = os.environ['OS_USERNAME']
self.rc_auth_url = os.environ['OS_AUTH_URL']
self.rc_tenant_name = os.environ['OS_TENANT_NAME']
# always override with CLI argument if provided
if pwd:
self.rc_password = pwd
# if password not know, check from env variable
elif self.rc_auth_url and not self.rc_password and success:
if 'OS_PASSWORD' in os.environ and not no_env:
self.rc_password = os.environ['OS_PASSWORD']
else:
# interactively ask for password
self.rc_password = getpass.getpass(
'Please enter your OpenStack Password: ')
if not self.rc_password:
self.rc_password = ""

View File

@ -0,0 +1,41 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from glanceclient.exc import ClientException
from glanceclient.v2 import client as glance_client
class GlanceHealth(object):
def __init__(self, keystone_instance):
authtoken = keystone_instance.keystone_return_authtoken()
glance_endpoint = (keystone_instance
.keystone_endpoint_find(service_type='image'))
self.glanceclient = glance_client.Client(glance_endpoint,
token=authtoken)
def glance_image_list(self):
try:
image_list = self.glanceclient.images.list()
except (ClientException, Exception) as e:
return (404, e.message, [])
return (200, "success", image_list)
def glance_image_create(self, image_url,
image_name, cont_format, disk_format):
try:
ret = self.glanceclient.images.create(location=image_url,
name=image_name,
container_format=cont_format,
disk_format=disk_format)
except (ClientException, Exception) as e:
return (404, e.message, [])
return (200, "success", ret)

View File

@ -0,0 +1,78 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from keystoneclient.exceptions import ClientException
from keystoneclient.v2_0 import client as keystone_client
class KeystoneHealth(object):
def __init__(self, creds):
self.keystoneclient = keystone_client.Client(**creds)
def keystone_service_list(self):
try:
service_list = self.keystoneclient.services.list()
except (ClientException, Exception) as e:
return (404, e.message, [])
return (200, "success", service_list)
def keystone_endpoint_find(self, service_type, endpoint_type='publicURL'):
return (self.keystoneclient
.service_catalog.url_for(
service_type=service_type,
endpoint_type=endpoint_type))
def keystone_return_authtoken(self):
return self.keystoneclient.auth_token
def _get_admin_user_id(self, admin_user):
try:
user_list = self.keystoneclient.users.list()
for user in user_list:
if user.name == admin_user:
return(200, "success", user.id)
except(ClientException, Exception) as e:
return (404, e.message, [])
return (404, "User not avaiable Failure", [])
def _get_admin_role_id(self, admin_role):
try:
role_list = self.keystoneclient.roles.list()
for role in role_list:
if role.name == admin_role:
return(200, "success", role.id)
except(ClientException, Exception) as e:
return (404, e.message, [])
return (404, "Role not avaiable Failure", [])
def keystone_tenant_create(self, tenant):
try:
tenant_res = self.keystoneclient.tenants.create(tenant_name=tenant,
enabled=True)
self._role_assign(tenant_res)
except (ClientException, Exception) as e:
return (404, e.message, [])
return (200, "success", tenant_res)
def _role_assign(self, tenant, role="admin", user="admin"):
role = self._get_admin_role_id("admin")[2]
user = self._get_admin_user_id("admin")[2]
self.keystoneclient.roles.add_user_role(user,
role,
tenant.id)
def keystone_tenant_delete(self, tenant_id):
try:
tenant_res = self.keystoneclient.tenants.delete(tenant_id)
except (ClientException, Exception) as e:
return (404, e.message, [])
return (200, "success", tenant_res)

View File

@ -0,0 +1,58 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from neutronclient.common.exceptions import NeutronException
from neutronclient.v2_0 import client as neutron_client
class NeutronHealth(object):
def __init__(self, creds):
creds['timeout'] = 30
self.neutronclient = neutron_client.Client(**creds)
def neutron_agent_list(self):
try:
agent_list = self.neutronclient.list_agents()
except (NeutronException, Exception) as e:
return (404, e.message, [])
return (200, "success", agent_list['agents'])
def network_create(self, network_name):
try:
self.neutronclient.format = 'json'
network = {'name': network_name, 'admin_state_up': True}
res = self.neutronclient.create_network({'network': network})
except (NeutronException, Exception) as e:
return (404, e.message, [])
return (200, "success", res)
def subnet_create(self, network_id, network_cidr):
try:
self.neutronclient.format = 'json'
subnet = {
"name": "cpulse_test",
"network_id": network_id,
"ip_version": 4,
"cidr": network_cidr
}
res = self.neutronclient.create_subnet({'subnet': subnet})
except (NeutronException, Exception) as e:
return (404, e.message, [])
return (200, "success", res)
def network_delete(self, network_id):
try:
self.neutronclient.format = 'json'
res = self.neutronclient.delete_network(network_id)
except (NeutronException, Exception) as e:
return (404, e.message, [])
return (200, "success", res)

View File

@ -0,0 +1,70 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from novaclient.client import Client
from novaclient.exceptions import ClientException
class NovaHealth(object):
def __init__(self, creden):
creden['timeout'] = 30
self.novaclient = Client(**creden)
def nova_service_list(self):
try:
service_list = self.novaclient.services.list()
except (ClientException, Exception) as e:
return (400, e.message, [])
return (200, "success", service_list)
def nova_create_server(self, server_name, image_name,
flavor_name, network_id):
try:
image = self.novaclient.images.find(name=image_name)
flavor = self.novaclient.flavors.find(name=flavor_name)
networks = [{'net-id': network_id}]
server_ret = self.novaclient.servers.create(server_name, image,
flavor, nics=networks)
except (ClientException, Exception) as e:
return (400, e.message, [])
return (200, "success", server_ret)
def nova_delete_server(self, server_id):
try:
ret = self.novaclient.servers.delete(server_id)
except (ClientException, Exception) as e:
return (400, e.message, [])
return (200, "success", ret)
def nova_get_server_state(self, server_id):
try:
ret = self.novaclient.servers.get(server_id)
except (ClientException, Exception) as e:
return (400, e.message, [])
return (200, "success", ret)
def nova_attach_volume(self, server_id, volume_id):
try:
ret = self.novaclient.volumes.create_server_volume(server_id,
volume_id,
"/dev/vdb")
except (ClientException, Exception) as e:
return (400, e.message, [])
return (200, "success", ret)
def nova_detach_volume(self, server_id, attachment_id):
try:
ret = self.novaclient.volumes.delete_server_volume(server_id,
attachment_id)
except (ClientException, Exception) as e:
return (400, e.message, [])
return (200, "success", ret)