Skip to content

Commit

Permalink
Add required updates
Browse files Browse the repository at this point in the history
Signed-off-by: rsuplina <[email protected]>
  • Loading branch information
rsuplina committed Jul 19, 2024
1 parent ad58700 commit 9694b40
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 61 deletions.
64 changes: 28 additions & 36 deletions plugins/modules/data_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from ansible_collections.cloudera.cluster.plugins.module_utils.cm_utils import (
ClouderaManagerMutableModule,
)
from cm_client import DataContextsResourceApi, ApiDataContextRef, ApiDataContextList
from cm_client import DataContextsResourceApi, ApiDataContextList

from cm_client import (
ClustersResourceApi,
Expand Down Expand Up @@ -65,7 +65,7 @@
state:
description:
- If I(state=present), the data context will be created or updated.
- If I(state=absent), the data context will be deleted deleted.
- If I(state=absent), the data context will be deleted
type: str
required: no
default: present
Expand Down Expand Up @@ -219,30 +219,28 @@ def process(self):

if self.state == "present":
if existing:
existing = {service["service_name"] for service in existing["services"]}
incoming = set(self.services)

if self.module._diff:
self.diff.update(
before=list(existing - incoming),
after=list(incoming - existing),
)
if existing != incoming:
existing_service = {
service["service_name"] for service in existing["services"]
}
incoming_service = set(self.services)
if existing_service != incoming_service:
if self.module._diff:
self.diff.update(
before=list(existing_service - incoming_service),
after=list(incoming_service - existing_service),
)
services = [
{"serviceName": service, "clusterName": self.cluster_name}
for service in incoming
for service in incoming_service
]
body = ApiDataContext(name=self.data_contex_name, services=services)
if not self.module.check_mode:
update_data_context = data_context_api.update_data_context(
body=body
body=ApiDataContext(
name=self.data_contex_name, services=services
)
).to_dict()

data_context_unparsed = ApiDataContextList(
items=[update_data_context]
)
self.data_context_output = parse_data_context_result(
data_context_unparsed
ApiDataContextList(items=[update_data_context])
)
self.changed = True
else:
Expand All @@ -252,49 +250,43 @@ def process(self):
{"serviceName": service, "clusterName": self.cluster_name}
for service in self.services
]
body = ApiDataContext(name=self.data_contex_name, services=services)
if not self.module.check_mode:
create_data_context = data_context_api.create_data_context(
body=body
body=ApiDataContext(
name=self.data_contex_name, services=services
)
).to_dict()

data_context_unparsed = ApiDataContextList(
items=[create_data_context]
)
self.data_context_output = parse_data_context_result(
data_context_unparsed
ApiDataContextList(items=[create_data_context])
)
self.changed = True

if self.state == "absent":
if existing:
if not self.module.check_mode:
remove_data_context = data_context_api.delete_data_context(
data_context_api.delete_data_context(
data_context_name=self.data_contex_name
).to_dict()

data_context_unparsed = ApiDataContextList(
items=[remove_data_context]
)
self.data_context_output = parse_data_context_result(
data_context_unparsed
)
self.changed = True


def main():
module = ClouderaManagerMutableModule.ansible_module(
argument_spec=dict(
name=dict(required=False, type="str"),
cluster=dict(required=True, type="str", aliases=["cluster_name"]),
services=dict(required=True, type="list"),
name=dict(required=True, type="str"),
cluster=dict(required=False, type="str", aliases=["cluster_name"]),
services=dict(required=False, type="list"),
state=dict(
type="str",
default="present",
choices=["present", "absent"],
),
),
supports_check_mode=True,
required_if=[
("state", "present", ("cluster", "services"), False),
],
)
result = ClouderaDataContext(module)

Expand Down
43 changes: 18 additions & 25 deletions plugins/modules/data_context_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def __init__(self, module):
super(ClouderaDataContextInfo, self).__init__(module)

# Set the parameters
self.data_contex_name = self.get_param("name")
self.data_context_name = self.get_param("name")

# Initialize the return value
self.data_context_info = []
Expand All @@ -145,34 +145,27 @@ def __init__(self, module):
@ClouderaManagerMutableModule.handle_process
def process(self):
data_context_api = DataContextsResourceApi(self.api_client)
try:
if self.data_contex_name:
if self.data_context_name:
try:
data_contex = data_context_api.read_data_context(
data_context_name=self.data_contex_name
data_context_name=self.data_context_name
).to_dict()

data_context_unparsed = ApiDataContextList(items=[data_contex])
self.data_context_info = parse_data_context_result(
data_context_unparsed
)

else:
data_contexts_info = data_context_api.read_data_contexts().to_dict()

data_context_unparsed = ApiDataContextList(
items=data_contexts_info.get("items", [])
)
self.data_context_info = parse_data_context_result(
data_context_unparsed
)

except ApiException as ex:
if ex.status == 500:
self.module.fail_json(
msg="Data Contex does not exist: " + self.data_contex_name
ApiDataContextList(items=[data_contex])
)
else:
raise ex
except ApiException as ex:
if ex.status == 500:
self.module.fail_json(
msg="Data Context does not exist: " + self.data_context_name
)
else:
raise ex
else:
data_contexts_info = data_context_api.read_data_contexts().to_dict()

self.data_context_info = parse_data_context_result(
ApiDataContextList(items=data_contexts_info.get("items", []))
)


def main():
Expand Down

0 comments on commit 9694b40

Please sign in to comment.