Skip to content

Commit

Permalink
CASMCMS-8799: Create v2-compatible session templates with v1 endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mharding-hpe committed Sep 11, 2023
1 parent 7071d46 commit dcca1ca
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- Removed non-v2 fields from v1 session template template
- Provide more useful example values in v1 and v2 session template templates
- Modify v1 session template create function to strip away v1-specific fields and create a v2-compatible
session template.
- Update API spec to reflect that no v1-format session template will exist inside BOS, because the
v1 session template creation endpoint will strip v1-specific fields and create a v2-format session template,
and even the v1 session template template endpoint will return a v2-compatible example template.
Expand Down
5 changes: 4 additions & 1 deletion api/openapi.yaml.in
Original file line number Diff line number Diff line change
Expand Up @@ -1961,7 +1961,10 @@ paths:
- $ref: '#/components/parameters/V1TenantHeaderParam'
post:
summary: Create Session Template
description: Create a new Session Template.
description: |
Create a new Session Template.

The created template will be modified if necessary to follow the BOS v2 session template format.
tags:
- sessiontemplate
x-openapi-router-controller: bos.server.controllers.v1.sessiontemplate
Expand Down
65 changes: 52 additions & 13 deletions src/bos/server/controllers/v1/sessiontemplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@

from bos.common.tenant_utils import no_v1_multi_tenancy_support
from bos.server import redis_db_utils as dbutils
from bos.server.models.v1_session_template import V1SessionTemplate as SessionTemplate # noqa: E501
from bos.server.models.v1_session_template import V1SessionTemplate # noqa: E501
from bos.server.models.v2_session_template import V2SessionTemplate # noqa: E501
from bos.server.utils import _canonize_xname
from bos.common.tenant_utils import get_tenant_aware_key
from ..v2.sessiontemplates import get_v2_sessiontemplate, get_v2_sessiontemplates, delete_v2_sessiontemplate
Expand Down Expand Up @@ -94,31 +95,69 @@ def create_v1_sessiontemplate(): # noqa: E501
sessiontemplate = None

try:
"""Convert the JSON request data into a SessionTemplate object.
"""Verify that we can convert the JSON request data into a
V1SessionTemplate object.
Any exceptions caught here would be generated from the model
(i.e. bos.server.models.session_template). Examples are
an exception for a session template missing the required name
field, or an exception for a session template name that does not
confirm to Kubernetes naming convention.
In this case return 400 with a description of the specific error.
"""
sessiontemplate = SessionTemplate.from_dict(connexion.request.get_json())
template_data = connexion.request.get_json()
V1SessionTemplate.from_dict(template_data)
except Exception as err:
return connexion.problem(
status=400, title="The session template could not be created.",
detail=str(err))

"""For now overwrite any existing template by name w/o warning.
Later this can be changed when we support patching operations.
This could also be changed to result in an HTTP 409 Conflict. TBD.
"""
LOGGER.debug("create_v1_sessiontemplate name: %s", sessiontemplate.name)
st_json = connexion.request.get_json()
# Strip out the v1-specific fields from the dictionary
v1_specific_st_fields = [ "cfs_url", "cfs_branch", "partition" ]
for v1_field_name in v1_specific_st_fields:
try:
del template_data[v1_field_name]
except KeyError:
pass

# Do the same for each boot set
# Oddly, boot_sets is not a required field, so only do this if it is present
if "boot_sets" in template_data:
v1_specific_bootset_fields = [ "network", "boot_ordinal", "shutdown_ordinal" ]
for bs in template_data["boot_sets"].values():
for v1_bs_field_name in v1_specific_bootset_fields:
try:
del bs[v1_bs_field_name]
except KeyError:
pass

# BOS v2 doesn't want the session template name inside the dictionary itself
# name is a required v1 field, though, so we can safely pop it here
session_template_id = template_data.pop("name")

# Now basically follow the same process as when creating a V2 session template (except in the end,
# if successful, we will return 201 status and the name of the template, to match the v1 API spec)
try:
"""Verify that we can convert the JSON request data into a
V2SessionTemplate object.
Any exceptions caught here would be generated from the model
(i.e. bos.server.models.session_template).
An example is an exception for a session template name that
does not conform to Kubernetes naming convention.
In this case return 400 with a description of the specific error.
"""
V2SessionTemplate.from_dict(template_data)
except Exception as err:
return connexion.problem(
status=400, title="The session template could not be created as a v2 template.",
detail=str(err))

template_data = sanitize_xnames(template_data)
template_data['name'] = session_template_id
# Tenants are not used in v1, but v1 and v2 share template storage
st_json["tenant"] = ""
template_key = get_tenant_aware_key(sessiontemplate.name, "")
DB.put(template_key, st_json)
return sessiontemplate.name, 201
template_data['tenant'] = ""
template_key = get_tenant_aware_key(session_template_id, "")
DB.put(template_key, template_data)
return session_template_id, 201


@no_v1_multi_tenancy_support
Expand Down

0 comments on commit dcca1ca

Please sign in to comment.