<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import logging <EOL> HAS_LIBS = False <EOL> HAS_SIX = False <EOL> try : <EOL> import requests <EOL> from salt . ext . six . moves . html_parser import HTMLParser <EOL> try : <EOL> import salt . ext . six as six <EOL> HAS_SIX = True <EOL> except ImportError : <EOL> try : <EOL> import six <EOL> except ImportError : <EOL> pass <EOL> HAS_LIBS = True <EOL> class ASAMHTMLParser ( HTMLParser ) : <EOL> def __init__ ( self ) : <EOL> HTMLParser . __init__ ( self ) <EOL> self . data = [ ] <EOL> def handle_starttag ( self , tag , attrs ) : <EOL> if tag != "<STR_LIT:a>" : <EOL> return <EOL> for attr in attrs : <EOL> if attr [ <NUM_LIT:0> ] != "<STR_LIT>" : <EOL> return <EOL> self . data . append ( attr [ <NUM_LIT:1> ] ) <EOL> except ImportError : <EOL> pass <EOL> log = logging . getLogger ( __name__ ) <EOL> def __virtual__ ( ) : <EOL> '''<STR_LIT>''' <EOL> if not HAS_LIBS or not HAS_SIX : <EOL> return False <EOL> if _get_asam_configuration ( ) is False : <EOL> return False <EOL> return True <EOL> def _get_asam_configuration ( driver_url = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> asam_config = __opts__ [ '<STR_LIT>' ] if '<STR_LIT>' in __opts__ else None <EOL> if asam_config : <EOL> try : <EOL> for asam_server , service_config in six . iteritems ( asam_config ) : <EOL> username = service_config . get ( '<STR_LIT:username>' , None ) <EOL> password = service_config . get ( '<STR_LIT:password>' , None ) <EOL> protocol = service_config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> port = service_config . get ( '<STR_LIT:port>' , <NUM_LIT> ) <EOL> if not username or not password : <EOL> log . error ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( asam_server ) <EOL> ) <EOL> return False <EOL> ret = { <EOL> '<STR_LIT>' : "<STR_LIT>" . format ( protocol , asam_server , port ) , <EOL> '<STR_LIT>' : "<STR_LIT>" . format ( protocol , asam_server , port ) , <EOL> '<STR_LIT>' : "<STR_LIT>" . format ( protocol , asam_server , port ) , <EOL> '<STR_LIT>' : "<STR_LIT>" . format ( protocol , asam_server , port ) , <EOL> '<STR_LIT:username>' : username , <EOL> '<STR_LIT:password>' : password <EOL> } <EOL> if ( not driver_url ) or ( driver_url == asam_server ) : <EOL> return ret <EOL> except Exception as exc : <EOL> log . error ( <EOL> "<STR_LIT>" . format ( exc ) <EOL> ) <EOL> return False <EOL> if driver_url : <EOL> log . error ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( driver_url ) <EOL> ) <EOL> return False <EOL> return False <EOL> def _make_post_request ( url , data , auth , verify = True ) : <EOL> r = requests . post ( url , data = data , auth = auth , verify = verify ) <EOL> if r . status_code != requests . codes . ok : <EOL> r . raise_for_status ( ) <EOL> else : <EOL> return r . text . split ( '<STR_LIT:\n>' ) <EOL> def _parse_html_content ( html_content ) : <EOL> parser = ASAMHTMLParser ( ) <EOL> for line in html_content : <EOL> if line . startswith ( "<STR_LIT>" ) : <EOL> html_content . remove ( line ) <EOL> else : <EOL> parser . feed ( line ) <EOL> return parser <EOL> def _get_platformset_name ( data , platform_name ) : <EOL> for item in data : <EOL> if platform_name in item and item . startswith ( '<STR_LIT>' ) : <EOL> parameter_list = item . split ( '<STR_LIT:&>' ) <EOL> for parameter in parameter_list : <EOL> if parameter . startswith ( "<STR_LIT>" ) : <EOL> return parameter . split ( '<STR_LIT:=>' ) [ <NUM_LIT:1> ] <EOL> return None <EOL> def _get_platforms ( data ) : <EOL> platform_list = [ ] <EOL> for item in data : <EOL> if item . startswith ( '<STR_LIT>' ) : <EOL> parameter_list = item . split ( '<STR_LIT>' , <NUM_LIT:1> ) [ <NUM_LIT:1> ] . split ( '<STR_LIT:&>' ) <EOL> for parameter in parameter_list : <EOL> if parameter . startswith ( "<STR_LIT>" ) : <EOL> platform_list . append ( parameter . split ( '<STR_LIT:=>' ) [ <NUM_LIT:1> ] ) <EOL> return platform_list <EOL> def _get_platform_sets ( data ) : <EOL> platform_set_list = [ ] <EOL> for item in data : <EOL> if item . startswith ( '<STR_LIT>' ) : <EOL> parameter_list = item . split ( '<STR_LIT>' , <NUM_LIT:1> ) [ <NUM_LIT:1> ] . split ( '<STR_LIT:&>' ) <EOL> for parameter in parameter_list : <EOL> if parameter . startswith ( "<STR_LIT>" ) : <EOL> platform_set_list . append ( parameter . split ( '<STR_LIT:=>' ) [ <NUM_LIT:1> ] . replace ( '<STR_LIT>' , '<STR_LIT:U+0020>' ) ) <EOL> return platform_set_list <EOL> def remove_platform ( name , server_url ) : <EOL> '''<STR_LIT>''' <EOL> config = _get_asam_configuration ( server_url ) <EOL> if not config : <EOL> return False <EOL> url = config [ '<STR_LIT>' ] <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT:false>' , <EOL> } <EOL> auth = ( <EOL> config [ '<STR_LIT:username>' ] , <EOL> config [ '<STR_LIT:password>' ] <EOL> ) <EOL> try : <EOL> html_content = _make_post_request ( url , data , auth , verify = False ) <EOL> except Exception as exc : <EOL> err_msg = "<STR_LIT>" . format ( server_url ) <EOL> log . error ( "<STR_LIT>" . format ( err_msg , exc ) ) <EOL> return { name : err_msg } <EOL> parser = _parse_html_content ( html_content ) <EOL> platformset_name = _get_platformset_name ( parser . data , name ) <EOL> if platformset_name : <EOL> log . debug ( platformset_name ) <EOL> data [ '<STR_LIT>' ] = name <EOL> data [ '<STR_LIT>' ] = str ( platformset_name ) <EOL> data [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> data [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> try : <EOL> html_content = _make_post_request ( url , data , auth , verify = False ) <EOL> except Exception as exc : <EOL> err_msg = "<STR_LIT>" . format ( server_url ) <EOL> log . error ( "<STR_LIT>" . format ( err_msg , exc ) ) <EOL> return { name : err_msg } <EOL> parser = _parse_html_content ( html_content ) <EOL> platformset_name = _get_platformset_name ( parser . data , name ) <EOL> if platformset_name : <EOL> return { name : "<STR_LIT>" . format ( server_url ) } <EOL> else : <EOL> return { name : "<STR_LIT>" . format ( server_url ) } <EOL> else : <EOL> return { name : "<STR_LIT>" . format ( server_url ) } <EOL> def list_platforms ( server_url ) : <EOL> '''<STR_LIT>''' <EOL> config = _get_asam_configuration ( server_url ) <EOL> if not config : <EOL> return False <EOL> url = config [ '<STR_LIT>' ] <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT:false>' , <EOL> } <EOL> auth = ( <EOL> config [ '<STR_LIT:username>' ] , <EOL> config [ '<STR_LIT:password>' ] <EOL> ) <EOL> try : <EOL> html_content = _make_post_request ( url , data , auth , verify = False ) <EOL> except Exception as exc : <EOL> err_msg = "<STR_LIT>" <EOL> log . error ( "<STR_LIT>" . format ( err_msg , exc ) ) <EOL> return { server_url : err_msg } <EOL> parser = _parse_html_content ( html_content ) <EOL> platform_list = _get_platforms ( parser . data ) <EOL> if platform_list : <EOL> return { server_url : platform_list } <EOL> else : <EOL> return { server_url : "<STR_LIT>" } <EOL> def list_platform_sets ( server_url ) : <EOL> '''<STR_LIT>''' <EOL> config = _get_asam_configuration ( server_url ) <EOL> if not config : <EOL> return False <EOL> url = config [ '<STR_LIT>' ] <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT:false>' , <EOL> } <EOL> auth = ( <EOL> config [ '<STR_LIT:username>' ] , <EOL> config [ '<STR_LIT:password>' ] <EOL> ) <EOL> try : <EOL> html_content = _make_post_request ( url , data , auth , verify = False ) <EOL> except Exception as exc : <EOL> err_msg = "<STR_LIT>" <EOL> log . error ( "<STR_LIT>" . format ( err_msg , exc ) ) <EOL> return { server_url : err_msg } <EOL> parser = _parse_html_content ( html_content ) <EOL> platform_set_list = _get_platform_sets ( parser . data ) <EOL> if platform_set_list : <EOL> return { server_url : platform_set_list } <EOL> else : <EOL> return { server_url : "<STR_LIT>" } <EOL> def add_platform ( name , platform_set , server_url ) : <EOL> '''<STR_LIT>''' <EOL> config = _get_asam_configuration ( server_url ) <EOL> if not config : <EOL> return False <EOL> platforms = list_platforms ( server_url ) <EOL> if name in platforms [ server_url ] : <EOL> return { name : "<STR_LIT>" . format ( server_url ) } <EOL> platform_sets = list_platform_sets ( server_url ) <EOL> if platform_set not in platform_sets [ server_url ] : <EOL> return { name : "<STR_LIT>" . format ( server_url ) } <EOL> url = config [ '<STR_LIT>' ] <EOL> data = { <EOL> '<STR_LIT>' : name , <EOL> '<STR_LIT>' : platform_set , <EOL> '<STR_LIT>' : '<STR_LIT:false>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> auth = ( <EOL> config [ '<STR_LIT:username>' ] , <EOL> config [ '<STR_LIT:password>' ] <EOL> ) <EOL> try : <EOL> html_content = _make_post_request ( url , data , auth , verify = False ) <EOL> except Exception as exc : <EOL> err_msg = "<STR_LIT>" . format ( server_url ) <EOL> log . error ( "<STR_LIT>" . format ( err_msg , exc ) ) <EOL> return { name : err_msg } <EOL> platforms = list_platforms ( server_url ) <EOL> if name in platforms [ server_url ] : <EOL> return { name : "<STR_LIT>" . format ( server_url ) } <EOL> else : <EOL> return { name : "<STR_LIT>" . format ( server_url ) } </s>
