<s> import os <EOL> import json <EOL> import logging <EOL> from functools import wraps <EOL> import six <EOL> from six . moves import urllib <EOL> from st2client . utils import httpclient <EOL> LOG = logging . getLogger ( __name__ ) <EOL> def add_auth_token_to_kwargs_from_env ( func ) : <EOL> @ wraps ( func ) <EOL> def decorate ( * args , ** kwargs ) : <EOL> if not kwargs . get ( '<STR_LIT>' ) and os . environ . get ( '<STR_LIT>' , None ) : <EOL> kwargs [ '<STR_LIT>' ] = os . environ . get ( '<STR_LIT>' ) <EOL> return func ( * args , ** kwargs ) <EOL> return decorate <EOL> class Resource ( object ) : <EOL> _alias = None <EOL> _display_name = None <EOL> _url_path = None <EOL> _plural = None <EOL> _plural_display_name = None <EOL> _repr_attributes = [ ] <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> for k , v in six . iteritems ( kwargs ) : <EOL> setattr ( self , k , v ) <EOL> def to_dict ( self , exclude_attributes = None ) : <EOL> """<STR_LIT>""" <EOL> exclude_attributes = exclude_attributes or [ ] <EOL> attributes = self . __dict__ . keys ( ) <EOL> attributes = [ attr for attr in attributes if not attr . startswith ( '<STR_LIT>' ) and <EOL> attr not in exclude_attributes ] <EOL> result = { } <EOL> for attribute in attributes : <EOL> value = getattr ( self , attribute , None ) <EOL> result [ attribute ] = value <EOL> return result <EOL> @ classmethod <EOL> def get_alias ( cls ) : <EOL> return cls . _alias if cls . _alias else cls . __name__ <EOL> @ classmethod <EOL> def get_display_name ( cls ) : <EOL> return cls . _display_name if cls . _display_name else cls . __name__ <EOL> @ classmethod <EOL> def get_plural_name ( cls ) : <EOL> if not cls . _plural : <EOL> raise Exception ( '<STR_LIT>' <EOL> '<STR_LIT>' % cls . __name__ ) <EOL> return cls . _plural <EOL> @ classmethod <EOL> def get_plural_display_name ( cls ) : <EOL> return ( cls . _plural_display_name <EOL> if cls . _plural_display_name <EOL> else cls . _plural ) <EOL> @ classmethod <EOL> def get_url_path_name ( cls ) : <EOL> if cls . _url_path : <EOL> return cls . _url_path <EOL> return cls . get_plural_name ( ) . lower ( ) <EOL> def serialize ( self ) : <EOL> return dict ( ( k , v ) <EOL> for k , v in six . iteritems ( self . __dict__ ) <EOL> if not k . startswith ( '<STR_LIT:_>' ) ) <EOL> @ classmethod <EOL> def deserialize ( cls , doc ) : <EOL> if type ( doc ) is not dict : <EOL> doc = json . loads ( doc ) <EOL> return cls ( ** doc ) <EOL> def __str__ ( self ) : <EOL> return str ( self . __repr__ ( ) ) <EOL> def __repr__ ( self ) : <EOL> if not self . _repr_attributes : <EOL> return super ( Resource , self ) . __repr__ ( ) <EOL> attributes = [ ] <EOL> for attribute in self . _repr_attributes : <EOL> value = getattr ( self , attribute , None ) <EOL> attributes . append ( '<STR_LIT>' % ( attribute , value ) ) <EOL> attributes = '<STR_LIT:U+002C>' . join ( attributes ) <EOL> class_name = self . __class__ . __name__ <EOL> result = '<STR_LIT>' % ( class_name , attributes ) <EOL> return result <EOL> class ResourceManager ( object ) : <EOL> def __init__ ( self , resource , endpoint , cacert = None , debug = False ) : <EOL> self . resource = resource <EOL> self . debug = debug <EOL> self . client = httpclient . HTTPClient ( endpoint , cacert = cacert , debug = debug ) <EOL> @ staticmethod <EOL> def handle_error ( response ) : <EOL> try : <EOL> content = response . json ( ) <EOL> fault = content . get ( '<STR_LIT>' , '<STR_LIT>' ) if content else '<STR_LIT>' <EOL> if fault : <EOL> response . reason += '<STR_LIT>' % fault <EOL> except Exception as e : <EOL> response . reason += ( '<STR_LIT>' <EOL> '<STR_LIT>' % str ( e ) ) <EOL> response . raise_for_status ( ) <EOL> @ add_auth_token_to_kwargs_from_env <EOL> def get_all ( self , ** kwargs ) : <EOL> url = '<STR_LIT>' % self . resource . get_url_path_name ( ) <EOL> limit = kwargs . pop ( '<STR_LIT>' , None ) <EOL> pack = kwargs . pop ( '<STR_LIT>' , None ) <EOL> prefix = kwargs . pop ( '<STR_LIT>' , None ) <EOL> user = kwargs . pop ( '<STR_LIT:user>' , None ) <EOL> params = kwargs . pop ( '<STR_LIT>' , { } ) <EOL> if limit and limit <= <NUM_LIT:0> : <EOL> limit = None <EOL> if limit : <EOL> params [ '<STR_LIT>' ] = limit <EOL> if pack : <EOL> params [ '<STR_LIT>' ] = pack <EOL> if prefix : <EOL> params [ '<STR_LIT>' ] = prefix <EOL> if user : <EOL> params [ '<STR_LIT:user>' ] = user <EOL> response = self . client . get ( url = url , params = params , ** kwargs ) <EOL> if response . status_code != <NUM_LIT:200> : <EOL> self . handle_error ( response ) <EOL> return [ self . resource . deserialize ( item ) <EOL> for item in response . json ( ) ] <EOL> @ add_auth_token_to_kwargs_from_env <EOL> def get_by_id ( self , id , ** kwargs ) : <EOL> url = '<STR_LIT>' % ( self . resource . get_url_path_name ( ) , id ) <EOL> response = self . client . get ( url , ** kwargs ) <EOL> if response . status_code == <NUM_LIT> : <EOL> return None <EOL> if response . status_code != <NUM_LIT:200> : <EOL> self . handle_error ( response ) <EOL> return self . resource . deserialize ( response . json ( ) ) <EOL> @ add_auth_token_to_kwargs_from_env <EOL> def get_property ( self , id_ , property_name , self_deserialize = True , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> token = None <EOL> if kwargs : <EOL> token = kwargs . pop ( '<STR_LIT>' , None ) <EOL> url = '<STR_LIT>' % ( self . resource . get_url_path_name ( ) , id_ , property_name , <EOL> urllib . parse . urlencode ( kwargs ) ) <EOL> else : <EOL> url = '<STR_LIT>' % ( self . resource . get_url_path_name ( ) , id_ , property_name ) <EOL> response = self . client . get ( url , token = token ) if token else self . client . get ( url ) <EOL> if response . status_code == <NUM_LIT> : <EOL> return None <EOL> if response . status_code != <NUM_LIT:200> : <EOL> self . handle_error ( response ) <EOL> if self_deserialize : <EOL> return [ self . resource . deserialize ( item ) for item in response . json ( ) ] <EOL> else : <EOL> return response . json ( ) <EOL> @ add_auth_token_to_kwargs_from_env <EOL> def get_by_ref_or_id ( self , ref_or_id , ** kwargs ) : <EOL> return self . get_by_id ( id = ref_or_id , ** kwargs ) <EOL> @ add_auth_token_to_kwargs_from_env <EOL> def query ( self , ** kwargs ) : <EOL> if not kwargs : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' in kwargs and kwargs . get ( '<STR_LIT>' ) <= <NUM_LIT:0> : <EOL> kwargs . pop ( '<STR_LIT>' ) <EOL> token = kwargs . get ( '<STR_LIT>' , None ) <EOL> params = kwargs . get ( '<STR_LIT>' , { } ) <EOL> print ( params ) <EOL> for k , v in six . iteritems ( kwargs ) : <EOL> if k != '<STR_LIT>' : <EOL> params [ k ] = v <EOL> url = '<STR_LIT>' % ( self . resource . get_url_path_name ( ) , <EOL> urllib . parse . urlencode ( params ) ) <EOL> response = self . client . get ( url , token = token ) if token else self . client . get ( url ) <EOL> if response . status_code == <NUM_LIT> : <EOL> return [ ] <EOL> if response . status_code != <NUM_LIT:200> : <EOL> self . handle_error ( response ) <EOL> items = response . json ( ) <EOL> instances = [ self . resource . deserialize ( item ) for item in items ] <EOL> return instances <EOL> @ add_auth_token_to_kwargs_from_env <EOL> def get_by_name ( self , name_or_id , ** kwargs ) : <EOL> instances = self . query ( name = name_or_id , ** kwargs ) <EOL> if not instances : <EOL> return None <EOL> else : <EOL> if len ( instances ) > <NUM_LIT:1> : <EOL> raise Exception ( '<STR_LIT>' % <EOL> ( self . resource . __name__ . lower ( ) , name_or_id ) ) <EOL> return instances [ <NUM_LIT:0> ] <EOL> @ add_auth_token_to_kwargs_from_env <EOL> def create ( self , instance , ** kwargs ) : <EOL> url = '<STR_LIT>' % self . resource . get_url_path_name ( ) <EOL> response = self . client . post ( url , instance . serialize ( ) , ** kwargs ) <EOL> if response . status_code != <NUM_LIT:200> : <EOL> self . handle_error ( response ) <EOL> instance = self . resource . deserialize ( response . json ( ) ) <EOL> return instance <EOL> @ add_auth_token_to_kwargs_from_env <EOL> def update ( self , instance , ** kwargs ) : <EOL> url = '<STR_LIT>' % ( self . resource . get_url_path_name ( ) , instance . id ) <EOL> response = self . client . put ( url , instance . serialize ( ) , ** kwargs ) <EOL> if response . status_code != <NUM_LIT:200> : <EOL> self . handle_error ( response ) <EOL> instance = self . resource . deserialize ( response . json ( ) ) <EOL> return instance <EOL> @ add_auth_token_to_kwargs_from_env <EOL> def delete ( self , instance , ** kwargs ) : <EOL> url = '<STR_LIT>' % ( self . resource . get_url_path_name ( ) , instance . id ) <EOL> response = self . client . delete ( url , ** kwargs ) <EOL> if response . status_code not in [ <NUM_LIT:200> , <NUM_LIT> , <NUM_LIT> ] : <EOL> self . handle_error ( response ) <EOL> return False <EOL> return True <EOL> @ add_auth_token_to_kwargs_from_env <EOL> def delete_by_id ( self , instance_id , ** kwargs ) : <EOL> url = '<STR_LIT>' % ( self . resource . get_url_path_name ( ) , instance_id ) <EOL> response = self . client . delete ( url , ** kwargs ) <EOL> if response . status_code not in [ <NUM_LIT:200> , <NUM_LIT> , <NUM_LIT> ] : <EOL> self . handle_error ( response ) <EOL> return False <EOL> try : <EOL> resp_json = response . json ( ) <EOL> if resp_json : <EOL> return resp_json <EOL> except : <EOL> pass <EOL> return True <EOL> class ActionAliasResourceManager ( ResourceManager ) : <EOL> def __init__ ( self , resource , endpoint , cacert = None , debug = False ) : <EOL> self . resource = resource <EOL> self . debug = debug <EOL> self . client = httpclient . HTTPClient ( root = endpoint , cacert = cacert , debug = debug ) <EOL> class LiveActionResourceManager ( ResourceManager ) : <EOL> @ add_auth_token_to_kwargs_from_env <EOL> def re_run ( self , execution_id , parameters = None , tasks = None , no_reset = None , ** kwargs ) : <EOL> url = '<STR_LIT>' % ( self . resource . get_url_path_name ( ) , execution_id ) <EOL> tasks = tasks or [ ] <EOL> no_reset = no_reset or [ ] <EOL> if list ( set ( no_reset ) - set ( tasks ) ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> data = { <EOL> '<STR_LIT>' : parameters , <EOL> '<STR_LIT>' : tasks , <EOL> '<STR_LIT>' : list ( set ( tasks ) - set ( no_reset ) ) <EOL> } <EOL> response = self . client . post ( url , data , ** kwargs ) <EOL> if response . status_code != <NUM_LIT:200> : <EOL> self . handle_error ( response ) <EOL> instance = self . resource . deserialize ( response . json ( ) ) <EOL> return instance <EOL> class TriggerInstanceResourceManager ( ResourceManager ) : <EOL> @ add_auth_token_to_kwargs_from_env <EOL> def re_emit ( self , trigger_instance_id , ** kwargs ) : <EOL> url = '<STR_LIT>' % ( self . resource . get_url_path_name ( ) , trigger_instance_id ) <EOL> response = self . client . post ( url , None ) <EOL> if response . status_code != <NUM_LIT:200> : <EOL> self . handle_error ( response ) <EOL> return response . json ( ) </s>
