in source/controlplaneapi/runtime/app.py [0:0]
def update_event(name, program):
"""
Update an event by name and program.
Body:
.. code-block:: python
{
"Description": string,
"ProgramId": string,
"SourceVideoUrl": string,
"SourceVideoAuth": object,
"SourceVideoMetadata": object,
"BootstrapTimeInMinutes": integer,
"Profile": string,
"ContentGroup": string,
"Start": timestamp,
"DurationMinutes": integer,
"Archive": boolean
}
Returns:
None
Raises:
400 - BadRequestError
404 - NotFoundError
409 - ConflictError
500 - ChaliceViewError
"""
try:
name = urllib.parse.unquote(name)
program = urllib.parse.unquote(program)
event = json.loads(app.current_request.raw_body.decode(), parse_float=Decimal)
validate(instance=event, schema=API_SCHEMA["update_event"])
print("Got a valid event schema")
event_table = ddb_resource.Table(EVENT_TABLE_NAME)
response = event_table.get_item(
Key={
"Name": name,
"Program": program
},
ConsistentRead=True
)
if "Item" not in response:
raise NotFoundError(f"Event '{name}' in Program '{program}' not found")
print(f"Updating the event '{name}' in program '{program}'")
if "ProgramId" in event and event["ProgramId"]:
program_id = event["ProgramId"]
existing_program_id = response["Item"]["ProgramId"] if "ProgramId" in response["Item"] else ""
if program_id != existing_program_id:
response = event_table.query(
IndexName=EVENT_PROGRAMID_INDEX,
KeyConditionExpression=Key("ProgramId").eq(program_id)
)
events = response["Items"]
while "LastEvaluatedKey" in response:
response = event_table.query(
ExclusiveStartKey=response["LastEvaluatedKey"],
IndexName=EVENT_PROGRAMID_INDEX,
KeyConditionExpression=Key("ProgramId").eq(program_id)
)
events.extend(response["Items"])
if len(events) > 0:
raise ConflictError(f"ProgramId '{program_id}' already exists in another event")
if "SourceVideoAuth" in event:
existing_auth_arn = response["Item"]["SourceVideoAuthSecretARN"] if "SourceVideoAuthSecretARN" in response[
"Item"] else ""
if existing_auth_arn:
sm_client.update_secret(
SecretId=existing_auth_arn,
SecretString=json.dumps(event["SourceVideoAuth"])
)
event["SourceVideoAuthSecretARN"] = existing_auth_arn
event.pop("SourceVideoAuth", None)
else:
response = sm_client.create_secret(
Name=f"/MRE/Event/{response['Item']['Id']}/SourceVideoAuth",
SecretString=json.dumps(event["SourceVideoAuth"]),
Tags=[
{
"Key": "Project",
"Value": "MRE"
},
{
"Key": "Program",
"Value": program
},
{
"Key": "Event",
"Value": name
}
]
)
event["SourceVideoAuthSecretARN"] = response["ARN"]
event.pop("SourceVideoAuth", None)
update_expression = "SET #Description = :Description, #ProgramId = :ProgramId, #Profile = :Profile, #ContentGroup = :ContentGroup, #Start = :Start, #DurationMinutes = :DurationMinutes, #Archive = :Archive"
expression_attribute_names = {
"#Description": "Description",
"#ProgramId": "ProgramId",
"#Profile": "Profile",
"#ContentGroup": "ContentGroup",
"#Start": "Start",
"#DurationMinutes": "DurationMinutes",
"#Archive": "Archive"
}
expression_attribute_values = {
":Description": event["Description"] if "Description" in event else (
response["Item"]["Description"] if "Description" in response["Item"] else ""),
":ProgramId": event["ProgramId"] if "ProgramId" in event else (
response["Item"]["ProgramId"] if "ProgramId" in response["Item"] else ""),
":Profile": event["Profile"] if "Profile" in event else response["Item"]["Profile"],
":ContentGroup": event["ContentGroup"] if "ContentGroup" in event else response["Item"]["ContentGroup"],
":Start": event["Start"] if "Start" in event else response["Item"]["Start"],
":DurationMinutes": event["DurationMinutes"] if "DurationMinutes" in event else response["Item"][
"DurationMinutes"],
":Archive": event["Archive"] if "Archive" in event else response["Item"]["Archive"]
}
if "Channel" not in response["Item"]:
update_expression += ", #SourceVideoUrl = :SourceVideoUrl, #SourceVideoAuthSecretARN = :SourceVideoAuthSecretARN, #SourceVideoMetadata = :SourceVideoMetadata, #BootstrapTimeInMinutes = :BootstrapTimeInMinutes"
expression_attribute_names["#SourceVideoUrl"] = "SourceVideoUrl"
expression_attribute_names["#SourceVideoAuthSecretARN"] = "SourceVideoAuthSecretARN"
expression_attribute_names["#SourceVideoMetadata"] = "SourceVideoMetadata"
expression_attribute_names["#BootstrapTimeInMinutes"] = "BootstrapTimeInMinutes"
expression_attribute_values[":SourceVideoUrl"] = event["SourceVideoUrl"] if "SourceVideoUrl" in event else \
response["Item"]["SourceVideoUrl"]
expression_attribute_values[":SourceVideoAuthSecretARN"] = event[
"SourceVideoAuthSecretARN"] if "SourceVideoAuthSecretARN" in event else (
response["Item"]["SourceVideoAuthSecretARN"] if "SourceVideoAuthSecretARN" in response["Item"] else "")
expression_attribute_values[":SourceVideoMetadata"] = event[
"SourceVideoMetadata"] if "SourceVideoMetadata" in event else (
response["Item"]["SourceVideoMetadata"] if "SourceVideoMetadata" in response["Item"] else {})
expression_attribute_values[":BootstrapTimeInMinutes"] = event[
"BootstrapTimeInMinutes"] if "BootstrapTimeInMinutes" in event else response["Item"][
"BootstrapTimeInMinutes"]
event_table.update_item(
Key={
"Name": name,
"Program": program
},
UpdateExpression=update_expression,
ExpressionAttributeNames=expression_attribute_names,
ExpressionAttributeValues=expression_attribute_values
)
except ValidationError as e:
print(f"Got jsonschema ValidationError: {str(e)}")
raise BadRequestError(e.message)
except NotFoundError as e:
print(f"Got chalice NotFoundError: {str(e)}")
raise
except Exception as e:
print(f"Unable to update the event '{name}' in program '{program}': {str(e)}")
raise ChaliceViewError(f"Unable to update the event '{name}' in program '{program}': {str(e)}")
else:
print(f"Successfully updated the event: {json.dumps(event, cls=DecimalEncoder)}")
return {}