Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions runpod/api/ctl_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ def create_template(
env: dict = None,
is_serverless: bool = False,
registry_auth_id: str = None,
readme: str = None,
):
"""
Create a template
Expand All @@ -298,6 +299,7 @@ def create_template(
inject EXAMPLE_VAR and EXAMPLE_VAR2 into the pod with the mentioned values
:param is_serverless: is the template serverless?
:param registry_auth_id: the id of the registry auth
:param readme: the README content to associate with the template

:example:

Expand All @@ -315,6 +317,7 @@ def create_template(
env=env,
is_serverless=is_serverless,
registry_auth_id=registry_auth_id,
readme=readme,
)
)

Expand Down
14 changes: 13 additions & 1 deletion runpod/api/mutations/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def generate_pod_template(
env: dict = None,
is_serverless: bool = False,
registry_auth_id: str = None,
readme: str = None,
):
"""Generate a string for a GraphQL mutation to create a new pod template."""
input_fields = [f'name: "{name}"', f'imageName: "{image_name}"']
Expand Down Expand Up @@ -59,7 +60,18 @@ def generate_pod_template(
else:
input_fields.append('containerRegistryAuthId : ""')

input_fields.extend(("startSsh: true", "isPublic: false", 'readme: ""'))
if readme is not None:
readme = (
readme.replace("\\", "\\\\")
.replace('"', '\\"')
.replace("\n", "\\n")
.replace("\r", "\\r")
)
input_fields.append(f'readme: "{readme}"')
else:
input_fields.append('readme: ""')

input_fields.extend(("startSsh: true", "isPublic: false"))
# Format the input fields into a string
input_fields_string = ", ".join(input_fields)

Expand Down
11 changes: 11 additions & 0 deletions tests/test_api/test_mutations_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def test_basic_required_fields(self):
self.assertIn('ports: ""', result) # Defaults
self.assertIn("env: []", result) # Defaults
self.assertIn("isServerless: false", result) # Defaults
self.assertIn('readme: ""', result) # Defaults

def test_optional_fields(self):
"""Test the optional fields are present in the generated template"""
Expand All @@ -43,3 +44,13 @@ def test_optional_fields(self):
)
self.assertIn("isServerless: true", result)
self.assertIn('containerRegistryAuthId : "test_auth"', result)

def test_readme_is_escaped_and_included(self):
"""Test that README content is included as a GraphQL string."""
result = generate_pod_template(
"test_name",
"test_image_name",
readme='line one "quoted"\nline two',
)

self.assertIn('readme: "line one \\"quoted\\"\\nline two"', result)