Example #1
0
def cluster_create(data: dict, op_ctx: ctx.OperationContext):
    """Request handler for cluster create operation.

    :return: Defined entity of the native cluster
    :rtype: container_service_extension.def_.models.DefEntity
    """
    rde_in_use = server_utils.get_rde_version_in_use()
    input_entity: dict = data[RequestKey.INPUT_SPEC]
    payload_version = input_entity.get(
        rde_constants.PayloadKey.PAYLOAD_VERSION_RDE_1_0.value)  # noqa: E501
    # Reject request >= 2.0
    _raise_error_if_unsupported_payload_version(payload_version)
    # Convert the input entity to runtime rde format
    converted_native_entity: AbstractNativeEntity = rde_utils.convert_input_rde_to_runtime_rde_format(
        input_entity)  # noqa: E501

    # Redirect to generic handler if the backend supports RDE-2.0
    if semantic_version.Version(rde_in_use) >= semantic_version.Version(
            rde_constants.RDEVersion.RDE_2_0_0.value):  # noqa: E501
        rde_data: dict = cluster_handler.cluster_create(
            data={RequestKey.INPUT_SPEC: converted_native_entity.to_dict()},
            op_ctx=op_ctx)
        new_rde = common_models.DefEntity(**rde_data)
    else:
        # Based on the runtime rde, call the appropriate backend method.
        svc = cluster_service_factory.ClusterServiceFactory(
            op_ctx).get_cluster_service()  # noqa: E501
        new_rde: common_models.DefEntity = svc.create_cluster(
            converted_native_entity)  # noqa: E501

    # convert the created rde back to input rde version
    return _convert_rde_to_1_0_format(new_rde.to_dict())
Example #2
0
    def force_delete_cluster_by_cluster_id(self, cluster_id):
        uri = f'{self._uri}/cluster/{cluster_id}/action/force-delete'
        response = self.do_request(uri=uri,
                                   method=shared_constants.RequestMethod.POST,
                                   accept_type='application/json')

        return common_models.DefEntity(**self.process_response(response))
Example #3
0
    def delete_nfs_node_by_node_name(self, cluster_id: str, node_name: str):
        uri = f"{self._cluster_uri}/{cluster_id}/nfs/{node_name}"
        response = self.do_request(
            uri=uri,
            method=shared_constants.RequestMethod.DELETE,
            accept_type='application/json')

        return common_models.DefEntity(**self.process_response(response))
Example #4
0
def cluster_create(data: dict, op_ctx: ctx.OperationContext):
    """Request handler for cluster create operation.

    :return: Defined entity of the native cluster
    :rtype: container_service_extension.def_.models.DefEntity
    """
    input_entity: dict = data[RequestKey.INPUT_SPEC]
    payload_version = input_entity.get(
        rde_constants.PayloadKey.PAYLOAD_VERSION)  # noqa: E501
    rde_utils.raise_error_if_unsupported_payload_version(payload_version)

    # Validate the Input payload based on the (Operation, payload_version).
    # Get the validator based on the payload_version
    # ToDo : Don't use default cloudapi_client. Use the specific versioned one
    rde_validator_factory.get_validator(
        rde_version=rde_constants.
        MAP_INPUT_PAYLOAD_VERSION_TO_RDE_VERSION[payload_version]).validate(
            cloudapi_client=op_ctx.cloudapi_client,
            sysadmin_client=op_ctx.sysadmin_client,
            entity=input_entity)

    def_entity_service = entity_service.DefEntityService(
        op_ctx.cloudapi_client)  # noqa: E501
    entity_type = server_utils.get_registered_def_entity_type()
    converted_entity: AbstractNativeEntity = rde_utils.convert_input_rde_to_runtime_rde_format(
        input_entity)  # noqa: E501
    def_entity = common_models.DefEntity(
        entity=converted_entity, entityType=entity_type.id)  # noqa: E501

    # No need to set org context for non sysadmin users
    org_context = None
    if op_ctx.client.is_sysadmin():
        org_resource = pyvcloud_utils.get_org(
            op_ctx.client,
            org_name=converted_entity.metadata.org_name)  # noqa: E501
        org_context = org_resource.href.split('/')[-1]

    _, task_href = def_entity_service.create_entity(
        entity_type_id=entity_type.id,
        entity=def_entity,
        tenant_org_context=org_context,
        is_request_async=True)

    task_resource = op_ctx.sysadmin_client.get_resource(task_href)
    entity_id = task_resource.Owner.get('id')
    def_entity = def_entity_service.get_entity(entity_id)
    def_entity.entity.status.task_href = task_href
    telemetry_handler.record_user_action_details(
        cse_operation=telemetry_constants.CseOperation.V36_CLUSTER_APPLY,
        cse_params={
            server_constants.CLUSTER_ENTITY:
            def_entity,
            telemetry_constants.PayloadKey.SOURCE_DESCRIPTION:
            thread_local_data.get_thread_local_data(
                ThreadLocalData.USER_AGENT)  # noqa: E501
        })
    return def_entity.to_dict()
Example #5
0
 def delete_nfs_node_by_node_name(self, cluster_id: str, node_name: str):
     uri = f"{self._cluster_uri}/{cluster_id}/nfs/{node_name}"
     response = self._client._do_request_prim(
         shared_constants.RequestMethod.DELETE,
         uri,
         self._client._session,
         media_type='application/json',
         accept_type='application/json')
     return common_models.DefEntity(
         **response_processor.process_response(response))
Example #6
0
    def upgrade_cluster_by_cluster_id(
            self, cluster_id: str,
            cluster_upgrade_definition: common_models.DefEntity):  # noqa: E501
        uri = f'{self._uri}/cluster/{cluster_id}/action/upgrade'
        entity_dict = asdict(cluster_upgrade_definition.entity)
        response = self.do_request(uri=uri,
                                   method=shared_constants.RequestMethod.POST,
                                   accept_type='application/json',
                                   media_type='application/json',
                                   payload=entity_dict)

        return common_models.DefEntity(**self.process_response(response))
Example #7
0
    def update_cluster_by_cluster_id(
            self, cluster_id,
            cluster_entity_definition: rde_1_0_0.NativeEntity):  # noqa: E501
        cluster_entity_dict = asdict(cluster_entity_definition)
        uri = f"{self._cluster_uri}/{cluster_id}"
        response = self.do_request(uri=uri,
                                   method=shared_constants.RequestMethod.PUT,
                                   accept_type='application/json',
                                   media_type='application/json',
                                   payload=cluster_entity_dict)

        return common_models.DefEntity(**self.process_response(response))
Example #8
0
def _convert_rde_to_1_0_format(rde_data: dict) -> dict:
    """Convert defined entity to RDE 1.0.

    :param DefEntity rde_data: Defined entity dictionary
    :return: converted defined entity
    :rtype: dict
    """
    new_rde = common_models.DefEntity(**rde_data)
    new_native_entity: AbstractNativeEntity = rde_utils.convert_runtime_rde_to_input_rde_version_format(  # noqa: E501
        new_rde.entity, rde_constants.RDEVersion.RDE_1_0_0)
    new_rde.entity = new_native_entity
    new_rde.entityType = common_models.EntityType.NATIVE_ENTITY_TYPE_1_0_0.value.get_id(
    )  # noqa: E501
    return new_rde.to_dict()
Example #9
0
 def upgrade_cluster_by_cluster_id(
         self, cluster_id: str,
         cluster_upgrade_definition: common_models.DefEntity):  # noqa: E501
     uri = f'{self._uri}/cluster/{cluster_id}/action/upgrade'
     entity_dict = asdict(cluster_upgrade_definition.entity)
     response = self._client._do_request_prim(
         shared_constants.RequestMethod.POST,
         uri,
         self._client._session,
         contents=entity_dict,
         media_type='application/json',
         accept_type='application/json')
     return common_models.DefEntity(
         **response_processor.process_response(response))
Example #10
0
 def update_cluster_by_cluster_id(
         self, cluster_id,
         cluster_entity_definition: rde_1_0_0.NativeEntity):  # noqa: E501
     cluster_entity_dict = asdict(cluster_entity_definition)
     uri = f"{self._cluster_uri}/{cluster_id}"
     response = self._client._do_request_prim(
         shared_constants.RequestMethod.PUT,
         uri,
         self._client._session,
         contents=cluster_entity_dict,
         media_type='application/json',
         accept_type='application/json')
     return common_models.DefEntity(
         **response_processor.process_response(response))
    def upgrade_cluster_by_cluster_id(self, cluster_id, cluster_def_entity, **kwargs):  # noqa: E501
        """Get the upgrade plan for give cluster id.

        :param str cluster_id: unique id of the cluster
        :param dict cluster_def_entity: defined entity
        :return: string containing upgrade cluster task href
        :rtype: str
        """
        # TODO: check if we really need to decode-encode-decode-encode
        cluster_upgrade_definition = common_models.DefEntity(**cluster_def_entity)  # noqa: E501
        cluster_def_entity = \
            self._native_cluster_api.upgrade_cluster_by_cluster_id(
                cluster_id, cluster_upgrade_definition)
        task_href = cluster_def_entity.entity.status.task_href
        return client_utils.construct_task_console_message(task_href)
Example #12
0
    def create_cluster(self, cluster_create_spec: dict):
        """Call create native cluster CSE server endpoint.

        :param dict cluster_create_spec: Cluster create specification
        :return: defined entity object representing the response
        :rtype: common_models.DefEntity
        """
        uri = self._clusters_uri
        response = self.do_request(uri=uri,
                                   method=shared_constants.RequestMethod.POST,
                                   accept_type='application/json',
                                   media_type='application/json',
                                   payload=cluster_create_spec)

        return common_models.DefEntity(**self.process_response(response))
Example #13
0
    def update_cluster_by_cluster_id(self, cluster_id: str,
                                     cluster_update_spec: dict):  # noqa: E501
        """Call update native cluster CSE server endpoint.

        :param str cluster_id: ID of the cluster
        :param dict cluster_update_spec: Update cluster specification
        :return: defined entity object representing the response
        :rtype: common_models.DefEntity
        """
        uri = f"{self._cluster_uri}/{cluster_id}"
        response = self.do_request(uri=uri,
                                   method=shared_constants.RequestMethod.PUT,
                                   accept_type='application/json',
                                   media_type='application/json',
                                   payload=cluster_update_spec)

        return common_models.DefEntity(**self.process_response(response))
Example #14
0
def cluster_resize(data: dict, op_ctx: ctx.OperationContext):
    """Request handler for cluster resize operation.

    Validate data before actual resize is delegated to cluster service.

    :return: Defined entity of the native cluster
    :rtype: container_service_extension.def_.models.DefEntity
    """
    rde_in_use = server_utils.get_rde_version_in_use()
    input_entity: dict = data[RequestKey.INPUT_SPEC]
    payload_version = input_entity.get(
        rde_constants.PayloadKey.PAYLOAD_VERSION_RDE_1_0.value)  # noqa: E501
    # Reject request >= 2.0
    _raise_error_if_unsupported_payload_version(payload_version)
    # Convert the input entity to runtime rde format
    converted_native_entity: AbstractNativeEntity = rde_utils.convert_input_rde_to_runtime_rde_format(
        input_entity)  # noqa: E501

    # Redirect to generic handler if the backend supports RDE-2.0
    if semantic_version.Version(rde_in_use) >= semantic_version.Version(
            rde_constants.RDEVersion.RDE_2_0_0.value):  # noqa: E501
        data[RequestKey.INPUT_SPEC] = converted_native_entity.to_dict()
        rde_data: dict = cluster_handler.cluster_update(
            data=data, op_ctx=op_ctx)  # noqa: E501
        new_rde = common_models.DefEntity(**rde_data)
    else:
        # Based on the runtime rde, call the appropriate backend method.
        svc = cluster_service_factory.ClusterServiceFactory(
            op_ctx).get_cluster_service()  # noqa: E501
        cluster_id = data[RequestKey.CLUSTER_ID]
        curr_entity = svc.entity_svc.get_entity(cluster_id)
        request_utils.validate_request_payload(
            converted_native_entity.spec.to_dict(),
            curr_entity.entity.spec.to_dict(),  # noqa: E501
            exclude_fields=[
                FlattenedClusterSpecKey1X.WORKERS_COUNT.value,
                FlattenedClusterSpecKey1X.NFS_COUNT.value,
                FlattenedClusterSpecKey1X.EXPOSE.value
            ])
        new_rde: common_models.DefEntity = svc.resize_cluster(
            cluster_id, converted_native_entity)  # noqa: E501

    # convert the resized rde back to input rde version
    return _convert_rde_to_1_0_format(new_rde.to_dict())
Example #15
0
def cluster_info(data: dict, op_ctx: ctx.OperationContext):
    """Request handler for cluster info operation.

    Required data: cluster_name
    Optional data and default values: org_name=None, ovdc_name=None

    (data validation handled in broker)

    :return: Dict
    """
    rde_in_use = server_utils.get_rde_version_in_use()

    # Redirect to generic handler if the backend supports RDE-2.0
    if semantic_version.Version(rde_in_use) >= semantic_version.Version(
            rde_constants.RDEVersion.RDE_2_0_0.value):  # noqa: E501
        rde_data: dict = cluster_handler.cluster_info(data=data, op_ctx=op_ctx)
        new_rde = common_models.DefEntity(**rde_data)
    else:
        svc = cluster_service_factory.ClusterServiceFactory(
            op_ctx).get_cluster_service(rde_in_use)  # noqa: E501
        cluster_id = data[RequestKey.CLUSTER_ID]
        new_rde = svc.get_cluster_info(cluster_id)

    return _convert_rde_to_1_0_format(new_rde.to_dict())