Best Python code snippet using localstack_python
provider.py
Source:provider.py
...146 )147 result.pop("EngineVersion", None)148 result.pop("ClusterConfig", None)149 return result150def _compatible_version_list_from_opensearch(151 compatible_version_list: Optional[CompatibleVersionsList],152) -> Optional[CompatibleElasticsearchVersionsList]:153 if compatible_version_list is not None:154 return [155 CompatibleVersionsMap(156 SourceVersion=_version_from_opensearch(version_map["SourceVersion"]),157 TargetVersions=[158 _version_from_opensearch(target_version)159 for target_version in version_map["TargetVersions"]160 ],161 )162 for version_map in compatible_version_list163 ]164@contextmanager165def exception_mapper():166 """Maps an exception thrown by the OpenSearch client to an exception thrown by the ElasticSearch API."""167 try:168 yield169 except ClientError as err:170 exception_types = {171 "AccessDeniedException": AccessDeniedException,172 "BaseException": EsBaseException,173 "ConflictException": ConflictException,174 "DisabledOperationException": DisabledOperationException,175 "InternalException": InternalException,176 "InvalidPaginationTokenException": InvalidPaginationTokenException,177 "InvalidTypeException": InvalidTypeException,178 "LimitExceededException": LimitExceededException,179 "ResourceAlreadyExistsException": ResourceAlreadyExistsException,180 "ResourceNotFoundException": ResourceNotFoundException,181 "ValidationException": ValidationException,182 }183 mapped_exception_type = exception_types.get(err.response["Error"]["Code"], EsBaseException)184 raise mapped_exception_type(err.response["Error"]["Message"])185class EsProvider(EsApi):186 def create_elasticsearch_domain(187 self,188 context: RequestContext,189 domain_name: DomainName,190 elasticsearch_version: ElasticsearchVersionString = None,191 elasticsearch_cluster_config: ElasticsearchClusterConfig = None,192 ebs_options: EBSOptions = None,193 access_policies: PolicyDocument = None,194 snapshot_options: SnapshotOptions = None,195 vpc_options: VPCOptions = None,196 cognito_options: CognitoOptions = None,197 encryption_at_rest_options: EncryptionAtRestOptions = None,198 node_to_node_encryption_options: NodeToNodeEncryptionOptions = None,199 advanced_options: AdvancedOptions = None,200 log_publishing_options: LogPublishingOptions = None,201 domain_endpoint_options: DomainEndpointOptions = None,202 advanced_security_options: AdvancedSecurityOptionsInput = None,203 auto_tune_options: AutoTuneOptionsInput = None,204 tag_list: TagList = None,205 ) -> CreateElasticsearchDomainResponse:206 opensearch_client = aws_stack.connect_to_service("opensearch", region_name=context.region)207 # If no version is given, we set our default elasticsearch version208 engine_version = (209 _version_to_opensearch(elasticsearch_version)210 if elasticsearch_version211 else constants.ELASTICSEARCH_DEFAULT_VERSION212 )213 kwargs = {214 "DomainName": domain_name,215 "EngineVersion": engine_version,216 "ClusterConfig": _clusterconfig_to_opensearch(elasticsearch_cluster_config),217 "EBSOptions": ebs_options,218 "AccessPolicies": access_policies,219 "SnapshotOptions": snapshot_options,220 "VPCOptions": vpc_options,221 "CognitoOptions": cognito_options,222 "EncryptionAtRestOptions": encryption_at_rest_options,223 "NodeToNodeEncryptionOptions": node_to_node_encryption_options,224 "AdvancedOptions": advanced_options,225 "LogPublishingOptions": log_publishing_options,226 "DomainEndpointOptions": domain_endpoint_options,227 "AdvancedSecurityOptions": advanced_security_options,228 "AutoTuneOptions": auto_tune_options,229 "TagList": tag_list,230 }231 # Filter the kwargs to not set None values at all (boto doesn't like that)232 kwargs = {key: value for key, value in kwargs.items() if value is not None}233 with exception_mapper():234 domain_status = opensearch_client.create_domain(**kwargs)["DomainStatus"]235 # record event236 event_publisher.fire_event(237 event_publisher.EVENT_ES_CREATE_DOMAIN,238 payload={"n": event_publisher.get_hash(domain_name)},239 )240 status = _domainstatus_from_opensearch(domain_status)241 return CreateElasticsearchDomainResponse(DomainStatus=status)242 def delete_elasticsearch_domain(243 self, context: RequestContext, domain_name: DomainName244 ) -> DeleteElasticsearchDomainResponse:245 opensearch_client = aws_stack.connect_to_service("opensearch", region_name=context.region)246 with exception_mapper():247 domain_status = opensearch_client.delete_domain(248 DomainName=domain_name,249 )["DomainStatus"]250 # record event251 event_publisher.fire_event(252 event_publisher.EVENT_ES_DELETE_DOMAIN,253 payload={"n": event_publisher.get_hash(domain_name)},254 )255 status = _domainstatus_from_opensearch(domain_status)256 return DeleteElasticsearchDomainResponse(DomainStatus=status)257 def describe_elasticsearch_domain(258 self, context: RequestContext, domain_name: DomainName259 ) -> DescribeElasticsearchDomainResponse:260 opensearch_client = aws_stack.connect_to_service("opensearch", region_name=context.region)261 with exception_mapper():262 opensearch_status = opensearch_client.describe_domain(263 DomainName=domain_name,264 )["DomainStatus"]265 status = _domainstatus_from_opensearch(opensearch_status)266 return DescribeElasticsearchDomainResponse(DomainStatus=status)267 def describe_elasticsearch_domains(268 self, context: RequestContext, domain_names: DomainNameList269 ) -> DescribeElasticsearchDomainsResponse:270 opensearch_client = aws_stack.connect_to_service("opensearch", region_name=context.region)271 with exception_mapper():272 opensearch_status_list = opensearch_client.describe_domains(273 DomainNames=domain_names,274 )["DomainStatusList"]275 status_list = [_domainstatus_from_opensearch(s) for s in opensearch_status_list]276 return DescribeElasticsearchDomainsResponse(DomainStatusList=status_list)277 def list_domain_names(278 self, context: RequestContext, engine_type: EngineType = None279 ) -> ListDomainNamesResponse:280 opensearch_client = aws_stack.connect_to_service("opensearch", region_name=context.region)281 # Only hand the EngineType param to boto if it's set282 kwargs = {}283 if engine_type:284 kwargs["EngineType"] = engine_type285 with exception_mapper():286 domain_names = opensearch_client.list_domain_names(**kwargs)["DomainNames"]287 return ListDomainNamesResponse(DomainNames=cast(Optional[DomainInfoList], domain_names))288 def list_elasticsearch_versions(289 self,290 context: RequestContext,291 max_results: MaxResults = None,292 next_token: NextToken = None,293 ) -> ListElasticsearchVersionsResponse:294 opensearch_client = aws_stack.connect_to_service("opensearch", region_name=context.region)295 # Construct the arguments as kwargs to not set None values at all (boto doesn't like that)296 kwargs = {297 key: value298 for key, value in {"MaxResults": max_results, "NextToken": next_token}.items()299 if value is not None300 }301 with exception_mapper():302 versions = opensearch_client.list_versions(**kwargs)303 return ListElasticsearchVersionsResponse(304 ElasticsearchVersions=[305 _version_from_opensearch(version) for version in versions["Versions"]306 ],307 NextToken=versions.get(next_token),308 )309 def get_compatible_elasticsearch_versions(310 self, context: RequestContext, domain_name: DomainName = None311 ) -> GetCompatibleElasticsearchVersionsResponse:312 opensearch_client = aws_stack.connect_to_service("opensearch", region_name=context.region)313 # Only hand the DomainName param to boto if it's set314 kwargs = {}315 if domain_name:316 kwargs["DomainName"] = domain_name317 with exception_mapper():318 compatible_versions_response = opensearch_client.get_compatible_versions(**kwargs)319 compatible_versions = compatible_versions_response.get("CompatibleVersions")320 return GetCompatibleElasticsearchVersionsResponse(321 CompatibleElasticsearchVersions=_compatible_version_list_from_opensearch(322 compatible_versions323 )324 )325 def describe_elasticsearch_domain_config(326 self, context: RequestContext, domain_name: DomainName327 ) -> DescribeElasticsearchDomainConfigResponse:328 opensearch_client = aws_stack.connect_to_service("opensearch", region_name=context.region)329 with exception_mapper():330 domain_config = opensearch_client.describe_domain_config(DomainName=domain_name).get(331 "DomainConfig"332 )333 return DescribeElasticsearchDomainConfigResponse(334 DomainConfig=_domainconfig_from_opensearch(domain_config)335 )...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!