1+ import boto3
2+ import json
3+ import os
4+ import uuid
5+
6+ from config import *
7+ from response import *
8+ from error_messages import *
9+ from identity_check import *
10+
11+ from system_parameter_store import SystemParameterStore
12+ from lambda_base_class import LambdaBaseClass
13+ from models .prebuild_dataset_model import PrebuildDatasetModel
14+
15+
16+ class CreatePrebuildDatasetClass (LambdaBaseClass ):
17+
18+ def __init__ (self ) -> None :
19+ super ().__init__ ()
20+ self .client_events = boto3 .client ('events' )
21+ self .client_step_func = boto3 .client ('stepfunctions' )
22+ self .prebuild_dataset_model = PrebuildDatasetModel (os .environ ["T_CONST_PREBUILD_DATASET" ])
23+ self .sm_create_prj_prebuild = os .environ ["SM_CREATE_PRJ_PREBUILD" ]
24+
25+ @LambdaBaseClass .parse_body
26+ def parser (self , body ):
27+ self .logger .debug (f"body in main_parser: { body } " )
28+
29+ self .id_token = body [KEY_NAME_ID_TOKEN ]
30+ self .name_id_prebuild_dataset = body ["name_id_prebuild" ]
31+ self .number_random = body ["number_random" ]
32+
33+ def _check_input_value (self ):
34+ prebuild_dataset = self .prebuild_dataset_model .get_prebuild_dataset (self .name_id_prebuild_dataset )
35+ if prebuild_dataset is None :
36+ raise Exception (MESS_ERR_INVALID_PREBUILD_DATASET_NAME .format (self .name_id_prebuild_dataset ))
37+
38+ ### check number max
39+ if self .number_random <= 0 or self .number_random >= prebuild_dataset [PrebuildDatasetModel .FIELD_TOTAL_IMAGES ]:
40+ self .number_random = prebuild_dataset [PrebuildDatasetModel .FIELD_TOTAL_IMAGES ]
41+
42+ ### udpate the link to s3
43+ self .s3_key = prebuild_dataset [PrebuildDatasetModel .FIELD_S3_KEY ]
44+ self .visual_name = prebuild_dataset [PrebuildDatasetModel .FIELD_VISUAL_NAME ]
45+
46+ return
47+
48+ def handle (self , event , context ):
49+
50+ ### parse body
51+ self .parser (event )
52+
53+ ### check identity
54+ identity_id = self .get_identity (self .id_token )
55+
56+ ### create project on DB
57+ _uuid = uuid .uuid4 ().hex
58+ project_id = f'{ self .visual_name } _{ _uuid } '
59+ s3_prefix = f'{ os .environ ["BUCKET_NAME" ]} /{ identity_id } /{ project_id } '
60+ db_client = boto3 .client ('dynamodb' )
61+ db_resource = boto3 .resource ('dynamodb' )
62+ try :
63+ is_sample = True
64+ gen_status = "GENERATING"
65+ table_prj = db_resource .Table (os .environ ["T_PROJECT" ])
66+ table_prj .put_item (
67+ Item = {
68+ 'ID' : _uuid ,
69+ 'project_id' : project_id ,
70+ 'identity_id' : identity_id ,
71+ 'project_name' : project_name ,
72+ 's3_prefix' : s3_prefix ,
73+ 'project_info' : project_info ,
74+ # 'sub': sub,
75+ 'created_date' : convert_current_date_to_iso8601 (),
76+ 'is_sample' : is_sample ,
77+ 'gen_status' : gen_status
78+ },
79+ ConditionExpression = Attr ('project_name' ).not_exists () & Attr ('identity_id' ).not_exists ()
80+ )
81+
82+ except db_resource .meta .client .exceptions .ConditionalCheckFailedException as e :
83+ print ('Error condition: ' , e )
84+ err_mess = const .MES_DUPLICATE_PROJECT_NAME .format (project_name )
85+ return convert_response ({"error" : True ,
86+ "success" : False ,
87+ "message" : err_mess ,
88+ "data" : None })
89+ except Exception as e :
90+ print ('Error: ' , repr (e ))
91+ return convert_response ({"error" : True ,
92+ "success" : False ,
93+ "message" : repr (e ),
94+ "data" : None })
95+
96+ ### call async step function
97+ stepfunction_input = {
98+ "identity_id" : identity_id ,
99+ "id_token" : self .id_token ,
100+ }
101+ response = self .client_step_func .start_execution (
102+ stateMachineArn = self .sm_create_prj_prebuild ,
103+ input = json .dumps (stepfunction_input )
104+ )
105+
106+ return generate_response (
107+ message = "OK" ,
108+ status_code = HTTPStatus .OK ,
109+ data = {},
110+ )
111+
112+ @error_response
113+ def lambda_handler (event , context ):
114+
115+ return CreatePrebuildDatasetClass ().handle (event , context )
116+
117+
0 commit comments