|  | 
|  | 1 | +# Copyright 2023 Cloudera, Inc. | 
|  | 2 | +# | 
|  | 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 4 | +# you may not use this file except in compliance with the License. | 
|  | 5 | +# You may obtain a copy of the License at | 
|  | 6 | +# | 
|  | 7 | +#      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 8 | +# | 
|  | 9 | +# Unless required by applicable law or agreed to in writing, software | 
|  | 10 | +# distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 12 | +# See the License for the specific language governing permissions and | 
|  | 13 | +# limitations under the License. | 
|  | 14 | + | 
|  | 15 | +from __future__ import (absolute_import, division, print_function) | 
|  | 16 | +__metaclass__ = type | 
|  | 17 | + | 
|  | 18 | +DOCUMENTATION = ''' | 
|  | 19 | +    lookup: datalake_service | 
|  | 20 | +    author: Webster Mudge (@wmudge) <wmudge@cloudera.com> | 
|  | 21 | +    short_description: Get the URL for a CDP Public Cloud Datalake service | 
|  | 22 | +    description: | 
|  | 23 | +        - Allows you to retrieve the URL for a given CDP Public Cloud Datalake service. | 
|  | 24 | +        - If no service name (or optionally Knox service name) is found on the specified Datalake, the lookup returns the value of I(default). | 
|  | 25 | +        - Otherwise, the lookup entry will be an empty list. | 
|  | 26 | +        - If the Datalake is not found or is ambigious, the lookup will return an error. | 
|  | 27 | +    options: | 
|  | 28 | +        _terms: | 
|  | 29 | +            description: | 
|  | 30 | +                - An endpoint C(serviceName) or list of them to lookup within the Datalake. | 
|  | 31 | +                - If I(knox_service=True), then these values will lookup against the endpoint C(knoxService). | 
|  | 32 | +            required: True | 
|  | 33 | +            sample: | 
|  | 34 | +                - CM-API | 
|  | 35 | +                - CM-UI | 
|  | 36 | +                - ATLAS_SERVER | 
|  | 37 | +                - RANGER_ADMIN | 
|  | 38 | +        environment: | 
|  | 39 | +            description: Name of the Environment of the Datalake to query | 
|  | 40 | +            type: string | 
|  | 41 | +        datalake: | 
|  | 42 | +            description: Name of the Datalake to query | 
|  | 43 | +            type: string | 
|  | 44 | +        default: | 
|  | 45 | +            description: What return when the service name is not found on the Datahub | 
|  | 46 | +            type: raw | 
|  | 47 | +            default: [] | 
|  | 48 | +        knox_service: | 
|  | 49 | +            description: Whether the terms are C(serviceName) or C(knoxService) values. | 
|  | 50 | +            type: boolean | 
|  | 51 | +            default: False | 
|  | 52 | +    notes: | 
|  | 53 | +        - You can pass the C(Undefined) object as C(default) to force an undefined error. | 
|  | 54 | +        - Requires C(cdpy). | 
|  | 55 | +''' | 
|  | 56 | + | 
|  | 57 | +EXAMPLES = ''' | 
|  | 58 | +- name: Retrieve the details for the Ranger Admin service, via Environment reference | 
|  | 59 | +  ansible.builtin.debug: | 
|  | 60 | +    msg: "{{ lookup('cloudera.cloud.datalake_service', 'RANGER_ADMIN', environment='example-env', wantlist=True) }}" | 
|  | 61 | +     | 
|  | 62 | +- name: Retrieve the details for the Ranger Admin service, via explicit Datalake reference | 
|  | 63 | +  ansible.builtin.debug: | 
|  | 64 | +    msg: "{{ lookup('cloudera.cloud.datalake_service', 'RANGER_ADMIN', datalake='example-dl', wantlist=True) }}" | 
|  | 65 | +     | 
|  | 66 | +- name: Return a generated list if the service does not exist | 
|  | 67 | +  ansible.builtin.debug: | 
|  | 68 | +    msg: "{{ query('cloudera.cloud.datalake_service', 'NO_SERVICE', environment='example-env', default=['something', 'else']) }}" | 
|  | 69 | +     | 
|  | 70 | +- name: Return multiple services from the same Datalake | 
|  | 71 | +  ansible.builtin.debug: | 
|  | 72 | +    msg: "{{ query('cloudera.cloud.datalake_service', 'RANGER_ADMIN', 'ATLAS_SERVER', 'CM-API', environment='example-env') }}" | 
|  | 73 | +     | 
|  | 74 | +- name: Return multiple services, specified as a list | 
|  | 75 | +  ansible.builtin.debug: | 
|  | 76 | +    msg: "{{ query('cloudera.cloud.datalake_service', ['RANGER_ADMIN', 'ATLAS_SERVER', 'CM-API'], environment='example-env') }}" | 
|  | 77 | +     | 
|  | 78 | +- name: Look up via Knox service | 
|  | 79 | +  ansible.builtin.debug: | 
|  | 80 | +    msg: "{{ query('cloudera.cloud.datalake_service', 'ATLAS_API', environment='example-env', knox_service=True) }}" | 
|  | 81 | +''' | 
|  | 82 | + | 
|  | 83 | +RETURN = ''' | 
|  | 84 | +  _list: | 
|  | 85 | +    description: List of lists of service URLs | 
|  | 86 | +    type: list | 
|  | 87 | +    elements: list | 
|  | 88 | +''' | 
|  | 89 | + | 
|  | 90 | +from ansible.errors import AnsibleError | 
|  | 91 | +from ansible.module_utils.common.text.converters import to_native | 
|  | 92 | + | 
|  | 93 | +from cdpy.cdpy import Cdpy | 
|  | 94 | +from cdpy.common import CdpError | 
|  | 95 | + | 
|  | 96 | +from ansible_collections.cloudera.cloud.plugins.lookup.cdp_service import CdpServiceLookupModule | 
|  | 97 | + | 
|  | 98 | + | 
|  | 99 | +class LookupModule(CdpServiceLookupModule): | 
|  | 100 | +    def run(self, terms, variables=None, **kwargs): | 
|  | 101 | +        self.set_options(var_options=variables, direct=kwargs) | 
|  | 102 | + | 
|  | 103 | +        if not self.get_option('datalake') and not self.get_option('environment'): | 
|  | 104 | +            raise AnsibleError("One of 'environment' or 'datalake' parameters must be present") | 
|  | 105 | + | 
|  | 106 | +        try: | 
|  | 107 | +            dl = None | 
|  | 108 | +            if self.get_option('datalake'): | 
|  | 109 | +                dl = Cdpy().datalake.describe_datalake(self.get_option('datalake')) | 
|  | 110 | +                if dl is None: | 
|  | 111 | +                    raise AnsibleError("No Datalake found for '%s'" % self.get_option('datalake')) | 
|  | 112 | +            else: | 
|  | 113 | +                env = Cdpy().datalake.describe_all_datalakes(self.get_option('environment'))                 | 
|  | 114 | +                if not env: | 
|  | 115 | +                    raise AnsibleError("No Environment found for '%s'" % self.get_option('environment')) | 
|  | 116 | +                elif len(env) > 1: | 
|  | 117 | +                    raise AnsibleError("Multiple Datalakes found for Enviroment '%s'" % self.get_option('environment')) | 
|  | 118 | +                dl = env[0] | 
|  | 119 | +            return self.parse_services(terms, dl['datalakeName'], dl, 'datalake') | 
|  | 120 | +        except CdpError as e: | 
|  | 121 | +            raise AnsibleError("Error connecting to CDP: %s" % to_native(e)) | 
0 commit comments