Skip to content

Commit 41ff3c3

Browse files
author
kkumara3
committed
Added error handling in client, and updated examples
1 parent 3313334 commit 41ff3c3

File tree

3 files changed

+64
-22
lines changed

3 files changed

+64
-22
lines changed

examples/grpc_cfg.py

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,65 @@
33
This is an example to show replace and merge configs work with a get command.
44
The example is using XRdocs vagrant topology for all the configurations
55
'''
6-
6+
from grpc.framework.interfaces.face.face import AbortionError
7+
import json
8+
from time import sleep
79
import sys
810
sys.path.insert(0, '../')
911
from lib.cisco_grpc_client import CiscoGRPCClient
10-
import json
11-
from time import sleep
1212

13-
class Example:
13+
14+
class Example(object):
1415
def __init__(self):
15-
self.client = CiscoGRPCClient('11.1.1.10', 57777, 10, 'vagrant', 'vagrant')
16+
self.client = CiscoGRPCClient('127.0.0.1', 57777, 10, 'vagrant', 'vagrant')
1617
def get(self):
1718
path = '{"Cisco-IOS-XR-ipv4-bgp-cfg:bgp": [null]}'
1819
result = self.client.getconfig(path)
19-
print result
20+
try:
21+
err, result = self.client.getconfig(path)
22+
if err:
23+
print err
24+
print json.dumps(json.loads(result))
25+
except AbortionError:
26+
print(
27+
'Unable to connect to local box, check your gRPC destination.'
28+
)
2029

2130
def replace(self):
2231
path = open('snips/bgp_start.json').read()
23-
result = self.client.replaceconfig(path)
24-
print result # If this is sucessful, then there should be no errors.
32+
try:
33+
response = self.client.replaceconfig(path)
34+
if response.errors:
35+
err = json.loads(response.errors)
36+
print err
37+
except AbortionError:
38+
print(
39+
'Unable to connect to local box, check your gRPC destination.'
40+
)
2541

2642
def merge(self):
2743
path = open('snips/bgp_merge.json').read()
28-
result = self.client.mergeconfig(path)
29-
print result # If this is sucessful, then there should be no errors.
44+
try:
45+
response = self.client.mergeconfig(path)
46+
if response.errors:
47+
err = json.loads(response.errors)
48+
print err
49+
except AbortionError:
50+
print(
51+
'Unable to connect to local box, check your gRPC destination.'
52+
)
3053

3154
def delete(self):
3255
path = open('snips/bgp_start.json').read()
33-
result = self.client.deleteconfig(path)
34-
print result # If this is sucessful, then there should be no errors.
56+
try:
57+
response = self.client.deleteconfig(path)
58+
if response.errors:
59+
err = json.loads(response.errors)
60+
print err
61+
except AbortionError:
62+
print(
63+
'Unable to connect to local box, check your gRPC destination.'
64+
)
3565

3666
def main():
3767
'''

examples/grpc_example.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
""" Basic gRPC getcall configuration. Shows how to set up the vairables
22
for tls and how to put into the class and get information from the box.
33
"""
4+
import json
5+
from grpc.framework.interfaces.face.face import AbortionError
46
import sys
57
sys.path.insert(0, '../')
68
from lib.cisco_grpc_client import CiscoGRPCClient
7-
import json
89

910
def main():
1011
'''
@@ -14,12 +15,19 @@ def main():
1415
ex: client = CiscoGRPCClient('11.1.1.10', 57777, 10, 'vagrant', 'vagrant')
1516
'''
1617
creds = open('ems.pem').read()
17-
options='ems.cisco.com'
18-
client = CiscoGRPCClient('11.1.1.10', 57777, 10, 'vagrant', 'vagrant', creds, options)
18+
options = 'ems.cisco.com'
19+
client = CiscoGRPCClient('127.0.0.1', 57777, 10, 'vagrant', 'vagrant', creds, options)
1920
#Test 1: Test Get config json requests
2021
path = '{"Cisco-IOS-XR-ip-static-cfg:router-static": [null]}'
21-
result = client.getconfig(path)
22-
print json.dumps(json.loads(result))
22+
try:
23+
err, result = client.getconfig(path)
24+
if err:
25+
print err
26+
print json.dumps(json.loads(result))
27+
except AbortionError:
28+
print(
29+
'Unable to connect to local box, check your gRPC destination.'
30+
)
2331

2432
if __name__ == '__main__':
2533
main()

lib/cisco_grpc_client.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,11 @@ def getconfig(self, path):
8080
"""
8181
message = ems_grpc_pb2.ConfigGetArgs(yangpathjson=path)
8282
responses = self._stub.GetConfig(message, self._timeout, metadata=self._metadata)
83-
objects = ''
83+
objects, err = '', ''
8484
for response in responses:
8585
objects += response.yangjson
86-
return objects
86+
err += response.errors
87+
return err, objects
8788

8889
def getsubscription(self, sub_id, unmarshal):
8990
"""Telemetry subscription function
@@ -152,9 +153,10 @@ def getoper(self, path):
152153
"""
153154
message = ems_grpc_pb2.GetOperArgs(yangpathjson=path)
154155
responses = self._stub.GetOper(message, self._timeout, metadata=self._metadata)
155-
objects = ''
156+
objects, err = '', ''
156157
for response in responses:
157158
objects += response.yangjson
159+
err += response.errors
158160
return objects
159161

160162
def cliconfig(self, cli):
@@ -178,9 +180,10 @@ def showcmdtextoutput(self, cli):
178180
stub = ems_grpc_pb2.beta_create_gRPCExec_stub(self._channel)
179181
message = ems_grpc_pb2.ShowCmdArgs(cli=cli)
180182
responses = stub.ShowCmdTextOutput(message, self._timeout, metadata=self._metadata)
181-
objects = ''
183+
objects, err = '', ''
182184
for response in responses:
183185
objects += response.output
186+
err += response.errors
184187
return objects
185188

186189
def showcmdjsonoutput(self, cli):
@@ -193,7 +196,8 @@ def showcmdjsonoutput(self, cli):
193196
stub = ems_grpc_pb2.beta_create_gRPCExec_stub(self._channel)
194197
message = ems_grpc_pb2.ShowCmdArgs(cli=cli)
195198
responses = stub.ShowCmdJSONOutput(message, self._timeout, metadata=self._metadata)
196-
objects = ''
199+
objects, err = '', ''
197200
for response in responses:
198201
objects += response.jsonoutput
202+
err += response.errors
199203
return objects

0 commit comments

Comments
 (0)