in src/sagemaker_core/tools/resources_codegen.py [0:0]
def _get_class_attributes(self, resource_name: str, class_methods: list) -> tuple:
"""Get the class attributes for a resource.
Args:
resource_name (str): The name of the resource.
class_methods (list): The class methods of the resource. Now it can only get the class
attributes if the resource has get or get_all method.
Returns:
tuple:
class_attributes: The class attributes and the formatted class attributes string.
class_attributes_string: The code string of the class attributes
attributes_and_documentation: A dict of doc strings of the class attributes
"""
if "get" in class_methods:
# Get the operation and shape for the 'get' method
get_operation = self.operations["Describe" + resource_name]
get_operation_shape = get_operation["output"]["shape"]
# Use 'get' operation input as the required class attributes.
# These are the mimumum identifing attributes for a resource object (ie, required for refresh())
get_operation_input_shape = get_operation["input"]["shape"]
required_attributes = self.shapes[get_operation_input_shape].get("required", [])
# Generate the class attributes based on the shape
class_attributes, class_attributes_string = (
self.shapes_extractor.generate_data_shape_members_and_string_body(
shape=get_operation_shape, required_override=tuple(required_attributes)
)
)
attributes_and_documentation = (
self.shapes_extractor.fetch_shape_members_and_doc_strings(get_operation_shape)
)
# Some resources are configured in the service.json inconsistently.
# These resources take in the main identifier in the create and get methods , but is not present in the describe response output
# Hence for consistent behaviour of functions such as refresh and delete, the identifiers are hardcoded
if resource_name == "ImageVersion":
class_attributes["image_name"] = "str"
class_attributes_string = "image_name: str\n" + class_attributes_string
if resource_name == "Workteam":
class_attributes["workteam_name"] = "str"
class_attributes_string = "workteam_name: str\n" + class_attributes_string
if resource_name == "Workforce":
class_attributes["workforce_name"] = "str"
class_attributes_string = "workforce_name: str\n" + class_attributes_string
if resource_name == "SubscribedWorkteam":
class_attributes["workteam_arn"] = "str"
class_attributes_string = "workteam_arn: str\n" + class_attributes_string
if resource_name == "HubContent":
class_attributes["hub_name"] = "Optional[str] = Unassigned()"
class_attributes_string = class_attributes_string.replace("hub_name: str", "")
class_attributes_string = (
class_attributes_string + "hub_name: Optional[str] = Unassigned()"
)
return class_attributes, class_attributes_string, attributes_and_documentation
elif "get_all" in class_methods:
# Get the operation and shape for the 'get_all' method
list_operation = self.operations["List" + resource_name + "s"]
list_operation_output_shape = list_operation["output"]["shape"]
list_operation_output_members = self.shapes[list_operation_output_shape]["members"]
# Use the object shape of 'get_all' operation output as the required class attributes.
filtered_list_operation_output_members = next(
{key: value}
for key, value in list_operation_output_members.items()
if key != "NextToken"
)
summaries_key = next(iter(filtered_list_operation_output_members))
summaries_shape_name = filtered_list_operation_output_members[summaries_key]["shape"]
summary_name = self.shapes[summaries_shape_name]["member"]["shape"]
required_attributes = self.shapes[summary_name].get("required", [])
# Generate the class attributes based on the shape
class_attributes, class_attributes_string = (
self.shapes_extractor.generate_data_shape_members_and_string_body(
shape=summary_name, required_override=tuple(required_attributes)
)
)
attributes_and_documentation = (
self.shapes_extractor.fetch_shape_members_and_doc_strings(summary_name)
)
return class_attributes, class_attributes_string, attributes_and_documentation
elif "create" in class_methods:
# Get the operation and shape for the 'create' method
create_operation = self.operations["Create" + resource_name]
create_operation_input_shape = create_operation["input"]["shape"]
create_operation_output_shape = create_operation["output"]["shape"]
# Generate the class attributes based on the input and output shape
class_attributes, class_attributes_string = self._get_resource_members_and_string_body(
resource_name, create_operation_input_shape, create_operation_output_shape
)
attributes_and_documentation = self._get_resouce_attributes_and_documentation(
create_operation_input_shape, create_operation_output_shape
)
return class_attributes, class_attributes_string, attributes_and_documentation
else:
return None