Skip to content

Commit 9c924c6

Browse files
committed
WIP: fabric_create.py and fabric_replace.py
Adding example scripts. These are not yet thoroughly tested, but seem to work.
1 parent cc7256c commit 9c924c6

File tree

3 files changed

+1062
-0
lines changed

3 files changed

+1062
-0
lines changed

examples/fabric_create.py

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#!/usr/bin/env python
2+
#
3+
# Copyright (c) 2024 Cisco and/or its affiliates.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
# pylint: disable=wrong-import-position
17+
from __future__ import absolute_import, division, print_function
18+
19+
__metaclass__ = type
20+
__author__ = "Allen Robel"
21+
22+
import argparse
23+
import json
24+
import logging
25+
import sys
26+
27+
from ndfc_python.fabric import Merged
28+
from ndfc_python.ndfc_python_logger import NdfcPythonLogger
29+
from ndfc_python.ndfc_python_sender import NdfcPythonSender
30+
from ndfc_python.parsers.parser_ansible_vault import parser_ansible_vault
31+
from ndfc_python.parsers.parser_config import parser_config
32+
from ndfc_python.parsers.parser_loglevel import parser_loglevel
33+
from ndfc_python.parsers.parser_nd_domain import parser_nd_domain
34+
from ndfc_python.parsers.parser_nd_ip4 import parser_nd_ip4
35+
from ndfc_python.parsers.parser_nd_password import parser_nd_password
36+
from ndfc_python.parsers.parser_nd_username import parser_nd_username
37+
from ndfc_python.parsers.parser_nxos_password import parser_nxos_password
38+
from ndfc_python.parsers.parser_nxos_username import parser_nxos_username
39+
from ndfc_python.read_config import ReadConfig
40+
from plugins.module_utils.common.response_handler import ResponseHandler
41+
from plugins.module_utils.common.rest_send_v2 import RestSend
42+
43+
44+
def setup_parser() -> argparse.Namespace:
45+
"""
46+
### Summary
47+
48+
Setup script-specific parser
49+
50+
Returns:
51+
argparse.Namespace
52+
"""
53+
description = "DESCRIPTION: "
54+
description += "Create/update fabrics."
55+
parser = argparse.ArgumentParser(
56+
parents=[
57+
parser_ansible_vault,
58+
parser_config,
59+
parser_loglevel,
60+
parser_nd_domain,
61+
parser_nd_ip4,
62+
parser_nd_password,
63+
parser_nd_username,
64+
parser_nxos_password,
65+
parser_nxos_username,
66+
],
67+
description=description,
68+
)
69+
return parser.parse_args()
70+
71+
72+
def main():
73+
"""
74+
main entry point for module execution
75+
"""
76+
args = setup_parser()
77+
NdfcPythonLogger()
78+
log = logging.getLogger("ndfc_python.main")
79+
log.setLevel = args.loglevel
80+
81+
try:
82+
ndfc_config = ReadConfig()
83+
ndfc_config.filename = args.config
84+
ndfc_config.commit()
85+
except ValueError as error:
86+
err_msg = f"Exiting: Error detail: {error}"
87+
log.error(err_msg)
88+
print(err_msg)
89+
sys.exit(1)
90+
91+
validated_config = ndfc_config.contents
92+
93+
try:
94+
ndfc_sender = NdfcPythonSender()
95+
ndfc_sender.args = args
96+
ndfc_sender.commit()
97+
except ValueError as error:
98+
err_msg = f"Exiting. Error detail: {error}"
99+
log.error(err_msg)
100+
print(err_msg)
101+
sys.exit(1)
102+
103+
params = {}
104+
params["check_mode"] = False
105+
params["state"] = "merged"
106+
params["config"] = validated_config.get("config")
107+
108+
rest_send = RestSend(params)
109+
rest_send.send_interval = 3
110+
rest_send.timeout = 9
111+
rest_send.sender = ndfc_sender.sender
112+
rest_send.response_handler = ResponseHandler()
113+
114+
try:
115+
task = Merged(params)
116+
task.rest_send = rest_send
117+
task.commit()
118+
except ValueError as error:
119+
err_msg = f"Exiting. Error detail: {error}"
120+
log.error(err_msg)
121+
print(err_msg)
122+
sys.exit(1)
123+
124+
task.results.build_final_result()
125+
# pylint: disable=unsupported-membership-test
126+
if True in task.results.failed:
127+
err_msg = "unable to create/update fabric(s)"
128+
log.error(err_msg)
129+
print(err_msg)
130+
print(json.dumps(task.results.final_result, indent=4, sort_keys=True))
131+
132+
133+
if __name__ == "__main__":
134+
main()

examples/fabric_replace.py

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#!/usr/bin/env python
2+
#
3+
# Copyright (c) 2024 Cisco and/or its affiliates.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
# pylint: disable=wrong-import-position
17+
from __future__ import absolute_import, division, print_function
18+
19+
__metaclass__ = type
20+
__author__ = "Allen Robel"
21+
22+
import argparse
23+
import json
24+
import logging
25+
import sys
26+
27+
from ndfc_python.fabric import Replaced
28+
from ndfc_python.ndfc_python_logger import NdfcPythonLogger
29+
from ndfc_python.ndfc_python_sender import NdfcPythonSender
30+
from ndfc_python.parsers.parser_ansible_vault import parser_ansible_vault
31+
from ndfc_python.parsers.parser_config import parser_config
32+
from ndfc_python.parsers.parser_loglevel import parser_loglevel
33+
from ndfc_python.parsers.parser_nd_domain import parser_nd_domain
34+
from ndfc_python.parsers.parser_nd_ip4 import parser_nd_ip4
35+
from ndfc_python.parsers.parser_nd_password import parser_nd_password
36+
from ndfc_python.parsers.parser_nd_username import parser_nd_username
37+
from ndfc_python.parsers.parser_nxos_password import parser_nxos_password
38+
from ndfc_python.parsers.parser_nxos_username import parser_nxos_username
39+
from ndfc_python.read_config import ReadConfig
40+
from plugins.module_utils.common.response_handler import ResponseHandler
41+
from plugins.module_utils.common.rest_send_v2 import RestSend
42+
43+
44+
def setup_parser() -> argparse.Namespace:
45+
"""
46+
### Summary
47+
48+
Setup script-specific parser
49+
50+
Returns:
51+
argparse.Namespace
52+
"""
53+
description = "DESCRIPTION: "
54+
description += "Create/update fabrics."
55+
parser = argparse.ArgumentParser(
56+
parents=[
57+
parser_ansible_vault,
58+
parser_config,
59+
parser_loglevel,
60+
parser_nd_domain,
61+
parser_nd_ip4,
62+
parser_nd_password,
63+
parser_nd_username,
64+
parser_nxos_password,
65+
parser_nxos_username,
66+
],
67+
description=description,
68+
)
69+
return parser.parse_args()
70+
71+
72+
def main():
73+
"""
74+
main entry point for module execution
75+
"""
76+
args = setup_parser()
77+
NdfcPythonLogger()
78+
log = logging.getLogger("ndfc_python.main")
79+
log.setLevel = args.loglevel
80+
81+
try:
82+
ndfc_config = ReadConfig()
83+
ndfc_config.filename = args.config
84+
ndfc_config.commit()
85+
except ValueError as error:
86+
err_msg = f"Exiting: Error detail: {error}"
87+
log.error(err_msg)
88+
print(err_msg)
89+
sys.exit(1)
90+
91+
validated_config = ndfc_config.contents
92+
93+
try:
94+
ndfc_sender = NdfcPythonSender()
95+
ndfc_sender.args = args
96+
ndfc_sender.commit()
97+
except ValueError as error:
98+
err_msg = f"Exiting. Error detail: {error}"
99+
log.error(err_msg)
100+
print(err_msg)
101+
sys.exit(1)
102+
103+
params = {}
104+
params["check_mode"] = False
105+
params["state"] = "replaced"
106+
params["config"] = validated_config.get("config")
107+
108+
rest_send = RestSend(params)
109+
rest_send.send_interval = 3
110+
rest_send.timeout = 9
111+
rest_send.sender = ndfc_sender.sender
112+
rest_send.response_handler = ResponseHandler()
113+
114+
try:
115+
task = Replaced(params)
116+
task.rest_send = rest_send
117+
task.commit()
118+
except ValueError as error:
119+
err_msg = f"Exiting. Error detail: {error}"
120+
log.error(err_msg)
121+
print(err_msg)
122+
sys.exit(1)
123+
124+
task.results.build_final_result()
125+
# pylint: disable=unsupported-membership-test
126+
if True in task.results.failed:
127+
err_msg = "unable to replace fabric(s)"
128+
log.error(err_msg)
129+
print(err_msg)
130+
print(json.dumps(task.results.final_result, indent=4, sort_keys=True))
131+
132+
133+
if __name__ == "__main__":
134+
main()

0 commit comments

Comments
 (0)