-
Notifications
You must be signed in to change notification settings - Fork 1
/
invitations.py
executable file
·114 lines (108 loc) · 5.37 KB
/
invitations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/python
from optparse import OptionParser
from lib import swarmtoolscore
import sys
import httplib
import json
def usage(script_name):
print "%s [send|respond|list_sent|list_received] \n"%(script_name)
print "Use '%s [method] --help for a method's usage and options."%(script_name)
sys.exit()
def send(hostname, api_key, swarm_id, to, resource_id, resource_type, description):
conn = httplib.HTTPConnection(hostname)
invitation = {"to": to, "resource_id": resource_id, "resource_type": resource_type}
if description != None:
invitation["description"] = description
invitation_json = json.dumps(invitation)
conn.request("POST", "/swarms/%s/invitations"%(swarm_id), invitation_json, {"x-bugswarmapikey":api_key})
resp = conn.getresponse()
txt = resp.read()
conn.close()
print json.dumps(json.loads(txt), sort_keys=True, indent=4)
def respond(hostname, api_key, resource_id, invitation_id, status):
conn = httplib.HTTPConnection(hostname)
if status != "accept" and status != "reject":
print "Invalid status. Must be 'accept' or 'reject'."
sys.exit()
response = {"status": status}
response_json = json.dumps(response)
conn.request("PUT", "/resources/%s/invitations/%s"%(resource_id, invitation_id), response_json, {"x-bugswarmapikey":api_key})
resp = conn.getresponse()
txt = resp.read()
conn.close()
print json.dumps(json.loads(txt), sort_keys=True, indent=4)
def list_sent(hostname, api_key, swarm_id):
conn = httplib.HTTPConnection(hostname)
conn.request("GET", "/swarms/%s/invitations"%(swarm_id), None, {"x-bugswarmapikey":api_key})
resp = conn.getresponse()
txt = resp.read()
conn.close()
print json.dumps(json.loads(txt), sort_keys=True, indent=4)
def list_received(hostname, api_key, resource_id):
conn = httplib.HTTPConnection(hostname)
if resource_id != None:
conn.request("GET", "/resources/%s/invitations"%(resource_id), None, {"x-bugswarmapikey":api_key})
else:
conn.request("GET", "/invitations", None, {"x-bugswarmapikey":api_key})
resp = conn.getresponse()
txt = resp.read()
conn.close()
print json.dumps(json.loads(txt), sort_keys=True, indent=4)
def main():
server_info = swarmtoolscore.get_server_info()
keys = swarmtoolscore.get_keys()
if len(sys.argv) == 1:
usage(sys.argv[0])
elif sys.argv[1] == "send":
opt_usage = "usage: \n %s SWARM_ID TO RESOURCE_ID RESOURCE_TYPE [options]"%(sys.argv[1])
opt_usage += "\n\n *SWARM_ID: The ID of the swarm that is inviting." \
+"\n *TO: The username of the owner of the resource being invited." \
+"\n *RESOURCE_ID: The ID of the resource being invited." \
+"\n *RESOURCE_TYPE: The type that the invited resource would have presence as in the inviting swarm. Valid types; 'producer', 'consumer', 'both'."
parser = OptionParser(usage = opt_usage)
parser.add_option("-d", "--description", dest="description", help="Set a descriptive message to accompany the invitation.", metavar="DESCRIPTION")
(options, args) = parser.parse_args()
if len(args) != 5:
print "Invalid number of args. See --help for correct usage."
sys.exit()
swarm_id = args[1]
to = args[2]
resource_id = args[3]
resource_type = args[4]
send(server_info["hostname"], keys["configuration"], swarm_id, to, resource_id, resource_type, options.description)
elif sys.argv[1] == "respond":
opt_usage = "usage: \n %s RESOURCE_ID INVITATION_ID STATUS"%(sys.argv[1])
opt_usage += "\n\n *RESOURCE_ID: The ID of the resource who's invitation is being responded to." \
+"\n *INVITATION_ID: The ID of the invitation being responded to." \
+"\n *STATUS: The respose status. Valid types; 'accept', 'reject'."
parser = OptionParser(usage = opt_usage)
(options, args) = parser.parse_args()
if len(args) != 4:
print "Invalid number of args. See --help for correct usage."
sys.exit()
resource_id = args[1]
invitation_id = args[2]
status = args[3]
respond(server_info["hostname"], keys["configuration"], resource_id, invitation_id, status)
elif sys.argv[1] == "list_sent":
opt_usage = "usage: \n %s SWARM_ID"%(sys.argv[1])
opt_usage += "\n\n *SWARM_ID: The ID of the swarm who's associated invitations will be listed."
parser = OptionParser(usage = opt_usage)
(options, args) = parser.parse_args()
if len(args) != 2:
print "Invalid number of args. See --help for correct usage."
sys.exit()
swarm_id = args[1]
list_sent(server_info["hostname"], keys["configuration"], swarm_id)
elif sys.argv[1] == "list_received":
opt_usage = "usage: \n %s"%(sys.argv[1])
parser = OptionParser(usage = opt_usage)
parser.add_option("-r", "--resource", dest="resource_id", help="Specify a resource who's invitations you want to list.", metavar="RESOURCE_ID")
(options, args) = parser.parse_args()
if len(args) != 1:
print "Invalid number of args. See --help for correct usage."
sys.exit()
list_received(server_info["hostname"], keys["configuration"], options.resource_id)
else:
usage(sys.argv[0])
main()