Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
| 3 | from planetstack.config import Config |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 4 | from observer.openstacksyncstep import OpenStackSyncStep |
| 5 | from core.models.network import * |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 6 | |
| 7 | class SyncNetworks(OpenStackSyncStep): |
| 8 | provides=[Network] |
Sapan Bhatia | 2ef36c8 | 2013-09-02 14:30:37 -0400 | [diff] [blame] | 9 | requested_interval = 0 |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 10 | |
| 11 | def save_network(self, network): |
| 12 | if not network.network_id: |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 13 | if network.template.sharedNetworkName: |
| 14 | network.network_id = network.template.sharedNetworkId |
| 15 | (network.subnet_id, network.subnet) = self.driver.get_network_subnet(network.network_id) |
| 16 | else: |
| 17 | network_name = network.name |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 18 | |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 19 | # create network |
| 20 | os_network = self.driver.create_network(network_name, shared=True) |
| 21 | network.network_id = os_network['id'] |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 22 | |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 23 | # create router |
| 24 | router = self.driver.create_router(network_name) |
| 25 | network.router_id = router['id'] |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 26 | |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 27 | # create subnet |
| 28 | next_subnet = self.get_next_subnet() |
| 29 | cidr = str(next_subnet.cidr) |
| 30 | ip_version = next_subnet.version |
| 31 | start = str(next_subnet[2]) |
| 32 | end = str(next_subnet[-2]) |
| 33 | subnet = self.driver.create_subnet(name=network_name, |
| 34 | network_id = network.network_id, |
| 35 | cidr_ip = cidr, |
| 36 | ip_version = ip_version, |
| 37 | start = start, |
| 38 | end = end) |
| 39 | network.subnet = cidr |
| 40 | network.subnet_id = subnet['id'] |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 41 | |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 42 | def sync_record(self, site): |
| 43 | if network.owner and network.owner.creator: |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 44 | try: |
| 45 | # update manager context |
| 46 | self.driver.init_caller(network.owner.creator, network.owner.name) |
| 47 | self.save_network(network) |
| 48 | logger.info("saved network: %s" % (network)) |
| 49 | except Exception,e: |
| 50 | logger.log_exc("save network failed: %s" % network) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 51 | raise e |
| 52 | |