def prepare_orchestration_save_result(self, saved_file_path):
        artifact_type = saved_file_path.split(":")[0]
        identifier = saved_file_path.replace("{0}:".format(artifact_type), "")
        saved_artifact = OrchestrationSavedArtifact(
            identifier=identifier, artifact_type=artifact_type)
        saved_artifact_info = OrchestrationSavedArtifactInfo(
            resource_name=self._resource_name,
            created_date=datetime.datetime.now(),
            restore_rules=self._get_restore_rules(),
            saved_artifact=saved_artifact,
        )
        save_response = OrchestrationSaveResult(
            saved_artifacts_info=saved_artifact_info)
        self._validate_artifact_info_object(saved_artifact_info)

        return str(jsonpickle.encode(save_response, unpicklable=False))
Beispiel #2
0
    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())
Beispiel #3
0
    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=get_schema())
Beispiel #4
0
    def test_works_with_standard_json_serializer(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)

        result = json.dumps(orchestration_save_result,
                            cls=SimpleJSONEncoder,
                            indent=True)

        validate(json.loads(result), schema=self.get_schema())
    def save(self,
             folder_path='',
             configuration_type='running',
             vrf_management_name=None,
             return_artifact=False):
        """Backup 'startup-config' or 'running-config' from device to provided file_system [ftp|tftp]
        Also possible to backup config to localhost
        :param folder_path:  tftp/ftp server where file be saved
        :param configuration_type: type of configuration that will be saved (StartUp or Running)
        :param vrf_management_name: Virtual Routing and Forwarding management name
        :return: status message / exception
        :rtype: OrchestrationSavedArtifact or str
        """

        if hasattr(self.resource_config, "vrf_management_name"):
            vrf_management_name = vrf_management_name or self.resource_config.vrf_management_name

        self._validate_configuration_type(configuration_type)
        folder_path = self.get_path(folder_path)
        system_name = re.sub('\s+', '_', self.resource_config.name)[:23]
        time_stamp = time.strftime("%d%m%y-%H%M%S", time.localtime())
        destination_filename = '{0}-{1}-{2}'.format(system_name,
                                                    configuration_type.lower(),
                                                    time_stamp)
        full_path = join(folder_path, destination_filename)
        folder_path = self.get_path(full_path)
        self.save_flow.execute_flow(
            folder_path=folder_path,
            configuration_type=configuration_type.lower(),
            vrf_management_name=vrf_management_name)

        if return_artifact:
            artifact_type = full_path.split(':')[0]
            identifier = full_path.replace("{0}:".format(artifact_type), "")
            return OrchestrationSavedArtifact(identifier=identifier,
                                              artifact_type=artifact_type)
        return destination_filename
Beispiel #6
0
    def orchestration_save(self,
                           context,
                           cancellation_context,
                           mode,
                           custom_params=None):
        """
        Saves the Shell state and returns a description of the saved artifacts and information
        This command is intended for API use only by sandbox orchestration scripts to implement
        a save and restore workflow
        :param ResourceCommandContext context: the context object containing resource and reservation info
        :param CancellationContext cancellation_context: Object to signal a request for cancellation. Must be enabled in drivermetadata.xml as well
        :param str mode: Snapshot save mode, can be one of two values 'shallow' (default) or 'deep'
        :param str custom_params: Set of custom parameters for the save operation
        :return: SavedResults serialized as JSON
        :rtype: OrchestrationSaveResult
        """

        # See below an example implementation, here we use jsonpickle for serialization,
        # to use this sample, you'll need to add jsonpickle to your requirements.txt file
        # The JSON schema is defined at: https://github.com/QualiSystems/sandbox_orchestration_standard/blob/master/save%20%26%20restore/saved_artifact_info.schema.json
        # You can find more information and examples examples in the spec document at https://github.com/QualiSystems/sandbox_orchestration_standard/blob/master/save%20%26%20restore/save%20%26%20restore%20standard.md
        '''
        # By convention, all dates should be UTC
        created_date = datetime.datetime.utcnow()

        # This can be any unique identifier which can later be used to retrieve the artifact
        # such as filepath etc.

        # By convention, all dates should be UTC
        created_date = datetime.datetime.utcnow()

        # This can be any unique identifier which can later be used to retrieve the artifact
        # such as filepath etc.
        identifier = created_date.strftime('%y_%m_%d %H_%M_%S_%f')

        orchestration_saved_artifact = OrchestrationSavedArtifact('REPLACE_WITH_ARTIFACT_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)

        return OrchestrationSaveResult(saved_artifacts_info)
        '''
        self._log(
            context,
            'GigamonDriver orchestration_save called: %s\r\n' % custom_params)

        p = json.loads(custom_params)
        if 'folder_path' not in p:
            raise Exception(
                'Input JSON should be {"folder_path": "tftp://host/path"}')

        identifier_url = self.save(context=context,
                                   cancellation_context=cancellation_context,
                                   configuration_type=p.get(
                                       'configuration_type', 'running'),
                                   folder_path=p['folder_path'],
                                   vrf_management_name='no-vrf')
        created_date = datetime.datetime.utcnow()
        orchestration_saved_artifact = OrchestrationSavedArtifact(
            'tftp', identifier_url)
        saved_artifacts_info = OrchestrationSavedArtifactInfo(
            resource_name="some_resource",
            created_date=created_date,
            restore_rules=OrchestrationRestoreRules(
                requires_same_resource=True),
            saved_artifact=orchestration_saved_artifact)

        self._log(context, 'GigamonDriver orchestration_save returning\r\n')

        return OrchestrationSaveResult(saved_artifacts_info)