-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcredentials_default_switch_save.py
More file actions
executable file
·146 lines (118 loc) · 3.96 KB
/
Copy pathcredentials_default_switch_save.py
File metadata and controls
executable file
·146 lines (118 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python3
"""
# credentials_default_switch_save.py
## Description
Save default switch credentials to the controller.
## Usage
1. Modify PYTHONPATH appropriately for your setup before running this script
``` bash
export PYTHONPATH=$PYTHONPATH:$HOME/repos/nd-python/lib
```
2. Optional, to enable logging.
``` bash
export ND_LOGGING_CONFIG=$HOME/repos/nd-python/lib/nd_python/logging_config.json
```
3. Edit ./examples/config/credentials_default_switch_save.yaml with desired values
4. Set credentials via script command line, environment variables, or Ansible Vault
5. Run the script (below we're using command line for credentials)
``` bash
./examples/credentials_default_switch_save.py \
--config ./examples/config/credentials_default_switch_save.yaml \
--nd-domain local \
--nd-ip4 10.1.1.1 \
--nd-password password \
--nd-username admin
```
"""
# pylint: disable=duplicate-code
import argparse
import logging
import sys
from nd_python.common.nd_python_logger import NdPythonLogger
from nd_python.common.nd_python_sender import NdPythonSender
from nd_python.common.read_config import ReadConfig
from nd_python.common.response_handler import ResponseHandler
from nd_python.common.rest_send_v2 import RestSend
from nd_python.credentials.default_switch_save import CredentialsDefaultSwitchSave
from nd_python.parsers.parser_ansible_vault import parser_ansible_vault
from nd_python.parsers.parser_config import parser_config
from nd_python.parsers.parser_loglevel import parser_loglevel
from nd_python.parsers.parser_nd_domain import parser_nd_domain
from nd_python.parsers.parser_nd_ip4 import parser_nd_ip4
from nd_python.parsers.parser_nd_password import parser_nd_password
from nd_python.parsers.parser_nd_username import parser_nd_username
from nd_python.validators.credentials.default_switch_save import CredentialsDefaultSwitchSaveConfigValidator
from pydantic import ValidationError
def action(cfg: CredentialsDefaultSwitchSaveConfigValidator) -> None:
"""
Given a validated configuration, save the default switch credentials.
"""
# Prepopulate error message in case of failure
errmsg = f"Error saving default switch credentials for {cfg.switch_username}, "
try:
instance = CredentialsDefaultSwitchSave()
instance.rest_send = rest_send
instance.config = cfg
instance.commit()
except ValueError as error:
errmsg += f"Error detail: {error}"
log.error(errmsg)
print(errmsg)
return
log.info(instance.result)
print(instance.result)
def setup_parser() -> argparse.Namespace:
"""
### Summary
Setup script-specific parser
Returns:
argparse.Namespace
"""
parser = argparse.ArgumentParser(
parents=[
parser_ansible_vault,
parser_config,
parser_loglevel,
parser_nd_domain,
parser_nd_ip4,
parser_nd_password,
parser_nd_username,
],
description="DESCRIPTION: Save default switch credentials to the controller.",
)
return parser.parse_args()
args = setup_parser()
NdPythonLogger()
log = logging.getLogger("nd_python.main")
log.setLevel(args.loglevel)
try:
user_config = ReadConfig()
user_config.filename = args.config
user_config.commit()
except ValueError as error:
msg = f"Exiting: Error detail: {error}"
log.error(msg)
print(msg)
sys.exit(1)
try:
validator = CredentialsDefaultSwitchSaveConfigValidator(**user_config.contents)
except ValidationError as error:
msg = f"{error}"
log.error(msg)
print(msg)
sys.exit(1)
try:
nd_sender = NdPythonSender()
nd_sender.args = args
nd_sender.commit()
except ValueError as error:
msg = f"Exiting. Error detail: {error}"
log.error(msg)
print(msg)
sys.exit(1)
rest_send = RestSend({})
rest_send.sender = nd_sender.sender
rest_send.response_handler = ResponseHandler()
rest_send.timeout = 2
rest_send.send_interval = 5
action(validator)