Added option in demo to disable waypoint redirection (and fixed typos in subnet.py)
[affinity.git] / scripts / affinity_control.py
1 #!/usr/local/bin/python
2
3 import httplib2
4 import json
5
6 class AffinityControl:
7
8     def __init__(self):
9         self.http = httplib2.Http(".cache")
10         self.http.add_credentials("admin", "admin")
11         self.url_prefix = "http://localhost:8080/affinity/nb/v2/affinity/default/"
12
13     # Add affinity group
14     def add_affinity_group(self, group_name, **kwargs):
15         # Create the group
16         resp, content = self.http.request(self.url_prefix + "create/group/%s" % group_name, "PUT")
17         if (resp.status != 201):
18             print "AffinityGroup %s could not be created" % group_name
19             return
20         # If a list of IPs is passed, add each one
21         if "ips" in kwargs:
22             ips = kwargs['ips']
23             for ip in ips:
24                 resp, content = self.http.request(self.url_prefix + "group/%s/add/ip/%s" % (group_name, ip), "PUT")
25                 if (resp.status != 201):
26                     print "IP %s could not be added to AffinityGroup %s" % (ip, group_name)
27                     return
28             print "AffinityGroup %s added successfully. IPs are %s" % (group_name, ips)
29         # If a subnet is passed, add that
30         elif "subnet" in kwargs:
31             ip, mask = kwargs['subnet'].split("/")
32             resp, content = self.http.request(self.url_prefix + "group/%s/addsubnet/ipprefix/%s/mask/%s" % (group_name, ip, mask), "PUT")
33             if (resp.status != 201):
34                 print "AffinityGroup could not be created for subnet %s/%s" % (ip, mask)
35                 return
36             print "AffinityGroup %s added successfully. Subnet is %s/%s" % (group_name, ip, mask)
37
38     # Add affinity link
39     def add_affinity_link(self, link_name, src_group, dst_group):
40         resp, content = self.http.request(self.url_prefix + "create/link/%s/from/%s/to/%s" % (link_name, src_group, dst_group), "PUT")
41         if (resp.status != 201):
42             print "AffinityLink %s could not be added between %s and %s" % (link_name, src_group, dst_group)
43             return
44         print "AffinityLink %s added between %s and %s" % (link_name, src_group, dst_group)
45
46     # Add waypoint
47     def add_waypoint(self, link_name, ip):
48         resp, content = self.http.request(self.url_prefix + "link/%s/setwaypoint/%s" % (link_name, ip), "PUT")
49         if (resp.status != 201):
50             print "Waypoint %s could not be set for link %s" % (ip, link_name)
51             return
52         print "Waypoint %s successfully set for link %s" % (ip, link_name)
53
54     # Enable waypoint
55     def enable_waypoint(self, link_name):
56         resp, content = self.http.request(self.url_prefix + "link/%s/enable" % link_name, "PUT")
57         if (resp.status != 201):
58             print "Waypoint could not be enabled for link %s" % link_name
59             return
60         print "Waypoint enabled for link %s" % link_name
61
62     # Disable waypoint
63     def disable_waypoint(self, link_name):
64         resp, content = self.http.request(self.url_prefix + "link/%s/disable" % link_name, "PUT")
65         if (resp.status != 201):
66             print "Waypoint could not be disabled for link %s" % link_name
67             return
68         print "Waypoint disabled for link %s" % link_name