Skip to content

Commit 6aa6c8d

Browse files
committed
add node product
1 parent 579449a commit 6aa6c8d

22 files changed

+826
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
.python-version
2+
.DS_Store
3+
.idea/
4+
__pycache__/

alembic.ini

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# A generic, single database configuration.
2+
3+
[alembic]
4+
# template used to generate migration files
5+
file_template = %%(year)d-%%(month).2d-%%(day).2d_%%(rev)s_%%(slug)s
6+
7+
# set to 'true' to run the environment during
8+
# the 'revision' command, regardless of autogenerate
9+
# revision_environment = false
10+
script_location = migrations
11+
version_locations = %(here)s/migrations/versions/schema
12+
# Logging configuration
13+
[loggers]
14+
keys = root,sqlalchemy,alembic
15+
16+
[handlers]
17+
keys = console
18+
19+
[formatters]
20+
keys = generic
21+
22+
[logger_root]
23+
level = WARN
24+
handlers = console
25+
qualname =
26+
27+
[logger_sqlalchemy]
28+
level = WARN
29+
handlers =
30+
qualname = sqlalchemy.engine
31+
32+
[logger_alembic]
33+
level = INFO
34+
handlers =
35+
qualname = alembic
36+
37+
[handler_console]
38+
class = StreamHandler
39+
args = (sys.stderr,)
40+
level = NOTSET
41+
formatter = generic
42+
43+
[formatter_generic]
44+
format = %(levelname)-5.5s [%(name)s] %(message)s
45+
datefmt = %H:%M:%S

main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
from orchestrator.cli.main import app as core_cli
33
from orchestrator.settings import AppSettings
44

5+
import products # noqa: F401 Side-effects
6+
import workflows # noqa: F401 Side-effects
7+
58
app = OrchestratorCore(base_settings=AppSettings())
69

710
if __name__ == "__main__":

migrations/env.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import logging
2+
import os
3+
from alembic import context
4+
from sqlalchemy import engine_from_config, pool
5+
6+
import orchestrator
7+
from orchestrator.db.database import BaseModel
8+
from orchestrator.settings import app_settings
9+
10+
# this is the Alembic Config object, which provides
11+
# access to the values within the .ini file in use.
12+
config = context.config
13+
14+
# Interpret the config file for Python logging.
15+
# This line sets up loggers basically.
16+
logger = logging.getLogger("alembic.env")
17+
18+
config.set_main_option("sqlalchemy.url", app_settings.DATABASE_URI)
19+
20+
# add your model's MetaData object here
21+
# for 'autogenerate' support
22+
# from myapp import mymodel
23+
# target_metadata = mymodel.Base.metadata
24+
target_metadata = BaseModel.metadata
25+
26+
# other values from the config, defined by the needs of env.py,
27+
# can be acquired:
28+
# my_important_option = config.get_main_option("my_important_option")
29+
# ... etc.
30+
31+
32+
def run_migrations_offline() -> None:
33+
"""Run migrations in 'offline' mode.
34+
35+
This configures the context with just a URL
36+
and not an Engine, though an Engine is acceptable
37+
here as well. By skipping the Engine creation
38+
we don't even need a DBAPI to be available.
39+
40+
Calls to context.execute() here emit the given string to the
41+
script output.
42+
43+
"""
44+
url = config.get_main_option("sqlalchemy.url")
45+
context.configure(
46+
url=url, target_metadata=target_metadata, literal_binds=True, dialect_opts={"paramstyle": "named"}
47+
)
48+
49+
with context.begin_transaction():
50+
context.run_migrations()
51+
52+
53+
def run_migrations_online() -> None:
54+
"""Run migrations in 'online' mode.
55+
56+
In this scenario we need to create an Engine
57+
and associate a connection with the context.
58+
59+
"""
60+
61+
# this callback is used to prevent an auto-migration from being generated
62+
# when there are no changes to the schema
63+
# reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
64+
def process_revision_directives(context, revision, directives): # type: ignore
65+
if getattr(config.cmd_opts, "autogenerate", False):
66+
script = directives[0]
67+
if script.upgrade_ops.is_empty():
68+
directives[:] = []
69+
logger.info("No changes in schema detected.")
70+
71+
engine = engine_from_config(
72+
config.get_section(config.config_ini_section), prefix="sqlalchemy.", poolclass=pool.NullPool
73+
)
74+
75+
connection = engine.connect()
76+
context.configure(
77+
connection=connection,
78+
target_metadata=target_metadata,
79+
process_revision_directives=process_revision_directives,
80+
compare_type=True,
81+
)
82+
try:
83+
with context.begin_transaction():
84+
context.run_migrations()
85+
finally:
86+
connection.close()
87+
88+
89+
if context.is_offline_mode():
90+
run_migrations_offline()
91+
else:
92+
run_migrations_online()

migrations/helpers.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from orchestrator.migrations.helpers import *
2+
3+
# Write your own helper functions below this line.

migrations/script.py.mako

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""${message}.
2+
3+
Revision ID: ${up_revision}
4+
Revises: ${down_revision | comma,n}
5+
Create Date: ${create_date}
6+
7+
"""
8+
import sqlalchemy as sa
9+
from alembic import op
10+
${imports if imports else ""}
11+
# revision identifiers, used by Alembic.
12+
revision = ${repr(up_revision)}
13+
down_revision = ${repr(down_revision)}
14+
branch_labels = ${repr(branch_labels)}
15+
depends_on = ${repr(depends_on)}
16+
17+
18+
def upgrade() -> None:
19+
${upgrades if upgrades else "pass"}
20+
21+
22+
def downgrade() -> None:
23+
${downgrades if downgrades else "pass"}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""Create data head.
2+
3+
Revision ID: a77227fe5455
4+
Revises:
5+
Create Date: 2023-10-24T15:39:44.415922
6+
7+
"""
8+
9+
# revision identifiers, used by Alembic.
10+
revision = "a77227fe5455"
11+
down_revision = None
12+
branch_labels = ("data",)
13+
depends_on = "da5c9f4cce1c"
14+
15+
16+
def upgrade() -> None:
17+
pass
18+
19+
20+
def downgrade() -> None:
21+
pass
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
"""Add node product.
2+
3+
Revision ID: c580416cfd12
4+
Revises: a77227fe5455
5+
Create Date: 2023-10-24 15:39:47.037726
6+
7+
"""
8+
from uuid import uuid4
9+
10+
from alembic import op
11+
from orchestrator.migrations.helpers import (
12+
create,
13+
create_workflow,
14+
delete,
15+
delete_workflow,
16+
ensure_default_workflows,
17+
)
18+
from orchestrator.targets import Target
19+
20+
# revision identifiers, used by Alembic.
21+
revision = "c580416cfd12"
22+
down_revision = "a77227fe5455"
23+
branch_labels = None
24+
depends_on = None
25+
26+
new_products = {
27+
"products": {
28+
"node Cisco": {
29+
"product_id": uuid4(),
30+
"product_type": "Node",
31+
"description": "Network node",
32+
"tag": "NODE",
33+
"status": "active",
34+
"product_blocks": [
35+
"Node",
36+
],
37+
"fixed_inputs": {
38+
"node_type": "Cisco",
39+
},
40+
},
41+
"node Nokia": {
42+
"product_id": uuid4(),
43+
"product_type": "Node",
44+
"description": "Network node",
45+
"tag": "NODE",
46+
"status": "active",
47+
"product_blocks": [
48+
"Node",
49+
],
50+
"fixed_inputs": {
51+
"node_type": "Nokia",
52+
},
53+
},
54+
},
55+
"product_blocks": {
56+
"Node": {
57+
"product_block_id": uuid4(),
58+
"description": "node product block",
59+
"tag": "NODE",
60+
"status": "active",
61+
"resources": {
62+
"node_name": "Unique name of the node",
63+
"role": "Role of the node in the network",
64+
"node_description": "Description of the node",
65+
"type": "Type of the node",
66+
"site": "Site where the node is located",
67+
"status": "Operational status of the node",
68+
"ims_id": "ID of the node in the inventory management system",
69+
"nrm_id": "ID of the node in the network resource manager",
70+
"ipv4_ipam_id": "ID of the node’s iPv4 loopback address in IPAM",
71+
"ipv6_ipam_id": "ID of the node’s iPv6 loopback address in IPAM",
72+
},
73+
"depends_on_block_relations": [],
74+
},
75+
},
76+
"workflows": {},
77+
}
78+
79+
new_workflows = [
80+
{
81+
"name": "create_node",
82+
"target": Target.CREATE,
83+
"description": "Create node",
84+
"product_type": "Node",
85+
},
86+
{
87+
"name": "modify_node",
88+
"target": Target.MODIFY,
89+
"description": "Modify node",
90+
"product_type": "Node",
91+
},
92+
{
93+
"name": "terminate_node",
94+
"target": Target.TERMINATE,
95+
"description": "Terminate node",
96+
"product_type": "Node",
97+
},
98+
{
99+
"name": "validate_node",
100+
"target": Target.SYSTEM,
101+
"description": "Validate node",
102+
"product_type": "Node",
103+
},
104+
]
105+
106+
107+
def upgrade() -> None:
108+
conn = op.get_bind()
109+
create(conn, new_products)
110+
for workflow in new_workflows:
111+
create_workflow(conn, workflow)
112+
ensure_default_workflows(conn)
113+
114+
115+
def downgrade() -> None:
116+
conn = op.get_bind()
117+
for workflow in new_workflows:
118+
delete_workflow(conn, workflow["name"])
119+
120+
delete(conn, new_products)

products/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from orchestrator.domain import SUBSCRIPTION_MODEL_REGISTRY
2+
3+
from products.product_types.node import Node
4+
5+
SUBSCRIPTION_MODEL_REGISTRY.update(
6+
{
7+
"node Cisco": Node,
8+
"node Nokia": Node,
9+
}
10+
) # fmt:skip

products/product_blocks/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)