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