@ -2,23 +2,12 @@
# -*- coding: utf-8 -*-
# (c) 2016, NetApp, Inc
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
#
#
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import , division , print_function
__metaclass__ = type
ANSIBLE_METADATA = { ' metadata_version ' : ' 1.0 ' ,
' status ' : [ ' preview ' ] ,
' supported_by ' : ' community ' }
@ -139,10 +128,9 @@ HEADERS = {
import json
from ansible . module_utils . basic import AnsibleModule
from ansible . module_utils . pycompat24 import get_exception
from ansible . module_utils . urls import open_url
from ansible . module_utils . six . moves . urllib . error import HTTPError
from ansible . module_utils . _text import to_native
from ansible . module_utils . urls import open_url
def request ( url , data = None , headers = None , method = ' GET ' , use_proxy = True ,
@ -153,9 +141,8 @@ def request(url, data=None, headers=None, method='GET', use_proxy=True,
force = force , last_mod_time = last_mod_time , timeout = timeout , validate_certs = validate_certs ,
url_username = url_username , url_password = url_password , http_agent = http_agent ,
force_basic_auth = force_basic_auth )
except HTTPError :
err = get_exception ( )
r = err . fp
except HTTPError as e :
r = e . fp
try :
raw_data = r . read ( )
@ -194,9 +181,8 @@ def get_hostgroups(module, ssid, api_url, user, pwd):
try :
rc , data = request ( url , headers = HEADERS , url_username = user , url_password = pwd )
return rc , data
except HTTPError :
err = get_exception ( )
module . fail_json ( msg = " Failed to get host groups. Id [ %s ]. Error [ %s ]. " % ( ssid , str ( err ) ) )
except HTTPError as e :
module . fail_json ( msg = " Failed to get host groups. Id [ %s ]. Error [ %s ]. " % ( ssid , to_native ( e ) ) )
def get_hostref ( module , ssid , name , api_url , user , pwd ) :
@ -204,9 +190,8 @@ def get_hostref(module, ssid, name, api_url, user, pwd):
url = api_url + all_hosts
try :
rc , data = request ( url , method = ' GET ' , headers = HEADERS , url_username = user , url_password = pwd )
except Exception :
err = get_exception ( )
module . fail_json ( msg = " Failed to get hosts. Id [ %s ]. Error [ %s ]. " % ( ssid , str ( err ) ) )
except Exception as e :
module . fail_json ( msg = " Failed to get hosts. Id [ %s ]. Error [ %s ]. " % ( ssid , to_native ( e ) ) )
for host in data :
if host [ ' name ' ] == name :
@ -230,9 +215,8 @@ def create_hostgroup(module, ssid, name, api_url, user, pwd, hosts=None):
post_data = json . dumps ( dict ( name = name , hosts = hostrefs ) )
try :
rc , data = request ( url , method = ' POST ' , data = post_data , headers = HEADERS , url_username = user , url_password = pwd )
except Exception :
err = get_exception ( )
module . fail_json ( msg = " Failed to create host group. Id [ %s ]. Error [ %s ]. " % ( ssid , str ( err ) ) )
except Exception as e :
module . fail_json ( msg = " Failed to create host group. Id [ %s ]. Error [ %s ]. " % ( ssid , to_native ( e ) ) )
return rc , data
@ -255,10 +239,9 @@ def update_hostgroup(module, ssid, name, api_url, user, pwd, hosts=None, new_nam
try :
rc , data = request ( url , method = ' POST ' , data = post_data , headers = HEADERS , url_username = user , url_password = pwd )
except Exception :
err = get_exception ( )
except Exception as e :
module . fail_json ( msg = " Failed to update host group. Group [ %s ]. Id [ %s ]. Error [ %s ]. " % ( gid , ssid ,
str ( err ) ) )
to_native ( e ) ) )
return rc , data
@ -269,9 +252,8 @@ def delete_hostgroup(module, ssid, group_id, api_url, user, pwd):
# TODO: Loop through hosts, do mapping to href, make new list to pass to data
try :
rc , data = request ( url , method = ' DELETE ' , headers = HEADERS , url_username = user , url_password = pwd )
except Exception :
err = get_exception ( )
module . fail_json ( msg = " Failed to delete host group. Group [ %s ]. Id [ %s ]. Error [ %s ]. " % ( group_id , ssid , str ( err ) ) )
except Exception as e :
module . fail_json ( msg = " Failed to delete host group. Group [ %s ]. Id [ %s ]. Error [ %s ]. " % ( group_id , ssid , to_native ( e ) ) )
return rc , data
@ -294,24 +276,22 @@ def get_hosts_in_group(module, ssid, group_name, api_url, user, pwd):
g_url = api_url + all_groups
try :
g_rc , g_data = request ( g_url , method = ' GET ' , headers = HEADERS , url_username = user , url_password = pwd )
except Exception :
err = get_exception ( )
except Exception as e :
module . fail_json (
msg = " Failed in first step getting hosts from group. Group: [ %s ]. Id [ %s ]. Error [ %s ]. " % ( group_name ,
ssid ,
str ( err ) ) )
to_native ( e ) ) )
all_hosts = ' storage-systems/ %s /hosts ' % ssid
h_url = api_url + all_hosts
try :
h_rc , h_data = request ( h_url , method = ' GET ' , headers = HEADERS , url_username = user , url_password = pwd )
except Exception :
err = get_exception ( )
except Exception as e :
module . fail_json (
msg = " Failed in second step getting hosts from group. Group: [ %s ]. Id [ %s ]. Error [ %s ]. " % (
group_name ,
ssid ,
str ( err ) ) )
to_native ( e ) ) )
hosts_in_group = [ ]
@ -371,9 +351,8 @@ def main():
if not exists :
try :
rc , data = create_hostgroup ( module , ssid , name , api_url , user , pwd , hosts )
except Exception :
err = get_exception ( )
module . fail_json ( msg = " Failed to create a host group. Id [ %s ]. Error [ %s ]. " % ( ssid , str ( err ) ) )
except Exception as e :
module . fail_json ( msg = " Failed to create a host group. Id [ %s ]. Error [ %s ]. " % ( ssid , to_native ( e ) ) )
hosts = get_hosts_in_group ( module , ssid , name , api_url , user , pwd )
module . exit_json ( changed = True , hosts = hosts , * * data )
@ -389,10 +368,9 @@ def main():
if set ( current_hosts ) != set ( hosts ) :
try :
rc , data = update_hostgroup ( module , ssid , name , api_url , user , pwd , hosts , new_name )
except Exception :
err = get_exception ( )
except Exception as e :
module . fail_json (
msg = " Failed to update host group. Group: [ %s ]. Id [ %s ]. Error [ %s ]. " % ( name , ssid , str ( err ) ) )
msg = " Failed to update host group. Group: [ %s ]. Id [ %s ]. Error [ %s ]. " % ( name , ssid , to_native ( e ) ) )
module . exit_json ( changed = True , hosts = hosts , * * data )
else :
for group in group_data :
@ -404,10 +382,9 @@ def main():
hg_id = get_hostgroup_id ( module , ssid , name , api_url , user , pwd )
try :
rc , data = delete_hostgroup ( module , ssid , hg_id , api_url , user , pwd )
except Exception :
err = get_exception ( )
except Exception as e :
module . fail_json (
msg = " Failed to delete host group. Group: [ %s ]. Id [ %s ]. Error [ %s ]. " % ( name , ssid , str ( err ) ) )
msg = " Failed to delete host group. Group: [ %s ]. Id [ %s ]. Error [ %s ]. " % ( name , ssid , to_native ( e ) ) )
module . exit_json ( changed = True , msg = " Host Group deleted " )
else :