Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 160 lines (151 sloc) 7.611 kB
2925045 user_resources is complete, but I still need to test it some more
Davis Barber authored
1 #!/usr/bin/python
2 from optparse import OptionParser
8f5b230 @jedahan move swarmtoolscore to its own directory
jedahan authored
3 from lib import swarmtoolscore
2925045 user_resources is complete, but I still need to test it some more
Davis Barber authored
4 import sys
5 import httplib
6 import json
7
8 def usage(script_name):
57d1ae2 @barberdt updates to reflect changes made to documentation
barberdt authored
9 print "%s [create|update|destroy|list|get_info|list_swarms] \n"%(script_name)
2925045 user_resources is complete, but I still need to test it some more
Davis Barber authored
10 print "Use '%s [method] --help for a method's usage and options."%(script_name)
11 sys.exit()
12
441f008 updated to comply with BUGsawrm 0.3.0 and added invitations.py. also …
Davis Barber authored
13 def create(hostname, api_key, name, machine_type, description, position):
14 create_resource = {"name": name, "machine_type": machine_type}
62ae4cb parsing args before method calls now. makes methods more standalone. …
Davis Barber authored
15 if description != None:
16 create_resource["description"] = description
17 if position != None:
18 latitude = position[1]
19 longitude = position[3]
4f540e2 @barberdt changes position requirements for creating a resource from a tuple of…
barberdt authored
20 latlon = {"latitude": float(latitude), "longitude": float(longitude)}
75265fd fixed some bugs
Davis Barber authored
21 create_resource["position"] = latlon
2925045 user_resources is complete, but I still need to test it some more
Davis Barber authored
22 create_resource_json = json.dumps(create_resource)
f874b14 @kgilmer bugswarm-tools: add support for hostname configuration property to
kgilmer authored
23 conn = httplib.HTTPConnection(hostname)
f5bc733 removed all content-type headers
Davis Barber authored
24 conn.request("POST", "/resources", create_resource_json, {"x-bugswarmapikey":api_key})
2925045 user_resources is complete, but I still need to test it some more
Davis Barber authored
25 resp = conn.getresponse()
26 txt = resp.read()
27 conn.close()
441f008 updated to comply with BUGsawrm 0.3.0 and added invitations.py. also …
Davis Barber authored
28 print json.dumps(json.loads(txt), sort_keys=True, indent=4)
2925045 user_resources is complete, but I still need to test it some more
Davis Barber authored
29
f874b14 @kgilmer bugswarm-tools: add support for hostname configuration property to
kgilmer authored
30 def update(hostname, api_key, resource_id, name, machine_type, description, position):
2925045 user_resources is complete, but I still need to test it some more
Davis Barber authored
31 update_resource = {}
62ae4cb parsing args before method calls now. makes methods more standalone. …
Davis Barber authored
32 if name != None:
33 update_resource["name"] = name
34 if machine_type != None:
35 update_resource["machine_type"] = machine_type
36 if description != None:
37 update_resource["description"] = description
38 if position != None:
39 latitude = position[1]
40 longitude = position[3]
75265fd fixed some bugs
Davis Barber authored
41 latlon = {"latitude": int(latitude), "longitude": int(longitude)}
42 update_resource["position"] = latlon
2925045 user_resources is complete, but I still need to test it some more
Davis Barber authored
43 update_resource_json = json.dumps(update_resource)
f874b14 @kgilmer bugswarm-tools: add support for hostname configuration property to
kgilmer authored
44 conn = httplib.HTTPConnection(hostname)
f5bc733 removed all content-type headers
Davis Barber authored
45 conn.request("PUT", "/resources/%s"%(resource_id), update_resource_json, {"x-bugswarmapikey":api_key})
2925045 user_resources is complete, but I still need to test it some more
Davis Barber authored
46 resp = conn.getresponse()
47 txt = resp.read()
48 conn.close()
441f008 updated to comply with BUGsawrm 0.3.0 and added invitations.py. also …
Davis Barber authored
49 print json.dumps(json.loads(txt), sort_keys=True, indent=4)
2925045 user_resources is complete, but I still need to test it some more
Davis Barber authored
50
f874b14 @kgilmer bugswarm-tools: add support for hostname configuration property to
kgilmer authored
51 def destroy(hostname, api_key, resource_id):
52 conn = httplib.HTTPConnection(hostname)
2925045 user_resources is complete, but I still need to test it some more
Davis Barber authored
53 conn.request("DELETE", "/resources/%s"%(resource_id), None, {"x-bugswarmapikey":api_key})
54 resp = conn.getresponse()
441f008 updated to comply with BUGsawrm 0.3.0 and added invitations.py. also …
Davis Barber authored
55 txt = resp.status
2925045 user_resources is complete, but I still need to test it some more
Davis Barber authored
56 conn.close()
441f008 updated to comply with BUGsawrm 0.3.0 and added invitations.py. also …
Davis Barber authored
57 if str(txt) == "204":
58 print "Great success! :)"
59 else:
60 print "Something went wrong! :("
61
57d1ae2 @barberdt updates to reflect changes made to documentation
barberdt authored
62 def list_resources(hostname, api_key):
f874b14 @kgilmer bugswarm-tools: add support for hostname configuration property to
kgilmer authored
63 conn = httplib.HTTPConnection(hostname)
2925045 user_resources is complete, but I still need to test it some more
Davis Barber authored
64 conn.request("GET", "/resources", None, {"x-bugswarmapikey":api_key})
65 resp = conn.getresponse()
66 txt = resp.read()
67 conn.close()
68 print json.dumps(json.loads(txt), sort_keys=True, indent=4)
69
57d1ae2 @barberdt updates to reflect changes made to documentation
barberdt authored
70 def get_info(hostname, api_key, resource_id):
f874b14 @kgilmer bugswarm-tools: add support for hostname configuration property to
kgilmer authored
71 conn = httplib.HTTPConnection(hostname)
2925045 user_resources is complete, but I still need to test it some more
Davis Barber authored
72 conn.request("GET", "/resources/%s"%(resource_id), None, {"x-bugswarmapikey":api_key})
73 resp = conn.getresponse()
74 txt = resp.read()
75 conn.close()
76 print json.dumps(json.loads(txt), sort_keys=True, indent=4)
77
57d1ae2 @barberdt updates to reflect changes made to documentation
barberdt authored
78 def list_swarms(hostname, api_key, resource_id):
f874b14 @kgilmer bugswarm-tools: add support for hostname configuration property to
kgilmer authored
79 conn = httplib.HTTPConnection(hostname)
2925045 user_resources is complete, but I still need to test it some more
Davis Barber authored
80 conn.request("GET", "/resources/%s/swarms"%(resource_id), None, {"x-bugswarmapikey":api_key})
81 resp = conn.getresponse()
82 txt = resp.read()
83 conn.close()
84 print json.dumps(json.loads(txt), sort_keys=True, indent=4)
85
86 def main():
9e239d1 cleaned up kgilmer's commit functionality to match the rest of the co…
Davis Barber authored
87 server_info = swarmtoolscore.get_server_info()
2925045 user_resources is complete, but I still need to test it some more
Davis Barber authored
88 keys = swarmtoolscore.get_keys()
89 if len(sys.argv) == 1:
90 usage(sys.argv[0])
345c581 adds printing of usage info if an unknown method is given.
Andrew Turley authored
91 elif sys.argv[1] == "create":
441f008 updated to comply with BUGsawrm 0.3.0 and added invitations.py. also …
Davis Barber authored
92 opt_usage = "usage: \n %s NAME MACHINE_TYPE [options]"%(sys.argv[1])
93 opt_usage += "\n\n *NAME: The name of the resource to create." \
94 +"\n *MACHINE_TYPE: The machine type of the resource to create. Valid types; 'pc', 'smartphone', 'bug'."
2925045 user_resources is complete, but I still need to test it some more
Davis Barber authored
95 parser = OptionParser(usage = opt_usage)
441f008 updated to comply with BUGsawrm 0.3.0 and added invitations.py. also …
Davis Barber authored
96 parser.add_option("-d", "--description", dest="description", help="Set the resource's description", metavar="DESCRIPTION")
4f540e2 @barberdt changes position requirements for creating a resource from a tuple of…
barberdt authored
97 parser.add_option("-p", "--position", dest="position", help="Set the resource's position. Must be a tuple of floats surounded by quotations.", metavar="\"(LATITUDE, LONGITUDE)\"")
2925045 user_resources is complete, but I still need to test it some more
Davis Barber authored
98 (options, args) = parser.parse_args()
441f008 updated to comply with BUGsawrm 0.3.0 and added invitations.py. also …
Davis Barber authored
99 if len(args) != 3:
62ae4cb parsing args before method calls now. makes methods more standalone. …
Davis Barber authored
100 print "Invalid number of args. See --help for correct usage."
101 sys.exit()
441f008 updated to comply with BUGsawrm 0.3.0 and added invitations.py. also …
Davis Barber authored
102 name = args[1]
103 machine_type = args[2]
da9bae5 adapts to comply with API key overhaul
Davis Barber authored
104 create(server_info["hostname"], keys["configuration"], name, machine_type, options.description, options.position)
345c581 adds printing of usage info if an unknown method is given.
Andrew Turley authored
105 elif sys.argv[1] == "update":
62ae4cb parsing args before method calls now. makes methods more standalone. …
Davis Barber authored
106 opt_usage = "usage: \n %s RESOURCE_ID [options]"%(sys.argv[1])
441f008 updated to comply with BUGsawrm 0.3.0 and added invitations.py. also …
Davis Barber authored
107 opt_usage += "\n\n *RESOURCE_ID: The ID of the resource to update."
2925045 user_resources is complete, but I still need to test it some more
Davis Barber authored
108 parser = OptionParser(usage = opt_usage)
441f008 updated to comply with BUGsawrm 0.3.0 and added invitations.py. also …
Davis Barber authored
109 parser.add_option("-n", "--name", dest="name", help="Set the resource's name", metavar="NAME")
110 parser.add_option("-t", "--type", dest="machine_type", help="Set the resource's machine type. Valid types; 'pc', 'smartphone', 'bug'.", metavar="MACHINE_TYPE")
111 parser.add_option("-d", "--description", dest="description", help="Set the resource's description", metavar="DESCRIPTION")
4f540e2 @barberdt changes position requirements for creating a resource from a tuple of…
barberdt authored
112 parser.add_option("-p", "--position", dest="position", help="Set the resource's position. Must be a tuple of floats surrounded by quotations.", metavar="\"(LATITUDE, LONGITUDE)\"")
2925045 user_resources is complete, but I still need to test it some more
Davis Barber authored
113 (options, args) = parser.parse_args()
62ae4cb parsing args before method calls now. makes methods more standalone. …
Davis Barber authored
114 if len(args) != 2:
115 print "Invalid number of args. See --help for correct usage."
116 sys.exit()
117 resource_id = args[1]
da9bae5 adapts to comply with API key overhaul
Davis Barber authored
118 update(server_info["hostname"], keys["configuration"], resource_id, options.name, options.machine_type, options.description, options.position)
345c581 adds printing of usage info if an unknown method is given.
Andrew Turley authored
119 elif sys.argv[1] == "destroy":
62ae4cb parsing args before method calls now. makes methods more standalone. …
Davis Barber authored
120 opt_usage = "usage: \n %s RESOURCE_ID"%(sys.argv[1])
441f008 updated to comply with BUGsawrm 0.3.0 and added invitations.py. also …
Davis Barber authored
121 opt_usage += "\n\n *RESOURCE_ID: The ID of the resource to destroy."
2925045 user_resources is complete, but I still need to test it some more
Davis Barber authored
122 parser = OptionParser(usage = opt_usage)
123 (options, args) = parser.parse_args()
62ae4cb parsing args before method calls now. makes methods more standalone. …
Davis Barber authored
124 if len(args) != 2:
125 print "Invalid number of args. See --help for correct usage."
126 sys.exit()
127 resource_id = args[1]
da9bae5 adapts to comply with API key overhaul
Davis Barber authored
128 destroy(server_info["hostname"], keys["configuration"], resource_id)
2811ae6 removed hidden Python terms and changed methods to 'list' from 'list_…
Davis Barber authored
129 elif sys.argv[1] == "list":
62ae4cb parsing args before method calls now. makes methods more standalone. …
Davis Barber authored
130 opt_usage = "usage: \n %s"%(sys.argv[1])
2925045 user_resources is complete, but I still need to test it some more
Davis Barber authored
131 parser = OptionParser(usage = opt_usage)
132 (options, args) = parser.parse_args()
62ae4cb parsing args before method calls now. makes methods more standalone. …
Davis Barber authored
133 if len(args) != 1:
134 print "Invalid number of args. See --help for correct usage."
135 sys.exit()
57d1ae2 @barberdt updates to reflect changes made to documentation
barberdt authored
136 list_resources(server_info["hostname"], keys["configuration"])
137 elif sys.argv[1] == "get_info":
62ae4cb parsing args before method calls now. makes methods more standalone. …
Davis Barber authored
138 opt_usage = "usage: \n %s RESOURCE_ID"%(sys.argv[1])
441f008 updated to comply with BUGsawrm 0.3.0 and added invitations.py. also …
Davis Barber authored
139 opt_usage += "\n\n *RESOURCE_ID: The ID of the resource who's info is desired."
2925045 user_resources is complete, but I still need to test it some more
Davis Barber authored
140 parser = OptionParser(usage = opt_usage)
141 (options, args) = parser.parse_args()
62ae4cb parsing args before method calls now. makes methods more standalone. …
Davis Barber authored
142 if len(args) != 2:
143 print "Invalid number of args. See --help for correct usage."
144 sys.exit()
145 resource_id = args[1]
57d1ae2 @barberdt updates to reflect changes made to documentation
barberdt authored
146 get_info(server_info["hostname"], keys["configuration"], resource_id)
147 elif sys.argv[1] == "list_swarms":
62ae4cb parsing args before method calls now. makes methods more standalone. …
Davis Barber authored
148 opt_usage = "usage: \n %s RESOURCE_ID"%(sys.argv[1])
441f008 updated to comply with BUGsawrm 0.3.0 and added invitations.py. also …
Davis Barber authored
149 opt_usage += "\n\n *RESOURCE_ID: The ID of the resource. The swarms that the resource is a member of will be listed."
2925045 user_resources is complete, but I still need to test it some more
Davis Barber authored
150 parser = OptionParser(usage = opt_usage)
151 (options, args) = parser.parse_args()
62ae4cb parsing args before method calls now. makes methods more standalone. …
Davis Barber authored
152 if len(args) != 2:
153 print "Invalid number of args. See --help for correct usage."
154 sys.exit()
155 resource_id = args[1]
57d1ae2 @barberdt updates to reflect changes made to documentation
barberdt authored
156 list_swarms(server_info["hostname"], keys["configuration"], resource_id)
345c581 adds printing of usage info if an unknown method is given.
Andrew Turley authored
157 else:
158 usage(sys.argv[0])
2925045 user_resources is complete, but I still need to test it some more
Davis Barber authored
159 main()
Something went wrong with that request. Please try again.