<s> import argparse <EOL> import httplib2 <EOL> import json <EOL> import logging <EOL> import os <EOL> import sys <EOL> from googleapiclient . discovery import build <EOL> from googleapiclient import errors <EOL> from googleapiclient import http <EOL> from oauth2client . client import flow_from_clientsecrets <EOL> from oauth2client . file import Storage <EOL> from oauth2client import gce <EOL> from oauth2client import tools <EOL> from oauth2client . tools import run_flow <EOL> import config <EOL> API_VERSION = '<STR_LIT>' <EOL> GCE_URL = '<STR_LIT>' % ( API_VERSION ) <EOL> GCE_SCOPE = '<STR_LIT>' <EOL> class GceHandler ( object ) : <EOL> def __init__ ( self , flags ) : <EOL> self . __flags = flags <EOL> project_dir = os . path . join ( <EOL> os . getenv ( '<STR_LIT>' ) , '<STR_LIT>' , '<STR_LIT>' , flags . project ) <EOL> client_secrets = os . path . join ( project_dir , '<STR_LIT>' ) <EOL> oauth2_storage = os . path . join ( project_dir , '<STR_LIT>' ) <EOL> flow = flow_from_clientsecrets ( client_secrets , scope = GCE_SCOPE ) <EOL> storage = Storage ( oauth2_storage ) <EOL> credentials = storage . get ( ) <EOL> if credentials is None or credentials . invalid : <EOL> credentials = run_flow ( flow , storage , flags ) <EOL> http = httplib2 . Http ( ) <EOL> self . __auth_http = credentials . authorize ( http ) <EOL> self . __gce_service = build ( '<STR_LIT>' , API_VERSION ) <EOL> self . __project_url = '<STR_LIT>' % ( GCE_URL , flags . project ) <EOL> def Run ( self ) : <EOL> if self . __flags . command == '<STR_LIT:list>' : <EOL> self . List ( ) <EOL> elif self . __flags . command == '<STR_LIT>' : <EOL> self . Insert ( ) <EOL> elif self . __flags . command == '<STR_LIT>' : <EOL> self . Delete ( ) <EOL> elif self . __flags . command == '<STR_LIT>' : <EOL> self . Print ( ) <EOL> def List ( self ) : <EOL> for instance in self . _ListInternal ( ) : <EOL> print instance [ '<STR_LIT:name>' ] <EOL> def _ListInternal ( self ) : <EOL> if self . __flags . config : <EOL> return self . _ListFromConfig ( ) <EOL> else : <EOL> return self . _ListExisting ( ) <EOL> def _ListExisting ( self ) : <EOL> request = self . __gce_service . instances ( ) . list ( project = self . __flags . project , <EOL> filter = None , <EOL> zone = self . __flags . zone ) <EOL> response = request . execute ( http = self . __auth_http ) <EOL> if response and '<STR_LIT>' in response : <EOL> return response [ '<STR_LIT>' ] <EOL> return [ ] <EOL> def _ListFromConfig ( self ) : <EOL> return config . ProcessConfig ( file = self . __flags . config , <EOL> project = self . __flags . project , <EOL> zone = self . __flags . zone ) <EOL> def _InstanceInSelection ( self , instance ) : <EOL> if self . __flags . instances is None : <EOL> return True <EOL> instances = self . __flags . instances . split ( '<STR_LIT:U+002C>' ) <EOL> return instance in instances <EOL> def _FormatJson ( self , obj ) : <EOL> if self . __flags . json_format == '<STR_LIT>' : <EOL> return '<STR_LIT:%s>' % obj <EOL> elif self . __flags . json_format == '<STR_LIT>' : <EOL> return json . dumps ( obj , indent = <NUM_LIT:2> , separators = ( '<STR_LIT:U+002C>' , '<STR_LIT>' ) ) <EOL> def _PrintJsonResponse ( self , name , response ) : <EOL> json_text = self . _FormatJson ( response ) <EOL> print '<STR_LIT>' % ( name , json_text ) <EOL> def _ExecuteBatchRequest ( self , requests ) : <EOL> """<STR_LIT>""" <EOL> def _BatchHttpRequestCallback ( request_id , response , exception ) : <EOL> if exception is not None : <EOL> print '<STR_LIT>' % exception <EOL> else : <EOL> self . _PrintJsonResponse ( request_id , response ) <EOL> batch = None <EOL> num_batch_requests = <NUM_LIT:0> <EOL> MAX_BATCH_SIZE = <NUM_LIT:1000> <EOL> for request_id , request in requests : <EOL> if batch is None : <EOL> batch = http . BatchHttpRequest ( <EOL> callback = _BatchHttpRequestCallback ) <EOL> batch . add ( request , request_id = request_id ) <EOL> num_batch_requests += <NUM_LIT:1> <EOL> if num_batch_requests == MAX_BATCH_SIZE : <EOL> batch . execute ( http = self . __auth_http ) <EOL> batch = None <EOL> num_batch_requests = <NUM_LIT:0> <EOL> if batch is not None : <EOL> batch . execute ( http = self . __auth_http ) <EOL> batch = None <EOL> def Insert ( self ) : <EOL> if self . __flags . config is None : <EOL> sys . stderr . write ( <EOL> '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> instances = self . _ListFromConfig ( ) <EOL> requests = [ ] <EOL> for instance in instances : <EOL> instance_name = instance [ '<STR_LIT:name>' ] <EOL> if not self . _InstanceInSelection ( instance_name ) : <EOL> if self . __flags . debug : <EOL> print '<STR_LIT>' % instance_name <EOL> continue <EOL> print '<STR_LIT>' % instance_name <EOL> if self . __flags . dry_run : <EOL> continue <EOL> request = self . __gce_service . instances ( ) . insert ( project = self . __flags . project , <EOL> body = instance , <EOL> strict = True , <EOL> zone = self . __flags . zone ) <EOL> requests . append ( ( instance_name , request ) ) <EOL> self . _ExecuteBatchRequest ( requests ) <EOL> print '<STR_LIT>' <EOL> def Delete ( self ) : <EOL> """<STR_LIT>""" <EOL> instances = self . _ListInternal ( ) <EOL> requests = [ ] <EOL> for instance in instances : <EOL> instance_name = instance [ '<STR_LIT:name>' ] <EOL> if not self . _InstanceInSelection ( instance_name ) : <EOL> if self . __flags . debug : <EOL> print '<STR_LIT>' % instance_name <EOL> continue <EOL> print '<STR_LIT>' % instance_name <EOL> if self . __flags . dry_run : <EOL> continue <EOL> request = self . __gce_service . instances ( ) . delete ( project = self . __flags . project , <EOL> instance = instance_name , <EOL> zone = self . __flags . zone ) <EOL> requests . append ( ( instance_name , request ) ) <EOL> self . _ExecuteBatchRequest ( requests ) <EOL> print '<STR_LIT>' <EOL> def Print ( self ) : <EOL> if self . __flags . config is None : <EOL> sys . stderr . write ( <EOL> '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> instances = self . _ListFromConfig ( ) <EOL> for instance in instances : <EOL> instance_name = instance [ '<STR_LIT:name>' ] <EOL> if not self . _InstanceInSelection ( instance_name ) : <EOL> if self . __flags . debug : <EOL> print '<STR_LIT>' % instance_name <EOL> continue <EOL> print '<STR_LIT>' % ( instance_name , self . _FormatJson ( instance ) ) <EOL> def main ( argv ) : <EOL> parser = argparse . ArgumentParser ( <EOL> description = __doc__ , <EOL> formatter_class = argparse . RawDescriptionHelpFormatter , <EOL> parents = [ tools . argparser ] ) <EOL> parser . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , required = True , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , required = True , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , default = False , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , default = False , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , default = '<STR_LIT>' , <EOL> choices = ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , default = '<STR_LIT>' , <EOL> choices = ( '<STR_LIT>' , '<STR_LIT:info>' , '<STR_LIT>' , '<STR_LIT:error>' ) , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , choices = ( '<STR_LIT:list>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> help = '<STR_LIT>' ) <EOL> flags = parser . parse_args ( argv [ <NUM_LIT:1> : ] ) <EOL> LOGGING = { <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT:info>' : logging . INFO , <EOL> '<STR_LIT>' : logging . WARNING , <EOL> '<STR_LIT:error>' : logging . ERROR , <EOL> } <EOL> logging . basicConfig ( level = LOGGING [ flags . logging ] ) <EOL> gce_handler = GceHandler ( flags ) <EOL> gce_handler . Run ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( sys . argv ) </s>
