File tree 8 files changed +92
-27
lines changed
python/diff-nautobot-understack
8 files changed +92
-27
lines changed Original file line number Diff line number Diff line change @@ -6,4 +6,5 @@ poetry lock
6
6
poetry install
7
7
8
8
export NB_TOKEN=<get_token_from_nautobot_dev>
9
- poetry run diff-network
9
+ poetry run diff-networks
10
+ poetry run diff-projects
Original file line number Diff line number Diff line change 5
5
OS_CLOUD = os .environ .get ("OS_CLOUD" , "uc-dev-infra" )
6
6
7
7
8
+ openstack .enable_logging (debug = True )
9
+
10
+
8
11
class API :
9
12
def __init__ (self ):
10
13
self .cloud_connection = openstack .connect (cloud = OS_CLOUD )
Original file line number Diff line number Diff line change 1
1
from pprint import pprint
2
- from diffsync import Diff
3
2
from diffsync .enum import DiffSyncFlags
4
3
from diff_nautobot_understack .network .adapters .openstack_network import (
5
4
Network as OpenstackNetwork ,
6
5
)
7
6
from diff_nautobot_understack .network .adapters .ucvni import Network as UcvniNetwork
8
7
9
8
10
- class MyDiff (Diff ):
11
- """Custom Diff class to control the order of the site objects."""
12
-
13
- @classmethod
14
- def order_children_site (cls , children ):
15
- """Return the site children ordered in alphabetical order."""
16
- keys = sorted (children .keys (), reverse = False )
17
- for key in keys :
18
- yield children [key ]
19
-
20
-
21
9
def openstack_network_diff_from_ucvni_network ():
22
10
openstack_network = OpenstackNetwork ()
23
11
openstack_network .load ()
Original file line number Diff line number Diff line change
1
+ from diffsync import Adapter
2
+ from diff_nautobot_understack .clients .nautobot import API
3
+
4
+ from diff_nautobot_understack .project import models
5
+
6
+
7
+ class Tenant (Adapter ):
8
+ project = models .ProjectModel
9
+
10
+ top_level = ["project" ]
11
+ type = "Tenant"
12
+
13
+ def __init__ (self , ** kwargs ):
14
+ super ().__init__ (** kwargs )
15
+ self .api_client = API ()
16
+
17
+ def load (self ):
18
+ url = "/api/tenancy/tenants/?include=relationships"
19
+
20
+ tenants_response = self .api_client .make_api_request (url , paginated = True )
21
+
22
+ for tenant in tenants_response :
23
+ self .add (
24
+ self .project (
25
+ id = tenant .get ("id" ),
26
+ name = tenant .get ("name" ),
27
+ description = tenant .get ("description" ),
28
+ )
29
+ )
Original file line number Diff line number Diff line change
1
+ from diffsync import Adapter
2
+ from diff_nautobot_understack .clients .openstack import API
3
+
4
+ from diff_nautobot_understack .project import models
5
+
6
+
7
+ class Project (Adapter ):
8
+ project = models .ProjectModel
9
+
10
+ top_level = ["project" ]
11
+ type = "OpenstackProject"
12
+
13
+ def __init__ (self , ** kwargs ):
14
+ super ().__init__ (** kwargs )
15
+ openstack_api = API ()
16
+ self .cloud = openstack_api .cloud_connection
17
+
18
+ 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
+ )
26
+ )
Original file line number Diff line number Diff line change 1
- import openstack
1
+ from pprint import pprint
2
+ from diffsync .enum import DiffSyncFlags
3
+ from diff_nautobot_understack .project .adapters .openstack_project import Project
4
+ from diff_nautobot_understack .project .adapters .nautobot_tenant import Tenant
2
5
3
- openstack .enable_logging (debug = True )
4
6
7
+ def openstack_project_diff_from_nautobot_tenant ():
8
+ openstack_project = Project ()
9
+ openstack_project .load ()
5
10
6
- def list_projects (conn ):
7
- print ("List Projects:" )
8
-
9
- for project in conn .identity .projects ():
10
- print (project )
11
-
12
-
13
- if __name__ == "__main__" :
14
- cloud_connection = openstack .connect (os_cloud = "uc-dev-infra" )
15
- list_projects (cloud_connection )
11
+ nautobot_tenant = Tenant ()
12
+ nautobot_tenant .load ()
13
+ openstack_project_destination_tenant_source = openstack_project .diff_from (
14
+ nautobot_tenant , flags = DiffSyncFlags .CONTINUE_ON_FAILURE
15
+ )
16
+ pprint (" Nautobot tenants ⟹ Openstack projects " )
17
+ summary = openstack_project_destination_tenant_source .summary ()
18
+ pprint (summary , width = 120 )
19
+ pprint (openstack_project_destination_tenant_source .dict (), width = 120 )
Original file line number Diff line number Diff line change
1
+ from diffsync import DiffSyncModel
2
+
3
+
4
+ class ProjectModel (DiffSyncModel ):
5
+ _modelname = "project"
6
+ _identifiers = ("id" ,)
7
+ _attributes = (
8
+ "name" ,
9
+ "description" ,
10
+ )
11
+
12
+ id : str
13
+ name : str
14
+ description : str
Original file line number Diff line number Diff line change @@ -13,8 +13,8 @@ diffsync = "^2.0.1"
13
13
oslo-log = " ^7.0.0"
14
14
15
15
[tool .poetry .scripts ]
16
- diff-network = " diff_nautobot_understack.network.main:openstack_network_diff_from_ucvni_network"
17
-
16
+ diff-networks = " diff_nautobot_understack.network.main:openstack_network_diff_from_ucvni_network"
17
+ diff-projects = " diff_nautobot_understack.project.main:openstack_project_diff_from_nautobot_tenant "
18
18
19
19
[build-system ]
20
20
requires = [" poetry-core" ]
You can’t perform that action at this time.
0 commit comments