<s> from __future__ import unicode_literals <EOL> import json <EOL> from six . moves . urllib . parse import urlparse , parse_qs <EOL> from moto . core . responses import _TemplateEnvironmentMixin <EOL> from . models import glacier_backends <EOL> from . utils import region_from_glacier_url , vault_from_glacier_url <EOL> class GlacierResponse ( _TemplateEnvironmentMixin ) : <EOL> def __init__ ( self , backend ) : <EOL> super ( GlacierResponse , self ) . __init__ ( ) <EOL> self . backend = backend <EOL> @ classmethod <EOL> def all_vault_response ( clazz , request , full_url , headers ) : <EOL> region_name = region_from_glacier_url ( full_url ) <EOL> response_instance = GlacierResponse ( glacier_backends [ region_name ] ) <EOL> return response_instance . _all_vault_response ( request , full_url , headers ) <EOL> def _all_vault_response ( self , request , full_url , headers ) : <EOL> vaults = self . backend . list_vaules ( ) <EOL> response = json . dumps ( { <EOL> "<STR_LIT>" : None , <EOL> "<STR_LIT>" : [ <EOL> vault . to_dict ( ) for vault in vaults <EOL> ] <EOL> } ) <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT:application/json>' <EOL> return <NUM_LIT:200> , headers , response <EOL> @ classmethod <EOL> def vault_response ( clazz , request , full_url , headers ) : <EOL> region_name = region_from_glacier_url ( full_url ) <EOL> response_instance = GlacierResponse ( glacier_backends [ region_name ] ) <EOL> return response_instance . _vault_response ( request , full_url , headers ) <EOL> def _vault_response ( self , request , full_url , headers ) : <EOL> method = request . method <EOL> parsed_url = urlparse ( full_url ) <EOL> querystring = parse_qs ( parsed_url . query , keep_blank_values = True ) <EOL> vault_name = vault_from_glacier_url ( full_url ) <EOL> if method == '<STR_LIT:GET>' : <EOL> return self . _vault_response_get ( vault_name , querystring , headers ) <EOL> elif method == '<STR_LIT>' : <EOL> return self . _vault_response_put ( vault_name , querystring , headers ) <EOL> elif method == '<STR_LIT>' : <EOL> return self . _vault_response_delete ( vault_name , querystring , headers ) <EOL> def _vault_response_get ( self , vault_name , querystring , headers ) : <EOL> vault = self . backend . get_vault ( vault_name ) <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT:application/json>' <EOL> return <NUM_LIT:200> , headers , json . dumps ( vault . to_dict ( ) ) <EOL> def _vault_response_put ( self , vault_name , querystring , headers ) : <EOL> self . backend . create_vault ( vault_name ) <EOL> return <NUM_LIT> , headers , "<STR_LIT>" <EOL> def _vault_response_delete ( self , vault_name , querystring , headers ) : <EOL> self . backend . delete_vault ( vault_name ) <EOL> return <NUM_LIT> , headers , "<STR_LIT>" <EOL> @ classmethod <EOL> def vault_archive_response ( clazz , request , full_url , headers ) : <EOL> region_name = region_from_glacier_url ( full_url ) <EOL> response_instance = GlacierResponse ( glacier_backends [ region_name ] ) <EOL> return response_instance . _vault_archive_response ( request , full_url , headers ) <EOL> def _vault_archive_response ( self , request , full_url , headers ) : <EOL> method = request . method <EOL> body = request . body <EOL> parsed_url = urlparse ( full_url ) <EOL> querystring = parse_qs ( parsed_url . query , keep_blank_values = True ) <EOL> vault_name = full_url . split ( "<STR_LIT:/>" ) [ - <NUM_LIT:2> ] <EOL> if method == '<STR_LIT:POST>' : <EOL> return self . _vault_archive_response_post ( vault_name , body , querystring , headers ) <EOL> def _vault_archive_response_post ( self , vault_name , body , querystring , headers ) : <EOL> vault = self . backend . get_vault ( vault_name ) <EOL> vault_id = vault . create_archive ( body ) <EOL> headers [ '<STR_LIT>' ] = vault_id <EOL> return <NUM_LIT> , headers , "<STR_LIT>" <EOL> @ classmethod <EOL> def vault_archive_individual_response ( clazz , request , full_url , headers ) : <EOL> region_name = region_from_glacier_url ( full_url ) <EOL> response_instance = GlacierResponse ( glacier_backends [ region_name ] ) <EOL> return response_instance . _vault_archive_individual_response ( request , full_url , headers ) <EOL> def _vault_archive_individual_response ( self , request , full_url , headers ) : <EOL> method = request . method <EOL> vault_name = full_url . split ( "<STR_LIT:/>" ) [ - <NUM_LIT:3> ] <EOL> archive_id = full_url . split ( "<STR_LIT:/>" ) [ - <NUM_LIT:1> ] <EOL> if method == '<STR_LIT>' : <EOL> vault = self . backend . get_vault ( vault_name ) <EOL> vault . delete_archive ( archive_id ) <EOL> return <NUM_LIT> , headers , "<STR_LIT>" <EOL> @ classmethod <EOL> def vault_jobs_response ( clazz , request , full_url , headers ) : <EOL> region_name = region_from_glacier_url ( full_url ) <EOL> response_instance = GlacierResponse ( glacier_backends [ region_name ] ) <EOL> return response_instance . _vault_jobs_response ( request , full_url , headers ) <EOL> def _vault_jobs_response ( self , request , full_url , headers ) : <EOL> method = request . method <EOL> body = request . body <EOL> account_id = full_url . split ( "<STR_LIT:/>" ) [ <NUM_LIT:1> ] <EOL> vault_name = full_url . split ( "<STR_LIT:/>" ) [ - <NUM_LIT:2> ] <EOL> if method == '<STR_LIT:GET>' : <EOL> jobs = self . backend . list_jobs ( vault_name ) <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT:application/json>' <EOL> return <NUM_LIT:200> , headers , json . dumps ( { <EOL> "<STR_LIT>" : [ <EOL> job . to_dict ( ) for job in jobs <EOL> ] , <EOL> "<STR_LIT>" : None , <EOL> } ) <EOL> elif method == '<STR_LIT:POST>' : <EOL> json_body = json . loads ( body . decode ( "<STR_LIT:utf-8>" ) ) <EOL> archive_id = json_body [ '<STR_LIT>' ] <EOL> job_id = self . backend . initiate_job ( vault_name , archive_id ) <EOL> headers [ '<STR_LIT>' ] = job_id <EOL> headers [ '<STR_LIT>' ] = "<STR_LIT>" . format ( account_id , vault_name , job_id ) <EOL> return <NUM_LIT> , headers , "<STR_LIT>" <EOL> @ classmethod <EOL> def vault_jobs_individual_response ( clazz , request , full_url , headers ) : <EOL> region_name = region_from_glacier_url ( full_url ) <EOL> response_instance = GlacierResponse ( glacier_backends [ region_name ] ) <EOL> return response_instance . _vault_jobs_individual_response ( request , full_url , headers ) <EOL> def _vault_jobs_individual_response ( self , request , full_url , headers ) : <EOL> vault_name = full_url . split ( "<STR_LIT:/>" ) [ - <NUM_LIT:3> ] <EOL> archive_id = full_url . split ( "<STR_LIT:/>" ) [ - <NUM_LIT:1> ] <EOL> vault = self . backend . get_vault ( vault_name ) <EOL> job = vault . describe_job ( archive_id ) <EOL> return <NUM_LIT:200> , headers , json . dumps ( job . to_dict ( ) ) <EOL> @ classmethod <EOL> def vault_jobs_output_response ( clazz , request , full_url , headers ) : <EOL> region_name = region_from_glacier_url ( full_url ) <EOL> response_instance = GlacierResponse ( glacier_backends [ region_name ] ) <EOL> return response_instance . _vault_jobs_output_response ( request , full_url , headers ) <EOL> def _vault_jobs_output_response ( self , request , full_url , headers ) : <EOL> vault_name = full_url . split ( "<STR_LIT:/>" ) [ - <NUM_LIT:4> ] <EOL> job_id = full_url . split ( "<STR_LIT:/>" ) [ - <NUM_LIT:2> ] <EOL> vault = self . backend . get_vault ( vault_name ) <EOL> output = vault . get_job_output ( job_id ) <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return <NUM_LIT:200> , headers , output </s>
