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 metric service create api #14

Merged
merged 4 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Generator

from spaceone.core.connector.space_connector import SpaceConnector
from spaceone.core.error import ERROR_BASE

from spaceone.inventory_v2.connector.collector_plugin_connector import (
BaseCollectorPluginConnector,
Expand Down Expand Up @@ -48,12 +49,36 @@ def collect(
yield self._convert_resource_data(resource_data)

@staticmethod
def _convert_resource_data(resource_data: dict) -> dict:
def _convert_match_rule(resource_data: dict, resource_type: str) -> dict:
for rule_values in resource_data.get("match_rules", {}).values():
for index, rule_value in enumerate(rule_values):
if rule_value == "cloud_service_id":
rule_values[index] = "asset_id"
elif rule_value == "cloud_service_type":
rule_values[index] = "asset_type_id"
elif rule_value == "cloud_service_group":
del rule_values[index]
elif rule_value == "reference.resource_id":
rule_values[index] = "resource_id"
elif rule_value == "group":
rule_values[index] = "asset_group_id"
elif rule_value == "name":
if resource_type == "inventory.AssetType":
rule_values[index] = "asset_type_id"
return resource_data

def _convert_resource_data(self, resource_data: dict) -> dict:

if "resource" in resource_data and "metadata" in resource_data["resource"]:
del resource_data["resource"]["metadata"]

_LOGGER.debug(
f"[_convert_resource_data] before convert resource_data: {resource_data}"
)

resource_type = resource_data.get("resource_type")
if resource_type == "inventory.Region":
pass

if resource_type in ["inventory.CloudService", "inventory.CloudServiceType"]:
if resource_type == "inventory.CloudService":
Expand All @@ -70,31 +95,35 @@ def _convert_resource_data(resource_data: dict) -> dict:
resource_type = resource_data.get("resource_type")

# convert match rule
for rule_values in resource_data.get("match_rules", {}).values():
for index, rule_value in enumerate(rule_values):
if rule_value == "cloud_service_id":
rule_values[index] = "asset_id"
elif rule_value == "cloud_service_type":
rule_values[index] = "asset_type_id"
elif rule_value == "cloud_service_group":
del rule_values[index]
elif rule_value == "reference.resource_id":
rule_values[index] = "resource_id"
elif rule_value == "group":
rule_values[index] = "asset_group_id"
resource_data = self._convert_match_rule(resource_data, resource_type)

# convert keywords [instance_size, instance_type, ]
if _resource := resource_data.get("resource"):
_resource_v1 = {}
# del _resource["metadata"]

if "instance_size" in _resource:
_resource_v1["instance_size"] = _resource.pop("instance_size")
_resource["data"]["instance_size"] = _resource.pop("instance_size")
if "instance_type" in _resource:
_resource_v1["instance_type"] = _resource.pop("instance_type")
del _resource["metadata"]
_resource["data"]["instance_type"] = _resource.pop("instance_type")

if "region_code" in _resource:
_resource["region_id"] = (
f"{_resource['provider']}-{_resource['region_code']}"
)

if resource_type == "inventory.Asset":
asset_type_id = f"{_resource['provider']}.{_resource['cloud_service_group']}.{_resource['cloud_service_type']}"
asset_type_id = f"{_resource['provider']}-{_resource['cloud_service_group']}-{_resource['cloud_service_type']}"
_resource["asset_type_id"] = asset_type_id
resource_data["asset_type_id"] = asset_type_id

if "reference" in _resource:
_resource["resource_id"] = _resource["reference"].get(
"resource_id"
)
_resource["external_link"] = _resource["reference"].get(
"external_link"
)

elif resource_type == "inventory.AssetType":

asset_type_id = f"at-{_resource['provider']}-{_resource['group']}-{_resource['name']}"
Expand All @@ -105,9 +134,10 @@ def _convert_resource_data(resource_data: dict) -> dict:
_resource["asset_type_id"] = asset_type_id
resource_data["asset_type_id"] = asset_type_id
resource_data["asset_groups"] = asset_groups
resource_data["icon"] = _resource.get("tags", {}).get("icon", "")

resource_data["resource"]["v1"] = _resource_v1
resource_data["icon"] = resource_data.get("tags", {}).get(
"spaceone:icon", ""
)

_LOGGER.debug(f"[_convert_resource_data] resource_data: {resource_data}")

Expand Down
13 changes: 13 additions & 0 deletions src/spaceone/inventory_v2/error/collect_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from spaceone.core.error import *


class ERROR_NOT_ALLOW_PINNING_KEYS(ERROR_INVALID_ARGUMENT):
_message = "Pinning keys not allowed. (key={key})"


class ERROR_METADATA_DICT_TYPE(ERROR_INVALID_ARGUMENT):
_message = "The value of metadata's {key} must be a dict type."


class ERROR_METADATA_LIST_VALUE_TYPE(ERROR_INVALID_ARGUMENT):
_message = "The value of metadata's {key} must be a list type."
5 changes: 4 additions & 1 deletion src/spaceone/inventory_v2/interface/grpc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
from .collector import Collector
from .job import Job
from .job_task import JobTask
from .metric import Metric
from .namespace_group import NamespaceGroup
from .namespace import Namespace
from .metric import Metric

_all_ = ["app"]

Expand All @@ -14,4 +16,5 @@
app.add_service(Job)
app.add_service(JobTask)
app.add_service(NamespaceGroup)
app.add_service(Namespace)
app.add_service(Namespace)
app.add_service(Metric)
12 changes: 8 additions & 4 deletions src/spaceone/inventory_v2/lib/rule_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ def dict_key_int_parser(data: dict) -> dict:
def make_query(
key: str, rules: dict, resource: dict, domain_id: str, workspace_id: str
) -> dict:
_filter = [
{"k": "domain_id", "v": domain_id, "o": "eq"},
{"k": "workspace_id", "v": workspace_id, "o": "eq"},
]
resource_type = resource.get("resource_type")
_filter = [{"k": "domain_id", "v": domain_id, "o": "eq"}]

if resource_type in ["inventory.Asset"]:
_filter.append({"k": "workspace_id", "v": workspace_id, "o": "eq"})
else:
workspaces = list({workspace_id, "*"})
_filter.append({"k": "workspace_id", "v": workspaces, "o": "in"})

for rule in rules[key]:
value = find_data(resource, rule)
Expand Down
24 changes: 19 additions & 5 deletions src/spaceone/inventory_v2/manager/asset_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@
"name",
"ip_addresses",
"account",
"instance_type",
"instance_size",
"reference",
"region_code",
"ref_region",
"resource_id",
"external_link",
"region_id",
"project_id",
"data",
]
Expand Down Expand Up @@ -116,6 +114,22 @@ def list_assets(
**query, target=target, reference_filter=reference_filter
)

def analyze_assets(
self,
query: dict,
change_filter: bool = False,
domain_id: str = None,
reference_filter: dict = None,
):
if change_filter:
query = self._change_filter_tags(query)
query = self._change_filter_project_group_id(query, domain_id)

# Append Query for DELETED filter (Temporary Logic)
query = self._append_state_query(query)

return self.asset_model.analyze(**query, reference_filter=reference_filter)

def _change_filter_tags(self, query: dict) -> dict:
change_filter = []

Expand Down
4 changes: 2 additions & 2 deletions src/spaceone/inventory_v2/manager/asset_type_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ def delete_asset_type_by_vo(asset_type_vo: AssetType) -> None:

def get_asset_type(
self,
asset_Type_id: str,
asset_type_id: str,
domain_id: str,
workspace_id: str = None,
) -> AssetType:
conditions = {
"asset_Type_id": asset_Type_id,
"asset_type_id": asset_type_id,
"domain_id": domain_id,
}

Expand Down
31 changes: 22 additions & 9 deletions src/spaceone/inventory_v2/manager/collecting_manager.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import datetime
import logging
import time
from datetime import datetime
from typing import Generator

from spaceone.core import utils
from spaceone.core.manager import BaseManager

from spaceone.inventory_v2.lib.resource_manager import ResourceManager
from spaceone.inventory_v2.manager.asset_manager import AssetManager
from spaceone.inventory_v2.manager.job_manager import JobManager
from spaceone.inventory_v2.manager.job_task_manager import JobTaskManager
from spaceone.inventory_v2.manager.collector_manager import CollectorManager
Expand All @@ -13,8 +16,6 @@
CollectorPluginManager,
)

# from spaceone.inventory.manager.namespace_manager import NamespaceManager
# from spaceone.inventory.manager.metric_manager import MetricManager
from spaceone.inventory_v2.model.job_task.database import JobTask
from spaceone.inventory_v2.error import *
from spaceone.inventory_v2.lib import rule_matcher
Expand Down Expand Up @@ -50,9 +51,6 @@ def collecting_resources(self, params: dict) -> bool:
'token': 'str'
}
"""
from spaceone.core import model

model.init_all(False)

# set token to transaction meta
token = params["token"]
Expand All @@ -71,6 +69,8 @@ def collecting_resources(self, params: dict) -> bool:
secret_data = params["secret_data"]
plugin_info = params["plugin_info"]

print(secret_info)

if is_sub_task:
_LOGGER.debug(
f"[collecting_resources] start sub task: {job_task_id} "
Expand All @@ -85,7 +85,7 @@ def collecting_resources(self, params: dict) -> bool:
self.job_task_mgr.push_job_task(params)
return True

job_task_vo = self.job_task_mgr.get(job_task_id, domain_id)
job_task_vo = self.job_task_mgr.get_job_task(job_task_id, domain_id)

# add workspace_id to params from secret_info
params["workspace_id"] = secret_info["workspace_id"]
Expand Down Expand Up @@ -233,7 +233,11 @@ def _upsert_collecting_resources(
total_count += 1

try:
if resource_type in ["inventory.Namespace", "inventory.Metric"]:
if resource_type in [
"inventory.NamespaceGroup",
"inventory.Namespace",
"inventory.Metric",
]:
# self._upsert_metric_and_namespace(resource_data, params)
# total_count -= 1
pass
Expand Down Expand Up @@ -377,6 +381,7 @@ def _upsert_resource(
request_data = resource_data.get("resource", {})
request_data["domain_id"] = domain_id
request_data["workspace_id"] = workspace_id
request_data["last_collected_at"] = datetime.utcnow()

service, manager = self._get_resource_map(resource_type)

Expand Down Expand Up @@ -421,7 +426,11 @@ def _upsert_resource(

try:
match_resource, total_count = self._query_with_match_rules(
request_data, match_rules, domain_id, workspace_id, manager
request_data,
match_rules,
domain_id,
workspace_id,
manager,
)

except ERROR_TOO_MANY_MATCH as e:
Expand Down Expand Up @@ -474,6 +483,7 @@ def _upsert_resource(
additional = self._set_error_addition_info(
resource_type, total_count, request_data
)
# todo: refactoring job task error
self.job_task_mgr.add_error(
job_task_vo, e.error_code, e.message, additional
)
Expand Down Expand Up @@ -590,8 +600,11 @@ def _query_with_match_rules(
query = rule_matcher.make_query(
order, match_rules, resource_data, domain_id, workspace_id
)
_LOGGER.debug(f"[_query_with_match_rules] query: {query}")
match_resource, total_count = resource_manager.find_resources(query)

_LOGGER.debug(f"[_query_with_match_rules] match_resource: {match_resource}")

if total_count > 1:
if data := resource_data.get("data"):
raise ERROR_TOO_MANY_MATCH(
Expand Down
8 changes: 4 additions & 4 deletions src/spaceone/inventory_v2/manager/collector_rule_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ def change_asset_data(
custom_collector_rule_vos,
) = self._get_collector_rules(collector_id, domain_id)

cloud_service_data = self._apply_collector_rule_to_asset_data(
asset_data = self._apply_collector_rule_to_asset_data(
asset_data, managed_collector_rule_vos, domain_id
)

cloud_service_data = self._apply_collector_rule_to_asset_data(
cloud_service_data, custom_collector_rule_vos, domain_id
asset_data = self._apply_collector_rule_to_asset_data(
asset_data, custom_collector_rule_vos, domain_id
)

return cloud_service_data
return asset_data

def _apply_collector_rule_to_asset_data(
self, asset_data: dict, collector_rule_vos: QuerySet, domain_id: str
Expand Down
Loading
Loading