Skip to content

Commit

Permalink
STABLE Version. : Alpha v1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Davi Mello authored and Davi Mello committed Jan 27, 2021
1 parent 1ec8aff commit 549f0c1
Show file tree
Hide file tree
Showing 14 changed files with 207 additions and 16 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@

export MAGI_YAML_AUTH_BASIC_USER=admin
export MAGI_YAML_AUTH_BASIC_PASS=admin
14 changes: 14 additions & 0 deletions magi/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import click
from magi.execution import run



@click.command()
@click.option('-d', '--dir', 'directory', required=True, help='The directory of yaml file to do deploy.')
# @click.option('-f', '--file', 'file', required=False, help='The yaml file name (file.: input.yaml || use .: input ) to do deploy.')
def command(directory: str = ""):
run(folder_path=directory)


if __name__ == '__main__':
command()
14 changes: 14 additions & 0 deletions magi/execution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import os
from magi.nodesolver import nodesolver
from magi.readfiles import read_yaml_files
from magi.rest_engine import rest_call

def run(folder_path:str=None)->None:

YAMLS_ON_FOLDER : dict = read_yaml_files(folder_path)

for yaml in YAMLS_ON_FOLDER:
request_list : list = nodesolver(YAMLS_ON_FOLDER[yaml])
for rest_request in request_list:
rest_call(**rest_request)

25 changes: 21 additions & 4 deletions magi/nodesolver.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import logging
from magi.sys_env import replace_by_sys_variables



def __get_basic_info__(raw_input:dict) -> dict:
return raw_input.get("metadata")
def __get_basic_info__(raw_input:dict, sys_env_replacer:bool=True) -> dict:
metadata : dict = raw_input.get("metadata")
if sys_env_replacer:
replace_by_sys_variables(metadata)
return metadata


def __metadata_auth__(raw_metadata_auth_input:dict) -> dict:
Expand Down Expand Up @@ -51,12 +55,13 @@ def __url__(url_base: str, url_endpoint: str) -> str:
return f"{base}/{endpoint}"

def __inject_basic_info__(metadata_input:dict, raw_request_input:dict) -> dict:
metadata: dict = metadata_input.copy()


result : dict = {}
for node_name in raw_request_input:
request_list : list = []
for request_item in raw_request_input[node_name]:
metadata: dict = metadata_input.copy()
request_item_copy: dict = request_item.copy()
request : dict = {}
if metadata.get("url") and request_item_copy.get("url"):
Expand All @@ -76,4 +81,16 @@ def __inject_basic_info__(metadata_input:dict, raw_request_input:dict) -> dict:


# TODO: Implements the method sort
def __sort_requests__(raw_input:dict) -> dict : ...
def __sort_requests__(raw_input:dict) -> list :
result : list = []
for node in raw_input:
for request in raw_input[node]:
result.append(request)
return result


def nodesolver(input_data: dict) -> list:
metadata: dict = __metadata__(input_data)
request_dict :dict = __inject_basic_info__(metadata_input=metadata, raw_request_input=__get_requests__(input_data))

return __sort_requests__(request_dict)
10 changes: 10 additions & 0 deletions magi/rest_engine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import requests
import logging


def rest_call(rest_engine = requests.request, **kwargs) -> int:
call : requests.Response = rest_engine(**kwargs)
logging.info(msg=f"REST_CALL: {kwargs['url']} -> method : {kwargs['method']} -> status_code : {call.status_code}")
logging.debug(msg=f"JSON : {kwargs['url']}")
print(f"REST_CALL: {kwargs['url']} -> method : {kwargs['method']} -> status_code : {call.status_code}")
return call.status_code
17 changes: 17 additions & 0 deletions magi/sys_env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import os

def replace_by_sys_variables(metadata_raw:dict, sys_var_prefix:str="MAGI_YAML") -> None:
"""
This method change the input object in memory.
"""

metadata: dict = metadata_raw

for key in metadata:
sys_var: str = f"{sys_var_prefix}_{str(key).upper()}"
if isinstance(metadata[key], dict):
replace_by_sys_variables(metadata_raw=metadata[key], sys_var_prefix=sys_var)

elif os.getenv(sys_var):
metadata[key] = os.getenv(sys_var)

3 changes: 2 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ pytest==6.2.1
coverage==5.3.1
setuptools==51.3.3
wheel==0.36.2
twine==3.3.0
twine==3.3.0
responses==0.12.1
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
requests==2.25.1
pyaml==20.4.0
pyaml==20.4.0
click==7.1.2
4 changes: 3 additions & 1 deletion test/mock/alpha/mock_test_1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ metadata: #basic_info
basic:
user: "mock_user"
password: "mock_pass"
url: "mock.site.localhost"
url: "http://mock.site.localhost"
requests:
test_default: #By default the "magi" will do a get
- url: "/dummy"
- url: "/dummy_2"

10 changes: 10 additions & 0 deletions test/mock/bitbucket/mock_test_nexus copy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
metadata: #basic_info
auth:
basic:
user: "admin"
password: "admin"
url: "http://127.0.0.1:8081"
requests:
test_default: #By default the "magi" will do a get
- url: "/service/rest/v1/repositories/docker/hosted"
- url: "/service/rest/v1/repositories"
22 changes: 22 additions & 0 deletions test/mock/bitbucket/mock_test_nexus.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
metadata: #basic_info
auth:
basic:
user: "admin"
password: "admin"
url: "http://127.0.0.1:8081"
requests:
test_default: #By default the "magi" will do a get
- url: "/service/rest/v1/repositories"
- url: "/service/rest/v1/repositories/helm/hosted"
method: 'post'
json:
name: internal_2
online: true
storage:
blobStoreName: default
strictContentTypeValidation: true
writePolicy: allow_once
cleanup:
policyNames:
- string

23 changes: 15 additions & 8 deletions test/test_nodesolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ def test__get_basic_info__(self):
'auth':
{'basic':
{'password': 'mock_pass', 'user': 'mock_user'}},
'url':'mock.site.localhost'}
'url':'http://mock.site.localhost'}

self.assertIsInstance(YAML, dict, "The Yaml must be a 'dict' instance")
self.assertIsInstance(__get_basic_info__(YAML), dict, "The method must return 'dict' instance")
self.assertEqual(__get_basic_info__(YAML), result, "The method must return the espect content.")
self.assertIsInstance(__get_basic_info__(raw_input=YAML.copy(), sys_env_replacer=False), dict, "The method must return 'dict' instance")
self.assertEqual(__get_basic_info__(raw_input=YAML.copy(), sys_env_replacer=False), result, "The method must return the espect content.")


def test__metadata_auth__(self):
Expand All @@ -38,7 +38,7 @@ def test__get_requests__(self):
YAML: dict = CASE1.get("mock_test_1")

result: dict = {
"test_default": [ {"url": "/dummy"} ]
"test_default": [ {"url": "/dummy"},{"url": "/dummy_2"}]
}

self.assertIsInstance(YAML, dict, "The Yaml must be a 'dict' instance")
Expand All @@ -50,7 +50,7 @@ def test__metadata__(self):
YAML: dict = CASE1.get("mock_test_1")

result: dict = {
"url": "mock.site.localhost",
"url": "http://mock.site.localhost",
"auth": ("mock_user", "mock_pass")
}

Expand All @@ -63,11 +63,18 @@ def test__inject_basic_info__(self):

result: dict = {
"test_default": [ {
"url": "mock.site.localhost/dummy",
"url": "http://mock.site.localhost/dummy",
"auth": ("mock_user", "mock_pass"),
"method": "GET"
} ]
},
{
"url": "http://mock.site.localhost/dummy_2",
"auth": ("mock_user", "mock_pass"),
"method": "GET"
}]
}

self.assertIsInstance(__inject_basic_info__(__metadata__(YAML), __get_requests__(YAML)), dict, "The method must return 'dict' instance")
self.assertEqual(__inject_basic_info__(__metadata__(YAML), __get_requests__(YAML)), result, "The method must return the espect content.")
self.assertEqual(__inject_basic_info__(__metadata__(YAML), __get_requests__(YAML)), result, "The method must return the espect content.")


44 changes: 44 additions & 0 deletions test/test_rest_engine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import unittest
import responses
import requests
import os
from magi.rest_engine import rest_call

# Project Magi dependencies
from magi.nodesolver import nodesolver
from magi.readfiles import read_yaml_files
from magi.rest_engine import rest_call



FOLDER: str = f"{os.path.dirname(__file__)}/mock"
CASE1: str = read_yaml_files(f"{FOLDER}/alpha")
YAML: str = CASE1["mock_test_1"]
request_list : list = nodesolver(YAML)


TEST_CASE_1_1 = {
"method": "GET",
"url": "http://mock.site.localhost/dummy",
"status": 200
}

TEST_CASE_1_2 = {
"method": "GET",
"url": "http://mock.site.localhost/dummy_2",
"status": 200
}

class Test_rest_engine(unittest.TestCase):

@responses.activate
def test_rest_call(self):
status_code: int = 200
# Using request Lib
responses.add(**TEST_CASE_1_1)
responses.add(**TEST_CASE_1_2)

for rest_request in request_list:
print(rest_request["url"])
self.assertEqual(status_code, rest_call(**rest_request))

31 changes: 31 additions & 0 deletions test/test_sys_env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import os
import unittest
from magi.sys_env import replace_by_sys_variables


class Test_sys_env(unittest.TestCase):

def test_replace_by_sys_variables(self):
CASE : dict = {
"test": {
"user": "hello",
"pass": "pass"
}
}

CASE_out : dict = {
"test": {
"user": "test",
"pass": "test_Pass0rd"
}
}

os.environ["MAGI_YAML_TEST_USER"] = CASE_out["test"]["user"]
os.environ["MAGI_YAML_TEST_PASS"] = CASE_out["test"]["pass"]


self.assertIsNone(replace_by_sys_variables(CASE.copy()))

replace_by_sys_variables(CASE.copy())
self.assertIsInstance(CASE, dict)
self.assertEqual(CASE, CASE_out)

0 comments on commit 549f0c1

Please sign in to comment.