-
Notifications
You must be signed in to change notification settings - Fork 26
/
delete_vm.py
31 lines (25 loc) · 1.13 KB
/
delete_vm.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
import argparse
import time
from ArubaCloud.PyArubaAPI import CloudInterface
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--datacenter', help='Specify datacenter to login.', action='store', type=int, dest='dc')
parser.add_argument('-p', '--pattern', help='Specify pattern to search.', action='store', dest='pattern')
parser.add_argument('-u', '--username', help='Specify username.', action='store', dest='username')
parser.add_argument('-w', '--password', help='Specify password.', action='store', dest='password')
p = parser.parse_args()
i = CloudInterface(dc=p.dc)
i.login(username=p.username, password=p.password, load=True)
i.get_servers()
maxretries = 5
for vm in i.vmlist.find(name=p.pattern):
if vm.status == 3:
vm.poweroff()
for w in range(maxretries):
server_detail = i.get_server_detail(server_id=vm.sid)
if server_detail['ServerStatus'] == 2:
break
else:
print("Waiting shutdown")
time.sleep(5)
i.delete_vm(server_id=vm.sid)