Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for AWS Resource Explorer #470

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions wrapanapi/systems/ec2.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import base64
import os
import re
import typing

import boto3
from boto3 import client as boto3client
Expand Down Expand Up @@ -482,6 +483,70 @@ def cleanup(self):
return self.delete()


class ResourceExplorerResource:
"""
This class represents a resource returned by Resource Explorer.
"""

def __init__(self, arn, region, resource_type, service, properties=[]):
self.arn = arn
self.region = region
self.resource_type = resource_type
self.service = service
self.properties = properties

def get_tag_value(self, key) -> str:
"""
Returns a tag value for a given tag key.
Tags are taken from the resource properties.

Args:
key: a tag key
"""
tags = self.get_tags(regex=f"^{key}$")
if len(tags) > 0:
return tags[0].get("Value")
return None

def get_tags(self, regex="") -> typing.List[dict]:
"""
Returns a list of tags (a dict with keys 'Key' and 'Value').
Tags are taken from the resource properties.

Args:
regex: a regular expressions for keys, default is ""
"""
list = []
for property in self.properties:
data = property.get("Data")
for tag in data:
key = tag.get("Key")
if re.match(regex, key):
list.append(tag)
return list

@property
def id(self) -> str:
"""
Returns the last part of the arn.
This part is used as id in aws cli.
"""
if self.arn:
return self.arn.split(":")[-1]
return None

@property
def name(self) -> str:
"""
Returns a name for the resource derived from the associated tag with key 'Name'.
If there is no such tag then the name is the id from arn.
"""
name = self.get_tag_value("Name")
if not name:
name = self.id
return name


class EC2System(System, VmMixin, TemplateMixin, StackMixin, NetworkMixin):
"""EC2 Management System, powered by boto

Expand Down Expand Up @@ -534,6 +599,7 @@ def __init__(self, **kwargs):
self.ssm_connection = boto3client("ssm", **connection_kwargs)
self.sns_connection = boto3client("sns", **connection_kwargs)
self.cw_events_connection = boto3client("events", **connection_kwargs)
self.resource_explorer_connection = boto3client("resource-explorer-2", **connection_kwargs)

self.kwargs = kwargs

Expand Down Expand Up @@ -1743,3 +1809,36 @@ def cleanup_resources(self):
self.remove_all_unused_nics()
self.remove_all_unused_volumes()
self.remove_all_unused_ips()

def list_resources(self, query="", view="") -> typing.List[ResourceExplorerResource]:
"""
Lists resources using AWS Resource Explorer (resource-explorer-2).

Args:
query: keywords and filters for resources; default is "" (all)
view: arn of the view to use for the query; default is "" (default view)

Return:
a list of resources satisfying the query

Examples:
Use query "tag.key:kubernetes.io/cluster/*" to list OCP resources
"""
args = {"QueryString": query}
if view:
args["ViewArn"] = view
list = []
paginator = self.resource_explorer_connection.get_paginator("search")
page_iterator = paginator.paginate(**args)
for page in page_iterator:
resources = page.get("Resources")
for r in resources:
resource = ResourceExplorerResource(
arn=r.get("Arn"),
region=r.get("Region"),
service=r.get("Service"),
properties=r.get("Properties"),
resource_type=r.get("ResourceType"),
)
list.append(resource)
return list