Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame^] | 1 | from plstackapi.openstack.client import OpenStackClient |
| 2 | from plstackapi.openstack.driver import OpenStackDriver |
| 3 | from plstackapi.core.api.auth import auth_check |
| 4 | from plstackapi.core.models import Site |
| 5 | |
| 6 | def lookup_site(fields): |
| 7 | site = None |
| 8 | if 'site' in fields: |
| 9 | if isinstance(fields['site'], int): |
| 10 | sites = Site.objects.filter(id=fields['site']) |
| 11 | else: |
| 12 | sites = Site.objects.filter(login_base=fields['site']) |
| 13 | if sites: |
| 14 | site = sites[0] |
| 15 | if not site: |
| 16 | raise Exception, "No such site: %s" % fields['site'] |
| 17 | return site |
| 18 | |
| 19 | def add_slice(auth, fields): |
| 20 | driver = OpenStackDriver(client = auth_check(auth)) |
| 21 | site = lookup_site(fields) |
| 22 | if site: fields['site'] = site |
| 23 | slice = Slice(**fields) |
| 24 | # create tenant |
| 25 | nova_fields = {'tenant_name': slice.name, |
| 26 | 'description': slice.description, |
| 27 | 'enabled': slice.enabled} |
| 28 | tenant = driver.create_tenant(**nova_fields) |
| 29 | slice.tenant_id=tenant.id |
| 30 | |
| 31 | # create network |
| 32 | network = driver.create_network(name=self.name) |
| 33 | self.network_id = network['id'] |
| 34 | |
| 35 | # create router |
| 36 | router = driver.create_router(name=self.name) |
| 37 | self.router_id = router['id'] |
| 38 | |
| 39 | slice.save() |
| 40 | return slice |
| 41 | |
| 42 | def update_slice(auth, id, **fields): |
| 43 | driver = OpenStackDriver(client = auth_check(auth)) |
| 44 | slices = Slice.objects.filter(id=id) |
| 45 | if not slices: |
| 46 | return |
| 47 | |
| 48 | # update tenant |
| 49 | slice = slices[0] |
| 50 | nova_fields = {} |
| 51 | if 'name' in fields: |
| 52 | nova_fields['tenant_name'] = fields['name'] |
| 53 | if 'description' in fields: |
| 54 | nova_fields['description'] = fields['description'] |
| 55 | if 'enabled' in fields: |
| 56 | nova_fields['enabled'] = fields['enabled'] |
| 57 | driver.update_tenant(slice.tenant_id, **nova_fields) |
| 58 | |
| 59 | # update db record |
| 60 | site = lookup_site(fields) |
| 61 | if site: fields['site'] = site |
| 62 | slice.update(**fields) |
| 63 | |
| 64 | return slice |
| 65 | |
| 66 | def delete_slice(auth, filter={}): |
| 67 | driver = OpenStackDriver(client = auth_check(auth)) |
| 68 | slices = Slice.objects.filter(**filter) |
| 69 | for slice in slices: |
| 70 | driver.delete_slice(id=slice.tenant_id) |
| 71 | slice.delete() |
| 72 | return 1 |
| 73 | |
| 74 | def get_slices(auth, filter={}): |
| 75 | client = auth_check(auth) |
| 76 | site = lookup_site(fields) |
| 77 | if site: fields['site'] = site |
| 78 | slices = Slice.objects.filter(**filter) |
| 79 | return slices |
| 80 | |
| 81 | |
| 82 | |