Skip to content

Commit

Permalink
fmt lint
Browse files Browse the repository at this point in the history
  • Loading branch information
lilatomic committed Jul 5, 2024
1 parent c17a896 commit 228d658
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
9 changes: 5 additions & 4 deletions llamazure_tools/migrate/dashboard.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
"""Migrate an Azure Dashboard to a different Log Analytics Workspace"""
import dataclasses
import json
from dataclasses import dataclass
from datetime import datetime
from pathlib import Path

import click
from azure.identity import DefaultAzureCredential

from llamazure.azrest.azrest import AzRest
from llamazure.rid import rid
from llamazure.rid.rid import Resource
from llamazure_tools.migrate.az_dashboards.portal.r.m.portal.portal import AzDashboards, Dashboard, PatchableDashboard
from llamazure_tools.migrate.az_dashboards.portal.r.m.portal.portal import AzDashboards, Dashboard, PatchableDashboard # pylint: disable=E0611,E0401
from llamazure_tools.migrate.util import JSONTraverser


Expand Down Expand Up @@ -53,7 +55,7 @@ def put_dashboard(self, transformed: dict):
def make_backup(self, dashboard: dict):
"""Create a backup of the current dashboard data."""
filename = self.backup_directory / Path(self.dashboard.name + datetime.utcnow().isoformat()).with_suffix(".json")
with open(filename, "w") as f:
with open(filename, "w", encoding="utf-8") as f:
json.dump(dashboard, f)


Expand All @@ -62,8 +64,7 @@ def make_backup(self, dashboard: dict):
@click.option("--replacements", help="A JSON string of the replacements to apply.")
@click.option("--backup-directory", type=click.Path(), help="The directory where backups will be stored.")
def migrate(resource_id: str, replacements: str, backup_directory: str):
from azure.identity import DefaultAzureCredential

"""Migrate an Azure Dashboard to a different Log Analytics Workspace"""
az = AzRest.from_credential(DefaultAzureCredential())

replacements = json.loads(replacements)
Expand Down
2 changes: 2 additions & 0 deletions llamazure_tools/migrate/util.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Utils"""
from dataclasses import dataclass
from typing import Any, Dict

Expand All @@ -9,6 +10,7 @@ class JSONTraverser:
replacements: Dict[str, str]

def traverse(self, obj: Any) -> Any:
"""Traverse a JSON structure and replace exact string matches"""
if isinstance(obj, dict):
return {key: self.traverse(value) for key, value in obj.items()}
elif isinstance(obj, list):
Expand Down
14 changes: 10 additions & 4 deletions llamazure_tools/migrate/workbook.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,47 @@
"""Migrate an Azure Workbook to a different Log Analytics Workspace"""
import json
from dataclasses import dataclass
from datetime import datetime
from pathlib import Path

import click
from azure.identity import DefaultAzureCredential

from llamazure.azrest.azrest import AzRest
from llamazure.rid import rid
from llamazure.rid.rid import Resource
from llamazure_tools.migrate.az_workbooks.applicationinsights.r.m.insights.workbooks import AzWorkbooks, Workbook
from llamazure_tools.migrate.az_workbooks.applicationinsights.r.m.insights.workbooks import AzWorkbooks, Workbook # pylint: disable=E0611,E0401
from llamazure_tools.migrate.util import JSONTraverser


@dataclass
class Migrator:
"""Migrate an Azure Workbook"""

az: AzRest
workbook: Resource
transformer: JSONTraverser
backup_directory: Path

def migrate(self):
"""Perform the migration"""
workbook = self.get_workbook()
print(workbook.model_dump_json(indent=2))
self.make_backup(workbook)
transformed = self.transform(workbook)
self.put_workbook(transformed)

def get_workbook(self) -> Workbook:
"""Retrieve the current workbook data from Azure."""
return self.az.call(AzWorkbooks.Get(self.workbook.sub.uuid, self.workbook.rg.name, self.workbook.name, canFetchContent=True))

def transform(self, workbook: Workbook) -> Workbook:
"""Transform the workbook data using the provided transformer."""
workbook.properties.serializedData = json.dumps(self.transformer.traverse(json.loads(workbook.properties.serializedData)))
return workbook

def put_workbook(self, transformed: Workbook):
print(transformed.model_dump_json(indent=2))
"""Update the dashboard in Azure with the transformed data."""
self.az.call(
AzWorkbooks.Update(self.workbook.sub.uuid, self.workbook.rg.name, self.workbook.name, transformed),
)
Expand All @@ -51,8 +58,7 @@ def make_backup(self, workbook: Workbook):
@click.option("--replacements", help="A JSON string of the replacements to apply.")
@click.option("--backup-directory", type=click.Path(), help="The directory where backups will be stored.")
def migrate(resource_id: str, replacements: str, backup_directory: str):
from azure.identity import DefaultAzureCredential

"""Migrate an Azure Workbook to a different Log Analytics Workspace"""
az = AzRest.from_credential(DefaultAzureCredential())

replacements = json.loads(replacements)
Expand Down

0 comments on commit 228d658

Please sign in to comment.