forked from VIRL-Open/virl-packet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvirl_packet_destroy.py
59 lines (41 loc) · 1.59 KB
/
virl_packet_destroy.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
#!/usr/bin/env python3
"""Deploy a VIRL system on Packet identified by the provided UUID.
Usage:
virl_packet_destroy -a <API_KEY> -u <UUID> [ ( --q | --qq | --json ) ]
Arguments:
-a <API_KEY> An API key associated with your Packet account.
-u <UUID> The UUID of the system to destroy
Options:
--q Display just the status, set exit-code
--qq Display nothing, set exit-code
--json Display the full JSON response, set exit-code
"""
import sys
import requests
import json
import simplejson
from docopt import docopt
IPXE_URL = "http://packet.virl.info"
API_URL = "https://api.packet.net"
CONTENT_TYPE = "application/json"
associated_status = {204: "Success", 401: "Unauthorized", 422: "Unprocessable", 403: "Forbidden", 404: "Not Found"}
headers = {}
if __name__ == '__main__':
arguments = docopt(__doc__, version='1.0.0rc3')
# Form the request header
headers.update({"content-type" : CONTENT_TYPE})
headers.update({"X-Auth-Token" : arguments['-a']})
# Form our URL with the Project-ID parameter
url = ('{0}/devices/{1}'.format(API_URL,arguments['-u']))
# Make the call to Packet
try:
r = requests.delete(url, headers=headers)
except requests.ConnectionError:
print ("An error occured while attempting to connect to Packet.")
sys.exit(1)
# Show some output, or not.
if arguments['--q']:
print(associated_status[r.status_code])
elif arguments['--json']:
print(simplejson.dumps(r.json(), sort_keys=True, indent=4))
sys.exit(r.status_code)