|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# Copyright 2025 Cloudera, Inc. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +from __future__ import absolute_import, division, print_function |
| 18 | + |
| 19 | +__metaclass__ = type |
| 20 | + |
| 21 | +DOCUMENTATION = """ |
| 22 | + lookup: cai_workspace |
| 23 | + author: Ronald Suplina (@rsuplina) <rsuplina@cloudera.com> |
| 24 | + short_description: Get the API URL for Cloudera AI Workspace in Cloudera on cloud. |
| 25 | + description: |
| 26 | + - Allows you to retrieve the API URL for a given Cloudera AI Workspace in Cloudera on cloud. |
| 27 | + - If the Environment is not found, the lookup will return an empty list. |
| 28 | + - If the workspace is not found in the specified Environment, the lookup will return the C(default) value. |
| 29 | + version_added: "3.2.0" |
| 30 | + options: |
| 31 | + _terms: |
| 32 | + description: |
| 33 | + - The name of the Cloudera AI Workspace or list of workspace names to query. |
| 34 | + - Returns the API endpoint URL for each workspace. |
| 35 | + required: True |
| 36 | + environment: |
| 37 | + description: Name of the Cloudera on cloud Environment where the workspace is deployed |
| 38 | + type: string |
| 39 | + required: True |
| 40 | + aliases: |
| 41 | + - env |
| 42 | + default: |
| 43 | + description: What to return when the workspace is not found |
| 44 | + type: raw |
| 45 | + default: [] |
| 46 | + notes: |
| 47 | + - You can pass the C(Undefined) object as C(default) to force an undefined error. |
| 48 | + - Requires C(cdpy). |
| 49 | +""" |
| 50 | + |
| 51 | +EXAMPLES = """ |
| 52 | +- name: Retrieve the API URL for a Cloudera AI Workspace |
| 53 | + ansible.builtin.debug: |
| 54 | + msg: "{{ lookup('cloudera.cloud.cai_workspace', 'my-workspace', env='example-env') }}" |
| 55 | +
|
| 56 | +- name: Use the workspace API URL in a task |
| 57 | + ansible.builtin.set_fact: |
| 58 | + cai_api: "{{ lookup('cloudera.cloud.cai_workspace', ml_workspace, env=cdp_env) }}" |
| 59 | +
|
| 60 | +- name: Return a default value if workspace does not exist |
| 61 | + ansible.builtin.debug: |
| 62 | + msg: "{{ lookup('cloudera.cloud.cai_workspace', 'my-workspace', env='example-env', default='http://default') }}" |
| 63 | +
|
| 64 | +- name: Retrieve API URLs for multiple workspaces |
| 65 | + ansible.builtin.debug: |
| 66 | + msg: "{{ query('cloudera.cloud.cai_workspace', 'workspace1', 'workspace2', env='example-env') }}" |
| 67 | +
|
| 68 | +- name: Retrieve multiple workspaces specified as a list |
| 69 | + ansible.builtin.debug: |
| 70 | + msg: "{{ query('cloudera.cloud.cai_workspace', ['workspace1', 'workspace2'], env='example-env') }}" |
| 71 | +""" |
| 72 | + |
| 73 | +RETURN = """ |
| 74 | + _list: |
| 75 | + description: List of API URLs for the queried Cloudera AI Workspaces |
| 76 | + type: list |
| 77 | + elements: str |
| 78 | +""" |
| 79 | + |
| 80 | +from ansible.errors import AnsibleError |
| 81 | +from ansible.plugins.lookup import LookupBase |
| 82 | +from ansible.module_utils.common.text.converters import to_native |
| 83 | + |
| 84 | +from cdpy.cdpy import Cdpy |
| 85 | +from cdpy.common import CdpError |
| 86 | + |
| 87 | + |
| 88 | +class LookupModule(LookupBase): |
| 89 | + def run(self, terms, variables=None, **kwargs): |
| 90 | + self.set_options(var_options=variables, direct=kwargs) |
| 91 | + |
| 92 | + if not terms: |
| 93 | + raise AnsibleError( |
| 94 | + "cai_workspace lookup requires at least one workspace name", |
| 95 | + ) |
| 96 | + |
| 97 | + env = self.get_option("environment") |
| 98 | + default = self.get_option("default") |
| 99 | + |
| 100 | + if not env: |
| 101 | + raise AnsibleError("cai_workspace lookup requires 'environment' parameter") |
| 102 | + |
| 103 | + results = [] |
| 104 | + |
| 105 | + try: |
| 106 | + cdpy = Cdpy() |
| 107 | + |
| 108 | + for term in LookupBase._flatten(terms): |
| 109 | + workspace_name = term |
| 110 | + |
| 111 | + workspace = cdpy.ml.describe_workspace(name=workspace_name, env=env) |
| 112 | + |
| 113 | + if workspace is None: |
| 114 | + if isinstance(default, list): |
| 115 | + results.extend(default) |
| 116 | + else: |
| 117 | + results.append(default) |
| 118 | + else: |
| 119 | + instance_url = workspace.get("instanceUrl") |
| 120 | + if instance_url: |
| 121 | + results.append(instance_url) |
| 122 | + else: |
| 123 | + if isinstance(default, list): |
| 124 | + results.extend(default) |
| 125 | + else: |
| 126 | + results.append(default) |
| 127 | + |
| 128 | + return results |
| 129 | + |
| 130 | + except CdpError as e: |
| 131 | + raise AnsibleError( |
| 132 | + "Error retrieving CAI workspace in environment '%s': %s" |
| 133 | + % (env, to_native(e)), |
| 134 | + ) |
0 commit comments