Skip to content

Commit 31e9599

Browse files
committed
PUC-740: review comments
1 parent ffdad74 commit 31e9599

File tree

11 files changed

+237
-418
lines changed

11 files changed

+237
-418
lines changed
+15-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11

2-
cd python/diff-nautobot-understack
3-
python3 -m venv .venv
4-
source .venv/bin/activate
5-
poetry lock
6-
poetry install
2+
1. cd python/diff-nautobot-understack
3+
2. python3 -m venv .venv
4+
3. source .venv/bin/activate
5+
4. poetry lock
6+
5. poetry install
7+
6. export nautobot_api_token=<get_token_from_nautobot_dev> (can be added in .env file too)
8+
7. export nautobot_url=https://nautobot.staging.undercloud.rackspace.net (no need to export for dev env)
9+
8. export OS_CLOUD=uc-dev-infra
10+
9. export OS_CLIENT_CONFIG_FILE=./my_clouds.yaml (set this if it is any other location [defined here](https://opendev.org/openstack/openstacksdk#getting-started))
711

8-
export NB_TOKEN=<get_token_from_nautobot_dev>
9-
poetry run diff-networks
10-
poetry run diff-projects
12+
- Below are some example commands
13+
```
14+
uc-diff --help
15+
uc-diff project undercloud -v
16+
uc-diff network
17+
```

python/diff-nautobot-understack/clouds.yaml

-31
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import typer
2+
from diff_nautobot_understack.project.main import (
3+
openstack_project_diff_from_nautobot_tenant,
4+
)
5+
from diff_nautobot_understack.network.main import (
6+
openstack_network_diff_from_ucvni_network,
7+
)
8+
from diff_nautobot_understack.settings import app_settings as settings
9+
10+
app = typer.Typer(
11+
name="diff",
12+
add_completion=False,
13+
help="compare data between Openstack and Nautobot.",
14+
)
15+
16+
17+
@app.command()
18+
def project(
19+
name: str,
20+
debug: bool = typer.Option(False, "--debug", "-v", help="Enable debug mode"),
21+
):
22+
"""Nautobot tenants ⟹ Openstack projects"""
23+
settings.debug = debug
24+
openstack_project_diff_from_nautobot_tenant(os_project=name)
25+
26+
27+
@app.command()
28+
def network(
29+
debug: bool = typer.Option(False, "--debug", "-v", help="Enable debug mode"),
30+
):
31+
"""Nautobot ucvnis ⟹ Openstack networks"""
32+
settings.debug = debug
33+
openstack_network_diff_from_ucvni_network()
34+
35+
36+
if __name__ == "__main__":
37+
app()

python/diff-nautobot-understack/diff_nautobot_understack/clients/nautobot.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
1-
import os
2-
31
import requests
42
import logging
53
import inspect
64
from urllib.parse import urljoin
75

8-
NB_URL = os.environ.get("NB_URL", "https://nautobot.dev.undercloud.rackspace.net")
9-
NB_TOKEN = os.environ.get("NB_TOKEN")
6+
from diff_nautobot_understack.settings import app_settings as settings
107

118

129
class API:
1310
CALLER_FRAME = 1
1411

1512
def __init__(self):
16-
self.base_url = NB_URL
13+
self.base_url = settings.nautobot_url
1714
self.s = requests.Session()
18-
self.token = NB_TOKEN
19-
self.s.headers.update({"Authorization": f"Token {NB_TOKEN}"})
15+
self.token = settings.nautobot_api_token
16+
self.s.headers.update({"Authorization": f"Token {self.token}"})
2017

2118
def make_api_request(
2219
self, url: str, payload: dict | None = None, paginated: bool = False
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import os
2-
31
import openstack
42

5-
OS_CLOUD = os.environ.get("OS_CLOUD", "uc-dev-infra")
6-
7-
8-
openstack.enable_logging(debug=True)
3+
from diff_nautobot_understack.settings import app_settings as settings
94

105

116
class API:
127
def __init__(self):
13-
self.cloud_connection = openstack.connect(cloud=OS_CLOUD)
8+
cloud_name = settings.os_cloud
9+
debug = settings.debug
10+
11+
openstack.enable_logging(debug=debug)
12+
self.cloud_connection = openstack.connect(cloud=cloud_name)

python/diff-nautobot-understack/diff_nautobot_understack/project/adapters/nautobot_tenant.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1+
import uuid
2+
13
from diffsync import Adapter
24
from diff_nautobot_understack.clients.nautobot import API
35

46
from diff_nautobot_understack.project import models
57

68

9+
def _remove_hyphens(tenant_id: str):
10+
uuid_obj = uuid.UUID(tenant_id)
11+
return str(uuid_obj.hex)
12+
13+
714
class Tenant(Adapter):
815
project = models.ProjectModel
916

@@ -13,16 +20,17 @@ class Tenant(Adapter):
1320
def __init__(self, **kwargs):
1421
super().__init__(**kwargs)
1522
self.api_client = API()
23+
self.tenant_name = kwargs["name"]
1624

1725
def load(self):
18-
url = "/api/tenancy/tenants/?include=relationships"
26+
url = f"/api/tenancy/tenants/?name={self.tenant_name}&include=relationships"
1927

2028
tenants_response = self.api_client.make_api_request(url, paginated=True)
2129

2230
for tenant in tenants_response:
2331
self.add(
2432
self.project(
25-
id=tenant.get("id"),
33+
id=_remove_hyphens(tenant.get("id")),
2634
name=tenant.get("name"),
2735
description=tenant.get("description"),
2836
)

python/diff-nautobot-understack/diff_nautobot_understack/project/adapters/openstack_project.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ class Project(Adapter):
1313
def __init__(self, **kwargs):
1414
super().__init__(**kwargs)
1515
openstack_api = API()
16+
self.project_name = kwargs["name"]
1617
self.cloud = openstack_api.cloud_connection
1718

1819
def load(self):
19-
for project in self.cloud.identity.projects():
20-
self.add(
21-
self.project(
22-
id=project.id,
23-
name=project.name,
24-
description=project.description,
25-
)
20+
os_project = self.cloud.get_project(name_or_id=self.project_name)
21+
self.add(
22+
self.project(
23+
id=os_project.id,
24+
name=os_project.name,
25+
description=os_project.description,
2626
)
27+
)

python/diff-nautobot-understack/diff_nautobot_understack/project/main.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
from diffsync.enum import DiffSyncFlags
33
from diff_nautobot_understack.project.adapters.openstack_project import Project
44
from diff_nautobot_understack.project.adapters.nautobot_tenant import Tenant
5+
from diff_nautobot_understack.settings import app_settings as settings
56

67

7-
def openstack_project_diff_from_nautobot_tenant():
8-
openstack_project = Project()
8+
def openstack_project_diff_from_nautobot_tenant(os_project=None):
9+
project_name = os_project if os_project is not None else settings.os_project
10+
openstack_project = Project(name=project_name)
911
openstack_project.load()
1012

11-
nautobot_tenant = Tenant()
13+
nautobot_tenant = Tenant(name=project_name)
1214
nautobot_tenant.load()
1315
openstack_project_destination_tenant_source = openstack_project.diff_from(
1416
nautobot_tenant, flags=DiffSyncFlags.CONTINUE_ON_FAILURE
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import Optional
2+
3+
from pydantic_settings import BaseSettings, SettingsConfigDict
4+
5+
6+
class Settings(BaseSettings):
7+
model_config = SettingsConfigDict(env_file="./.env", env_file_encoding="utf-8")
8+
9+
nautobot_api_token: Optional[str] = None
10+
nautobot_url: str = "https://nautobot.dev.undercloud.rackspace.net"
11+
debug: bool = False
12+
os_cloud: Optional[str] = None
13+
os_project: Optional[str] = "default"
14+
os_client_config_file: Optional[str] = None
15+
16+
17+
Settings.model_rebuild()
18+
app_settings = Settings()

0 commit comments

Comments
 (0)