Skip to content

Commit

Permalink
Merge pull request #35 from QualiSystems/dev
Browse files Browse the repository at this point in the history
merging to master - new classes for save and restore
  • Loading branch information
doppleware committed Aug 1, 2016
2 parents 2da8b5f + 9356c49 commit edf306f
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ install:
- bash ./install_dependencies.sh

script:
- python runtests.py --with-coverage --cover-package=package
- python runtests.py --with-coverage --cover-package=cloudshell
- python setup.py test
- python setup.py sdist --format zip

Expand Down
2 changes: 2 additions & 0 deletions cloudshell/shell/core/interfaces/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)
51 changes: 51 additions & 0 deletions cloudshell/shell/core/interfaces/save_restore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from datetime import datetime


class OrchestrationSavedArtifact(object):
def __init__(self, artifact_type, identifier):
"""
Represents a saved artifact according to the save and restore standard.
This is a base class which can be extended with additional attributes required to later
restore the artifact such as location or credentials.
:param str artifact_type: Describes the type of saved artifact (e.g. vCenter_image, tftp_file, network_location)
:param str identifier: A unique identifier for the saved artifact (e.g the image url, a file full path)
"""
self.artifact_type = artifact_type
self.identifier = identifier


class OrchestrationSaveResult(object):
def __init__(self, saved_artifacts_info):
"""
Container class for the orhcestration_save result
:param OrchestrationSavedArtifactInfo saved_artifacts_info: An object describing the artifacts saved by this operation
"""
self.saved_artifacts_info = saved_artifacts_info


class OrchestrationRestoreRules(object):
def __init__(self, requires_same_resource, additional_rules={}):
"""
Container class for the orhcestration_save result
:param OrchestrationSavedArtifactInfo saved_artifacts_info: An object describing the artifacts saved by this operation
"""
self.requires_same_resource = requires_same_resource
for rule in additional_rules:
setattr(self, rule, additional_rules[rule])


class OrchestrationSavedArtifactInfo(object):
def __init__(self, resource_name, created_date, restore_rules, saved_artifact):
"""
This object describes the saved artifacts from a specific save operation.
This information is stored and may later be sent to the Shell as a part of a restore
operation.
:param str resource_name: The name of the resource on which the save operation was performed
:param datetime created_date: The time in which this save operation occurred
:param OrchestrationRestoreRules restore_rules: A list of rules governing constraints on restoring the saved artifact
:param OrchestrationSavedArtifact saved_artifact: The description of the saved artifact itself, the saved artifact can be of different types
"""
self.resource_name = resource_name
self.created_date = created_date
self.restore_rules = restore_rules
self.saved_artifact = saved_artifact
4 changes: 2 additions & 2 deletions cloudshell/tests/test_session/test_cloudshell_session.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from unittest import TestCase

from mock import Mock, mock, MagicMock
import mock
from mock import Mock, MagicMock

from cloudshell.shell.core.driver_context import ResourceContextDetails, ResourceCommandContext, \
ReservationContextDetails, ConnectivityContext, ResourceRemoteCommandContext
Expand Down
3 changes: 1 addition & 2 deletions cloudshell/tests/test_session/test_logging_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
from logging import Logger
from unittest import TestCase

from mock import mock

import mock
from cloudshell.shell.core.driver_context import AutoLoadCommandContext, ResourceContextDetails, ResourceCommandContext, \
ResourceRemoteCommandContext, ReservationContextDetails
from cloudshell.shell.core.session.logging_session import LoggingSessionContext
Expand Down
111 changes: 111 additions & 0 deletions cloudshell/tests/test_session/test_save_and_restore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
from unittest import TestCase
import jsonpickle
from jsonschema import validate
import datetime

from cloudshell.shell.core.interfaces.save_restore import OrchestrationSavedArtifact, \
OrchestrationSavedArtifactInfo, OrchestrationSaveResult, OrchestrationRestoreRules


class TestSaveAndRestore(TestCase):

def get_schema(self):
return {
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"definitions": {
"artifact": {
"type": "object",
"properties": {
"artifact_type": {
"type": "string"
},
"identifier": {
"type": "string"
}
},
"required": [
"artifact_type",
"identifier"
]
}
},
"properties": {
"saved_artifacts_info": {
"type": "object",
"properties": {
"resource_name": {
"type": "string"
},
"created_date": {
"type": "string",
"format": "date-time"
},
"restore_rules": {
"type": "object",
"properties": {
"requires_same_resource": {
"type": "boolean"
}
},
"required": [
"requires_same_resource"
]
},
"saved_artifact": {
"allOf": [
{
"$ref": "#/definitions/artifact"
},
{
"properties": {}
}
],
"additionalProperties": True
}
},
"required": [
"resource_name",
"created_date",
"restore_rules",
"saved_artifact"
]
}
},
"required": [
"saved_artifacts_info"
]
}

def test_serializes_to_schema(self):
created_date = datetime.datetime.now()
identifier = created_date.strftime('%y_%m_%d %H_%M_%S_%f')

orchestration_saved_artifact = OrchestrationSavedArtifact('test_type', identifier)

saved_artifacts_info = OrchestrationSavedArtifactInfo(
resource_name="some_resource",
created_date=created_date,
restore_rules=OrchestrationRestoreRules(requires_same_resource=True),
saved_artifact=orchestration_saved_artifact)

orchestration_save_result = OrchestrationSaveResult(saved_artifacts_info)
json_string = jsonpickle.encode(orchestration_save_result, unpicklable=False)
validate(jsonpickle.loads(json_string), schema=self.get_schema())

def test_can_serialize_custom_rules(self):
created_date = datetime.datetime.now()
identifier = created_date.strftime('%y_%m_%d %H_%M_%S_%f')

orchestration_saved_artifact = OrchestrationSavedArtifact('test_type', identifier)

saved_artifacts_info = OrchestrationSavedArtifactInfo(
resource_name="some_resource",
created_date=created_date,
restore_rules=OrchestrationRestoreRules(requires_same_resource=True, additional_rules={'some_rule': 'True'}),
saved_artifact=orchestration_saved_artifact)

orchestration_save_result = OrchestrationSaveResult(saved_artifacts_info)
json_string = jsonpickle.encode(orchestration_save_result, unpicklable=False)
validate(jsonpickle.loads(json_string), schema=self.get_schema())

7 changes: 4 additions & 3 deletions install_dependencies.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/usr/bin/env bash
if [ "${TRAVIS_BRANCH}" = "master" ]; then
if [ "${TRAVIS_BRANCH}" = "master" ]
then
pip install -r requirements.txt
pip install cloudshell-automation-api>=7.0.0.0,<7.1.0.0
else
pip install -r requirements.txt --index-url https://testpypi.python.org/simple
pip install cloudshell-automation-api>=7.0.0.0,<7.1.0.0 --index-url https://testpypi.python.org/simple
pip install -r requirements.txt --extra-index-url https://testpypi.python.org/simple
pip install cloudshell-automation-api>=7.0.0.0,<7.1.0.0 --extra-index-url https://testpypi.python.org/simple
fi

pip install -r test_requirements.txt
Expand Down
4 changes: 3 additions & 1 deletion test_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ mock
teamcity-messages
jsonpickle
nose-exclude
inject
inject
cloudshell-automation-api
jsonschema

0 comments on commit edf306f

Please sign in to comment.