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