-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.py
151 lines (125 loc) · 4.77 KB
/
test.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import requests
import os
import json
from requests.exceptions import ConnectionError, ConnectTimeout, HTTPError
user = f"{os.environ.get('OS_USERNAME')}"
password = os.environ.get('OS_PASSWORD')
bb = "bb271"
region = os.environ.get("OS_REGION_NAME")
url = f"https://nsx-ctl-{bb}.cc.{region}.cloud.sap"
infa = "policy/api/v1/infra/domains/default"
id = "02445586-8135-4e6b-9b76-120a03ca4ffc"
def get_session():
session = requests.Session()
session.auth = (user, password)
return session
def put_group():
session = get_session()
dummy_group = {'id': id,
'display_name': id,
'path': f"/infra/domains/default/groups/{id}",
"_revision": 0,
'expression': [
{'value': f"security_group|{id}",
'member_type': 'SegmentPort',
'key': 'Tag',
'operator': 'EQUALS',
'resource_type': 'Condition'
}],
'tags': [{'scope': 'age', 'tag': 1705666590},
{'scope': 'revision_number', 'tag': 600},
{'scope': 'dummy', 'tag': "dummy"}]
}
u = f"{url}/{infa}/groups/{id}"
print(f"running put : {u}")
try:
res = session.put(u, json=dummy_group)
except (HTTPError, ConnectionError, ConnectTimeout) as err:
print(err)
if res.ok:
print("everything is fine")
else:
print("something went wrong")
print(res)
def bulk():
child_c = [
{'resource_type': 'ChildGroup',
'Group': {'id': id, 'display_name': id,
'path': f'/infra/domains/default/groups/{id}', 'expression': [
{'value': f'security_group|{id}', 'member_type': 'SegmentPort',
'key': 'Tag', 'operator': 'EQUALS', 'resource_type': 'Condition'}],
'tags': [{'scope': 'age', 'tag': 1705662784}, {'scope': 'revision_number', 'tag': 200}],
'_revision': None, 'resource_type': 'Group'}},
{'resource_type': 'ChildGroup',
'Group': {'id': '05060e8a-a7f0-416c-a9aa-62d431c2d5e7', 'display_name': '05060e8a-a7f0-416c-a9aa-62d431c2d5e7',
'path': '/infra/domains/default/groups/05060e8a-a7f0-416c-a9aa-62d431c2d5e7', 'expression': [
{'value': 'security_group|05060e8a-a7f0-416c-a9aa-62d431c2d5e7', 'member_type': 'SegmentPort',
'key': 'Tag', 'operator': 'EQUALS', 'resource_type': 'Condition'}],
'tags': [{'scope': 'age', 'tag': 1705662784}, {'scope': 'revision_number', 'tag': 200}],
'_revision': None, 'resource_type': 'Group'}}
]
container = {"resource_type": "Infra",
"children": [
{
"resource_type": "ChildResourceReference",
"id": "default",
"target_type": "Domain",
"children": child_c
}
]
}
session = get_session()
u = f"{url}/policy/api/v1/infra"
print(f"running patch: {u}")
json_formatted_str = json.dumps(container, indent=4)
print(json_formatted_str)
try:
res = session.patch(u, json=container)
except (HTTPError, ConnectionError, ConnectTimeout) as err:
print(err)
print("something went wrong")
exit(-1)
if res.ok:
print("everything is fine")
else:
print("something went wrong")
def get_revison_number():
session = get_session()
u = f"{url}/{infa}/groups/{id}"
print(f"running get: {u}")
try:
res = session.get(u)
except (HTTPError, ConnectionError, ConnectTimeout) as err:
print(err)
print("something went wrong")
exit(-1)
if res.ok:
print("everything is fine")
json_formatted_str = json.dumps(res.json(), indent=4)
print(json_formatted_str)
else:
print("something went wrong")
print(res)
def search():
port_id = "e8215be9-ebaa-4715-8eb8-71a3204391dc"
SEARCH_DSL_QUERY = {
"query": f"resource_type: Group",
"dsl": f"{port_id}",
"data_source": "INTENT",
"exclude_internal_types": "true"
}
session = get_session()
u = f"{url}/policy/api/v1/search"
res = session.get(u, params=SEARCH_DSL_QUERY)
if res.ok:
print(res)
sgs = sorted(res.json()['results'], key=lambda k: k['id'])
for sg in res.json()['results']:
json_formatted_str = json.dumps(sg, indent=4)
#print(json_formatted_str)
print(f"{sg['id']}, ")
if __name__ == "__main__":
#bulk()
#get_revison_number()
#put_group()
search()