Esempio n. 1
0
def create_cluster(behavior_ctx: BehaviorRequestContext):
    entity_id: str = behavior_ctx.entity_id
    input_entity: dict = behavior_ctx.entity
    cloudapi_client: CloudApiClient = behavior_ctx.op_ctx.cloudapi_client

    payload_version: str = 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
    input_rde_version = rde_constants.MAP_INPUT_PAYLOAD_VERSION_TO_RDE_VERSION[
        payload_version]  # noqa: E501
    rde_validator_factory.get_validator(
        rde_version=input_rde_version). \
        validate(cloudapi_client=cloudapi_client, entity=input_entity)

    # Convert the input entity to runtime rde format.
    # Based on the runtime rde, call the appropriate backend method.
    converted_input_entity = rde_utils.convert_input_rde_to_runtime_rde_format(
        input_entity)  # noqa: E501
    svc = cluster_service_factory.ClusterServiceFactory(
        behavior_ctx).get_cluster_service()  # noqa: E501
    return svc.create_cluster(
        entity_id=entity_id,
        input_native_entity=converted_input_entity)  # noqa: E501
Esempio n. 2
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())
Esempio n. 3
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
    """
    input_entity: dict = data[RequestKey.INPUT_SPEC]
    # Convert the input entity to runtime rde format.
    # Based on the runtime rde, call the appropriate backend method.
    converted_native_entity: AbstractNativeEntity = rde_utils.convert_input_rde_to_runtime_rde_format(
        input_entity)  # noqa: E501
    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
        ])
    new_rde: common_models.DefEntity = svc.resize_cluster(
        cluster_id, converted_native_entity)  # noqa: E501
    # convert the resized rde back to input rde version
    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
    return new_rde.to_dict()
Esempio n. 4
0
def update_cluster(behavior_ctx: RequestContext):
    entity_id: str = behavior_ctx.entity_id
    input_entity: dict = behavior_ctx.entity
    cloudapi_client: CloudApiClient = behavior_ctx.op_ctx.cloudapi_client

    payload_version: str = input_entity.get(
        rde_constants.PayloadKey.PAYLOAD_VERSION)  # noqa: E501
    rde_utils.raise_error_if_unsupported_payload_version(
        payload_version=payload_version)

    # Validate the Input payload based on the (Operation, payload_version).
    # Get the validator based on the payload_version
    kind = input_entity['kind']
    is_tkgm_cluster = (kind == ClusterEntityKind.TKG_M.value)
    sysadmin_client = behavior_ctx.op_ctx.sysadmin_client
    input_rde_version = rde_constants.MAP_INPUT_PAYLOAD_VERSION_TO_RDE_VERSION[
        payload_version]  # noqa: E501
    rde_validator_factory.get_validator(
        rde_version=input_rde_version). \
        validate(cloudapi_client=cloudapi_client,
                 sysadmin_client=sysadmin_client,
                 operation=BehaviorOperation.UPDATE_CLUSTER,
                 entity_id=entity_id,
                 entity=input_entity,
                 is_tkgm_cluster=is_tkgm_cluster)

    # Convert the input entity to runtime rde format.
    # Based on the runtime rde, call the appropriate backend method.
    converted_input_entity = rde_utils.convert_input_rde_to_runtime_rde_format(
        input_entity)  # noqa: E501
    svc = cluster_service_factory.ClusterServiceFactory(
        behavior_ctx).get_cluster_service()  # noqa: E501
    return svc.update_cluster(
        entity_id, input_native_entity=converted_input_entity)  # noqa: E501
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,
                                       entity=input_entity)

    # Convert the input entity to runtime rde format.
    # Based on the runtime rde, call the appropriate backend method.
    converted_native_entity = rde_utils.convert_input_rde_to_runtime_rde_format(input_entity)  # noqa: E501
    svc = cluster_service_factory.ClusterServiceFactory(op_ctx).get_cluster_service()  # noqa: E501
    # TODO: Response RDE must be compatible with the request RDE.
    #  Conversions may be needed especially if there is a major version
    #  difference in the input RDE and runtime RDE.
    return svc.create_cluster(converted_native_entity).to_dict()
Esempio n. 6
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()
Esempio n. 7
0
def cluster_update(data: dict, op_ctx: ctx.OperationContext):
    """Request handler for cluster resize operation.

    :return: Defined entity of the native cluster
    :rtype: container_service_extension.def_.models.DefEntity
    """
    cluster_id = data[RequestKey.CLUSTER_ID]
    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
    kind = input_entity['kind']
    is_tkgm_cluster = (kind == ClusterEntityKind.TKG_M.value)
    sysadmin_client = op_ctx.sysadmin_client
    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=sysadmin_client, entity_id=cluster_id,
                 entity=input_entity,
                 operation=BehaviorOperation.UPDATE_CLUSTER,
                 is_tkgm_cluster=is_tkgm_cluster)

    # Convert the input entity to runtime rde format.
    # Based on the runtime rde, call the appropriate backend method.
    def_entity_service = entity_service.DefEntityService(
        op_ctx.cloudapi_client)  # noqa: E501
    converted_native_entity: AbstractNativeEntity = rde_utils.convert_input_rde_to_runtime_rde_format(
        input_entity)  # noqa: E501

    changes = {'entity.spec': converted_native_entity.spec}
    updated_def_entity, task_href = def_entity_service.update_entity(
        entity_id=cluster_id,
        invoke_hooks=True,
        is_request_async=True,
        changes=changes)
    updated_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:
            updated_def_entity,
            telemetry_constants.PayloadKey.SOURCE_DESCRIPTION:
            thread_local_data.get_thread_local_data(
                ThreadLocalData.USER_AGENT)  # noqa: E501
        })
    # TODO: Response RDE must be compatible with the request RDE.
    #  Conversions may be needed especially if there is a major version
    #  difference in the input RDE and runtime RDE.
    return updated_def_entity.to_dict()
Esempio n. 8
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())
Esempio n. 9
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]
    # Convert the input entity to runtime rde format.
    # Based on the runtime rde, call the appropriate backend method.
    converted_native_entity: AbstractNativeEntity = rde_utils.convert_input_rde_to_runtime_rde_format(
        input_entity)  # noqa: E501
    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
    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
    return new_rde.to_dict()
def cluster_update(data: dict, op_ctx: ctx.OperationContext):
    """Request handler for cluster resize operation.

    :return: Defined entity of the native cluster
    :rtype: container_service_extension.def_.models.DefEntity
    """
    cluster_id = data[RequestKey.CLUSTER_ID]
    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, entity_id=cluster_id,
                 entity=input_entity,
                 operation=BehaviorOperation.UPDATE_CLUSTER)

    # Convert the input entity to runtime rde format.
    # Based on the runtime rde, call the appropriate backend method.
    def_entity_service = entity_service.DefEntityService(
        op_ctx.cloudapi_client)  # noqa: E501
    converted_native_entity: AbstractNativeEntity = rde_utils.convert_input_rde_to_runtime_rde_format(
        input_entity)  # noqa: E501
    cluster_def_entity: common_models.DefEntity = def_entity_service.get_entity(
        cluster_id)  # noqa: E501
    cluster_def_entity.entity.spec = converted_native_entity.spec
    updated_def_entity, task_href = def_entity_service.update_entity(
        entity_id=cluster_id,
        entity=cluster_def_entity,
        invoke_hooks=True,
        is_request_async=True)
    updated_def_entity.entity.status.task_href = task_href
    # TODO: Response RDE must be compatible with the request RDE.
    #  Conversions may be needed especially if there is a major version
    #  difference in the input RDE and runtime RDE.
    return updated_def_entity.to_dict()