|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +# Copyright: (c) 2021, Shreyas Srish <[email protected]> |
| 5 | +# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 | + |
| 7 | +from __future__ import absolute_import, division, print_function |
| 8 | +__metaclass__ = type |
| 9 | + |
| 10 | +DOCUMENTATION = r''' |
| 11 | +--- |
| 12 | +module: aci_cloud_aws_provider |
| 13 | +short_description: Manage Cloud AWS Provider (cloud:AwsProvider) |
| 14 | +description: |
| 15 | +- Manage AWS provider on Cisco Cloud ACI. |
| 16 | +author: |
| 17 | +- Shreyas Srish (@shrsr) |
| 18 | +options: |
| 19 | + access_key_id: |
| 20 | + description: |
| 21 | + - Cloud Access Key ID. |
| 22 | + type: str |
| 23 | + account_id: |
| 24 | + description: |
| 25 | + - AWS Account ID. |
| 26 | + type: str |
| 27 | + is_account_in_org: |
| 28 | + description: |
| 29 | + - Is Account in Organization. |
| 30 | + type: bool |
| 31 | + is_trusted: |
| 32 | + description: |
| 33 | + - Trusted Tenant |
| 34 | + type: bool |
| 35 | + secret_access_key: |
| 36 | + description: |
| 37 | + - Cloud Secret Access Key. |
| 38 | + type: str |
| 39 | + tenant: |
| 40 | + description: |
| 41 | + - Name of tenant. |
| 42 | + type: str |
| 43 | + state: |
| 44 | + description: |
| 45 | + - Use C(present) or C(absent) for adding or removing. |
| 46 | + - Use C(query) for listing an object or multiple objects. |
| 47 | + type: str |
| 48 | + choices: [ absent, present, query ] |
| 49 | + default: present |
| 50 | +extends_documentation_fragment: |
| 51 | + - cisco.aci.aci |
| 52 | +
|
| 53 | +notes: |
| 54 | + - More information about the internal APIC class B(cloud:AwsProvider) from |
| 55 | + - L(the APIC Management Information Model reference,https://developer.cisco.com/docs/apic-mim-ref/). |
| 56 | +''' |
| 57 | + |
| 58 | +EXAMPLES = r''' |
| 59 | +- name: Create aws provider again after deletion as not trusted |
| 60 | + cisco.aci.aci_cloud_aws_provider: |
| 61 | + host: apic |
| 62 | + username: admin |
| 63 | + password: SomeSecretePassword |
| 64 | + tenant: ansible_test |
| 65 | + account_id: 111111111111 |
| 66 | + is_trusted: yes |
| 67 | + state: present |
| 68 | + delegate_to: localhost |
| 69 | +
|
| 70 | +- name: Delete aws provider |
| 71 | + cisco.aci.aci_cloud_aws_provider: |
| 72 | + host: apic |
| 73 | + username: admin |
| 74 | + password: SomeSecretePassword |
| 75 | + tenant: ansible_test |
| 76 | + account_id: 111111111111 |
| 77 | + is_trusted: yes |
| 78 | + state: absent |
| 79 | + delegate_to: localhost |
| 80 | +
|
| 81 | +- name: Query aws provider |
| 82 | + cisco.aci.aci_cloud_aws_provider: |
| 83 | + host: apic |
| 84 | + username: admin |
| 85 | + password: SomeSecretePassword |
| 86 | + state: query |
| 87 | + delegate_to: localhost |
| 88 | +
|
| 89 | +- name: Query all aws provider |
| 90 | + cisco.aci.aci_cloud_aws_provider: |
| 91 | + host: apic |
| 92 | + username: admin |
| 93 | + password: SomeSecretePassword |
| 94 | + state: query |
| 95 | + delegate_to: localhost |
| 96 | +''' |
| 97 | + |
| 98 | +RETURN = r''' |
| 99 | +current: |
| 100 | + description: The existing configuration from the APIC after the module has finished |
| 101 | + returned: success |
| 102 | + type: list |
| 103 | + sample: |
| 104 | + [ |
| 105 | + { |
| 106 | + "fvTenant": { |
| 107 | + "attributes": { |
| 108 | + "descr": "Production environment", |
| 109 | + "dn": "uni/tn-production", |
| 110 | + "name": "production", |
| 111 | + "nameAlias": "", |
| 112 | + "ownerKey": "", |
| 113 | + "ownerTag": "" |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + ] |
| 118 | +error: |
| 119 | + description: The error information as returned from the APIC |
| 120 | + returned: failure |
| 121 | + type: dict |
| 122 | + sample: |
| 123 | + { |
| 124 | + "code": "122", |
| 125 | + "text": "unknown managed object class foo" |
| 126 | + } |
| 127 | +raw: |
| 128 | + description: The raw output returned by the APIC REST API (xml or json) |
| 129 | + returned: parse error |
| 130 | + type: str |
| 131 | + sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 132 | +sent: |
| 133 | + description: The actual/minimal configuration pushed to the APIC |
| 134 | + returned: info |
| 135 | + type: list |
| 136 | + sample: |
| 137 | + { |
| 138 | + "fvTenant": { |
| 139 | + "attributes": { |
| 140 | + "descr": "Production environment" |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | +previous: |
| 145 | + description: The original configuration from the APIC before the module has started |
| 146 | + returned: info |
| 147 | + type: list |
| 148 | + sample: |
| 149 | + [ |
| 150 | + { |
| 151 | + "fvTenant": { |
| 152 | + "attributes": { |
| 153 | + "descr": "Production", |
| 154 | + "dn": "uni/tn-production", |
| 155 | + "name": "production", |
| 156 | + "nameAlias": "", |
| 157 | + "ownerKey": "", |
| 158 | + "ownerTag": "" |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + ] |
| 163 | +proposed: |
| 164 | + description: The assembled configuration from the user-provided parameters |
| 165 | + returned: info |
| 166 | + type: dict |
| 167 | + sample: |
| 168 | + { |
| 169 | + "fvTenant": { |
| 170 | + "attributes": { |
| 171 | + "descr": "Production environment", |
| 172 | + "name": "production" |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | +filter_string: |
| 177 | + description: The filter string used for the request |
| 178 | + returned: failure or debug |
| 179 | + type: str |
| 180 | + sample: ?rsp-prop-include=config-only |
| 181 | +method: |
| 182 | + description: The HTTP method used for the request to the APIC |
| 183 | + returned: failure or debug |
| 184 | + type: str |
| 185 | + sample: POST |
| 186 | +response: |
| 187 | + description: The HTTP response from the APIC |
| 188 | + returned: failure or debug |
| 189 | + type: str |
| 190 | + sample: OK (30 bytes) |
| 191 | +status: |
| 192 | + description: The HTTP status from the APIC |
| 193 | + returned: failure or debug |
| 194 | + type: int |
| 195 | + sample: 200 |
| 196 | +url: |
| 197 | + description: The HTTP url used for the request to the APIC |
| 198 | + returned: failure or debug |
| 199 | + type: str |
| 200 | + sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 201 | +''' |
| 202 | + |
| 203 | +from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec |
| 204 | +from ansible.module_utils.basic import AnsibleModule |
| 205 | + |
| 206 | + |
| 207 | +def main(): |
| 208 | + argument_spec = aci_argument_spec() |
| 209 | + argument_spec.update({ |
| 210 | + 'access_key_id': dict(type='str'), |
| 211 | + 'account_id': dict(type='str'), |
| 212 | + 'is_account_in_org': dict(type='bool'), |
| 213 | + 'is_trusted': dict(type='bool'), |
| 214 | + 'secret_access_key': dict(type='str'), |
| 215 | + 'tenant': dict(type='str'), |
| 216 | + 'state': dict(type='str', default='present', choices=['absent', 'present', 'query']), |
| 217 | + |
| 218 | + }) |
| 219 | + |
| 220 | + module = AnsibleModule( |
| 221 | + argument_spec=argument_spec, |
| 222 | + supports_check_mode=True, |
| 223 | + required_if=[ |
| 224 | + ['state', 'absent', ['tenant']], |
| 225 | + ['state', 'present', ['tenant']], |
| 226 | + ], |
| 227 | + ) |
| 228 | + |
| 229 | + aci = ACIModule(module) |
| 230 | + |
| 231 | + access_key_id = module.params.get('access_key_id') |
| 232 | + account_id = module.params.get('account_id') |
| 233 | + annotation = module.params.get('annotation') |
| 234 | + is_account_in_org = aci.boolean(module.params.get('is_account_in_org')) |
| 235 | + is_trusted = aci.boolean(module.params.get('is_trusted')) |
| 236 | + secret_access_key = module.params.get('secret_access_key') |
| 237 | + tenant = module.params.get('tenant') |
| 238 | + state = module.params.get('state') |
| 239 | + child_configs = [] |
| 240 | + |
| 241 | + aci.construct_url( |
| 242 | + root_class={ |
| 243 | + 'aci_class': 'fvTenant', |
| 244 | + 'aci_rn': 'tn-{0}'.format(tenant), |
| 245 | + 'target_filter': 'eq(fvTenant.name, "{0}")'.format(tenant), |
| 246 | + 'module_object': tenant |
| 247 | + }, |
| 248 | + subclass_1={ |
| 249 | + 'aci_class': 'cloudAwsProvider', |
| 250 | + 'aci_rn': 'awsprovider'.format(), |
| 251 | + 'target_filter': {'account_id': account_id}, |
| 252 | + 'module_object': account_id |
| 253 | + }, |
| 254 | + child_classes=[], |
| 255 | + ) |
| 256 | + |
| 257 | + aci.get_existing() |
| 258 | + |
| 259 | + if state == 'present': |
| 260 | + aci.payload( |
| 261 | + aci_class='cloudAwsProvider', |
| 262 | + class_config={ |
| 263 | + 'accessKeyId': access_key_id, |
| 264 | + 'accountId': account_id, |
| 265 | + 'annotation': annotation, |
| 266 | + 'isAccountInOrg': is_account_in_org, |
| 267 | + 'isTrusted': is_trusted, |
| 268 | + 'secretAccessKey': secret_access_key, |
| 269 | + }, |
| 270 | + child_configs=child_configs |
| 271 | + ) |
| 272 | + |
| 273 | + aci.get_diff(aci_class='cloudAwsProvider') |
| 274 | + |
| 275 | + aci.post_config() |
| 276 | + |
| 277 | + elif state == 'absent': |
| 278 | + aci.delete_config() |
| 279 | + |
| 280 | + aci.exit_json() |
| 281 | + |
| 282 | + |
| 283 | +if __name__ == "__main__": |
| 284 | + main() |
0 commit comments