8 Class for controlling subnets. Right now, just adds subnets and
9 checks whether they exist, because that's all we need.
14 self.http = httplib2.Http(".cache")
15 self.http.add_credentials("admin", "admin")
16 self.url_prefix = "http://localhost:8080/controller/nb/v2/subnetservice/default/"
18 # Checks whether subnet exists. Checks against the actual subnet
19 # string (e.g., "10.0.0.255/1"), not the subnet name. Will not
20 # catch things like overlapping subnets.
21 def exists(self, subnet):
22 resp, content = self.http.request(self.url_prefix + "subnets", "GET")
23 if (resp.status != 200):
24 print "Fatal error - can't check for subnet existence"
27 data = json.loads(content)
28 for key in data["subnetConfig"]:
29 if (key["subnet"] == subnet):
33 # Add a subnet if it doesn't already exist.
34 def add_subnet(self, subnet_name, subnet):
35 if (self.exists(subnet)):
36 print "Subnet", subnet, "already exists"
38 subnet_config = dict(name=subnet_name, subnet=subnet)
39 json_data = json.dumps(subnet_config)
40 resp, content = self.http.request(self.url_prefix + "subnet/" + subnet_name, "POST", json_data, {'Content-Type': 'application/json'})
41 if (resp.status == 201):
42 print "Subnet", subnet, "added"
44 print "Subnet", subnet, "could not be added"