Skip to content

Commit 9b09824

Browse files
authored
Merge pull request #51 from miott/dev
Fix issue with check_config and set prefix.
2 parents bb9dc4b + 770d9e7 commit 9b09824

File tree

7 files changed

+61
-21
lines changed

7 files changed

+61
-21
lines changed

src/cisco_gnmi/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@
3030
from .xe import XEClient
3131
from .builder import ClientBuilder
3232

33-
__version__ = "1.0.7"
33+
__version__ = "1.0.8"

src/cisco_gnmi/builder.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
from .util import gen_target_netloc, get_cert_from_target, get_cn_from_cert
3232

3333

34+
LOGGER = logging.getLogger(__name__)
35+
logger = LOGGER
36+
37+
3438
class ClientBuilder(object):
3539
"""Builder for the creation of a gNMI client.
3640
Supports construction of base Client and XRClient.
@@ -134,8 +138,8 @@ def set_os(self, name=None):
134138
if name not in self.os_class_map.keys():
135139
raise Exception("OS not supported!")
136140
else:
141+
LOGGER.debug("Using %s wrapper.", name or "Client")
137142
self.__client_class = self.os_class_map[name]
138-
logging.debug("Using %s wrapper.", name or "Client")
139143
return self
140144

141145
def set_secure(
@@ -257,7 +261,7 @@ def set_channel_option(self, name, value):
257261
found_index = index
258262
break
259263
if found_index is not None:
260-
logging.warning("Found existing channel option %s, overwriting!", name)
264+
LOGGER.warning("Found existing channel option %s, overwriting!", name)
261265
self.__channel_options[found_index] = new_option
262266
else:
263267
self.__channel_options.append(new_option)
@@ -279,26 +283,26 @@ def construct(self):
279283
self.__root_certificates, self.__private_key, self.__certificate_chain
280284
)
281285
if self.__username and self.__password:
286+
LOGGER.debug("Using username/password call authentication.")
282287
channel_metadata_creds = grpc.metadata_call_credentials(
283288
CiscoAuthPlugin(self.__username, self.__password)
284289
)
285-
logging.debug("Using username/password call authentication.")
286290
if channel_ssl_creds and channel_metadata_creds:
291+
LOGGER.debug("Using SSL/metadata authentication composite credentials.")
287292
channel_creds = grpc.composite_channel_credentials(
288293
channel_ssl_creds, channel_metadata_creds
289294
)
290-
logging.debug("Using SSL/metadata authentication composite credentials.")
291295
else:
296+
LOGGER.debug("Using SSL credentials, no metadata authentication.")
292297
channel_creds = channel_ssl_creds
293-
logging.debug("Using SSL credentials, no metadata authentication.")
294298
if self.__ssl_target_name_override is not False:
295299
if self.__ssl_target_name_override is None:
296300
if not self.__root_certificates:
297301
raise Exception("Deriving override requires root certificate!")
298302
self.__ssl_target_name_override = get_cn_from_cert(
299303
self.__root_certificates
300304
)
301-
logging.warning(
305+
LOGGER.warning(
302306
"Overriding SSL option from certificate could increase MITM susceptibility!"
303307
)
304308
self.set_channel_option(

src/cisco_gnmi/client.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
from . import util
3232

3333

34+
LOGGER = logging.getLogger(__name__)
35+
logger = LOGGER
36+
37+
3438
class Client(object):
3539
"""gNMI gRPC wrapper client to ease usage of gNMI.
3640
@@ -109,6 +113,7 @@ def capabilities(self):
109113
proto.gnmi_pb2.CapabilityResponse
110114
"""
111115
message = proto.gnmi_pb2.CapabilityRequest()
116+
LOGGER.debug(str(message))
112117
response = self.service.Capabilities(message)
113118
return response
114119

@@ -163,6 +168,9 @@ def get(
163168
request.use_models = use_models
164169
if extension:
165170
request.extension = extension
171+
172+
LOGGER.debug(str(request))
173+
166174
get_response = self.service.Get(request)
167175
return get_response
168176

@@ -190,7 +198,7 @@ def set(
190198
"""
191199
request = proto.gnmi_pb2.SetRequest()
192200
if prefix:
193-
request.prefix = prefix
201+
request.prefix.CopyFrom(prefix)
194202
test_list = [updates, replaces, deletes]
195203
if not any(test_list):
196204
raise Exception("At least update, replace, or delete must be specified!")
@@ -207,6 +215,9 @@ def set(
207215
request.delete.extend(deletes)
208216
if extensions:
209217
request.extension.extend(extensions)
218+
219+
LOGGER.debug(str(request))
220+
210221
response = self.service.Set(request)
211222
return response
212223

@@ -244,6 +255,9 @@ def validate_request(request):
244255
)
245256
if extensions:
246257
subscribe_request.extensions.extend(extensions)
258+
259+
LOGGER.debug(str(subscribe_request))
260+
247261
return subscribe_request
248262

249263
response_stream = self.service.Subscribe(

src/cisco_gnmi/nx.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
from .client import Client, proto, util
3131

3232

33+
LOGGER = logging.getLogger(__name__)
34+
logger = LOGGER
35+
36+
3337
class NXClient(Client):
3438
"""NX-OS-specific wrapper for gNMI functionality.
3539

src/cisco_gnmi/util.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
from urlparse import urlparse
3838

3939

40+
LOGGER = logging.getLogger(__name__)
41+
logger = LOGGER
42+
43+
4044
def gen_target_netloc(target, netloc_prefix="//", default_port=9339):
4145
"""Parses and validates a supplied target URL for gRPC calls.
4246
Uses urllib to parse the netloc property from the URL.
@@ -51,11 +55,11 @@ def gen_target_netloc(target, netloc_prefix="//", default_port=9339):
5155
if not parsed_target.netloc:
5256
raise ValueError("Unable to parse netloc from target URL %s!" % target)
5357
if parsed_target.scheme:
54-
logging.debug("Scheme identified in target, ignoring and using netloc.")
58+
LOGGER.debug("Scheme identified in target, ignoring and using netloc.")
5559
target_netloc = parsed_target
5660
if parsed_target.port is None:
5761
ported_target = "%s:%i" % (parsed_target.hostname, default_port)
58-
logging.debug("No target port detected, reassembled to %s.", ported_target)
62+
LOGGER.debug("No target port detected, reassembled to %s.", ported_target)
5963
target_netloc = gen_target_netloc(ported_target)
6064
return target_netloc
6165

@@ -120,11 +124,11 @@ def get_cn_from_cert(cert_pem):
120124
cert_cns = cert_parsed.subject.get_attributes_for_oid(x509.oid.NameOID.COMMON_NAME)
121125
if len(cert_cns) > 0:
122126
if len(cert_cns) > 1:
123-
logging.warning(
127+
LOGGER.warning(
124128
"Multiple CNs found for certificate, defaulting to the first one."
125129
)
126130
cert_cn = cert_cns[0].value
127-
logging.debug("Using %s as certificate CN.", cert_cn)
131+
LOGGER.debug("Using %s as certificate CN.", cert_cn)
128132
else:
129-
logging.warning("No CN found for certificate.")
133+
LOGGER.warning("No CN found for certificate.")
130134
return cert_cn

src/cisco_gnmi/xe.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
from .client import Client, proto, util
3131

3232

33+
LOGGER = logging.getLogger(__name__)
34+
logger = LOGGER
35+
36+
3337
class XEClient(Client):
3438
"""IOS XE-specific wrapper for gNMI functionality.
3539
Assumes IOS XE 16.12+
@@ -108,7 +112,13 @@ def delete_xpaths(self, xpaths, prefix=None):
108112
paths.append(self.parse_xpath_to_gnmi_path(xpath))
109113
return self.set(deletes=paths)
110114

111-
def set_json(self, update_json_configs=None, replace_json_configs=None, ietf=True):
115+
def set_json(
116+
self,
117+
update_json_configs=None,
118+
replace_json_configs=None,
119+
ietf=True,
120+
prefix=None,
121+
):
112122
"""A convenience wrapper for set() which assumes JSON payloads and constructs desired messages.
113123
All parameters are optional, but at least one must be present.
114124
@@ -132,15 +142,15 @@ def set_json(self, update_json_configs=None, replace_json_configs=None, ietf=Tru
132142
raise Exception("Must supply at least one set of configurations to method!")
133143

134144
def check_configs(name, configs):
135-
if isinstance(name, string_types):
136-
logging.debug("Handling %s as JSON string.", name)
145+
if isinstance(configs, string_types):
146+
LOGGER.debug("Handling %s as JSON string.", name)
137147
try:
138148
configs = json.loads(configs)
139149
except:
140150
raise Exception("{name} is invalid JSON!".format(name=name))
141151
configs = [configs]
142-
elif isinstance(name, dict):
143-
logging.debug("Handling %s as already serialized JSON object.", name)
152+
elif isinstance(configs, dict):
153+
LOGGER.debug("Handling %s as already serialized JSON object.", name)
144154
configs = [configs]
145155
elif not isinstance(configs, (list, set)):
146156
raise Exception(
@@ -171,7 +181,7 @@ def create_updates(name, configs):
171181

172182
updates = create_updates("update_json_configs", update_json_configs)
173183
replaces = create_updates("replace_json_configs", replace_json_configs)
174-
return self.set(updates=updates, replaces=replaces)
184+
return self.set(prefix=prefix, updates=updates, replaces=replaces)
175185

176186
def get_xpaths(self, xpaths, data_type="ALL", encoding="JSON_IETF"):
177187
"""A convenience wrapper for get() which forms proto.gnmi_pb2.Path from supplied xpaths.

src/cisco_gnmi/xr.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
from .client import Client, proto, util
3131

3232

33+
LOGGER = logging.getLogger(__name__)
34+
logger = LOGGER
35+
36+
3337
class XRClient(Client):
3438
"""IOS XR-specific wrapper for gNMI functionality.
3539
@@ -130,14 +134,14 @@ def set_json(self, update_json_configs=None, replace_json_configs=None, ietf=Tru
130134

131135
def check_configs(name, configs):
132136
if isinstance(name, string_types):
133-
logging.debug("Handling %s as JSON string.", name)
137+
LOGGER.debug("Handling %s as JSON string.", name)
134138
try:
135139
configs = json.loads(configs)
136140
except:
137141
raise Exception("{name} is invalid JSON!".format(name=name))
138142
configs = [configs]
139143
elif isinstance(name, dict):
140-
logging.debug("Handling %s as already serialized JSON object.", name)
144+
LOGGER.debug("Handling %s as already serialized JSON object.", name)
141145
configs = [configs]
142146
elif not isinstance(configs, (list, set)):
143147
raise Exception(

0 commit comments

Comments
 (0)