in src/sagemaker_core/tools/resources_codegen.py [0:0]
def generate_create_method(self, resource_name: str, **kwargs) -> str:
"""
Auto-generate the CREATE method for a resource.
Args:
resource_name (str): The resource name.
Returns:
str: The formatted Create Method template.
"""
# Get the operation and shape for the 'create' method
operation_name = "Create" + resource_name
operation_metadata = self.operations[operation_name]
operation_input_shape_name = operation_metadata["input"]["shape"]
# Generate the arguments for the 'create' method
create_args = self._generate_create_method_args(operation_input_shape_name, resource_name)
operation_input_args = self._generate_operation_input_args(
operation_metadata, is_class_method=True
)
# Convert the resource name to snake case
resource_lower = convert_to_snake_case(resource_name)
# Convert the operation name to snake case
operation = convert_to_snake_case(operation_name)
docstring = self._generate_docstring(
title=f"Create a {resource_name} resource",
operation_name=operation_name,
resource_name=resource_name,
operation_input_shape_name=operation_input_shape_name,
include_session_region=True,
include_return_resource_docstring=True,
include_intelligent_defaults_errors=True,
)
if "Describe" + resource_name in self.operations:
# If the resource has Describe method, call Describe API and return its value
get_args = self._generate_get_args(resource_name, operation_input_shape_name)
# Format the method using the CREATE_METHOD_TEMPLATE
if kwargs["needs_defaults_decorator"]:
formatted_method = CREATE_METHOD_TEMPLATE.format(
docstring=docstring,
resource_name=resource_name,
create_args=create_args,
resource_lower=resource_lower,
# TODO: change service name based on the service - runtime, sagemaker, etc.
service_name="sagemaker",
operation_input_args=operation_input_args,
operation=operation,
get_args=get_args,
)
else:
formatted_method = CREATE_METHOD_TEMPLATE_WITHOUT_DEFAULTS.format(
docstring=docstring,
resource_name=resource_name,
create_args=create_args,
resource_lower=resource_lower,
# TODO: change service name based on the service - runtime, sagemaker, etc.
service_name="sagemaker",
operation_input_args=operation_input_args,
operation=operation,
get_args=get_args,
)
# Return the formatted method
return formatted_method
else:
# If the resource does not have Describe method, return a instance with
# the input and output of Create method
decorator = "@classmethod"
serialize_operation_input = SERIALIZE_INPUT_TEMPLATE.format(
operation_input_args=operation_input_args
)
initialize_client = INITIALIZE_CLIENT_TEMPLATE.format(service_name="sagemaker")
call_operation_api = CALL_OPERATION_API_TEMPLATE.format(
operation=convert_to_snake_case(operation_name)
)
operation_output_shape_name = operation_metadata["output"]["shape"]
deserialize_response = DESERIALIZE_INPUT_AND_RESPONSE_TO_CLS_TEMPLATE.format(
operation_output_shape=operation_output_shape_name
)
formatted_method = GENERIC_METHOD_TEMPLATE.format(
docstring=docstring,
decorator=decorator,
method_name="create",
method_args=add_indent("cls,\n", 4) + create_args,
return_type=f'Optional["{resource_name}"]',
serialize_operation_input=serialize_operation_input,
initialize_client=initialize_client,
call_operation_api=call_operation_api,
deserialize_response=deserialize_response,
)
# Return the formatted method
return formatted_method