Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/develop' into featu…
Browse files Browse the repository at this point in the history
…re/asynch-save
  • Loading branch information
ayeleta committed Feb 7, 2017
2 parents 8674d7e + f7f00b1 commit 2ccddfe
Show file tree
Hide file tree
Showing 18 changed files with 594 additions and 77 deletions.
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
after_success: coveralls
install:
- pip install -r ./test_requirements.txt
- pip install "cloudshell-core>=2.0.0,<2.1.0"
- pip install "cloudshell-automation-api>=7.1,<7.3"
- pip install "cloudshell-shell-core>=2.3.0,<2.4.0"
- pip install pytest-cov
language: python
notifications:
webhools: "https://qualisystemslab-cloudshell-orch-sandbox.getbadges.io/api/app/webhook/96453247-3989-49f9-a6c0-201036e14a70"
python:
- "2.7"
script:
py.test --cov=sandbox_scripts
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# cloudshell-orch-sandbox

[![Coverage Status](https://coveralls.io/repos/github/QualiSystemsLab/cloudshell-orch-sandbox/badge.svg?branch=develop)](https://coveralls.io/github/QualiSystemsLab/cloudshell-orch-sandbox?branch=develop) [![Build Status](https://travis-ci.org/QualiSystemsLab/cloudshell-orch-sandbox.svg?branch=develop)](https://travis-ci.org/QualiSystemsLab/cloudshell-orch-sandbox)

Package for sandbox orchestration script functions


2 changes: 1 addition & 1 deletion sandbox_scripts/QualiEnvironmentUtils/QualiUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ def __init__(self, name, message):
self.name = name

def __str__(self):
return 'CloudShell error at ' + self.name + '. Error is:' + self.message
return 'CloudShell error at ' + self.name + '. Error is: ' + self.message
65 changes: 25 additions & 40 deletions sandbox_scripts/QualiEnvironmentUtils/Sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
from cloudshell.helpers.scripts import cloudshell_scripts_helpers as helpers
from os.path import *

from quali_utils.quali_packaging import PackageEditor
from quali_api_client import QualiAPIClient


SEVERITY_INFO = 20
SEVERITY_ERROR = 40
Expand Down Expand Up @@ -46,15 +43,14 @@ def __init__(self, reservation_id, logger):

# ----------------------------------
# ----------------------------------
def write_message_to_output(self, message, severity_level=SEVERITY_INFO):
def _write_message_to_output(self, message, severity_level=SEVERITY_INFO):
"""
Write a message to the output window
"""

if severity_level == SEVERITY_INFO:
self.api_session.WriteMessageToReservationOutput(self.id, message)
elif severity_level == SEVERITY_ERROR:
self.api_session.WriteMessageToReservationOutput(self.id, '<font color="red">' + message + '</font>')
self.api_session.WriteMessageToReservationOutput(self.id, '<font color="red">' + message + '</font>')

# ----------------------------------
def report_error(self, error_message, raise_error=True, write_to_output_window=False):
Expand All @@ -64,9 +60,10 @@ def report_error(self, error_message, raise_error=True, write_to_output_window=F
:param bool raise_error: Do you want to throw an exception
:param bool write_to_output_window: Would you like to write the message to the output window
"""
self._logger.error(error_message)
if self._logger:
self._logger.error(error_message)
if write_to_output_window:
self.write_message_to_output(error_message, SEVERITY_ERROR)
self._write_message_to_output(error_message, SEVERITY_ERROR)
if raise_error:
raise QualiError(self.id, error_message)

Expand All @@ -77,9 +74,10 @@ def report_info(self, message, write_to_output_window=False):
:param str message: The message you would like to present
:param bool write_to_output_window: Would you like to write the message to the output window?
"""
self._logger.info(message)
if self._logger:
self._logger.info(message)
if write_to_output_window:
self.write_message_to_output(message, SEVERITY_INFO)
self._write_message_to_output(message, SEVERITY_INFO)

# ----------------------------------
def get_root_resources(self):
Expand Down Expand Up @@ -127,9 +125,6 @@ def get_details(self):
"""
try:
return self.api_session.GetReservationDetails(self.id)
except QualiError as qe:
err = "Failed to get the Sandbox's details. " + str(qe)
self.report_error(error_message=err)
except:
err = "Failed to get the Sandbox's details. Unexpected error: " + str(sys.exc_info()[0])
self.report_error(error_message=err)
Expand Down Expand Up @@ -170,7 +165,8 @@ def activate_connectors(self, write_to_output=True):
bi_endpoints.append(endpoint.Target)
bi_endpoints.append(endpoint.Source)
if not bi_endpoints:
self.report_info(message="No connectors to connect for reservation {0}".format(self.id))
self.report_info(message="No connectors to connect for reservation {0}".format(self.id),
write_to_output_window=write_to_output)
return
self.api_session.ConnectRoutesInReservation(self.id, bi_endpoints, 'bi')
self.report_info(message="Connectors connected", write_to_output_window=write_to_output)
Expand Down Expand Up @@ -203,7 +199,8 @@ def activate_routes(self, write_to_output=True):
uni_endpoints.append(route_endpoint.Target)

if not bi_endpoints and not uni_endpoints:
self.report_info(message="No routes to connect for reservation {0}".format(self.id))
self.report_info(message="No routes to connect for reservation {0}".format(self.id),
write_to_output_window=write_to_output)
return
if bi_endpoints:
self.api_session.ConnectRoutesInReservation(self.id, bi_endpoints, 'bi')
Expand Down Expand Up @@ -256,31 +253,19 @@ def enqueue_command(self, commandName, commandInputs=[], printOutput=False):
# -----------------------------------------
# -----------------------------------------
def save_sandbox_as_blueprint(self, blueprint_name, write_to_output=True):
snapshot_exist = True

try:
full_path = None
tp = self.api_session.GetActiveTopologyNames()

for value in tp.Topologies:
filename = basename(value)
if filename == blueprint_name:
full_path = value
break

if full_path is None:
snapshot_exist = False
#TODO - fullpath should be passed as a param to the function and not hard coded
# save the current Sandbox as a new Blueprint with the given snapshot name
fullpath = 'Snapshots'
self.api_session.SaveReservationAsTopology(self.id, topologyName=blueprint_name,folderFullPath=fullpath, includeInactiveRoutes=True)

except CloudShellAPIError as error:
err = "Failed to save sandbox as blueprint. " + error.message
self.report_error(error_message=err, write_to_output_window=write_to_output)
if snapshot_exist:
err = "Blueprint " + blueprint_name + " already exist. Please select a different name."
self.report_error(error_message=err, write_to_output_window=write_to_output)
raise Exception('Blueprint name already exist. Please select a different name.')
# save the current Sandbox as a new Blueprint with the given snapshot name
fullpath = 'Snapshots'
self.api_session.SaveReservationAsTopology(self.id, topologyName=blueprint_name,folderFullPath=fullpath, includeInactiveRoutes=True)
self.report_error(error_message=err, raise_error=True, write_to_output_window=write_to_output)
# if snapshot_exist:
# err = "Blueprint " + blueprint_name + " already exist. Please select a different name."
# self.report_error(error_message=err, write_to_output_window=write_to_output)
# raise Exception('Blueprint name already exist. Please select a different name.')

#update the new snapshot with the user as owner
username = helpers.get_reservation_context_details().owner_user
Expand All @@ -292,8 +277,8 @@ def save_sandbox_as_blueprint(self, blueprint_name, write_to_output=True):
# -----------------------------------------
def is_abstract(self, resource_alias):
for abstract_resource in self.blueprint_details.AbstractResources:
if resource_alias == abstract_resource.Alias:
return True
if resource_alias == abstract_resource.Alias:
return True
return False

# -----------------------------------------
Expand All @@ -312,8 +297,8 @@ def get_storage_server_resource(self):
def get_config_set_pool_resource(self):
root_resources = self.get_root_resources()
for resource in root_resources:
if resource.model.lower() == 'config set pool':
return resource
if resource.model.lower() == 'config set pool':
return resource
return None


Expand Down
36 changes: 36 additions & 0 deletions sandbox_scripts/QualiEnvironmentUtils/SnapshotManager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#TODO: implement this file/move code from NetworkSaveNResource
from sandbox_scripts.QualiEnvironmentUtils.StorageManager import StorageManager
from sandbox_scripts.QualiEnvironmentUtils.QualiUtils import QualiError
'''
class SnapshotManager(object):
def __init__(self, storage_client):
self.storage_client = storage_client
self.config_files_root = self.storage_client.get_configs_root()
# ----------------------------------
# Is this Sandbox originates from a snapshot Blueprint?
# ----------------------------------
def is_snapshot(self,fileName = " "):
# check if there is a directory with the Blueprint's name under the snapshots dir
if fileName != " ":
env_dir = self.config_files_root + '/Snapshots/' + fileName
else:
env_dir = self.config_files_root + '/Snapshots/' + self.sandbox.Blueprint_name
env_dir = env_dir.replace(' ', '_')
return self.storage_client.dir_exist(env_dir)
# ----------------------------------
# delete file name on storage
# ----------------------------------
def delete_src_file(self,fileName):
env_dir = self.config_files_root + '/Snapshots/' + fileName
env_dir = env_dir.replace(' ', '_')
self.storage_client.delete(env_dir)
# ----------------------------------
def create_snapshot_dir(self,snapshot_name):
env_dir = self.config_files_root + '/Snapshots/' + snapshot_name.strip()
if not self.storage_client.dir_exist(env_dir):
self.storage_client.create_dir(env_dir, write_to_output=True)
'''
1 change: 1 addition & 0 deletions sandbox_scripts/QualiEnvironmentUtils/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Loading

0 comments on commit 2ccddfe

Please sign in to comment.