def validate_cluster_update_request_and_check_cluster_upgrade( input_spec: rde_2_0_0.ClusterSpec, # noqa: E501 reference_spec: rde_2_0_0.ClusterSpec) -> bool: # noqa: E501 """Validate the desired spec with curr spec and check if upgrade operation. :param dict input_spec: input spec :param dict reference_spec: reference spec to validate the desired spec :return: true if cluster operation is upgrade and false if operation is resize :rtype: bool :raises: BadRequestError for invalid payload. """ exclude_fields = [] if reference_spec.topology.workers.count == 0: # Exclude worker nodes' sizing class and storage profile from # validation if worker count is 0 exclude_fields.append( FlattenedClusterSpecKey2X.WORKERS_SIZING_CLASS.value) # noqa: E501 exclude_fields.append(FlattenedClusterSpecKey2X. WORKERS_STORAGE_PROFILE.value) # noqa: E501 if reference_spec.topology.nfs.count == 0: # Exclude nfs nodes' sizing class and storage profile from validation # if nfs count is 0 exclude_fields.append(FlattenedClusterSpecKey2X.NFS_SIZING_CLASS.value) exclude_fields.append( FlattenedClusterSpecKey2X.NFS_STORAGE_PROFILE.value) # noqa: E501 input_spec_dict = input_spec.to_dict() reference_spec_dict = reference_spec.to_dict() diff_fields = \ rde_utils.find_diff_fields(input_spec_dict, reference_spec_dict, exclude_fields=exclude_fields) # noqa: E501 # Raise exception if empty diff if not diff_fields: raise BadRequestError( "No change in cluster specification") # noqa: E501 # Raise exception if fields which cannot be changed are updated keys_with_invalid_value = \ [k for k in diff_fields if k not in VALID_UPDATE_FIELDS_2X] if len(keys_with_invalid_value) > 0: err_msg = f"Invalid input values found in {sorted(keys_with_invalid_value)}" # noqa: E501 raise BadRequestError(err_msg) is_resize_operation = False if FlattenedClusterSpecKey2X.WORKERS_COUNT.value in diff_fields or \ FlattenedClusterSpecKey2X.NFS_COUNT.value in diff_fields: is_resize_operation = True is_upgrade_operation = False if FlattenedClusterSpecKey2X.TEMPLATE_NAME.value in diff_fields or \ FlattenedClusterSpecKey2X.TEMPLATE_REVISION.value in diff_fields: is_upgrade_operation = True # Raise exception if resize and upgrade are performed at the same time if is_resize_operation and is_upgrade_operation: err_msg = "Cannot resize and upgrade the cluster at the same time" raise BadRequestError(err_msg) return is_upgrade_operation
def validate_request_payload(input_spec: dict, reference_spec: dict, exclude_fields=None): """Validate the desired spec with the current spec. :param dict input_spec: input spec :param dict reference_spec: reference spec to validate the desired spec :param list exclude_fields: exclude the list of given flattened-keys from validation # noqa: E501 :return: true on successful validation :rtype: bool :raises: BadRequestError on encountering invalid payload value """ keys_with_invalid_value = rde_utils.find_diff_fields( input_spec, reference_spec, exclude_fields=exclude_fields) if len(keys_with_invalid_value) > 0: error_msg = f"Invalid input values found in {sorted(keys_with_invalid_value)}" # noqa: E501 raise BadRequestError(error_msg)
def validate_cluster_update_request_and_check_cluster_upgrade( input_spec: dict, reference_spec: dict) -> bool: # noqa: E501 """Validate the desired spec with curr spec and check if upgrade operation. :param dict input_spec: input spec :param dict reference_spec: reference spec to validate the desired spec :return: true if cluster operation is upgrade and false if operation is resize :rtype: bool :raises: BadRequestError for invalid payload. """ diff_fields = \ rde_utils.find_diff_fields(input_spec, reference_spec, exclude_fields=[]) # noqa: E501 # Raise exception if empty diff if not diff_fields: raise BadRequestError( "No change in cluster specification") # noqa: E501 # Raise exception if fields which cannot be changed are updated keys_with_invalid_value = [ k for k in diff_fields if k not in VALID_UPDATE_FIELDS ] # noqa: E501 if len(keys_with_invalid_value) > 0: err_msg = f"Invalid input values found in {sorted(keys_with_invalid_value)}" # noqa: E501 raise BadRequestError(err_msg) is_resize_operation = False if FlattenedClusterSpecKey.WORKERS_COUNT.value in diff_fields or \ FlattenedClusterSpecKey.NFS_COUNT.value in diff_fields: is_resize_operation = True is_upgrade_operation = False if FlattenedClusterSpecKey.TEMPLATE_NAME.value in diff_fields or \ FlattenedClusterSpecKey.TEMPLATE_REVISION.value in diff_fields: is_upgrade_operation = True # Raise exception if resize and upgrade are performed at the same time if is_resize_operation and is_upgrade_operation: err_msg = "Cannot resize and upgrade the cluster at the same time" raise BadRequestError(err_msg) return is_upgrade_operation
def validate_request_payload(input_spec: dict, reference_spec: dict, exclude_fields=None): """Validate the desired spec with the current spec. :param dict input_spec: input spec :param dict reference_spec: reference spec to validate the desired spec :param list exclude_fields: exclude the list of given flattened-keys from validation # noqa: E501 :raises: BadRequestError on encountering invalid payload value """ keys_with_invalid_value = rde_utils.find_diff_fields( input_spec, reference_spec, exclude_fields=exclude_fields) if len(keys_with_invalid_value) > 0: err_msg = "Change detected in immutable field(s) [" for k in sorted(keys_with_invalid_value): err_msg += \ f"`{k}` found : {keys_with_invalid_value[k]['actual']} " \ f"expected : {keys_with_invalid_value[k]['expected']}, " err_msg += "]." raise BadRequestError(err_msg)
def validate_cluster_update_request_and_check_cluster_upgrade( input_spec: rde_2_1_0.ClusterSpec, reference_spec: rde_2_1_0.ClusterSpec, is_tkgm_cluster: bool, ) -> bool: """ Validate the desired spec with curr spec and check if upgrade operation. :param dict input_spec: input spec :param dict reference_spec: reference spec to validate the desired spec :param bool is_tkgm_cluster: True implies that this is a TKGm cluster :return: true if cluster operation is upgrade and false if operation is resize :rtype: bool :raises: BadRequestError for invalid payload. """ # Since these fields are only in the spec section, and since we create # the reference_spec section from a status section, we will always have # null for the POD and SVC CIDRs. Hence we cannot validate it until there # is a better way to get the spec details. exclude_fields = [ FlattenedClusterSpecKey2X.POD_CIDR.value, FlattenedClusterSpecKey2X.SVC_CIDR.value, ] if reference_spec.topology.workers.count == 0: # Exclude worker nodes' sizing class and storage profile from # validation if worker count is 0 exclude_fields.append( FlattenedClusterSpecKey2X.WORKERS_SIZING_CLASS.value) # noqa: E501 exclude_fields.append(FlattenedClusterSpecKey2X. WORKERS_STORAGE_PROFILE.value) # noqa: E501 exclude_fields.append( FlattenedClusterSpecKey2X.WORKERS_CPU_COUNT.value) # noqa: E501 exclude_fields.append( FlattenedClusterSpecKey2X.WORKERS_MEMORY_MB.value) # noqa: E501 if reference_spec.topology.nfs.count == 0: # Exclude nfs nodes' sizing class and storage profile from validation # if nfs count is 0 exclude_fields.append(FlattenedClusterSpecKey2X.NFS_SIZING_CLASS.value) exclude_fields.append( FlattenedClusterSpecKey2X.NFS_STORAGE_PROFILE.value) # noqa: E501 # Allow empty template revisions (== 0) if the value is the default # for TKGm clusters only if is_tkgm_cluster: if (reference_spec.distribution.template_revision == 1 and input_spec.distribution.template_revision == 0 # default value ): exclude_fields.append( FlattenedClusterSpecKey2X.TEMPLATE_REVISION.value) input_spec_dict = input_spec.to_dict() reference_spec_dict = reference_spec.to_dict() diff_fields = rde_utils.find_diff_fields(input_spec_dict, reference_spec_dict, exclude_fields=exclude_fields) is_upgrade_operation = False if not diff_fields: return is_upgrade_operation keys_with_invalid_value = {} for k, v in diff_fields.items(): if k not in VALID_UPDATE_FIELDS_2X: keys_with_invalid_value[k] = v # Raise exception if fields which cannot be changed are updated if len(keys_with_invalid_value) > 0: err_msg = "Change detected in immutable field(s) [" for k in sorted(keys_with_invalid_value): err_msg += \ f"{k} found : {keys_with_invalid_value[k]['actual']} " \ f"expected : {keys_with_invalid_value[k]['expected']}, " err_msg += "]." raise BadRequestError(err_msg) is_resize_operation = False if FlattenedClusterSpecKey2X.WORKERS_COUNT.value in diff_fields or \ FlattenedClusterSpecKey2X.NFS_COUNT.value in diff_fields: is_resize_operation = True if FlattenedClusterSpecKey2X.TEMPLATE_NAME.value in diff_fields or \ FlattenedClusterSpecKey2X.TEMPLATE_REVISION.value in diff_fields: is_upgrade_operation = True # Raise exception if resize and upgrade are performed at the same time if is_resize_operation and is_upgrade_operation: err_msg = "Cannot resize and upgrade the cluster at the same time" raise BadRequestError(err_msg) return is_upgrade_operation