| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 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 = r''' |
| 14 |
--- |
| 15 |
module: intersight_rest_api |
| 16 |
short_description: REST API configuration for Cisco Intersight |
| 17 |
description: |
| 18 |
- Direct REST API configuration for Cisco Intersight. |
| 19 |
- All REST API resources and properties must be specified. |
| 20 |
- For more information see L(Cisco Intersight,https://intersight.com/apidocs). |
| 21 |
extends_documentation_fragment: intersight |
| 22 |
options: |
| 23 |
resource_path: |
| 24 |
description: |
| 25 |
- Resource URI being configured related to api_uri. |
| 26 |
type: str |
| 27 |
required: yes |
| 28 |
query_params: |
| 29 |
description: |
| 30 |
- Query parameters for the Intersight API query languange. |
| 31 |
type: dict |
| 32 |
update_method: |
| 33 |
description: |
| 34 |
- The HTTP method used for update operations. |
| 35 |
- Some Intersight resources require POST operations for modifications. |
| 36 |
- json-patch is used for partial updates. |
| 37 |
- json-patch is only supported for patch operations on existing resources and requires the list_body to be a list of dictionaries. |
| 38 |
- See L(The Intersight API Docs, https://intersight.com/apidocs/introduction/methods/) for details on JSON Patch. |
| 39 |
type: str |
| 40 |
choices: [ patch, post, json-patch ] |
| 41 |
default: patch |
| 42 |
api_body: |
| 43 |
description: |
| 44 |
- The paylod for API requests used to modify resources. |
| 45 |
type: dict |
| 46 |
list_body: |
| 47 |
description: |
| 48 |
- The paylod for API requests used to modify resources. |
| 49 |
- Should be used instead of api_body if a list is required in the API payload. |
| 50 |
type: list |
| 51 |
elements: dict |
| 52 |
return_list: |
| 53 |
description: |
| 54 |
- If C(yes), will return a list of API results in the api_response. |
| 55 |
- By default only the 1st element of the API Results list is returned. |
| 56 |
- Can only be used with GET operations. |
| 57 |
type: bool |
| 58 |
default: no |
| 59 |
state: |
| 60 |
description: |
| 61 |
- If C(present), will verify the resource is present and will create if needed. |
| 62 |
- If C(absent), will verify the resource is absent and will delete if needed. |
| 63 |
type: str |
| 64 |
choices: [present, absent] |
| 65 |
default: present |
| 66 |
author: |
| 67 |
- David Soper (@dsoper2) |
| 68 |
- CiscoUcs (@CiscoUcs) |
| 69 |
''' |
| 70 |
|
| 71 |
EXAMPLES = r''' |
| 72 |
- name: Configure Boot Policy |
| 73 |
intersight_rest_api: |
| 74 |
api_private_key: "{{ api_private_key }}" |
| 75 |
api_key_id: "{{ api_key_id }}" |
| 76 |
api_key_uri: "{{ api_key_uri }}" |
| 77 |
validate_certs: "{{ validate_certs }}" |
| 78 |
resource_path: /boot/PrecisionPolicies |
| 79 |
query_params: |
| 80 |
$filter: "Name eq 'vmedia-localdisk'" |
| 81 |
api_body: { |
| 82 |
"Name": "vmedia-localdisk", |
| 83 |
"ConfiguredBootMode": "Legacy", |
| 84 |
"BootDevices": [ |
| 85 |
{ |
| 86 |
"ObjectType": "boot.VirtualMedia", |
| 87 |
"Enabled": true, |
| 88 |
"Name": "remote-vmedia", |
| 89 |
"Subtype": "cimc-mapped-dvd" |
| 90 |
}, |
| 91 |
{ |
| 92 |
"ObjectType": "boot.LocalDisk", |
| 93 |
"Enabled": true, |
| 94 |
"Name": "localdisk", |
| 95 |
"Slot": "MRAID", |
| 96 |
"Bootloader": null |
| 97 |
} |
| 98 |
], |
| 99 |
} |
| 100 |
state: present |
| 101 |
|
| 102 |
- name: Delete Boot Policy |
| 103 |
intersight_rest_api: |
| 104 |
api_private_key: "{{ api_private_key }}" |
| 105 |
api_key_id: "{{ api_key_id }}" |
| 106 |
api_key_uri: "{{ api_key_uri }}" |
| 107 |
validate_certs: "{{ validate_certs }}" |
| 108 |
resource_path: /boot/PrecisionPolicies |
| 109 |
query_params: |
| 110 |
$filter: "Name eq 'vmedia-localdisk'" |
| 111 |
state: absent |
| 112 |
''' |
| 113 |
|
| 114 |
RETURN = r''' |
| 115 |
api_response: |
| 116 |
description: The API response output returned by the specified resource. |
| 117 |
returned: always |
| 118 |
type: dict |
| 119 |
sample: |
| 120 |
"api_response": { |
| 121 |
"BootDevices": [ |
| 122 |
{ |
| 123 |
"Enabled": true, |
| 124 |
"Name": "remote-vmedia", |
| 125 |
"ObjectType": "boot.VirtualMedia", |
| 126 |
"Subtype": "cimc-mapped-dvd" |
| 127 |
}, |
| 128 |
{ |
| 129 |
"Bootloader": null, |
| 130 |
"Enabled": true, |
| 131 |
"Name": "boot-lun", |
| 132 |
"ObjectType": "boot.LocalDisk", |
| 133 |
"Slot": "MRAID" |
| 134 |
} |
| 135 |
], |
| 136 |
"ConfiguredBootMode": "Legacy", |
| 137 |
"Name": "vmedia-localdisk", |
| 138 |
"ObjectType": "boot.PrecisionPolicy", |
| 139 |
} |
| 140 |
''' |
| 141 |
|
| 142 |
from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec, compare_values |
| 143 |
from ansible.module_utils.basic import AnsibleModule |
| 144 |
|
| 145 |
|
| 146 |
def main(): |
| 147 |
argument_spec = intersight_argument_spec.copy() |
| 148 |
argument_spec.update( |
| 149 |
resource_path=dict(type='str', required=True), |
| 150 |
query_params=dict(type='dict'), |
| 151 |
update_method=dict(type='str', choices=['patch', 'post', 'json-patch'], default='patch'), |
| 152 |
api_body=dict(type='dict'), |
| 153 |
list_body=dict(type='list', elements='dict'), |
| 154 |
return_list=dict(type='bool', default=False), |
| 155 |
state=dict(type='str', choices=['absent', 'present'], default='present'), |
| 156 |
) |
| 157 |
|
| 158 |
module = AnsibleModule( |
| 159 |
argument_spec, |
| 160 |
supports_check_mode=True, |
| 161 |
mutually_exclusive=[ |
| 162 |
['return_list', 'api_body'], |
| 163 |
['return_list', 'state'], |
| 164 |
['api_body', 'list_body'], |
| 165 |
], |
| 166 |
) |
| 167 |
|
| 168 |
intersight = IntersightModule(module) |
| 169 |
intersight.result['api_response'] = {} |
| 170 |
intersight.result['trace_id'] = '' |
| 171 |
|
| 172 |
if module.params['list_body']: |
| 173 |
module.params['api_body'] = module.params['list_body'] |
| 174 |
|
| 175 |
# determine requested operation (config, delete, or neither (get resource only)) |
| 176 |
if module.params['state'] == 'present': |
| 177 |
request_delete = False |
| 178 |
# api_body implies resource configuration through post/patch |
| 179 |
# no api_body implies a get operation |
| 180 |
request_config = bool(module.params['api_body']) |
| 181 |
if request_config and not module.params['query_params']: |
| 182 |
# no query_params will try to create the resource without getting the current state |
| 183 |
get_resource = False |
| 184 |
else: |
| 185 |
get_resource = True |
| 186 |
else: # state == 'absent' |
| 187 |
# state == 'absent' with no query_params is not permitted to avoid accidental deletion |
| 188 |
if not module.params['query_params']: |
| 189 |
raise ValueError('Please specify query_params when state is absent') |
| 190 |
request_delete = True |
| 191 |
request_config = False |
| 192 |
get_resource = True |
| 193 |
|
| 194 |
if get_resource: |
| 195 |
# get the current state of the resource |
| 196 |
intersight.get_resource( |
| 197 |
resource_path=module.params['resource_path'], |
| 198 |
query_params=module.params['query_params'], |
| 199 |
return_list=module.params['return_list'], |
| 200 |
) |
| 201 |
|
| 202 |
moid = None |
| 203 |
resource_values_match = False |
| 204 |
if (request_config or request_delete) and intersight.result['api_response'].get('Moid'): |
| 205 |
# resource exists and moid was returned |
| 206 |
moid = intersight.result['api_response']['Moid'] |
| 207 |
if request_config: |
| 208 |
resource_values_match = compare_values(module.params['api_body'], intersight.result['api_response']) |
| 209 |
else: # request_delete |
| 210 |
intersight.delete_resource( |
| 211 |
moid=moid, |
| 212 |
resource_path=module.params['resource_path'], |
| 213 |
) |
| 214 |
|
| 215 |
if request_config and not resource_values_match: |
| 216 |
intersight.configure_resource( |
| 217 |
moid=moid, |
| 218 |
resource_path=module.params['resource_path'], |
| 219 |
body=module.params['api_body'], |
| 220 |
query_params=module.params['query_params'], |
| 221 |
update_method=module.params['update_method'], |
| 222 |
) |
| 223 |
if module.params['return_list'] and not isinstance(intersight.result['api_response'], list): |
| 224 |
intersight.result['api_response'] = [] |
| 225 |
|
| 226 |
module.exit_json(**intersight.result) |
| 227 |
|
| 228 |
|
| 229 |
if __name__ == '__main__': |
| 230 |
main() |