The script makes an API GET call to {{TCO_URL}}/v2/catalog/metric-entities and fetches all the metric entities which is then converted into JSON Schema and a relevant Python class is generated for every metric entity segregated based on the domain in a package structure.
The script generate_catalog_metrics.py
can be used to generate Metric Model classes as shown below:
python3 generate_catalog_metrics.py --host <host_name> --port <port>
python3 generate_catalog_metrics.py --host 10.225.67.98 --port 30002
The generated Catalog Metric Python classes are present under /collector-sdk/models/ folder
json_data = generatePythonClasses(arguments.host, arguments.port, arguments.user, arguments.pwd, arguments.protocol) builder = SchemaBuilder() # Load the json string to a variable entity = json_data["entity"] schema = {} for e in entity: builder.add_object(e) schema = builder.to_schema() #Create a Title field in the schema equal to metricType from Json Data. metricType = e["metricType"] schema["title"] = metricType fileName = snakecase(metricType) #If File name starts with a digit, append underscore at the prefix. if fileName is not None and fileName[0].isdigit(): fileName = "_" + fileName domain = snakecase(e["domain"]) dir_path = "models/"+ domain + "/" Path(dir_path).mkdir(parents=True, exist_ok=True) if arguments.host is None or arguments.port is None: print("Host and Port cannot be empty") else: file_path = dir_path + fileName +".py" if not os.path.exists(file_path) or Config.override_generated_class: generate_python_file(schema, Path(file_path))
By default, username = admin, password = changeme, protocol = https, override flag is set to True (the script overrides and creates the classes each time)
Sample generated Python class -
from typing import List from models.metric import TCOMetric class processor(TCOMetric): class metrics(list): class items(TCOMetric): def __init__(self, values: dict = None): values = values if values is not None else {} self.name: str = values.get("name", None) self.description: str = values.get("description", None) self.unit: str = values.get("unit", None) def __repr__(self): return "items[" + ", ".join((f"name: {repr(self.name)}", f"description: {repr(self.description)}", f"unit: {repr(self.unit)}",)) + "]" def __init__(self, values: list = None): super().__init__() values = values if values is not None else [] self[:] = [self.items(value) for value in values] class properties(list): class items(TCOMetric): def __init__(self, values: dict = None): values = values if values is not None else {} self.name: str = values.get("name", None) self.description: str = values.get("description", None) def __repr__(self): return "items[" + ", ".join((f"name: {repr(self.name)}", f"description: {repr(self.description)}",)) + "]" def __init__(self, values: list = None): super().__init__() values = values if values is not None else [] self[:] = [self.items(value) for value in values] class tags(list): class items(TCOMetric): def __init__(self, values: dict = None): values = values if values is not None else {} self.name: str = values.get("name", None) self.description: str = values.get("description", None) def __repr__(self): return "items[" + ", ".join((f"name: {repr(self.name)}", f"description: {repr(self.description)}",)) + "]" def __init__(self, values: list = None): super().__init__() values = values if values is not None else [] self[:] = [self.items(value) for value in values] def __init__(self, values: dict = None): values = values if values is not None else {} self.domain: str = values.get("domain", None) self.sub_domains: List[str] = values.get("sub_domains", None) self.description: str = values.get("description", None) self.metricType: str = values.get("metricType", None) self.metrics: List[items] = self.metrics(values=values.get("metrics")) self.properties: List[items] = self.properties(values=values.get("properties")) self.tags: List[items] = self.tags(values=values.get("tags")) self.tco_internal: List[str] = values.get("tco_internal", None) self.is_base_model: bool = values.get("is_base_model", None) def __repr__(self): return "processor[" + ", ".join((f"domain: {repr(self.domain)}", f"sub_domains: {repr(self.sub_domains)}", f"description: {repr(self.description)}", f"metricType: {repr(self.metricType)}", f"metrics: {repr(self.metrics)}", f"properties: {repr(self.properties)}", f"tags: {repr(self.tags)}", f"tco_internal: {repr(self.tco_internal)}", f"is_base_model: {repr(self.is_base_model)}", )) + "]"