1from moto.core import ACCOUNT_ID 2from moto.core.responses import BaseResponse 3from .models import elastictranscoder_backends 4import json 5import re 6 7 8class ElasticTranscoderResponse(BaseResponse): 9 SERVICE_NAME = "elastictranscoder" 10 11 @property 12 def elastictranscoder_backend(self): 13 return elastictranscoder_backends[self.region] 14 15 def pipelines(self, request, full_url, headers): 16 self.setup_class(request, full_url, headers) 17 if request.method == "POST": 18 return self.create_pipeline() 19 elif request.method == "GET": 20 return self.list_pipelines() 21 else: 22 return self.update_pipeline() 23 24 def individual_pipeline(self, request, full_url, headers): 25 self.setup_class(request, full_url, headers) 26 if request.method == "GET": 27 return self.read_pipeline() 28 elif request.method == "DELETE": 29 return self.delete_pipeline() 30 else: 31 return self.update_pipeline() 32 33 def create_pipeline(self): 34 name = self._get_param("Name") 35 input_bucket = self._get_param("InputBucket") 36 output_bucket = self._get_param("OutputBucket") 37 role = self._get_param("Role") 38 aws_kms_key_arn = self._get_param("AwsKmsKeyArn") 39 notifications = self._get_param("Notifications") 40 content_config = self._get_param("ContentConfig") 41 thumbnail_config = self._get_param("ThumbnailConfig") 42 if not role: 43 return self.err("Role cannot be blank") 44 if not re.match("^arn:aws:iam::[0-9]+:role/.+", role): 45 return self.err("Role ARN is invalid: {}".format(role)) 46 if not output_bucket and not content_config: 47 return self.err( 48 "[OutputBucket and ContentConfig:Bucket are not allowed to both be null.]" 49 ) 50 if output_bucket and content_config: 51 return self.err("[OutputBucket and ContentConfig are mutually exclusive.]") 52 if content_config and not thumbnail_config: 53 return self.err( 54 "[ThumbnailConfig:Bucket is not allowed to be null if ContentConfig is specified.]" 55 ) 56 pipeline, warnings = self.elastictranscoder_backend.create_pipeline( 57 name=name, 58 input_bucket=input_bucket, 59 output_bucket=output_bucket, 60 role=role, 61 aws_kms_key_arn=aws_kms_key_arn, 62 notifications=notifications, 63 content_config=content_config, 64 thumbnail_config=thumbnail_config, 65 ) 66 return ( 67 201, 68 {"status": 201}, 69 json.dumps({"Pipeline": pipeline.to_dict(), "Warnings": warnings}), 70 ) 71 72 def list_pipelines(self): 73 return ( 74 200, 75 {}, 76 json.dumps({"Pipelines": self.elastictranscoder_backend.list_pipelines()}), 77 ) 78 79 def validate_pipeline_id(self, pipeline_id): 80 r = "^\\d{13}-\\w{6}$" 81 if not re.match(r, pipeline_id): 82 msg = "1 validation error detected: Value '{}' at 'id' failed to satisfy constraint: Member must satisfy regular expression pattern: {}".format( 83 pipeline_id, r 84 ) 85 return self.err(msg) 86 try: 87 self.elastictranscoder_backend.read_pipeline(pipeline_id) 88 return None 89 except KeyError: 90 msg = "The specified pipeline was not found: account={}, pipelineId={}.".format( 91 ACCOUNT_ID, pipeline_id 92 ) 93 return ( 94 404, 95 {"status": 404, "x-amzn-ErrorType": "ResourceNotFoundException"}, 96 json.dumps({"message": msg}), 97 ) 98 99 def read_pipeline(self): 100 _id = self.path.rsplit("/", 1)[-1] 101 err = self.validate_pipeline_id(_id) 102 if err: 103 return err 104 pipeline = self.elastictranscoder_backend.read_pipeline(_id) 105 return 200, {}, json.dumps({"Pipeline": pipeline.to_dict()}) 106 107 def update_pipeline(self): 108 _id = self.path.rsplit("/", 1)[-1] 109 name = self._get_param("Name") 110 input_bucket = self._get_param("InputBucket") 111 role = self._get_param("Role") 112 aws_kms_key_arn = self._get_param("AwsKmsKeyArn") 113 notifications = self._get_param("Notifications") 114 content_config = self._get_param("ContentConfig") 115 thumbnail_config = self._get_param("ThumbnailConfig") 116 err = self.validate_pipeline_id(_id) 117 if err: 118 return err 119 pipeline, warnings = self.elastictranscoder_backend.update_pipeline( 120 id=_id, 121 name=name, 122 input_bucket=input_bucket, 123 role=role, 124 aws_kms_key_arn=aws_kms_key_arn, 125 notifications=notifications, 126 content_config=content_config, 127 thumbnail_config=thumbnail_config, 128 ) 129 130 return ( 131 200, 132 {}, 133 json.dumps({"Pipeline": pipeline.to_dict(), "Warnings": warnings}), 134 ) 135 136 def delete_pipeline(self): 137 _id = self.path.rsplit("/", 1)[-1] 138 self.elastictranscoder_backend.delete_pipeline(pipeline_id=_id) 139 return 200, {}, "{}" 140 141 def err(self, msg): 142 return ( 143 400, 144 {"status": 400, "x-amzn-ErrorType": "ValidationException"}, 145 json.dumps({"message": msg}), 146 ) 147