-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgg-ip
executable file
·93 lines (73 loc) · 2.24 KB
/
gg-ip
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
#!/usr/bin/env python
"""Tool to obtain info about IP addresses on GoGrid
Examples:
* List all avaiable ips:
./gg-ip
* List only public ips (pu stands for public, pr stands for private):
./gg-ip -t pu
* Make output more verbose, i.e. show id, subnet mask and private/public info:
./gg-ip -v -t pu
* List unassigned (i.e. not used by running servers) public ips:
./gg-ip -t pu -s u
In the previous example u states for unassigned, a states for assigned, I guess you got it
"""
import sys
import getopt
from GoGridManager import GoGridManager
from GoGridClient import Error403
if __name__ == "__main__":
account = "default"
verbose = False
state = None
type = "all"
datacenter = None
try:
opts, args = getopt.getopt(sys.argv[1:], "d:p:s:t:v")
except getopt.GetoptError, err:
print str(err)
sys.exit(2)
for o, a in opts:
if o == "-d":
datacenter = a
elif o == "-p":
account = a
elif o == "-v":
verbose = True
elif o == "-t":
type = a
elif o == "-s":
state = a
if type[0:2] not in ["al", "pu", "pr"]:
print "Type should be one of: al(l) pu(blic) pr(ivate)"
sys.exit(2)
if state is not None:
if state[0] not in ['u', 'a']:
print "State should be one of: u(nassigned), a(ssigned)"
sys.exit(2)
manager = GoGridManager(account=account)
try:
if type.startswith("pu"):
type_arg = "Public"
elif type.startswith("pr"):
type_arg = "Private"
else:
type_arg = "all"
if state is not None:
if state.startswith("u"):
state_arg = "Unassigned"
elif state.startswith("a"):
state_arg = "Assigned"
else:
state_arg = None
else:
state_arg = None
ips = manager.get_ips(type=type_arg, state=state_arg, datacenter=datacenter)
except (Error403,), e:
print e
sys.exit(1)
if verbose:
for ip in ips:
print "%s %s %s %s" % (ip.id, ip.ip, ip.subnet, ip.datacenter)
else:
for ip in ips:
print ip.ip