"Fossies" - the Fresh Open Source Software Archive

Member "ansible-2.9.27/lib/ansible/modules/network/a10/a10_server_axapi3.py" (11 Oct 2021, 8948 Bytes) of package /linux/misc/ansible-2.9.27.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "a10_server_axapi3.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 2.9.24_vs_4.3.0.

    1 #!/usr/bin/python
    2 # -*- coding: utf-8 -*-
    3 
    4 # Copyright: (c) 2014, Mischa Peters <mpeters@a10networks.com>
    5 # Copyright: (c) 2016, Eric Chou <ericc@a10networks.com>
    6 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
    7 
    8 from __future__ import absolute_import, division, print_function
    9 __metaclass__ = type
   10 
   11 
   12 ANSIBLE_METADATA = {'metadata_version': '1.1',
   13                     'status': ['preview'],
   14                     'supported_by': 'community'}
   15 
   16 
   17 DOCUMENTATION = '''
   18 ---
   19 module: a10_server_axapi3
   20 version_added: 2.3
   21 short_description: Manage A10 Networks AX/SoftAX/Thunder/vThunder devices
   22 description:
   23     - Manage SLB (Server Load Balancer) server objects on A10 Networks devices via aXAPIv3.
   24 author:
   25   - Eric Chou (@ericchou1)
   26 extends_documentation_fragment:
   27   - a10
   28   - url
   29 options:
   30   server_name:
   31     description:
   32       - The SLB (Server Load Balancer) server name.
   33     required: true
   34     aliases: ['server']
   35   server_ip:
   36     description:
   37       - The SLB (Server Load Balancer) server IPv4 address.
   38     required: true
   39     aliases: ['ip', 'address']
   40   server_status:
   41     description:
   42       - The SLB (Server Load Balancer) virtual server status.
   43     default: enable
   44     aliases: ['action']
   45     choices: ['enable', 'disable']
   46   server_ports:
   47     description:
   48       - A list of ports to create for the server. Each list item should be a dictionary which specifies the C(port:)
   49         and C(protocol:).
   50     aliases: ['port']
   51   operation:
   52     description:
   53       - Create, Update or Remove SLB server. For create and update operation, we use the IP address and server
   54         name specified in the POST message. For delete operation, we use the server name in the request URI.
   55     default: create
   56     choices: ['create', 'update', 'remove']
   57   validate_certs:
   58     description:
   59       - If C(no), SSL certificates will not be validated. This should only be used
   60         on personally controlled devices using self-signed certificates.
   61     type: bool
   62     default: 'yes'
   63 
   64 '''
   65 
   66 RETURN = '''
   67 #
   68 '''
   69 
   70 EXAMPLES = '''
   71 # Create a new server
   72 - a10_server:
   73     host: a10.mydomain.com
   74     username: myadmin
   75     password: mypassword
   76     server: test
   77     server_ip: 1.1.1.100
   78     validate_certs: false
   79     server_status: enable
   80     write_config: yes
   81     operation: create
   82     server_ports:
   83       - port-number: 8080
   84         protocol: tcp
   85         action: enable
   86       - port-number: 8443
   87         protocol: TCP
   88 
   89 '''
   90 import json
   91 
   92 from ansible.module_utils.network.a10.a10 import axapi_call_v3, a10_argument_spec, axapi_authenticate_v3, axapi_failure
   93 from ansible.module_utils.network.a10.a10 import AXAPI_PORT_PROTOCOLS
   94 from ansible.module_utils.basic import AnsibleModule
   95 from ansible.module_utils.urls import url_argument_spec
   96 
   97 
   98 VALID_PORT_FIELDS = ['port-number', 'protocol', 'action']
   99 
  100 
  101 def validate_ports(module, ports):
  102     for item in ports:
  103         for key in item:
  104             if key not in VALID_PORT_FIELDS:
  105                 module.fail_json(msg="invalid port field (%s), must be one of: %s" % (key, ','.join(VALID_PORT_FIELDS)))
  106 
  107         # validate the port number is present and an integer
  108         if 'port-number' in item:
  109             try:
  110                 item['port-number'] = int(item['port-number'])
  111             except Exception:
  112                 module.fail_json(msg="port-number entries in the port definitions must be integers")
  113         else:
  114             module.fail_json(msg="port definitions must define the port-number field")
  115 
  116         # validate the port protocol is present, no need to convert to the internal API integer value in v3
  117         if 'protocol' in item:
  118             protocol = item['protocol']
  119             if not protocol:
  120                 module.fail_json(msg="invalid port protocol, must be one of: %s" % ','.join(AXAPI_PORT_PROTOCOLS))
  121             else:
  122                 item['protocol'] = protocol
  123         else:
  124             module.fail_json(msg="port definitions must define the port protocol (%s)" % ','.join(AXAPI_PORT_PROTOCOLS))
  125 
  126         # 'status' is 'action' in AXAPIv3
  127         # no need to convert the status, a.k.a action, to the internal API integer value in v3
  128         # action is either enabled or disabled
  129         if 'action' in item:
  130             action = item['action']
  131             if action not in ['enable', 'disable']:
  132                 module.fail_json(msg="server action must be enable or disable")
  133         else:
  134             item['action'] = 'enable'
  135 
  136 
  137 def main():
  138     argument_spec = a10_argument_spec()
  139     argument_spec.update(url_argument_spec())
  140     argument_spec.update(
  141         dict(
  142             operation=dict(type='str', default='create', choices=['create', 'update', 'delete']),
  143             server_name=dict(type='str', aliases=['server'], required=True),
  144             server_ip=dict(type='str', aliases=['ip', 'address'], required=True),
  145             server_status=dict(type='str', default='enable', aliases=['action'], choices=['enable', 'disable']),
  146             server_ports=dict(type='list', aliases=['port'], default=[]),
  147         )
  148     )
  149 
  150     module = AnsibleModule(
  151         argument_spec=argument_spec,
  152         supports_check_mode=False
  153     )
  154 
  155     host = module.params['host']
  156     username = module.params['username']
  157     password = module.params['password']
  158     operation = module.params['operation']
  159     write_config = module.params['write_config']
  160     slb_server = module.params['server_name']
  161     slb_server_ip = module.params['server_ip']
  162     slb_server_status = module.params['server_status']
  163     slb_server_ports = module.params['server_ports']
  164 
  165     axapi_base_url = 'https://{0}/axapi/v3/'.format(host)
  166     axapi_auth_url = axapi_base_url + 'auth/'
  167     signature = axapi_authenticate_v3(module, axapi_auth_url, username, password)
  168 
  169     # validate the ports data structure
  170     validate_ports(module, slb_server_ports)
  171 
  172     json_post = {
  173         "server-list": [
  174             {
  175                 "name": slb_server,
  176                 "host": slb_server_ip
  177             }
  178         ]
  179     }
  180 
  181     # add optional module parameters
  182     if slb_server_ports:
  183         json_post['server-list'][0]['port-list'] = slb_server_ports
  184 
  185     if slb_server_status:
  186         json_post['server-list'][0]['action'] = slb_server_status
  187 
  188     slb_server_data = axapi_call_v3(module, axapi_base_url + 'slb/server/', method='GET', body='', signature=signature)
  189 
  190     # for empty slb server list
  191     if axapi_failure(slb_server_data):
  192         slb_server_exists = False
  193     else:
  194         slb_server_list = [server['name'] for server in slb_server_data['server-list']]
  195         if slb_server in slb_server_list:
  196             slb_server_exists = True
  197         else:
  198             slb_server_exists = False
  199 
  200     changed = False
  201     if operation == 'create':
  202         if slb_server_exists is False:
  203             result = axapi_call_v3(module, axapi_base_url + 'slb/server/', method='POST', body=json.dumps(json_post), signature=signature)
  204             if axapi_failure(result):
  205                 module.fail_json(msg="failed to create the server: %s" % result['response']['err']['msg'])
  206             changed = True
  207         else:
  208             module.fail_json(msg="server already exists, use state='update' instead")
  209             changed = False
  210         # if we changed things, get the full info regarding result
  211         if changed:
  212             result = axapi_call_v3(module, axapi_base_url + 'slb/server/' + slb_server, method='GET', body='', signature=signature)
  213         else:
  214             result = slb_server_data
  215     elif operation == 'delete':
  216         if slb_server_exists:
  217             result = axapi_call_v3(module, axapi_base_url + 'slb/server/' + slb_server, method='DELETE', body='', signature=signature)
  218             if axapi_failure(result):
  219                 module.fail_json(msg="failed to delete server: %s" % result['response']['err']['msg'])
  220             changed = True
  221         else:
  222             result = dict(msg="the server was not present")
  223     elif operation == 'update':
  224         if slb_server_exists:
  225             result = axapi_call_v3(module, axapi_base_url + 'slb/server/', method='PUT', body=json.dumps(json_post), signature=signature)
  226             if axapi_failure(result):
  227                 module.fail_json(msg="failed to update server: %s" % result['response']['err']['msg'])
  228             changed = True
  229         else:
  230             result = dict(msg="the server was not present")
  231 
  232     # if the config has changed, save the config unless otherwise requested
  233     if changed and write_config:
  234         write_result = axapi_call_v3(module, axapi_base_url + 'write/memory/', method='POST', body='', signature=signature)
  235         if axapi_failure(write_result):
  236             module.fail_json(msg="failed to save the configuration: %s" % write_result['response']['err']['msg'])
  237 
  238     # log out gracefully and exit
  239     axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature)
  240     module.exit_json(changed=changed, content=result)
  241 
  242 
  243 if __name__ == '__main__':
  244     main()