Skip to content

Commit acb07f7

Browse files
committed
created configloader.py
added a configuration loader which checks for existing config files. If the folder is not found it is created. If the file is not found the default empty config file is copied to users home directory.
1 parent eaf0c6d commit acb07f7

10 files changed

Lines changed: 160 additions & 6 deletions

File tree

.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gridscale_api_client_python.iml

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/profiles_settings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/config.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
projects:
2+
- name: default
3+
userId: user123
4+
token: pass123
5+
url: https://api.gridscale.io
6+
- name: something-else
7+
userId: user456
8+
token: pass456
9+
url: https://api.gridscale.io

examples/configloader.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import shutil
2+
import yaml
3+
import sys
4+
import os.path
5+
6+
#TODO: change active project
7+
project = "somthing-else"
8+
9+
def which_path():
10+
#check if os is linux
11+
if(sys.platform == "linux" or sys.platform == "linux2"):
12+
path = "~/.config/gridscale"
13+
path = os.path.expanduser(path)
14+
if not os.path.exists(path):
15+
os.makedirs(path)
16+
#check if os is windows
17+
elif(sys.platform == "win32" or sys.platform == "cygwin" or sys.platform == "msys"):
18+
path = "%APPDATA%\gridscale"
19+
path = os.path.expanduser(path)
20+
if not os.path.exists(path):
21+
os.makedirs(path)
22+
#check if os is mac os
23+
elif(sys.platform == "darwin" or sys.platform == "os2" or sys.platform == "os2emx"):
24+
path = "~/Library/Application Support/gridscale"
25+
path = os.path.expanduser(path)
26+
if not os.path.exists(path):
27+
os.makedirs(path)
28+
else:
29+
"Operating System not supported"
30+
31+
return path
32+
33+
def create_config(path):
34+
cwd = os.getcwd()
35+
shutil.copyfile(f"{cwd}/config.yaml", path)
36+
print(f"New config file created, edit config file at: {path}")
37+
return
38+
39+
def load_config(projectname, goal, path):
40+
with open(f"{path}", 'r') as stream:
41+
try:
42+
data = yaml.safe_load(stream)
43+
44+
for value in data.values():
45+
for x in range(len(value)):
46+
result = value[x]
47+
#returns userID and token for the selected project
48+
if (result.get("name") == projectname):
49+
userid = result.get("userId")
50+
token = result.get("token")
51+
except yaml.YAMLError as exc:
52+
print(exc)
53+
54+
if(goal == "id"):
55+
return userid
56+
elif(goal == "token"):
57+
return token
58+
59+
def load_token(project):
60+
syspath = which_path() + "/config.yaml"
61+
goal = "token"
62+
63+
# check if config file exists
64+
if not os.path.exists(syspath):
65+
create_config(syspath)
66+
return load_config(project, goal, syspath)
67+
else:
68+
return load_config(project, goal, syspath)
69+
70+
71+
def load_userid(project):
72+
syspath = which_path() + "/config.yaml"
73+
goal = "id"
74+
75+
# check if config file exists
76+
if not os.path.exists(syspath):
77+
create_config(syspath)
78+
return load_config(project, goal, syspath)
79+
else:
80+
return load_config(project, goal, syspath)

examples/examples.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from gs_api_client import SyncGridscaleApiClient, GridscaleApiClient, models
88
from gs_api_client import Configuration
99

10+
from configloader import load_token, load_userid
11+
1012

1113
if __name__ == '__main__':
1214

@@ -16,11 +18,12 @@
1618
# api_config.debug = True
1719
api_config.host = 'https://api.gridscale.io'
1820

19-
#TODO: Insert your API token and User ID
20-
api_config.api_key['X-Auth-Token'] = "AUTH_TOKEN"
21-
api_config.api_key['X-Auth-UserId'] = "USER_UUID"
22-
# api_config.debug = True
23-
21+
#TODO: Change project
22+
project = "default"
23+
api_config.api_key['X-Auth-Token'] = load_token(project)
24+
api_config.api_key['X-Auth-UserId'] = load_userid(project)
25+
api_config.debug = True
26+
2427
print('-' * 80)
2528
client = SyncGridscaleApiClient(configuration=api_config, http_info=False)
2629

@@ -34,7 +37,7 @@
3437
get_templates_response = client.get_templates()
3538
templates = get_templates_response['templates'].values()
3639
template_by_name = index_by_key(templates, 'name')
37-
template = template_by_name['Debian 9']
40+
template = template_by_name['Debian 11']
3841

3942
# create storage
4043
create_storage_response = client.create_storage({

0 commit comments

Comments
 (0)