|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +# Copyright 2021 Cloudera, Inc. All Rights Reserved. |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | + |
| 18 | +from ansible.module_utils.basic import AnsibleModule |
| 19 | +from ansible_collections.cloudera.cloud.plugins.module_utils.cdp_common import CdpModule |
| 20 | + |
| 21 | +ANSIBLE_METADATA = {'metadata_version': '1.1', |
| 22 | + 'status': ['preview'], |
| 23 | + 'supported_by': 'community'} |
| 24 | + |
| 25 | +DOCUMENTATION = r''' |
| 26 | +--- |
| 27 | +module: dw_dbc |
| 28 | +short_description: Create CDP Database Catalog |
| 29 | +description: |
| 30 | + - Create CDP Database Catalog |
| 31 | +author: |
| 32 | + - "Webster Mudge (@wmudge)" |
| 33 | + - "Dan Chaffelson (@chaffelson)" |
| 34 | +requirements: |
| 35 | + - cdpy |
| 36 | +options: |
| 37 | + cluster_id: |
| 38 | + description: ID of cluster where Database Catalog should be created. |
| 39 | + type: str |
| 40 | + required: True |
| 41 | + name: |
| 42 | + description: Name of the Database Catalog. |
| 43 | + type: str |
| 44 | + required: True |
| 45 | + load_demo_data: |
| 46 | + description: Set this to true if you demo data should be loaded into the Database Catalog. |
| 47 | + type: str |
| 48 | + required: False |
| 49 | + wait: |
| 50 | + description: |
| 51 | + - Flag to enable internal polling to wait for the Data Catalog to achieve the declared state. |
| 52 | + - If set to FALSE, the module will return immediately. |
| 53 | + type: bool |
| 54 | + required: False |
| 55 | + default: True |
| 56 | + delay: |
| 57 | + description: |
| 58 | + - The internal polling interval (in seconds) while the module waits for the Data Catalog to achieve the declared |
| 59 | + state. |
| 60 | + type: int |
| 61 | + required: False |
| 62 | + default: 15 |
| 63 | + aliases: |
| 64 | + - polling_delay |
| 65 | + timeout: |
| 66 | + description: |
| 67 | + - The internal polling timeout (in seconds) while the module waits for the Data Catalog to achieve the declared |
| 68 | + state. |
| 69 | + type: int |
| 70 | + required: False |
| 71 | + default: 3600 |
| 72 | + aliases: |
| 73 | + - polling_timeout |
| 74 | +extends_documentation_fragment: |
| 75 | + - cloudera.cloud.cdp_sdk_options |
| 76 | + - cloudera.cloud.cdp_auth_options |
| 77 | +''' |
| 78 | + |
| 79 | +EXAMPLES = r''' |
| 80 | +# Note: These examples do not set authentication details. |
| 81 | +
|
| 82 | +# Create Database Catalog |
| 83 | +- cloudera.cloud.dw_dbc: |
| 84 | + name: example-database-catalog |
| 85 | + cluster_id: example-cluster-id |
| 86 | +''' |
| 87 | + |
| 88 | +RETURN = r''' |
| 89 | +--- |
| 90 | +dbcs: |
| 91 | + description: The information about the named Database Catalog. |
| 92 | + type: list |
| 93 | + returned: always |
| 94 | + elements: complex |
| 95 | + contains: |
| 96 | + id: |
| 97 | + description: The id of the Database Catalog. |
| 98 | + returned: always |
| 99 | + type: str |
| 100 | + name: |
| 101 | + description: The name of the Database Catalog. |
| 102 | + returned: always |
| 103 | + type: str |
| 104 | + status: |
| 105 | + description: The status of the Database Catalog. |
| 106 | + returned: always |
| 107 | + type: str |
| 108 | +sdk_out: |
| 109 | + description: Returns the captured CDP SDK log. |
| 110 | + returned: when supported |
| 111 | + type: str |
| 112 | +sdk_out_lines: |
| 113 | + description: Returns a list of each line of the captured CDP SDK log. |
| 114 | + returned: when supported |
| 115 | + type: list |
| 116 | + elements: str |
| 117 | +''' |
| 118 | + |
| 119 | + |
| 120 | +class DwDbc(CdpModule): |
| 121 | + def __init__(self, module): |
| 122 | + super(DwDbc, self).__init__(module) |
| 123 | + |
| 124 | + # Set variables |
| 125 | + self.cluster_id = self._get_param('cluster_id') |
| 126 | + self.name = self._get_param('name') |
| 127 | + self.load_demo_data = self._get_param('load_demo_data') |
| 128 | + self.state = self._get_param('state') |
| 129 | + self.wait = self._get_param('wait') |
| 130 | + self.delay = self._get_param('delay') |
| 131 | + self.timeout = self._get_param('timeout') |
| 132 | + |
| 133 | + # Initialize return values |
| 134 | + self.dbcs = [] |
| 135 | + |
| 136 | + # Execute logic process |
| 137 | + self.process() |
| 138 | + |
| 139 | + @CdpModule._Decorators.process_debug |
| 140 | + def process(self): |
| 141 | + self.name = self.cdpy.dw.create_dbc(cluster_id=self.cluster_id, name=self.name, |
| 142 | + load_demo_data=self.load_demo_data) |
| 143 | + if self.wait: |
| 144 | + self.target = self.cdpy.sdk.wait_for_state( |
| 145 | + describe_func=self.cdpy.dw.describe_dbc, |
| 146 | + params=dict(cluster_id=self.cluster_id, dbc_id=self.name['dbcId']), |
| 147 | + state='Running', delay=self.delay, timeout=self.timeout |
| 148 | + ) |
| 149 | + else: |
| 150 | + self.target = self.cdpy.dw.describe_dbc(cluster_id=self.cluster_id, dbc_id=self.name['dbcId']) |
| 151 | + self.dbcs.append(self.target) |
| 152 | + |
| 153 | + |
| 154 | +def main(): |
| 155 | + module = AnsibleModule( |
| 156 | + argument_spec=CdpModule.argument_spec( |
| 157 | + cluster_id=dict(required=True, type='str', aliases=['cluster_id']), |
| 158 | + name = dict(required=True, type='str', aliases=['name']), |
| 159 | + load_demo_data=dict(required=False, type='bool', aliases=['load_demo_data']), |
| 160 | + state=dict(required=False, type='str', choices=['present', 'absent'], default='present'), |
| 161 | + wait = dict(required=False, type='bool', default=True), |
| 162 | + delay = dict(required=False, type='int', aliases=['polling_delay'], default=15), |
| 163 | + timeout = dict(required=False, type='int', aliases=['polling_timeout'], default=3600) |
| 164 | + ), |
| 165 | + supports_check_mode=True |
| 166 | + ) |
| 167 | + |
| 168 | + result = DwDbc(module) |
| 169 | + output = dict(changed=False, dbcs=result.dbcs) |
| 170 | + |
| 171 | + if result.debug: |
| 172 | + output.update(sdk_out=result.log_out, sdk_out_lines=result.log_lines) |
| 173 | + |
| 174 | + module.exit_json(**output) |
| 175 | + |
| 176 | + |
| 177 | +if __name__ == '__main__': |
| 178 | + main() |
0 commit comments