Commit 9eab6201 authored by Spiros Koulouzis's avatar Spiros Koulouzis

added semaphore api definition

parent 6c4561c2
......@@ -4,7 +4,7 @@
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="jdk" jdkName="Python 3.8 (deployer)" jdkType="Python SDK" />
<orderEntry type="jdk" jdkName="Python 3.7 (deployer)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="PyDocumentationSettings">
......
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8 (deployer)" project-jdk-type="Python SDK" />
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7 (deployer)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
......@@ -26,22 +26,22 @@ class SemaphoreHelper:
self.projects_api = api.ProjectsApi(api_client=api_client)
def create_project_if_not_exists(self, name, git_url, private_key,inventory_contents,playbook_name):
the_project = self.create_project(name)
key = self.create_ssh_key(name,the_project.id,private_key)
inventory= self.create_inventory(name,the_project.id,key.id,inventory_contents)
repository = self.create_repository(name,the_project.id,key.id,git_url)
template = self.create_template(the_project.id,key.id,inventory.id,repository.id,playbook_name)
task = Task(template_id=template.id, playbook=playbook_name)
self.project_api.project_project_id_tasks_post(the_project.id, task)
tasks = self.project_api.project_project_id_tasks_get(the_project.id)
for task in tasks:
print(task)
return the_project
# def create_project_if_not_exists(self, name, git_url, private_key,inventory_contents,playbook_name):
# the_project = self.create_project(name)
# key = self.create_ssh_key(name,the_project.id,private_key)
# inventory= self.create_inventory(name,the_project.id,key.id,inventory_contents)
# repository = self.create_repository(name,the_project.id,key.id,git_url)
#
# template = self.create_template(the_project.id,key.id,inventory.id,repository.id,playbook_name)
#
# task = Task(template_id=template.id, playbook=playbook_name)
# self.project_api.project_project_id_tasks_post(the_project.id, task)
#
# tasks = self.project_api.project_project_id_tasks_get(the_project.id)
# for task in tasks:
# print(task)
#
# return the_project
def find_projects_by_name(self, name):
projects = self.projects_api.projects_get()
......
This diff is collapsed.
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 2.7 (semaphore-python-client-generated)" project-jdk-type="Python SDK" />
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7 (semaphore-python-client-generated)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
......@@ -4,7 +4,7 @@
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="jdk" jdkName="Python 2.7 (semaphore-python-client-generated)" jdkType="Python SDK" />
<orderEntry type="jdk" jdkName="Python 3.7 (semaphore-python-client-generated)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="PyDocumentationSettings">
......
......@@ -2,4 +2,4 @@ certifi >= 14.05.14
six >= 1.10
python_dateutil >= 2.5.3
setuptools >= 21.0.0
urllib3 >= 1.15.1
urllib3== 1.25.8
......@@ -638,3 +638,6 @@ class ApiClient(object):
if klass_name:
instance = self.__deserialize(data, klass_name)
return instance
import os
import sys
import urllib3
from semaphore_client import Configuration, ApiClient, api, ProjectRequest, Login, Repository, Inventory, \
AccessKeyRequest, InventoryRequest, RepositoryRequest, TemplateRequest, Task
class SemaphoreHelper:
def __init__(self, semaphore_base_url, username, password):
self.init_semaphore_client(semaphore_base_url, username, password)
def init_semaphore_client(self, polemarch_base_url, username, password):
configuration = Configuration()
configuration.host = polemarch_base_url
configuration.username = username
configuration.password = password
api_client = ApiClient(configuration=configuration)
if not api_client.cookie:
self.authentication_api = api.AuthenticationApi(api_client=api_client)
login_body = Login(auth=username, password=password)
self.authentication_api.auth_login_post(login_body)
tokens = self.authentication_api.user_tokens_get()
self.default_api = api.DefaultApi(api_client=api_client)
self.project_api = api.ProjectApi(api_client=api_client)
self.user_api = api.UserApi (api_client=api_client)
self.projects_api = api.ProjectsApi(api_client=api_client)
def find_projects_by_name(self, name):
projects = self.projects_api.projects_get()
projects_with_name = []
for project in projects:
if project.name == name:
projects_with_name.append(project)
return projects_with_name
def project_with_name_exists(self, name):
projects = self.find_projects_by_name(name)
if not projects:
return False
return True
def create_project(self, name):
project_request = ProjectRequest(name=name)
self.projects_api.projects_post(project_request)
projects = self.find_projects_by_name(name)
return projects[len(projects)-1].id
def create_ssh_key(self, name, project_id, private_key):
key_request = AccessKeyRequest(name=name, type='ssh', project_id=project_id, secret=private_key)
self.project_api.project_project_id_keys_post(project_id, key_request)
keys = self.project_api.project_project_id_keys_get(project_id, name, 'asc', key_type='ssh')
return keys[len(keys) - 1].id
def create_inventory(self, name, project_id,ssh_key_id, inventory_contents):
inventory_request = InventoryRequest( name=name, project_id=project_id, inventory=inventory_contents,
ssh_key_id=ssh_key_id, type='static')
self.project_api.project_project_id_inventory_post(project_id,inventory_request)
inventories = self.project_api.project_project_id_inventory_get(project_id, name, 'asc')
return inventories[len(inventories) - 1].id
def create_repository(self, name, project_id, key_id, git_url):
repository_request = RepositoryRequest(name=name, project_id=project_id, git_url=git_url, ssh_key_id=key_id)
self.project_api.project_project_id_repositories_post(project_id, repository=repository_request)
repositories = self.project_api.project_project_id_repositories_get(project_id, name, 'asc')
return repositories[len(repositories) - 1].id
def create_template(self, project_id,key_id,inventory_id,repository_id,playbook_name):
template_request = TemplateRequest(ssh_key_id=key_id, project_id=project_id, inventory_id=inventory_id,
repository_id=repository_id, alias=playbook_name, playbook=playbook_name)
self.project_api.project_project_id_templates_post(project_id, template_request)
templates = self.project_api.project_project_id_templates_get(project_id, playbook_name, 'asc')
return templates[len(templates) - 1].id
def execute_task(self, project_id, template_id, playbook_name):
task = Task(template_id=template_id, playbook=playbook_name)
self.project_api.project_project_id_tasks_post(project_id, task)
tasks = self.project_api.project_project_id_tasks_get(project_id)
return tasks[len(tasks) - 1].id
def get_task(self,project_id,task_id):
return self.project_api.project_project_id_tasks_task_id_get(project_id,task_id)
@classmethod
def service_is_up(cls, url):
code = None
try:
http = urllib3.PoolManager()
r = http.request('HEAD', url)
except Exception as e:
return False
return True
\ No newline at end of file
coverage>=4.0.3
nose>=1.3.7
pluggy>=0.3.1
pluggy>=0.13.1
py>=1.4.31
randomize>=0.13
names==0.3.0
\ No newline at end of file
from semaphore_client import Configuration, ApiClient, Login, api
class TestHelper:
def __init__(self, semaphore_base_url=None,semaphore_username=None,semaphore_password=None):
if not semaphore_base_url:
self.semaphore_base_url = 'http://localhost:3000/api'
if not semaphore_username:
self.semaphore_username = 'admin'
if not semaphore_password:
self.semaphore_password = 'password'
self.init_semaphore_client()
def init_semaphore_client(self):
configuration = Configuration()
configuration.host = self.semaphore_base_url
configuration.username = self.semaphore_username
configuration.password = self.semaphore_password
self.api_client = ApiClient(configuration=configuration)
self.authentication_api = api.AuthenticationApi(api_client=self.api_client)
self.default_api = api.DefaultApi(api_client=self.api_client)
self.project_api = api.ProjectApi(api_client=self.api_client)
self.user_api = api.UserApi(api_client=self.api_client)
self.projects_api = api.ProjectsApi(api_client=self.api_client)
\ No newline at end of file
......@@ -19,31 +19,26 @@ import semaphore_client
from semaphore_client import APIToken
from semaphore_client.models.login import Login # noqa: E501
from semaphore_client.rest import ApiException
from test.test_helper import TestHelper
from semaphore_client.semaphore_helper import SemaphoreHelper
class TestLogin(unittest.TestCase):
"""Login unit test stubs"""
def setUp(self):
self.helper = TestHelper()
pass
self.semaphore_base_url = 'http://localhost:3000/api'
if SemaphoreHelper.service_is_up(self.semaphore_base_url):
self.username = 'admin'
self.password = 'password'
def tearDown(self):
pass
def testLogin(self):
"""Test Login"""
login = semaphore_client.models.login.Login() # noq
login.password = self.helper.semaphore_password
login.auth = self.helper.semaphore_username
self.helper.authentication_api.auth_login_post(login)
cookie = self.helper.authentication_api.api_client.cookie
self.assertIsNotNone(cookie)
tokens = self.helper.authentication_api.user_tokens_get()
self.assertIsNotNone(tokens)
newToken = self.helper.authentication_api.user_tokens_post()
self.assertIsNotNone(newToken)
if SemaphoreHelper.service_is_up(self.semaphore_base_url):
self.helper = SemaphoreHelper(self.semaphore_base_url, self.username, self.password)
......
......@@ -14,26 +14,75 @@
from __future__ import absolute_import
import unittest
import yaml
import semaphore_client
from semaphore_client.models.task import Task # noqa: E501
from semaphore_client.rest import ApiException
from semaphore_client.semaphore_helper import SemaphoreHelper
import names
yaml.Dumper.ignore_aliases = lambda *args : True
class TestTask(unittest.TestCase):
"""Task unit test stubs"""
def setUp(self):
pass
self.semaphore_base_url = 'http://localhost:3000/api'
if SemaphoreHelper.service_is_up(self.semaphore_base_url):
self.username = 'admin'
self.password = 'password'
self.project_name = names.get_first_name()
self.private_key = '-----BEGIN RSA PRIVATE KEY-----MIIEowIBAAKCAQEAg0blRNV6cm3RTiivpzE8HR4JzKZRVIBZ7bxeNoMz0' \
'-----END RSA PRIVATE KEY-----'
vms = []
vm = {}
vm['role'] = 'master'
vm['public_ip'] = '192.168.1.10'
vm['user_name'] = 'user_name'
vms.append(vm)
vm['role'] = 'worker'
vm['public_ip'] = '192.168.1.11'
vm['role'] = 'worker'
vm['public_ip'] = '192.168.1.12'
vms.append(vm)
self.inventory_contents = yaml.dump( self.build_yml_inventory(vms),default_flow_style=False)
self.git_url = 'https://github.com/skoulouzis/playbooks.git'
self.playbook_name = 'get_ip_addresses.yaml'
self.semaphore_helper = SemaphoreHelper(self.semaphore_base_url, self.username, self.password)
def tearDown(self):
pass
def testTask(self):
"""Test Task"""
# FIXME: construct object with mandatory attributes with example values
# model = semaphore_client.models.task.Task() # noqa: E501
pass
if SemaphoreHelper.service_is_up(self.semaphore_base_url):
project_id = self.semaphore_helper.create_project(self.project_name)
key_id = self.semaphore_helper.create_ssh_key(self.project_name, project_id, self.private_key)
inventory_id = self.semaphore_helper.create_inventory(self.project_name, project_id, key_id, self.inventory_contents)
repository_id = self.semaphore_helper.create_repository(self.project_name, project_id, key_id, self.git_url)
template_id = self.semaphore_helper.create_template(project_id, key_id, inventory_id, repository_id, self.playbook_name)
task_id = self.semaphore_helper.execute_task(project_id, template_id, self.playbook_name)
task = self.semaphore_helper.get_task(project_id, task_id)
def build_yml_inventory(self, vms):
inventory = {}
all = {}
vars = {'ansible_ssh_common_args':'-o StrictHostKeyChecking=no'}
vars['ansible_ssh_user'] = vms[0]['user_name']
children = {}
for vm in vms:
role = vm['role']
public_ip = vm['public_ip']
if role not in children:
hosts = {}
else:
hosts = children[role]
hosts[public_ip] = vars
children[role] = hosts
all['children'] = children
inventory['all'] = all
return inventory
if __name__ == '__main__':
......
......@@ -2,12 +2,16 @@
<project version="4">
<component name="ChangeListManager">
<list default="true" id="3f84153d-6ed1-4691-94d6-53105266f15e" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/../deployer/test/test_deployer.py" beforeDir="false" afterPath="$PROJECT_DIR$/../deployer/test/test_deployer.py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../semaphore-python-client-generated/semaphore_client/semaphore_helper.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../deployer/.idea/drip-deployer.iml" beforeDir="false" afterPath="$PROJECT_DIR$/../deployer/.idea/drip-deployer.iml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../deployer/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../deployer/.idea/misc.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../deployer/service/semaphore_helper.py" beforeDir="false" afterPath="$PROJECT_DIR$/../deployer/service/semaphore_helper.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../semaphore-python-client-generated/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../semaphore-python-client-generated/.idea/misc.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../semaphore-python-client-generated/.idea/semaphore-python-client-generated.iml" beforeDir="false" afterPath="$PROJECT_DIR$/../semaphore-python-client-generated/.idea/semaphore-python-client-generated.iml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../semaphore-python-client-generated/test-requirements.txt" beforeDir="false" afterPath="$PROJECT_DIR$/../semaphore-python-client-generated/test-requirements.txt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../semaphore-python-client-generated/test/test_helper.py" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/../semaphore-python-client-generated/test/test_login.py" beforeDir="false" afterPath="$PROJECT_DIR$/../semaphore-python-client-generated/test/test_login.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/sure_tosca_client/api_client.py" beforeDir="false" afterPath="$PROJECT_DIR$/sure_tosca_client/api_client.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/test/test_default_api.py" beforeDir="false" afterPath="$PROJECT_DIR$/test/test_default_api.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../sure_tosca-flask-server/sure_tosca/service/tosca_template_service.py" beforeDir="false" afterPath="$PROJECT_DIR$/../sure_tosca-flask-server/sure_tosca/service/tosca_template_service.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../sure_tosca-flask-server/sure_tosca/test/test_default_controller.py" beforeDir="false" afterPath="$PROJECT_DIR$/../sure_tosca-flask-server/sure_tosca/test/test_default_controller.py" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
......@@ -26,7 +30,7 @@
</component>
<component name="PropertiesComponent">
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/../sure_tosca-flask-server" />
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
<property name="settings.editor.selected.configurable" value="com.jetbrains.python.configuration.PyActiveSdkModuleConfigurable" />
</component>
<component name="RunManager">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment