Skip to content

Commit

Permalink
profile_info (#558)
Browse files Browse the repository at this point in the history
  • Loading branch information
yec-akamai authored Aug 15, 2024
1 parent 149d3b3 commit 217b05f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 70 deletions.
2 changes: 1 addition & 1 deletion docs/modules/profile_info.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Get info about a Linode Profile.
## Return Values
- `profile` - The profile info in JSON serialized form.
- `profile` - The returned Profile.

- Sample Response:
```json
Expand Down
25 changes: 20 additions & 5 deletions plugins/module_utils/linode_common_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ class InfoModuleResult:
samples (Optional[List[str]]): A list of sample results for this field.
get (Optional[Callable]): A function to call out to the API and return the data
for this field.
NOTE: This is only relevant for secondary results.
NOTE: This is only relevant for secondary results
or primary result without any attributes.
"""

field_name: str
Expand All @@ -82,7 +83,7 @@ class InfoModuleResult:
docs_url: Optional[str] = None
samples: Optional[List[str]] = None
get: Optional[
Callable[[LinodeClient, Dict[str, Any], Dict[str, Any]], Any]
Callable[[LinodeClient, Dict[str, Any], Optional[Dict[str, Any]]], Any]
] = None


Expand Down Expand Up @@ -138,6 +139,16 @@ def exec_module(self, **kwargs: Any) -> Optional[dict]:
)
break

if primary_result is None:
# Get the primary result using the result get function
try:
primary_result = self.primary_result.get(self.client, kwargs)
except Exception as exception:
self.fail(
msg="Failed to get result for "
f"{self.primary_result.display_name}: {exception}"
)

if primary_result is None:
raise ValueError("Expected a result; got None")

Expand Down Expand Up @@ -220,10 +231,14 @@ def run(self) -> None:
"""
Initializes and runs the info module.
"""
attribute_names = [v.name for v in self.attributes]
attribute_names = (
[[v.name for v in self.attributes]]
if len(self.attributes) > 0
else None
)

super().__init__(
module_arg_spec=self.module_arg_spec,
required_one_of=[attribute_names],
mutually_exclusive=[attribute_names],
required_one_of=attribute_names,
mutually_exclusive=attribute_names,
)
81 changes: 17 additions & 64 deletions plugins/modules/profile_info.py
Original file line number Diff line number Diff line change
@@ -1,84 +1,37 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

"""This module allows users to retrieve information about the current Linode profile."""
"""This module contains all of the functionality for Linode Profile info."""

from __future__ import absolute_import, division, print_function

from typing import Any, List, Optional

import ansible_collections.linode.cloud.plugins.module_utils.doc_fragments.profile_info as docs
from ansible_collections.linode.cloud.plugins.module_utils.linode_common import (
LinodeModuleBase,
)
from ansible_collections.linode.cloud.plugins.module_utils.linode_docs import (
global_authors,
global_requirements,
)
from ansible_specdoc.objects import (
FieldType,
SpecDocMeta,
SpecField,
SpecReturnValue,
from ansible_collections.linode.cloud.plugins.module_utils.linode_common_info import (
InfoModule,
InfoModuleResult,
)
from ansible_specdoc.objects import FieldType

spec = {
# Disable the default values
"label": SpecField(type=FieldType.string, required=False, doc_hide=True),
"state": SpecField(type=FieldType.string, required=False, doc_hide=True),
}


SPECDOC_META = SpecDocMeta(
description=["Get info about a Linode Profile."],
requirements=global_requirements,
author=global_authors,
options=spec,
module = InfoModule(
examples=docs.specdoc_examples,
return_values={
"profile": SpecReturnValue(
description="The profile info in JSON serialized form.",
docs_url="https://techdocs.akamai.com/linode-api/reference/get-profile",
type=FieldType.dict,
sample=docs.result_profile_samples,
)
},
primary_result=InfoModuleResult(
display_name="Profile",
field_name="profile",
field_type=FieldType.dict,
docs_url="https://techdocs.akamai.com/linode-api/reference/get-profile",
samples=docs.result_profile_samples,
get=lambda client, params: client.profile()._raw_json,
),
)

SPECDOC_META = module.spec

DOCUMENTATION = r"""
"""
EXAMPLES = r"""
"""
RETURN = r"""
"""


class Module(LinodeModuleBase):
"""Module for getting info about a Linode Profile"""

def __init__(self) -> None:
self.required_one_of: List[str] = []
self.results = {"profile": None}

self.module_arg_spec = SPECDOC_META.ansible_spec

super().__init__(
module_arg_spec=self.module_arg_spec,
required_one_of=self.required_one_of,
)

def exec_module(self, **kwargs: Any) -> Optional[dict]:
"""Entrypoint for volume info module"""

self.results["profile"] = self.client.profile()._raw_json

return self.results


def main() -> None:
"""Constructs and calls the profile_info module"""
Module()


if __name__ == "__main__":
main()
module.run()

0 comments on commit 217b05f

Please sign in to comment.