|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Copyright (c) 2018 StackHPC Ltd. |
| 4 | +# Apache 2 Licence |
| 5 | + |
| 6 | +from __future__ import absolute_import, division, print_function |
| 7 | +__metaclass__ = type |
| 8 | + |
| 9 | +ANSIBLE_METADATA = {'metadata_version': '1.1', |
| 10 | + 'status': ['preview'], |
| 11 | + 'supported_by': 'community'} |
| 12 | + |
| 13 | +DOCUMENTATION = ''' |
| 14 | +--- |
| 15 | +module: os_server_interface |
| 16 | +short_description: Create, update and delete server interface. |
| 17 | +author: bharat@stackhpc.com |
| 18 | +version_added: "1.0" |
| 19 | +description: |
| 20 | + - Create, update and delete server interface using Openstack Nova API. |
| 21 | +notes: |
| 22 | + - This module returns C(network_interface) fact, which |
| 23 | + contains information about server network interfaces. |
| 24 | +requirements: |
| 25 | + - "python >= 2.6" |
| 26 | + - "openstacksdk" |
| 27 | + - "python-novaclient" |
| 28 | +options: |
| 29 | + cloud: |
| 30 | + description: |
| 31 | + - Cloud name inside cloud.yaml file. |
| 32 | + type: str |
| 33 | + state: |
| 34 | + description: |
| 35 | + - Must be `present`, `query` or `absent`. |
| 36 | + type: str |
| 37 | + server_id: |
| 38 | + description: |
| 39 | + - Server name or uuid. |
| 40 | + type: str |
| 41 | + interfaces: |
| 42 | + description: |
| 43 | + - List of network interface names. |
| 44 | + type: list of str |
| 45 | +extends_documentation_fragment: openstack |
| 46 | +''' |
| 47 | + |
| 48 | +EXAMPLES = ''' |
| 49 | +# Attach interfaces to <server_id>: |
| 50 | +- os_container_infra: |
| 51 | + cloud: mycloud |
| 52 | + state: present |
| 53 | + server_id: xxxxx-xxxxx-xxxx-xxxx |
| 54 | + interfaces: |
| 55 | + - p3-lln |
| 56 | + - p3-bdn |
| 57 | + register: result |
| 58 | +- debug: |
| 59 | + var: result |
| 60 | +''' |
| 61 | + |
| 62 | +from ansible.module_utils.basic import AnsibleModule |
| 63 | +from ansible.utils.display import Display |
| 64 | +from novaclient.client import Client |
| 65 | +from novaclient.exceptions import NotFound |
| 66 | +import openstack |
| 67 | +import time |
| 68 | + |
| 69 | +class OpenStackAuthConfig(Exception): |
| 70 | + pass |
| 71 | + |
| 72 | +class ServerInterface(object): |
| 73 | + def __init__(self, **kwargs): |
| 74 | + self.server_id = kwargs['server_id'] |
| 75 | + self.state = kwargs['state'] |
| 76 | + |
| 77 | + self.connect(**kwargs) |
| 78 | + self.interfaces = [] |
| 79 | + for interface in kwargs['interfaces']: |
| 80 | + network = self.cloud.network.find_network(interface) |
| 81 | + if not network: |
| 82 | + raise Exception("Unable to find network '%s'" % interface) |
| 83 | + self.interfaces.append(network) |
| 84 | + |
| 85 | + def connect(self, **kwargs): |
| 86 | + if kwargs['auth_type'] == 'password': |
| 87 | + if kwargs['cloud']: |
| 88 | + self.cloud = openstack.connect(cloud=kwargs['cloud']) |
| 89 | + elif kwargs['auth']: |
| 90 | + self.cloud = openstack.connect(**kwargs['auth']) |
| 91 | + else: |
| 92 | + self.cloud = openstack.connect() |
| 93 | + else: |
| 94 | + raise OpenStackAuthConfig('Only `password` auth_type is supported.') |
| 95 | + self.cloud.authorize() |
| 96 | + self.client = Client('2', session=self.cloud.session) |
| 97 | + |
| 98 | + def get_server(self): |
| 99 | + try: |
| 100 | + server = self.client.servers.find(id=self.server_id) |
| 101 | + except NotFound: |
| 102 | + server = self.client.servers.find(name=self.server_id) |
| 103 | + return server |
| 104 | + |
| 105 | + def apply(self): |
| 106 | + changed = False |
| 107 | + self.server = server = self.get_server() |
| 108 | + if self.state != 'query': |
| 109 | + attached_interfaces = server.interface_list() |
| 110 | + for interface in self.interfaces: |
| 111 | + interface_exists = False |
| 112 | + for attached_interface in attached_interfaces: |
| 113 | + if interface.id == attached_interface.net_id: |
| 114 | + if self.state == 'absent': |
| 115 | + server.interface_detach(port_id=attached_interface.port_id) |
| 116 | + changed = True |
| 117 | + elif self.state == 'present': |
| 118 | + interface_exists = True |
| 119 | + if interface_exists == False and self.state == 'present': |
| 120 | + server.interface_attach(port_id=None, net_id=interface.id, fixed_ip=None) |
| 121 | + changed = True |
| 122 | + if changed: |
| 123 | + self.server = self.get_server() |
| 124 | + return changed |
| 125 | + |
| 126 | +if __name__ == '__main__': |
| 127 | + module = AnsibleModule( |
| 128 | + argument_spec = dict( |
| 129 | + cloud=dict(required=False, type='str'), |
| 130 | + auth=dict(required=False, type='dict'), |
| 131 | + auth_type=dict(default='password', required=False, type='str'), |
| 132 | + state=dict(default='present', choices=['present','absent', 'query']), |
| 133 | + server_id=dict(required=True, type='str'), |
| 134 | + interfaces=dict(default=[], type='list'), |
| 135 | + ), |
| 136 | + supports_check_mode=False |
| 137 | + ) |
| 138 | + |
| 139 | + display = Display() |
| 140 | + |
| 141 | + try: |
| 142 | + server_interface = ServerInterface(**module.params) |
| 143 | + changed = server_interface.apply() |
| 144 | + except Exception as e: |
| 145 | + module.fail_json(msg=repr(e)) |
| 146 | + |
| 147 | + server = server_interface.server |
| 148 | + |
| 149 | + module.exit_json( |
| 150 | + changed=changed, |
| 151 | + server_name=server.name, |
| 152 | + server_networks=server.networks, |
| 153 | + ) |
0 commit comments