-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref: Migrate event_list module to ListModule base (#561)
* Migrate event_list module to ListModule base * Respect number of pages in get_all_paginated(...)
- Loading branch information
1 parent
77d59ba
commit 4eca745
Showing
4 changed files
with
26 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,132 +1,32 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
|
||
"""This module allows users to list Linode events.""" | ||
"""This file contains the implementation of the event_list module.""" | ||
|
||
from __future__ import absolute_import, division, print_function | ||
|
||
from typing import Any, Dict, Optional | ||
|
||
import ansible_collections.linode.cloud.plugins.module_utils.doc_fragments.event_list 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_collections.linode.cloud.plugins.module_utils.linode_helper import ( | ||
construct_api_filter, | ||
get_all_paginated, | ||
from ansible_collections.linode.cloud.plugins.module_utils.linode_common_list import ( | ||
ListModule, | ||
) | ||
from ansible_specdoc.objects import ( | ||
FieldType, | ||
SpecDocMeta, | ||
SpecField, | ||
SpecReturnValue, | ||
) | ||
|
||
spec_filter = { | ||
"name": SpecField( | ||
type=FieldType.string, | ||
required=True, | ||
description=[ | ||
"The name of the field to filter on.", | ||
"Valid filterable attributes can be found here: " | ||
"https://techdocs.akamai.com/linode-api/reference/get-events", | ||
], | ||
), | ||
"values": SpecField( | ||
type=FieldType.list, | ||
element_type=FieldType.string, | ||
required=True, | ||
description=[ | ||
"A list of values to allow for this field.", | ||
"Fields will pass this filter if at least one of these values matches.", | ||
], | ||
), | ||
} | ||
|
||
spec = { | ||
# Disable the default values | ||
"state": SpecField(type=FieldType.string, required=False, doc_hide=True), | ||
"label": SpecField(type=FieldType.string, required=False, doc_hide=True), | ||
"order": SpecField( | ||
type=FieldType.string, | ||
description=["The order to list events in."], | ||
default="asc", | ||
choices=["desc", "asc"], | ||
), | ||
"order_by": SpecField( | ||
type=FieldType.string, description=["The attribute to order events by."] | ||
), | ||
"filters": SpecField( | ||
type=FieldType.list, | ||
element_type=FieldType.dict, | ||
suboptions=spec_filter, | ||
description=["A list of filters to apply to the resulting events."], | ||
), | ||
"count": SpecField( | ||
type=FieldType.integer, | ||
description=[ | ||
"The number of results to return.", | ||
"If undefined, all results will be returned.", | ||
], | ||
), | ||
} | ||
|
||
SPECDOC_META = SpecDocMeta( | ||
description=["List and filter on Linode events."], | ||
requirements=global_requirements, | ||
author=global_authors, | ||
options=spec, | ||
module = ListModule( | ||
result_display_name="Events", | ||
result_field_name="events", | ||
endpoint_template="/account/events", | ||
result_docs_url="https://techdocs.akamai.com/linode-api/reference/get-events", | ||
examples=docs.specdoc_examples, | ||
return_values={ | ||
"events": SpecReturnValue( | ||
description="The returned events.", | ||
docs_url="https://techdocs.akamai.com/linode-api/reference/get-events", | ||
type=FieldType.list, | ||
elements=FieldType.dict, | ||
sample=docs.result_events_samples, | ||
) | ||
}, | ||
result_samples=docs.result_events_samples, | ||
) | ||
|
||
SPECDOC_META = module.spec | ||
|
||
DOCUMENTATION = r""" | ||
""" | ||
EXAMPLES = r""" | ||
""" | ||
RETURN = r""" | ||
""" | ||
|
||
|
||
class Module(LinodeModuleBase): | ||
"""Module for getting info about a Linode events""" | ||
|
||
def __init__(self) -> None: | ||
self.module_arg_spec = SPECDOC_META.ansible_spec | ||
self.results: Dict[str, Any] = {"events": []} | ||
|
||
super().__init__(module_arg_spec=self.module_arg_spec) | ||
|
||
def exec_module(self, **kwargs: Any) -> Optional[dict]: | ||
"""Entrypoint for event list module""" | ||
|
||
filter_dict = construct_api_filter(self.module.params) | ||
|
||
self.results["events"] = get_all_paginated( | ||
self.client, | ||
"/account/events", | ||
filter_dict, | ||
num_results=self.module.params["count"], | ||
) | ||
return self.results | ||
|
||
|
||
def main() -> None: | ||
"""Constructs and calls the module""" | ||
Module() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() | ||
module.run() |