-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_check.py
104 lines (81 loc) · 2.89 KB
/
resource_check.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
# Check if a resource has showed up in DivvyCloud. If it has, check to see if there are any compliance violations on it.
# Currently this looks for any insights but filtering in check_for_violations() can pull out specific insights if needed
import json
import requests
import getpass
import time
# Instance ID and region for the instance
instance_id = "i-003014f07c517c6abc"
instance_region = "us-east-1"
# Username/password to authenticate against the API
username = ""
password = "" # Leave this blank if you don't want it in plaintext and it'll prompt you to input it when running the script.
# API URL
base_url = "https://sales-demo.divvycloud.com"
# Param validation
if not username:
username = input("Username: ")
if not password:
passwd = getpass.getpass('Password:')
else:
passwd = password
if not base_url:
base_url = input("Base URL (EX: http://localhost:8001 or http://45.59.252.4:8001): ")
# Full URL
login_url = base_url + '/v2/public/user/login'
# Shorthand helper function
def get_auth_token():
response = requests.post(
url=login_url,
data=json.dumps({"username": username, "password": passwd}),
headers={
'Content-Type': 'application/json;charset=UTF-8',
'Accept': 'application/json'
})
return response.json()['session_id']
auth_token = get_auth_token()
headers = {
'Content-Type': 'application/json;charset=UTF-8',
'Accept': 'application/json',
'X-Auth-Token': auth_token
}
# Get Reource info
def check_for_resource(resource_id):
data = {}
response = requests.get(
url=base_url + '/v2/public/resource/' + resource_id + '/detail',
data=json.dumps(data),
headers=headers
)
# If the resource isn't seen, it'll return a 404. If we see it (200), return success
if response.status_code != 200:
return
else:
return True
# Get Instance violation info. It'll be returned in an array if there are any
def check_for_violation(resource_id):
data = {}
response = requests.get(
url=base_url + '/v2/public/insights/' + resource_id + '/violations/get',
data=json.dumps(data),
headers=headers
)
return response.json()
resource_id = "instance:1:" + instance_region + ":" + instance_id + ":"
# Check if the resource is showing in divvycloud yet
while True:
found_resource = check_for_resource(resource_id)
if found_resource:
print("Found resource. Checking for compliance")
break
else:
print("Resource not found yet. Sleeping for 60")
time.sleep(60)
# Check for insight violations
instance_violations = check_for_violation(resource_id)
if instance_violations:
print("======== Violations found for the insight ========")
for violation in instance_violations:
print(violation['name'])
if not instance_violations:
print("No violations found on the instance")