Added option in demo to disable waypoint redirection (and fixed typos in subnet.py)
[affinity.git] / scripts / subnet.py
1 #!/usr/bin/python
2
3 import httplib2
4 import json
5 import sys
6
7 '''
8 Class for controlling subnets.  Right now, just adds subnets and
9 checks whether they exist, because that's all we need.
10 '''
11 class SubnetControl:
12
13     def __init__(self):
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/"
17
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"
25             sys.exit(-1)
26
27         data = json.loads(content)
28         for key in data["subnetConfig"]:
29             if (key["subnet"] == subnet):
30                 return True
31         return False
32
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"
37             return
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"
43         else:
44             print "Subnet", subnet, "could not be added"