blob: e64f0a44b518f6879f44d9c9909cd2385e4917cf [file] [log] [blame]
Sapan Bhatia24836f12013-08-27 10:16:05 -04001import os
2import base64
3from planetstack.config import Config
Sapan Bhatia04c94ad2013-09-02 18:00:28 -04004from observer.openstacksyncstep import OpenStackSyncStep
5from core.models.network import *
Sapan Bhatia24836f12013-08-27 10:16:05 -04006
7class SyncNetworks(OpenStackSyncStep):
8 provides=[Network]
Sapan Bhatia2ef36c82013-09-02 14:30:37 -04009 requested_interval = 0
Sapan Bhatia24836f12013-08-27 10:16:05 -040010
11 def save_network(self, network):
12 if not network.network_id:
Sapan Bhatia04c94ad2013-09-02 18:00:28 -040013 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 Bhatia24836f12013-08-27 10:16:05 -040018
Sapan Bhatia04c94ad2013-09-02 18:00:28 -040019 # create network
20 os_network = self.driver.create_network(network_name, shared=True)
21 network.network_id = os_network['id']
Sapan Bhatia24836f12013-08-27 10:16:05 -040022
Sapan Bhatia04c94ad2013-09-02 18:00:28 -040023 # create router
24 router = self.driver.create_router(network_name)
25 network.router_id = router['id']
Sapan Bhatia24836f12013-08-27 10:16:05 -040026
Sapan Bhatia04c94ad2013-09-02 18:00:28 -040027 # 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 Bhatia24836f12013-08-27 10:16:05 -040041
Sapan Bhatia24836f12013-08-27 10:16:05 -040042 def sync_record(self, site):
43 if network.owner and network.owner.creator:
Sapan Bhatia04c94ad2013-09-02 18:00:28 -040044 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 Bhatia24836f12013-08-27 10:16:05 -040051 raise e
52