Tony Mack | 9d0ae4b | 2013-03-26 13:22:32 -0400 | [diff] [blame] | 1 | from keystoneclient.v2_0 import client as keystone_client |
| 2 | from glance import client as glance_client |
| 3 | from novaclient.v1_1 import client as nova_client |
| 4 | from quantumclient.v2_0 import client as quantum_client |
| 5 | |
| 6 | from plstackapi.planetstack.config import Config |
| 7 | |
| 8 | def parse_novarc(filename): |
| 9 | opts = {} |
| 10 | f = open(filename, 'r') |
| 11 | for line in f: |
| 12 | try: |
| 13 | line = line.replace('export', '').strip() |
| 14 | parts = line.split('=') |
| 15 | if len(parts) > 1: |
| 16 | value = parts[1].replace("\'", "") |
| 17 | value = value.replace('\"', '') |
| 18 | opts[parts[0]] = value |
| 19 | except: |
| 20 | pass |
| 21 | f.close() |
| 22 | return opts |
| 23 | |
| 24 | class Client: |
| 25 | def __init__(self, username=None, password=None, tenant=None, url=None, config=None, *args, **kwds): |
| 26 | if config: |
| 27 | config = Config(config) |
| 28 | else: |
| 29 | config = Config() |
| 30 | self.username = config.nova_admin_user |
| 31 | self.password = config.nova_admin_password |
| 32 | self.tenant = config.nova_admin_tenant |
| 33 | self.url = config.nova_url |
| 34 | |
| 35 | if username: |
| 36 | self.username = username |
| 37 | if password: |
| 38 | self.password = password |
| 39 | if tenant: |
| 40 | self.tenant = tenant |
| 41 | if url: |
| 42 | self.url = url |
| 43 | |
| 44 | if '@' in self.username: |
| 45 | self.username = self.username[:self.username.index('@')] |
| 46 | |
| 47 | class KeystoneClient(Client): |
| 48 | def __init__(self, *args, **kwds): |
| 49 | Client.__init__(self, *args, **kwds) |
| 50 | self.client = keystone_client.Client(username=self.username, |
| 51 | password=self.password, |
| 52 | tenant_name=self.tenant, |
| 53 | auth_url=self.url) |
| 54 | |
| 55 | def connect(self, *args, **kwds): |
| 56 | self.__init__(*args, **kwds) |
| 57 | |
| 58 | def __getattr__(self, name): |
| 59 | return getattr(self.client, name) |
| 60 | |
| 61 | |
| 62 | class GlanceClient(Client): |
| 63 | def __init__(self, *args, **kwds): |
| 64 | Client.__init__(self, *args, **kwds) |
| 65 | self.client = glance_client.get_client(host='0.0.0.0', |
| 66 | username=self.username, |
| 67 | password=self.password, |
| 68 | tenant=self.tenant, |
| 69 | auth_url=self.url) |
| 70 | def __getattr__(self, name): |
| 71 | return getattr(self.client, name) |
| 72 | |
| 73 | class NovaClient(Client): |
| 74 | def __init__(self, *args, **kwds): |
| 75 | Client.__init__(self, *args, **kwds) |
| 76 | self.client = nova_client.Client(username=self.username, |
| 77 | api_key=self.password, |
| 78 | project_id=self.tenant, |
| 79 | auth_url=self.url, |
| 80 | region_name='', |
| 81 | extensions=[], |
| 82 | service_type='compute', |
| 83 | service_name='', |
| 84 | ) |
| 85 | |
| 86 | def connect(self, *args, **kwds): |
| 87 | self.__init__(*args, **kwds) |
| 88 | |
| 89 | def __getattr__(self, name): |
| 90 | return getattr(self.client, name) |
| 91 | |
| 92 | class QuantumClient(Client): |
| 93 | def __init__(self, *args, **kwds): |
| 94 | Client.__init__(self, *args, **kwds) |
| 95 | self.client = quantum_client.Client(username=self.username, |
| 96 | password=self.password, |
| 97 | tenant_name=self.tenant, |
| 98 | auth_url=self.url) |
| 99 | def connect(self, *args, **kwds): |
| 100 | self.__init__(*args, **kwds) |
| 101 | |
| 102 | def __getattr__(self, name): |
| 103 | return getattr(self.client, name) |
| 104 | |
| 105 | class OpenStackShell: |
| 106 | """ |
| 107 | A simple native shell to the openstack backend services. |
| 108 | This class can receive all nova calls to the underlying testbed |
| 109 | """ |
| 110 | |
| 111 | def __init__ ( self, *args, **kwds) : |
| 112 | # instantiate managers |
| 113 | self.keystone = KeystoneClient(*args, **kwds) |
| 114 | self.glance = GlanceClient(*args, **kwds) |
| 115 | self.nova = NovaClient(*args, **kwds) |
| 116 | self.quantum = QuantumClient(*args, **kwds) |
| 117 | |
| 118 | def authenticate(self): |
| 119 | return self.keystone.authenticate() |