-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
150 lines (115 loc) · 4.16 KB
/
main.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
__author__ = "Gabs the CSE"
__copyright__ = "Copyright 2021, Gabs the Creator"
__credits__ = ["Gabriel Cerioni"]
__license__ = "GPL"
__version__ = "1.0.0"
__maintainer__ = "Gabriel Cerioni"
__email__ = "[email protected]"
__status__ = "Production"
import re
import os
import random
import logging
from gql import Client, gql
from gql.transport.requests import RequestsHTTPTransport
from gql.transport.requests import log as requests_logger
# Configs (if this gets bigger, I'll provide a config file... or even Hashicorp Vault)
# logging.basicConfig(filename='gabs_graphql.log', filemode='a', format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO)
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO)
requests_logger.setLevel(logging.WARNING)
# API_KEY = "<YOUR_KEY>"
# API_ENDPOINT = "https://app.harness.io/gateway/api/graphql?accountId=<ACC_ID>"
API_KEY = os.environ.get('HARNESS_GRAPHQL_API_KEY')
API_ENDPOINT = os.environ.get('HARNESS_GRAPHQL_ENDPOINT')
def generic_graphql_query(query):
req_headers = {
'x-api-key': API_KEY
}
_transport = RequestsHTTPTransport(
url=API_ENDPOINT,
headers=req_headers,
use_json=True,
)
# Create a GraphQL client using the defined transport
client = Client(transport=_transport, fetch_schema_from_transport=True)
# Provide a GraphQL query
generic_query = gql(query)
# Execute the query on the transport
result = client.execute(generic_query)
return result
def generic_graphql_mutation(mutation_query, params):
req_headers = {
'x-api-key': API_KEY
}
_transport = RequestsHTTPTransport(
url=API_ENDPOINT,
headers=req_headers,
use_json=True,
)
# Create a GraphQL client using the defined transport
client = Client(transport=_transport, fetch_schema_from_transport=True)
# Provide a GraphQL query
generic_query = gql(mutation_query)
# Execute the query on the transport
result = client.execute(generic_query, variable_values=params)
return result
def multiple_dummy_users_loader(user_amount, name_template, email_template, usergroupids_list):
for i in range(1, user_amount+1):
name = "{0}_{1}".format(name_template, i)
email = re.sub(r'(@)', r'_{0}\1'.format(i), email_template)
hash_mutation = random.getrandbits(32)
mutation_query = '''
mutation createUser($user: CreateUserInput!) {
createUser(input: $user) {
user {
id
email
name
userGroups(limit: 5) {
nodes {
id
name
}
}
}
clientMutationId
}
}
'''
query_variables = {"user": {"name": name, "email": email, "clientMutationId": str(hash_mutation), "userGroupIds": usergroupids_list}}
generic_graphql_mutation(mutation_query, query_variables)
def get_harness_account_users():
offset = 0
has_more = True
total_user_list = []
while has_more:
query = '''{
users(limit: 100, offset: ''' + str(offset) + ''') {
pageInfo {
total
limit
hasMore
offset
}
nodes {
name
}
}
}'''
generic_query_result = generic_graphql_query(query)
loop_user_list = generic_query_result["users"]["nodes"]
total_user_list.extend(loop_user_list)
#total = generic_query_result["users"]["pageInfo"]["total"]
has_more = bool(generic_query_result["users"]["pageInfo"]["hasMore"])
if has_more:
offset = offset + 100
return total_user_list
if __name__ == '__main__':
logging.info("Starting the Program...")
logging.info("Getting all users from your Harness Account")
result_from_query = get_harness_account_users()
logging.info("Done! You have {0} users in your Account!".format(len(result_from_query)))
print("")
logging.info("Printing the User List on your STDOUT")
print(result_from_query)
logging.info("Program Exited!")