From 0b2b804cef7be99bb672f32a016b9b3a01ceaade Mon Sep 17 00:00:00 2001 From: Tony Mack Date: Sun, 7 Apr 2013 19:55:27 -0400 Subject: [PATCH] initial checkin --- plstackapi/openstack/client.py | 123 +++++++++++++++++++++++ plstackapi/planetstack/api/__init__.py | 0 plstackapi/planetstack/api/roles.py | 34 +++++++ plstackapi/planetstack/views/__init__.py | 0 plstackapi/planetstack/views/api_root.py | 12 +++ 5 files changed, 169 insertions(+) create mode 100644 plstackapi/openstack/client.py create mode 100644 plstackapi/planetstack/api/__init__.py create mode 100644 plstackapi/planetstack/api/roles.py create mode 100644 plstackapi/planetstack/views/__init__.py create mode 100644 plstackapi/planetstack/views/api_root.py diff --git a/plstackapi/openstack/client.py b/plstackapi/openstack/client.py new file mode 100644 index 0000000..04aef25 --- /dev/null +++ b/plstackapi/openstack/client.py @@ -0,0 +1,123 @@ +from keystoneclient.v2_0 import client as keystone_client +from glance import client as glance_client +from novaclient.v1_1 import client as nova_client +from quantumclient.v2_0 import client as quantum_client + +from plstackapi.planetstack.config import Config + +def parse_novarc(filename): + opts = {} + f = open(filename, 'r') + for line in f: + try: + line = line.replace('export', '').strip() + parts = line.split('=') + if len(parts) > 1: + value = parts[1].replace("\'", "") + value = value.replace('\"', '') + opts[parts[0]] = value + except: + pass + f.close() + return opts + +class Client: + def __init__(self, username=None, password=None, tenant=None, url=None, config=None, *args, **kwds): + if config: + config = Config(config) + else: + config = Config() + self.username = config.nova_admin_user + self.password = config.nova_admin_password + self.tenant = config.nova_admin_tenant + self.url = config.nova_url + + if username: + self.username = username + if password: + self.password = password + if tenant: + self.tenant = tenant + if url: + self.url = url + + if '@' in self.username: + self.username = self.username[:self.username.index('@')] + +class KeystoneClient(Client): + def __init__(self, *args, **kwds): + Client.__init__(self, *args, **kwds) + self.client = keystone_client.Client(username=self.username, + password=self.password, + tenant_name=self.tenant, + auth_url=self.url) + + def connect(self, *args, **kwds): + self.__init__(*args, **kwds) + + def __getattr__(self, name): + return getattr(self.client, name) + + +class GlanceClient(Client): + def __init__(self, *args, **kwds): + Client.__init__(self, *args, **kwds) + self.client = glance_client.get_client(host='0.0.0.0', + username=self.username, + password=self.password, + tenant=self.tenant, + auth_url=self.url) + def __getattr__(self, name): + return getattr(self.client, name) + +class NovaClient(Client): + def __init__(self, *args, **kwds): + Client.__init__(self, *args, **kwds) + self.client = nova_client.Client(username=self.username, + api_key=self.password, + project_id=self.tenant, + auth_url=self.url, + region_name='', + extensions=[], + service_type='compute', + service_name='', + ) + + def connect(self, *args, **kwds): + self.__init__(*args, **kwds) + + def __getattr__(self, name): + return getattr(self.client, name) + +class QuantumClient(Client): + def __init__(self, *args, **kwds): + Client.__init__(self, *args, **kwds) + self.client = quantum_client.Client(username=self.username, + password=self.password, + tenant_name=self.tenant, + auth_url=self.url) + def connect(self, *args, **kwds): + self.__init__(*args, **kwds) + + def __getattr__(self, name): + return getattr(self.client, name) + +class OpenStackClient: + """ + A simple native shell to the openstack backend services. + This class can receive all nova calls to the underlying testbed + """ + + def __init__ ( self, *args, **kwds) : + # instantiate managers + self.keystone = KeystoneClient(*args, **kwds) + self.glance = GlanceClient(*args, **kwds) + self.nova = NovaClient(*args, **kwds) + self.quantum = QuantumClient(*args, **kwds) + + def connect(self, *args, **kwds): + self.__init__(*args, **kwds) + + + def authenticate(self): + return self.keystone.authenticate() diff --git a/plstackapi/planetstack/api/__init__.py b/plstackapi/planetstack/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/plstackapi/planetstack/api/roles.py b/plstackapi/planetstack/api/roles.py new file mode 100644 index 0000000..cd737b0 --- /dev/null +++ b/plstackapi/planetstack/api/roles.py @@ -0,0 +1,34 @@ +from plstackapi.openstack.client import OpenStackClient +from plstackapi.openstack.driver import OpenStackDriver +from plstackapi.planetstack.models import * + + +def auth_check(auth): + client = OpenStackShell(username=auth['Username'], + password=auth['AuthMethod', + tenant=auth['LoginBase']) + client.authenticate() + return client + + +def add_role(auth, name): + client = auth_check(auth) + keystone_role = client.keystone.roles.create(name) + role = Role(role_type=name, role_id=keystone_role.id) + role.save() + return role + +def delete_role(auth, name): + client = auth_check(auth) + role = Role.objects.filter(role_type=name) + client.keystone.roles.delete(role.role_id) + role.delete() + return 1 + +def get_roles(auth, filter): + client = auth_check(auth) + roles = Role.objects.filter(**filter) + return roles + + + diff --git a/plstackapi/planetstack/views/__init__.py b/plstackapi/planetstack/views/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/plstackapi/planetstack/views/api_root.py b/plstackapi/planetstack/views/api_root.py new file mode 100644 index 0000000..707e06b --- /dev/null +++ b/plstackapi/planetstack/views/api_root.py @@ -0,0 +1,12 @@ +from rest_framework.decorators import api_view +from rest_framework.response import Response +from rest_framework.reverse import reverse + +@api_view(['GET']) +def api_root(request, format=None): + return Response({ + 'nodes': reverse('node-list', request=request, format=format), + 'sites': reverse('site-list', request=request, format=format), + 'deploymentNetworks': reverse('deploymentnetwork-list', request=request, format=format), + 'slices': reverse('slice-list', request=request, format=format) + }) -- 2.47.0