Vasyl Saienko | 8403d17 | 2017-04-27 14:21:46 +0300 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | import logging |
| 4 | from functools import wraps |
| 5 | LOG = logging.getLogger(__name__) |
| 6 | |
| 7 | # Import third party libs |
| 8 | HAS_IRONIC = False |
| 9 | try: |
| 10 | from ironicclient import client |
| 11 | HAS_IRONIC = True |
| 12 | except ImportError: |
| 13 | pass |
| 14 | |
| 15 | __opts__ = {} |
| 16 | |
| 17 | |
| 18 | def __virtual__(): |
| 19 | ''' |
| 20 | Only load this module if ironic is installed on this minion. |
| 21 | ''' |
| 22 | if HAS_IRONIC: |
| 23 | return 'ironicng' |
| 24 | return False |
| 25 | |
| 26 | |
| 27 | def _autheticate(func_name): |
| 28 | ''' |
| 29 | Authenticate requests with the salt keystone module and format return data |
| 30 | ''' |
| 31 | @wraps(func_name) |
| 32 | def decorator_method(*args, **kwargs): |
| 33 | ''' |
| 34 | Authenticate request and format return data |
| 35 | ''' |
| 36 | connection_args = {'profile': kwargs.pop('profile', None)} |
| 37 | nkwargs = {} |
| 38 | for kwarg in kwargs: |
| 39 | if 'connection_' in kwarg: |
| 40 | connection_args.update({kwarg: kwargs[kwarg]}) |
| 41 | elif '__' not in kwarg: |
| 42 | nkwargs.update({kwarg: kwargs[kwarg]}) |
| 43 | kstone = __salt__['keystone.auth'](**connection_args) |
| 44 | token = kstone.auth_token |
| 45 | |
| 46 | if kwargs.get('connection_endpoint_type') == None: |
| 47 | endpoint_type = 'internalURL' |
| 48 | else: |
| 49 | endpoint_type = kwargs.get('connection_endpoint_type') |
| 50 | |
| 51 | endpoint = kstone.service_catalog.url_for( |
| 52 | service_type='baremetal', |
| 53 | endpoint_type=endpoint_type) |
| 54 | ironic_interface = client.get_client( |
| 55 | 1, |
| 56 | ironic_url=endpoint, os_auth_token=token) |
| 57 | return func_name(ironic_interface, *args, **nkwargs) |
| 58 | return decorator_method |
| 59 | |
| 60 | |
| 61 | @_autheticate |
| 62 | def list_nodes(ironic_interface, **kwargs): |
| 63 | ''' |
| 64 | list all ironic nodes |
| 65 | CLI Example: |
| 66 | .. code-block:: bash |
| 67 | salt '*' ironic.list_nodes |
| 68 | ''' |
| 69 | return {'nodes': [x.to_dict() for x |
| 70 | in ironic_interface.node.list(**kwargs)]} |
| 71 | |
| 72 | |
| 73 | @_autheticate |
| 74 | def create_node(ironic_interface, **kwargs): |
| 75 | ''' |
| 76 | create ironic node |
| 77 | CLI Example: |
| 78 | .. code-block:: bash |
| 79 | salt '*' ironic.create_node |
| 80 | ''' |
| 81 | return ironic_interface.node.create(**kwargs).to_dict() |
| 82 | |
| 83 | |
| 84 | @_autheticate |
| 85 | def delete_node(ironic_interface, node_id): |
| 86 | ''' |
| 87 | delete ironic node |
| 88 | |
| 89 | :param node_id: UUID or Name of the node. |
| 90 | CLI Example: |
| 91 | .. code-block:: bash |
| 92 | salt '*' ironic.delete_node |
| 93 | ''' |
| 94 | ironic_interface.node.delete(node_id) |
| 95 | |
| 96 | |
| 97 | @_autheticate |
| 98 | def show_node(ironic_interface, node_id): |
| 99 | ''' |
| 100 | show info about ironic node |
| 101 | :param node_id: UUID or Name of the node. |
| 102 | CLI Example: |
| 103 | .. code-block:: bash |
| 104 | salt '*' ironic.show_node |
| 105 | ''' |
| 106 | return ironic_interface.node.get(node_id).to_dict() |
| 107 | |
| 108 | |
| 109 | @_autheticate |
| 110 | def create_port(ironic_interface, address, node_name=None, |
| 111 | node_uuid=None, **kwargs): |
| 112 | ''' |
| 113 | create ironic port |
| 114 | CLI Example: |
| 115 | .. code-block:: bash |
| 116 | salt '*' ironic.crate_port |
| 117 | ''' |
| 118 | node_uuid = node_uuid or ironic_interface.node.get( |
| 119 | node_name).to_dict()['uuid'] |
| 120 | return ironic_interface.port.create( |
| 121 | address=address, node_uuid=node_uuid, **kwargs).to_dict() |
| 122 | |
| 123 | |
| 124 | @_autheticate |
| 125 | def list_ports(ironic_interface, **kwargs): |
| 126 | ''' |
| 127 | list all ironic ports |
| 128 | CLI Example: |
| 129 | .. code-block:: bash |
| 130 | salt '*' ironic.list_ports |
| 131 | ''' |
| 132 | |
| 133 | return {'ports': [x.to_dict() for x |
| 134 | in ironic_interface.port.list(**kwargs)]} |
| 135 | |
| 136 | |