Skip to content

Commit

Permalink
[resotoworker][fix] Do not use the name to compute the resource ident…
Browse files Browse the repository at this point in the history
…ifier (#1811)
  • Loading branch information
aquamatthias committed Nov 2, 2023
1 parent b4c4fb1 commit 0366429
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 14 deletions.
7 changes: 6 additions & 1 deletion plugins/aws/resoto_plugin_aws/resource/base.py
Expand Up @@ -5,7 +5,7 @@
from concurrent.futures import Future
from datetime import datetime, timezone, timedelta
from functools import lru_cache
from typing import Any, Callable, ClassVar, Dict, Iterator, List, Optional, Type, TypeVar
from typing import Any, Callable, ClassVar, Dict, Iterator, List, Optional, Type, TypeVar, Tuple
from math import ceil

from attr import evolve
Expand Down Expand Up @@ -107,6 +107,11 @@ class AwsResource(BaseResource, ABC):
# The AWS specific identifier of the resource. Not available for all resources.
arn: Optional[str] = None

def _keys(self) -> Tuple[Any, ...]:
if self.arn is not None:
return tuple(list(super()._keys()) + [self.arn])
return super()._keys()

def update_resource_tag(self, client: AwsClient, key: str, value: str) -> bool:
return False

Expand Down
10 changes: 9 additions & 1 deletion plugins/aws/resoto_plugin_aws/resource/cognito.py
@@ -1,5 +1,5 @@
from attrs import define, field
from typing import ClassVar, Dict, List, Optional, Type
from typing import ClassVar, Dict, List, Optional, Type, Tuple, Any
from resoto_plugin_aws.aws_client import AwsClient
from resoto_plugin_aws.resource.base import AwsApiSpec, AwsResource, GraphBuilder
from resoto_plugin_aws.resource.iam import AwsIamRole
Expand Down Expand Up @@ -91,6 +91,13 @@ class AwsCognitoUser(AwsResource, BaseUser):
enabled: Optional[bool] = field(default=None)
user_status: Optional[str] = field(default=None)
mfa_options: List[AwsCognitoMFAOptionType] = field(factory=list)
pool_name: Optional[str] = None

def _keys(self) -> Tuple[Any, ...]:
# in case different user pools include the same user: we add the pool name to the keys
if self.pool_name is not None:
return tuple(list(super()._keys()) + [self.pool_name])
return super()._keys()

@classmethod
def service_name(cls) -> str:
Expand Down Expand Up @@ -197,6 +204,7 @@ def add_tags(pool: AwsCognitoUserPool) -> None:
builder.submit_work(service_name, add_tags, pool_instance)
for user in builder.client.list(service_name, "list-users", "Users", UserPoolId=pool_instance.id):
if user_instance := AwsCognitoUser.from_api(user, builder):
user_instance.pool_name = pool_instance.name
builder.add_node(user_instance, user)
builder.add_edge(from_node=pool_instance, edge_type=EdgeType.default, node=user_instance)
for group in builder.client.list(service_name, "list-groups", "Groups", UserPoolId=pool_instance.id):
Expand Down
9 changes: 7 additions & 2 deletions plugins/digitalocean/resoto_plugin_digitalocean/resources.py
@@ -1,6 +1,6 @@
import logging
from attrs import define
from typing import ClassVar, Dict, List, Optional
from typing import ClassVar, Dict, List, Optional, Tuple, Any

from resoto_plugin_digitalocean.client import StreamingWrapper
from resoto_plugin_digitalocean.client import get_team_credentials
Expand Down Expand Up @@ -46,6 +46,11 @@ class DigitalOceanResource(BaseResource):
kind: ClassVar[str] = "digitalocean_resource"
urn: str = ""

def _keys(self) -> Tuple[Any, ...]:
if self.urn:
return tuple(list(super()._keys()) + [self.urn])
return super()._keys()

def delete_uri_path(self) -> Optional[str]:
return None

Expand Down Expand Up @@ -75,7 +80,7 @@ def delete(self, graph: Graph) -> bool:
raise NotImplementedError

def to_json(self) -> Json:
return _to_json(self, strip_nulls=True, keep_untouched=set(["tags"]))
return _to_json(self, strip_nulls=True, keep_untouched={"tags"})


@define(eq=False, slots=False)
Expand Down
7 changes: 6 additions & 1 deletion plugins/gcp/resoto_plugin_gcp/resources/base.py
Expand Up @@ -5,7 +5,7 @@
from concurrent.futures import Future
from threading import Lock
from types import TracebackType
from typing import Callable, List, ClassVar, Optional, TypeVar, Type, Any, Dict, Set
from typing import Callable, List, ClassVar, Optional, TypeVar, Type, Any, Dict, Set, Tuple

from attr import define, field
from google.auth.credentials import Credentials as GoogleAuthCredentials
Expand Down Expand Up @@ -267,6 +267,11 @@ class GcpResource(BaseResource):
link: Optional[str] = None
label_fingerprint: Optional[str] = None

def _keys(self) -> Tuple[Any, ...]:
if self.link is not None:
return tuple(list(super()._keys()) + [self.link])
return super()._keys()

def delete(self, graph: Graph) -> bool:
if not self.api_spec:
return False
Expand Down
15 changes: 6 additions & 9 deletions resotolib/resotolib/baseresources.py
Expand Up @@ -196,15 +196,7 @@ def _keys(self) -> Tuple[Any, ...]:
"""
if self._graph is None:
raise RuntimeError(f"_keys() called on {self.rtdname} before resource was added to graph")
return (
self.kind,
self.cloud().id,
self.account().id,
self.region().id,
self.zone().id,
self.id,
self.name,
)
return self.kind, self.cloud().id, self.account().id, self.region().id, self.zone().id, self.id

@property
def safe_name(self) -> str:
Expand Down Expand Up @@ -717,6 +709,11 @@ class BaseRegion(BaseResource):
kind: ClassVar[str] = "region"
metadata: ClassVar[Dict[str, Any]] = {"icon": "region", "group": "control"}

def _keys(self) -> Tuple[Any, ...]:
if self._graph is None:
raise RuntimeError(f"_keys() called on {self.rtdname} before resource was added to graph")
return self.kind, self.cloud().id, self.account().id, self.region().id, self.zone().id, self.id, self.name

def region(self, graph: Optional[Any] = None) -> BaseRegion:
return self

Expand Down

0 comments on commit 0366429

Please sign in to comment.