-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/mp init blank template (#229)
* Add template selector * Add Blank template adapter
- Loading branch information
Showing
6 changed files
with
252 additions
and
27 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
173 changes: 173 additions & 0 deletions
173
vmware_aria_operations_integration_sdk/adapter_template/new_adapter_template.py
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 |
---|---|---|
@@ -0,0 +1,173 @@ | ||
# Copyright 2022-2023 VMware, Inc. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
import sys | ||
from typing import List | ||
|
||
import aria.ops.adapter_logging as logging | ||
from aria.ops.adapter_instance import AdapterInstance | ||
from aria.ops.definition.adapter_definition import AdapterDefinition | ||
from aria.ops.result import CollectResult | ||
from aria.ops.result import EndpointResult | ||
from aria.ops.result import TestResult | ||
from aria.ops.timer import Timer | ||
from constants import ADAPTER_KIND | ||
from constants import ADAPTER_NAME | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def get_adapter_definition() -> AdapterDefinition: | ||
""" | ||
The adapter definition defines the object types and attribute types (metric/property) that are present | ||
in a collection. Setting these object types and attribute types helps VMware Aria Operations to | ||
validate, process, and display the data correctly. | ||
:return: AdapterDefinition | ||
""" | ||
with Timer(logger, "Get Adapter Definition"): | ||
definition = AdapterDefinition(ADAPTER_KIND, ADAPTER_NAME) | ||
|
||
# TODO: Add parameters and credentials | ||
|
||
# The key 'container_memory_limit' is a special key read by the VMware Aria Operations | ||
# collector to determine how much memory to allocate to the docker container running | ||
# this adapter. It does not need to be read inside the adapter code. However, removing | ||
# the definition from the object model will remove the ability to change the container | ||
# memory limit during the adapter's configuration, and the VMware Aria Operations collector | ||
# will give 1024 MB of memory to the container running the adapter instance. | ||
definition.define_int_parameter( | ||
"container_memory_limit", | ||
label="Adapter Memory Limit (MB)", | ||
description="Sets the maximum amount of memory VMware Aria Operations can " | ||
"allocate to the container running this adapter instance.", | ||
required=True, | ||
advanced=True, | ||
default=1024, | ||
) | ||
|
||
# TODO: Add object types, including identifiers, metrics, and properties | ||
|
||
logger.debug(f"Returning adapter definition: {definition.to_json()}") | ||
return definition | ||
|
||
|
||
def test(adapter_instance: AdapterInstance) -> TestResult: | ||
with Timer(logger, "Test"): | ||
result = TestResult() | ||
try: | ||
# A typical test connection will generally consist of: | ||
# 1. Read identifier values from adapter_instance that are required to | ||
# connect to the target(s) | ||
# 2. Connect to the target(s), and retrieve some sample data | ||
# 3. Disconnect cleanly from the target (ensure this happens even if an | ||
# error occurs) | ||
# 4. If any of the above failed, return an error, otherwise pass. | ||
|
||
# TODO: Add connection testing logic | ||
pass # TODO: Remove pass statement | ||
|
||
except Exception as e: | ||
logger.error("Unexpected connection test error") | ||
logger.exception(e) | ||
result.with_error("Unexpected connection test error: " + repr(e)) | ||
finally: | ||
# TODO: If any connections are still open, make sure they are closed before returning | ||
logger.debug(f"Returning test result: {result.get_json()}") | ||
return result | ||
|
||
|
||
def collect(adapter_instance: AdapterInstance) -> CollectResult: | ||
with Timer(logger, "Collection"): | ||
result = CollectResult() | ||
try: | ||
# A typical collection will generally consist of: | ||
# 1. Read identifier values from adapter_instance that are required to | ||
# connect to the target(s) | ||
# 2. Connect to the target(s), and retrieve data | ||
# 3. Add the data into a CollectResult's objects, properties, metrics, etc | ||
# 4. Disconnect cleanly from the target (ensure this happens even if an | ||
# error occurs) | ||
# 5. Return the CollectResult. | ||
|
||
# TODO: Add collection logic | ||
pass # TODO: Remove pass statement | ||
|
||
except Exception as e: | ||
logger.error("Unexpected collection error") | ||
logger.exception(e) | ||
result.with_error("Unexpected collection error: " + repr(e)) | ||
finally: | ||
# TODO: If any connections are still open, make sure they are closed before returning | ||
logger.debug(f"Returning collection result {result.get_json()}") | ||
return result | ||
|
||
|
||
def get_endpoints(adapter_instance: AdapterInstance) -> EndpointResult: | ||
with Timer(logger, "Get Endpoints"): | ||
result = EndpointResult() | ||
# In the case that an SSL Certificate is needed to communicate to the target, | ||
# add each URL that the adapter uses here. Often this will be derived from a | ||
# 'host' parameter in the adapter instance. In this Adapter we don't use any | ||
# HTTPS connections, so we won't add any. If we did, we might do something like | ||
# this: | ||
# result.with_endpoint(adapter_instance.get_identifier_value("host")) | ||
# | ||
# Multiple endpoints can be returned, like this: | ||
# result.with_endpoint(adapter_instance.get_identifier_value("primary_host")) | ||
# result.with_endpoint(adapter_instance.get_identifier_value("secondary_host")) | ||
# | ||
# This 'get_endpoints' method will be run before the 'test' method, | ||
# and VMware Aria Operations will use the results to extract a certificate from | ||
# each URL. If the certificate is not trusted by the VMware Aria Operations | ||
# Trust Store, the user will be prompted to either accept or reject the | ||
# certificate. If it is accepted, the certificate will be added to the | ||
# AdapterInstance object that is passed to the 'test' and 'collect' methods. | ||
# Any certificate that is encountered in those methods should then be validated | ||
# against the certificate(s) in the AdapterInstance. | ||
|
||
# TODO: Add any additional endpoints if any | ||
|
||
logger.debug(f"Returning endpoints: {result.get_json()}") | ||
return result | ||
|
||
|
||
# Main entry point of the adapter. You should not need to modify anything below this line. | ||
def main(argv: List[str]) -> None: | ||
logging.setup_logging("adapter.log") | ||
# Start a new log file by calling 'rotate'. By default, the last five calls will be | ||
# retained. If the logs are not manually rotated, the 'setup_logging' call should be | ||
# invoked with the 'max_size' parameter set to a reasonable value, e.g., | ||
# 10_489_760 (10MB). | ||
logging.rotate() | ||
logger.info(f"Running adapter code with arguments: {argv}") | ||
if len(argv) != 3: | ||
# `inputfile` and `outputfile` are always automatically appended to the | ||
# argument list by the server | ||
logger.error("Arguments must be <method> <inputfile> <ouputfile>") | ||
exit(1) | ||
|
||
method = argv[0] | ||
try: | ||
if method == "test": | ||
test(AdapterInstance.from_input()).send_results() | ||
elif method == "endpoint_urls": | ||
get_endpoints(AdapterInstance.from_input()).send_results() | ||
elif method == "collect": | ||
collect(AdapterInstance.from_input()).send_results() | ||
elif method == "adapter_definition": | ||
result = get_adapter_definition() | ||
if type(result) is AdapterDefinition: | ||
result.send_results() | ||
else: | ||
logger.info( | ||
"get_adapter_definition method did not return an AdapterDefinition" | ||
) | ||
exit(1) | ||
else: | ||
logger.error(f"Command {method} not found") | ||
exit(1) | ||
finally: | ||
logger.info(Timer.graph()) | ||
|
||
|
||
if __name__ == "__main__": | ||
main(sys.argv[1:]) |
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
Oops, something went wrong.