@itentialopensource/adapter-meraki 0.8.1 → 0.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/adapter.js CHANGED
@@ -511,7 +511,7 @@ class Meraki extends AdapterBaseCl {
511
511
  log.trace(origin);
512
512
 
513
513
  // make sure we are set up for device broker getDevice
514
- if (!this.props.devicebroker || !this.props.devicebroker.getDevice || !this.props.devicebroker.getDevice.path) {
514
+ if (!this.allProps.devicebroker || !this.allProps.devicebroker.getDevice || this.allProps.devicebroker.getDevice.length === 0 || !this.allProps.devicebroker.getDevice[0].path) {
515
515
  const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Missing Properties', ['devicebroker.getDevice.path'], null, null, null);
516
516
  log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
517
517
  return callback(null, errorObj);
@@ -542,130 +542,38 @@ class Meraki extends AdapterBaseCl {
542
542
  log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
543
543
  return callback(null, errorObj);
544
544
  }
545
- // get the uuid from the device
546
- const { uuid } = devs.list[0];
547
-
548
- // !! using Generic makes it easier on the Adapter Builder (just need to change the path)
549
- // !! you can also replace with a specific call if that is easier
550
- let uriPath = '';
551
- let uriMethod = 'GET';
552
- let callQuery = {};
553
- let callBody = {};
554
- let callHeaders = {};
555
- let nameField = 'name';
556
- let nameArray = ['name'];
557
- let ostypeField = 'ostype';
558
- let ostypeArray = ['ostype'];
559
- let ostypePrefix = '';
560
- let portField = 'port';
561
- let portArray = ['port'];
562
- let ipField = 'ipaddress';
563
- let ipArray = ['ipaddress'];
564
- if (this.props.devicebroker.getDevice.path) {
565
- uriPath = `${this.props.devicebroker.getDevice.path}`;
566
- uriPath = uriPath.replace('{deviceid}', uuid);
567
- }
568
- if (this.props.devicebroker.getDevice.method) {
569
- uriMethod = this.props.devicebroker.getDevice.method;
570
- }
571
- if (this.props.devicebroker.getDevice.query) {
572
- try {
573
- callQuery = JSON.parse(this.props.devicebroker.getDevice.query);
574
- } catch (e) {
575
- log.warn('Could not parse query parameter for getDevice call');
576
- }
577
- }
578
- if (this.props.devicebroker.getDevice.body) {
579
- try {
580
- callBody = this.props.devicebroker.getDevice.body;
581
- } catch (e) {
582
- log.warn('Could not parse body for getDevice call');
583
- }
584
- }
585
- if (this.props.devicebroker.getDevice.headers) {
586
- try {
587
- callHeaders = this.props.devicebroker.getDevice.headers;
588
- } catch (e) {
589
- log.warn('Could not parse headers for getDevice call');
590
- }
591
- }
592
- if (this.props.devicebroker.getDevice.name_field) {
593
- nameField = this.props.devicebroker.getDevice.name_field;
594
- nameArray = nameField.split('.');
595
- }
596
- if (this.props.devicebroker.getDevice.ostype_field) {
597
- ostypeField = this.props.devicebroker.getDevice.ostype_field;
598
- ostypeArray = ostypeField.split('.');
599
- }
600
- if (this.props.devicebroker.getDevice.ostype_prefix) {
601
- ostypePrefix = this.props.devicebroker.getDevice.ostype_prefix;
602
- }
603
- if (this.props.devicebroker.getDevice.port_field) {
604
- portField = this.props.devicebroker.getDevice.port_field;
605
- portArray = portField.split('.');
606
- }
607
- if (this.props.devicebroker.getDevice.ip_field) {
608
- ipField = this.props.devicebroker.getDevice.ip_field;
609
- ipArray = ipField.split('.');
545
+
546
+ const callPromises = [];
547
+ for (let i = 0; i < this.allProps.devicebroker.getDevice.length; i += 1) {
548
+ // Perform component calls here.
549
+ callPromises.push(
550
+ new Promise((resolve, reject) => {
551
+ this.iapMakeBrokerCall('getDevice', this.allProps.devicebroker.getDevice[i], devs.list[0], null, (callRet, callErr) => {
552
+ // return an error
553
+ if (callErr) {
554
+ reject(callErr);
555
+ } else {
556
+ // return the data
557
+ resolve(callRet);
558
+ }
559
+ });
560
+ })
561
+ );
610
562
  }
611
563
 
612
- return this.genericAdapterRequest(uriPath, uriMethod, callQuery, callBody, callHeaders, (result, error) => {
613
- // if we received an error or their is no response on the results return an error
614
- if (error) {
615
- return callback(null, error);
616
- }
617
- if (!result.response) {
618
- const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Invalid Response', ['getDevice'], null, null, null);
619
- log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
620
- return callback(null, errorObj);
621
- }
564
+ // return an array of repsonses
565
+ return Promise.all(callPromises).then((results) => {
566
+ let myResult = {};
567
+ results.forEach((result) => {
568
+ myResult = { ...myResult, ...result };
569
+ });
622
570
 
623
- // return the response
624
- // !! format the data we send back
625
- // !! these fields are config manager fields you need to map to the data we receive
626
- const thisDevice = result.response;
627
- let thisName = thisDevice;
628
- for (let i = 0; i < nameArray.length; i += 1) {
629
- if (!Object.hasOwnProperty.call(thisName, nameArray[i])) {
630
- const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Invalid Response', ['getDevicesFiltered'], null, null, null);
631
- log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
632
- return callback(null, errorObj);
633
- }
634
- thisName = thisName[nameArray[i]];
635
- }
636
- thisDevice.name = thisName;
637
- let thisOstype = thisDevice;
638
- for (let i = 0; i < ostypeArray.length; i += 1) {
639
- if (!Object.hasOwnProperty.call(thisOstype, ostypeArray[i])) {
640
- const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Invalid Response', ['getDevicesFiltered'], null, null, null);
641
- log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
642
- return callback(null, errorObj);
643
- }
644
- thisOstype = thisOstype[ostypeArray[i]];
645
- }
646
- thisDevice.ostype = ostypePrefix + thisOstype;
647
- let thisPort = thisDevice;
648
- for (let i = 0; i < portArray.length; i += 1) {
649
- if (!Object.hasOwnProperty.call(thisPort, portArray[i])) {
650
- const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Invalid Response', ['getDevicesFiltered'], null, null, null);
651
- log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
652
- return callback(null, errorObj);
653
- }
654
- thisPort = thisPort[portArray[i]];
655
- }
656
- thisDevice.port = thisPort;
657
- let thisIp = thisDevice;
658
- for (let i = 0; i < ipArray.length; i += 1) {
659
- if (!Object.hasOwnProperty.call(thisIp, ipArray[i])) {
660
- const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Invalid Response', ['getDevicesFiltered'], null, null, null);
661
- log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
662
- return callback(null, errorObj);
663
- }
664
- thisIp = thisIp[ipArray[i]];
665
- }
666
- thisDevice.ipaddress = thisIp;
667
- return callback(thisDevice);
668
- });
571
+ return callback(myResult, null);
572
+ })
573
+ .catch((error) => {
574
+ log.debug(`Caught ${JSON.stringify(error)}`);
575
+ return callback(null, error);
576
+ });
669
577
  });
670
578
  } catch (ex) {
671
579
  const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Caught Exception', null, null, null, ex);
@@ -689,7 +597,7 @@ class Meraki extends AdapterBaseCl {
689
597
  log.trace(origin);
690
598
 
691
599
  // make sure we are set up for device broker getDevicesFiltered
692
- if (!this.props.devicebroker || !this.props.devicebroker.getDevicesFiltered || !this.props.devicebroker.getDevicesFiltered.path) {
600
+ if (!this.allProps.devicebroker || !this.allProps.devicebroker.getDevicesFiltered || this.allProps.devicebroker.getDevicesFiltered.length === 0 || !this.allProps.devicebroker.getDevicesFiltered[0].path) {
693
601
  const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Missing Properties', ['devicebroker.getDevicesFiltered.path'], null, null, null);
694
602
  log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
695
603
  return callback(null, errorObj);
@@ -703,248 +611,69 @@ class Meraki extends AdapterBaseCl {
703
611
  }
704
612
  log.debug(`Device Filter Options: ${JSON.stringify(options)}`);
705
613
 
706
- // TODO - get pagination working
707
- // const nextToken = options.start;
708
- // const maxResults = options.limit;
709
-
710
- // set up the filter of Device Names
711
- let filterName = [];
712
- if (options && options.filter && options.filter.name) {
713
- // when this hack is removed, remove the lint ignore above
714
- if (Array.isArray(options.filter.name)) {
715
- // eslint-disable-next-line prefer-destructuring
716
- filterName = options.filter.name;
717
- } else {
718
- filterName = [options.filter.name];
719
- }
720
- }
721
-
722
- // TODO - get sort and order working
723
- /*
724
- if (options && options.sort) {
725
- reqObj.uriOptions.sort = JSON.stringify(options.sort);
726
- }
727
- if (options && options.order) {
728
- reqObj.uriOptions.order = options.order;
729
- }
730
- */
731
614
  try {
732
- // !! using Generic makes it easier on the Adapter Builder (just need to change the path)
733
- // !! you can also replace with a specific call if that is easier
734
- let uriPath = '';
735
- let uriMethod = 'GET';
736
- let callQuery = {};
737
- let callBody = {};
738
- let callHeaders = {};
739
- let nameField = 'name';
740
- let nameArray = ['name'];
741
- let ostypeField = 'ostype';
742
- let ostypeArray = ['ostype'];
743
- let ostypePrefix = '';
744
- let portField = 'port';
745
- let portArray = ['port'];
746
- let ipField = 'ipaddress';
747
- let ipArray = ['ipaddress'];
748
- if (this.props.devicebroker.getDevicesFiltered.path) {
749
- uriPath = this.props.devicebroker.getDevicesFiltered.path;
750
- }
751
- if (this.props.devicebroker.getDevicesFiltered.method) {
752
- uriMethod = this.props.devicebroker.getDevicesFiltered.method;
753
- }
754
- if (this.props.devicebroker.getDevicesFiltered.query) {
755
- try {
756
- callQuery = this.props.devicebroker.getDevicesFiltered.query;
757
- } catch (e) {
758
- log.warn('Could not parse query parameter for getDevicesFiltered call');
759
- }
760
- }
761
- if (this.props.devicebroker.getDevicesFiltered.body) {
762
- try {
763
- callBody = this.props.devicebroker.getDevicesFiltered.body;
764
- } catch (e) {
765
- log.warn('Could not parse body for getDevicesFiltered call');
766
- }
767
- }
768
- if (this.props.devicebroker.getDevicesFiltered.headers) {
769
- try {
770
- callHeaders = this.props.devicebroker.getDevicesFiltered.headers;
771
- } catch (e) {
772
- log.warn('Could not parse headers for getDevicesFiltered call');
773
- }
774
- }
775
- if (this.props.devicebroker.getDevicesFiltered.name_field) {
776
- nameField = this.props.devicebroker.getDevicesFiltered.name_field;
777
- nameArray = nameField.split('.');
778
- }
779
- if (this.props.devicebroker.getDevicesFiltered.ostype_field) {
780
- ostypeField = this.props.devicebroker.getDevicesFiltered.ostype_field;
781
- ostypeArray = ostypeField.split('.');
782
- }
783
- if (this.props.devicebroker.getDevicesFiltered.ostype_prefix) {
784
- ostypePrefix = this.props.devicebroker.getDevicesFiltered.ostype_prefix;
785
- }
786
- if (this.props.devicebroker.getDevicesFiltered.port_field) {
787
- portField = this.props.devicebroker.getDevicesFiltered.port_field;
788
- portArray = portField.split('.');
789
- }
790
- if (this.props.devicebroker.getDevicesFiltered.ip_field) {
791
- ipField = this.props.devicebroker.getDevicesFiltered.ip_field;
792
- ipArray = ipField.split('.');
793
- }
794
-
795
- return this.genericAdapterRequest(uriPath, uriMethod, callQuery, callBody, callHeaders, (result, error) => {
796
- // if we received an error or their is no response on the results return an error
797
- if (error) {
798
- return callback(null, error);
799
- }
800
- if (!result.response) {
801
- const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Invalid Response', ['getDevicesFiltered'], null, null, null);
802
- log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
803
- return callback(null, errorObj);
804
- }
805
-
806
- // !! go through the response - may have to look for sub object
807
- // handle an array of devices
808
- if (Array.isArray(result.response)) {
809
- const myDevices = [];
810
-
811
- for (let d = 0; d < result.response.length; d += 1) {
812
- // !! format the data we send back
813
- // !! these fields are config manager fields you need to map to the data we receive
814
- const thisDevice = result.response;
815
- let thisName = thisDevice;
816
- for (let i = 0; i < nameArray.length; i += 1) {
817
- if (!Object.hasOwnProperty.call(thisName, nameArray[i])) {
818
- const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Invalid Response', ['getDevicesFiltered'], null, null, null);
819
- log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
820
- return callback(null, errorObj);
821
- }
822
- thisName = thisName[nameArray[i]];
823
- }
824
- thisDevice.name = thisName;
825
- let thisOstype = thisDevice;
826
- for (let i = 0; i < ostypeArray.length; i += 1) {
827
- if (!Object.hasOwnProperty.call(thisOstype, ostypeArray[i])) {
828
- const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Invalid Response', ['getDevicesFiltered'], null, null, null);
829
- log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
830
- return callback(null, errorObj);
831
- }
832
- thisOstype = thisOstype[ostypeArray[i]];
833
- }
834
- thisDevice.ostype = ostypePrefix + thisOstype;
835
- let thisPort = thisDevice;
836
- for (let i = 0; i < portArray.length; i += 1) {
837
- if (!Object.hasOwnProperty.call(thisPort, portArray[i])) {
838
- const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Invalid Response', ['getDevicesFiltered'], null, null, null);
839
- log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
840
- return callback(null, errorObj);
841
- }
842
- thisPort = thisPort[portArray[i]];
843
- }
844
- thisDevice.port = thisPort;
845
- let thisIp = thisDevice;
846
- for (let i = 0; i < ipArray.length; i += 1) {
847
- if (!Object.hasOwnProperty.call(thisIp, ipArray[i])) {
848
- const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Invalid Response', ['getDevicesFiltered'], null, null, null);
849
- log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
850
- return callback(null, errorObj);
851
- }
852
- thisIp = thisIp[ipArray[i]];
853
- }
854
- thisDevice.ipaddress = thisIp;
855
-
856
- // if there is no filter - return the device
857
- if (filterName.length === 0) {
858
- myDevices.push(thisDevice);
859
- } else {
860
- // if we have to match a filter
861
- let found = false;
862
- for (let f = 0; f < filterName.length; f += 1) {
863
- if (thisDevice.name.indexOf(filterName[f]) >= 0) {
864
- found = true;
865
- break;
866
- }
867
- }
868
- // matching device
869
- if (found) {
870
- myDevices.push(thisDevice);
615
+ // TODO - get pagination working
616
+ // const nextToken = options.start;
617
+ // const maxResults = options.limit;
618
+
619
+ // set up the filter of Device Names
620
+ let filterName = [];
621
+ if (options && options.filter && options.filter.name) {
622
+ // when this hack is removed, remove the lint ignore above
623
+ if (Array.isArray(options.filter.name)) {
624
+ // eslint-disable-next-line prefer-destructuring
625
+ filterName = options.filter.name;
626
+ } else {
627
+ filterName = [options.filter.name];
628
+ }
629
+ }
630
+
631
+ // TODO - get sort and order working
632
+ /*
633
+ if (options && options.sort) {
634
+ reqObj.uriOptions.sort = JSON.stringify(options.sort);
635
+ }
636
+ if (options && options.order) {
637
+ reqObj.uriOptions.order = options.order;
638
+ }
639
+ */
640
+ const callPromises = [];
641
+ for (let i = 0; i < this.allProps.devicebroker.getDevicesFiltered.length; i += 1) {
642
+ // Perform component calls here.
643
+ callPromises.push(
644
+ new Promise((resolve, reject) => {
645
+ this.iapMakeBrokerCall('getDevicesFiltered', this.allProps.devicebroker.getDevicesFiltered[i], {}, filterName, (callRet, callErr) => {
646
+ // return an error
647
+ if (callErr) {
648
+ reject(callErr);
649
+ } else {
650
+ // return the data
651
+ resolve(callRet);
871
652
  }
872
- }
873
- }
874
- log.debug(`${origin}: Found #${myDevices.length} devices.`);
875
- log.debug(`Devices: ${JSON.stringify(myDevices)}`);
876
- return callback({ total: myDevices.length, list: myDevices });
877
- }
878
- // handle a single device response
879
- // !! format the data we send back
880
- // !! these fields are config manager fields you need to map to the data we receive
881
- const thisDevice = result.response;
882
- let thisName = thisDevice;
883
- for (let i = 0; i < nameArray.length; i += 1) {
884
- if (!Object.hasOwnProperty.call(thisName, nameArray[i])) {
885
- const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Invalid Response', ['getDevicesFiltered'], null, null, null);
886
- log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
887
- return callback(null, errorObj);
888
- }
889
- thisName = thisName[nameArray[i]];
890
- }
891
- thisDevice.name = thisName;
892
- let thisOstype = thisDevice;
893
- for (let i = 0; i < ostypeArray.length; i += 1) {
894
- if (!Object.hasOwnProperty.call(thisOstype, ostypeArray[i])) {
895
- const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Invalid Response', ['getDevicesFiltered'], null, null, null);
896
- log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
897
- return callback(null, errorObj);
898
- }
899
- thisOstype = thisOstype[ostypeArray[i]];
900
- }
901
- thisDevice.ostype = ostypePrefix + thisOstype;
902
- let thisPort = thisDevice;
903
- for (let i = 0; i < portArray.length; i += 1) {
904
- if (!Object.hasOwnProperty.call(thisPort, portArray[i])) {
905
- const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Invalid Response', ['getDevicesFiltered'], null, null, null);
906
- log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
907
- return callback(null, errorObj);
653
+ });
654
+ })
655
+ );
656
+ }
657
+
658
+ // return an array of repsonses
659
+ return Promise.all(callPromises).then((results) => {
660
+ let myResult = [];
661
+ results.forEach((result) => {
662
+ if (Array.isArray(result)) {
663
+ myResult = [...myResult, ...result];
664
+ } else if (Object.keys(result).length > 0) {
665
+ myResult.push(result);
908
666
  }
909
- thisPort = thisPort[portArray[i]];
910
- }
911
- thisDevice.port = thisPort;
912
- let thisIp = thisDevice;
913
- for (let i = 0; i < ipArray.length; i += 1) {
914
- if (!Object.hasOwnProperty.call(thisIp, ipArray[i])) {
915
- const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Invalid Response', ['getDevicesFiltered'], null, null, null);
916
- log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
917
- return callback(null, errorObj);
918
- }
919
- thisIp = thisIp[ipArray[i]];
920
- }
921
- thisDevice.ipaddress = thisIp;
922
-
923
- // if there is no filter - return the device
924
- if (filterName.length === 0) {
925
- log.debug(`${origin}: Found #1 device.`);
926
- log.debug(`Device: ${JSON.stringify(thisDevice)}`);
927
- return callback({ total: 1, list: [thisDevice] });
928
- }
667
+ });
929
668
 
930
- // if there is a filter need to check for matching device
931
- let found = false;
932
- for (let f = 0; f < filterName.length; f += 1) {
933
- if (thisDevice.name.indexOf(filterName[f]) >= 0) {
934
- found = true;
935
- break;
936
- }
937
- }
938
- // matching device
939
- if (found) {
940
- log.debug(`${origin}: Found #1 device.`);
941
- log.debug(`Device Found: ${JSON.stringify(thisDevice)}`);
942
- return callback({ total: 1, list: [thisDevice] });
943
- }
944
- // not a matching device
945
- log.debug(`${origin}: No matching device found.`);
946
- return callback({ total: 0, list: [] });
947
- });
669
+ log.debug(`${origin}: Found #${myResult.length} devices.`);
670
+ log.debug(`Devices: ${JSON.stringify(myResult)}`);
671
+ return callback({ total: myResult.length, list: myResult });
672
+ })
673
+ .catch((error) => {
674
+ log.debug(`Caught ${JSON.stringify(error)}`);
675
+ return callback(null, error);
676
+ });
948
677
  } catch (ex) {
949
678
  const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Caught Exception', null, null, null, ex);
950
679
  log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
@@ -967,7 +696,7 @@ class Meraki extends AdapterBaseCl {
967
696
  log.trace(origin);
968
697
 
969
698
  // make sure we are set up for device broker isAlive
970
- if (!this.props.devicebroker || !this.props.devicebroker.isAlive || !this.props.devicebroker.isAlive.path) {
699
+ if (!this.allProps.devicebroker || !this.allProps.devicebroker.isAlive || this.allProps.devicebroker.isAlive.length === 0 || !this.allProps.devicebroker.isAlive[0].path) {
971
700
  const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Missing Properties', ['devicebroker.isAlive.path'], null, null, null);
972
701
  log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
973
702
  return callback(null, errorObj);
@@ -998,84 +727,42 @@ class Meraki extends AdapterBaseCl {
998
727
  log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
999
728
  return callback(null, errorObj);
1000
729
  }
1001
- // get the uuid from the device
1002
- const { uuid } = devs.list[0];
1003
-
1004
- // !! using Generic makes it easier on the Adapter Builder (just need to change the path)
1005
- // !! you can also replace with a specific call if that is easier
1006
- let uriPath = '';
1007
- let uriMethod = 'GET';
1008
- let callQuery = {};
1009
- let callBody = {};
1010
- let callHeaders = {};
1011
- let statusField = 'status';
1012
- let statusArray = ['status'];
1013
- let statusValue = 'true';
1014
- if (this.props.devicebroker.isAlive.path) {
1015
- uriPath = `${this.props.devicebroker.isAlive.path}`;
1016
- uriPath = uriPath.replace('{deviceid}', uuid);
1017
- }
1018
- if (this.props.devicebroker.isAlive.method) {
1019
- uriMethod = this.props.devicebroker.isAlive.method;
1020
- }
1021
- if (this.props.devicebroker.isAlive.query) {
1022
- try {
1023
- callQuery = this.props.devicebroker.isAlive.query;
1024
- } catch (e) {
1025
- log.warn('Could not parse query parameter for isAlive call');
1026
- }
1027
- }
1028
- if (this.props.devicebroker.isAlive.body) {
1029
- try {
1030
- callBody = this.props.devicebroker.isAlive.body;
1031
- } catch (e) {
1032
- log.warn('Could not parse body for isAlive call');
1033
- }
1034
- }
1035
- if (this.props.devicebroker.isAlive.headers) {
1036
- try {
1037
- callHeaders = this.props.devicebroker.isAlive.headers;
1038
- } catch (e) {
1039
- log.warn('Could not parse headers for isAlive call');
1040
- }
1041
- }
1042
- if (this.props.devicebroker.isAlive.response && this.props.devicebroker.isAlive.status_field) {
1043
- statusField = this.props.devicebroker.isAlive.status_field;
1044
- statusArray = statusField.split('.');
1045
- }
1046
- if (this.props.devicebroker.isAlive.response && this.props.devicebroker.isAlive.status_value) {
1047
- statusValue = this.props.devicebroker.isAlive.response.status_value;
1048
- }
1049
730
 
1050
- return this.genericAdapterRequest(uriPath, uriMethod, callQuery, callBody, callHeaders, (result, error) => {
1051
- // if we received an error or their is no response on the results return an error
1052
- if (error) {
1053
- return callback(null, error);
1054
- }
731
+ const callPromises = [];
732
+ for (let i = 0; i < this.allProps.devicebroker.isAlive.length; i += 1) {
733
+ // Perform component calls here.
734
+ callPromises.push(
735
+ new Promise((resolve, reject) => {
736
+ this.iapMakeBrokerCall('isAlive', this.allProps.devicebroker.isAlive[i], devs.list[0], null, (callRet, callErr) => {
737
+ // return an error
738
+ if (callErr) {
739
+ reject(callErr);
740
+ } else {
741
+ // return the data
742
+ resolve(callRet);
743
+ }
744
+ });
745
+ })
746
+ );
747
+ }
1055
748
 
1056
- // !! should update this to make sure we are checking for the appropriate object/field
1057
- if (!result.response || !result.response.returnObj) {
1058
- const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Invalid Response', ['isAlive'], null, null, null);
1059
- log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
1060
- return callback(null, errorObj);
1061
- }
1062
- let thisObj = result.response.returnObj;
1063
- for (let i = 0; i < statusArray.length; i += 1) {
1064
- if (!Object.hasOwnProperty.call(thisObj, statusArray[i])) {
1065
- const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Invalid Response', ['isAlive'], null, null, null);
1066
- log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
1067
- return callback(null, errorObj);
1068
- }
1069
- thisObj = thisObj[statusArray[i]];
1070
- }
749
+ // return an array of repsonses
750
+ return Promise.all(callPromises).then((results) => {
751
+ let myResult = {};
752
+ results.forEach((result) => {
753
+ myResult = { ...myResult, ...result };
754
+ });
1071
755
 
1072
- // !! return the response - Update to the appropriate object/field
1073
756
  let response = true;
1074
- if (thisObj.toString() !== statusValue) {
757
+ if (myResult.isAlive !== null && myResult.isAlive !== undefined && myResult.isAlive === false) {
1075
758
  response = false;
1076
759
  }
1077
760
  return callback(response);
1078
- });
761
+ })
762
+ .catch((error) => {
763
+ log.debug(`Caught ${JSON.stringify(error)}`);
764
+ return callback(null, error);
765
+ });
1079
766
  });
1080
767
  } catch (ex) {
1081
768
  const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Caught Exception', null, null, null, ex);
@@ -1100,7 +787,7 @@ class Meraki extends AdapterBaseCl {
1100
787
  log.trace(origin);
1101
788
 
1102
789
  // make sure we are set up for device broker getConfig
1103
- if (!this.props.devicebroker || !this.props.devicebroker.getConfig || !this.props.devicebroker.getConfig.path) {
790
+ if (!this.allProps.devicebroker || !this.allProps.devicebroker.getConfig || this.allProps.devicebroker.getConfig.length === 0 || !this.allProps.devicebroker.getConfig[0].path) {
1104
791
  const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Missing Properties', ['devicebroker.getConfig.path'], null, null, null);
1105
792
  log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
1106
793
  return callback(null, errorObj);
@@ -1131,57 +818,42 @@ class Meraki extends AdapterBaseCl {
1131
818
  log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
1132
819
  return callback(null, errorObj);
1133
820
  }
1134
- // get the uuid from the device
1135
- const { uuid } = devs.list[0];
1136
821
 
1137
- // !! using Generic makes it easier on the Adapter Builder (just need to change the path)
1138
- // !! you can also replace with a specific call if that is easier
1139
- let uriPath = '';
1140
- let uriMethod = 'GET';
1141
- let callQuery = {};
1142
- let callBody = {};
1143
- let callHeaders = {};
1144
- if (this.props.devicebroker.getConfig.path) {
1145
- uriPath = `${this.props.devicebroker.getConfig.path}`;
1146
- uriPath = uriPath.replace('{deviceid}', uuid);
1147
- }
1148
- if (this.props.devicebroker.getConfig.method) {
1149
- uriMethod = this.props.devicebroker.getConfig.method;
1150
- }
1151
- if (this.props.devicebroker.getConfig.query) {
1152
- try {
1153
- callQuery = this.props.devicebroker.getConfig.query;
1154
- } catch (e) {
1155
- log.warn('Could not parse query parameter for getConfig call');
1156
- }
1157
- }
1158
- if (this.props.devicebroker.getConfig.body) {
1159
- try {
1160
- callBody = this.props.devicebroker.getConfig.body;
1161
- } catch (e) {
1162
- log.warn('Could not parse body for getConfig call');
1163
- }
1164
- }
1165
- if (this.props.devicebroker.getConfig.headers) {
1166
- try {
1167
- callHeaders = this.props.devicebroker.getConfig.headers;
1168
- } catch (e) {
1169
- log.warn('Could not parse headers for getConfig call');
1170
- }
822
+ const callPromises = [];
823
+ for (let i = 0; i < this.allProps.devicebroker.getConfig.length; i += 1) {
824
+ // Perform component calls here.
825
+ callPromises.push(
826
+ new Promise((resolve, reject) => {
827
+ this.iapMakeBrokerCall('getConfig', this.allProps.devicebroker.getConfig[i], devs.list[0], null, (callRet, callErr) => {
828
+ // return an error
829
+ if (callErr) {
830
+ reject(callErr);
831
+ } else {
832
+ // return the data
833
+ resolve(callRet);
834
+ }
835
+ });
836
+ })
837
+ );
1171
838
  }
1172
839
 
1173
- return this.genericAdapterRequest(uriPath, uriMethod, callQuery, callBody, callHeaders, (result, error) => {
1174
- // if we received an error or their is no response on the results return an error
1175
- if (error) {
1176
- return callback(null, error);
1177
- }
840
+ // return an array of repsonses
841
+ return Promise.all(callPromises).then((results) => {
842
+ let myResult = {};
843
+ results.forEach((result) => {
844
+ myResult = { ...myResult, ...result };
845
+ });
1178
846
 
1179
847
  // return the result
1180
848
  const newResponse = {
1181
- response: JSON.stringify(result.response, null, 2)
849
+ response: JSON.stringify(myResult, null, 2)
1182
850
  };
1183
- return callback(newResponse);
1184
- });
851
+ return callback(newResponse, null);
852
+ })
853
+ .catch((error) => {
854
+ log.debug(`Caught ${JSON.stringify(error)}`);
855
+ return callback(null, error);
856
+ });
1185
857
  });
1186
858
  } catch (ex) {
1187
859
  const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Caught Exception', null, null, null, ex);
@@ -1204,7 +876,7 @@ class Meraki extends AdapterBaseCl {
1204
876
  log.trace(origin);
1205
877
 
1206
878
  // make sure we are set up for device broker getCount
1207
- if (!this.props.devicebroker || !this.props.devicebroker.getCount || !this.props.devicebroker.getCount.path) {
879
+ if (!this.allProps.devicebroker || !this.allProps.devicebroker.getCount || this.allProps.devicebroker.getCount.length === 0 || !this.allProps.devicebroker.getCount[0].path) {
1208
880
  const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Missing Properties', ['devicebroker.getCount.path'], null, null, null);
1209
881
  log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
1210
882
  return callback(null, errorObj);
@@ -1213,50 +885,38 @@ class Meraki extends AdapterBaseCl {
1213
885
  // verify the required fields have been provided
1214
886
 
1215
887
  try {
1216
- // !! using Generic makes it easier on the Adapter Builder (just need to set the path or call in properties)
1217
- // !! you can also replace with a specific call if that is easier
1218
- let uriPath = '';
1219
- let uriMethod = 'GET';
1220
- let callQuery = {};
1221
- let callBody = {};
1222
- let callHeaders = {};
1223
- if (this.props.devicebroker.getCount.path) {
1224
- uriPath = this.props.devicebroker.getCount.path;
1225
- }
1226
- if (this.props.devicebroker.getCount.method) {
1227
- uriMethod = this.props.devicebroker.getCount.method;
1228
- }
1229
- if (this.props.devicebroker.getCount.query) {
1230
- try {
1231
- callQuery = this.props.devicebroker.getCount.query;
1232
- } catch (e) {
1233
- log.warn('Could not parse query parameter for getCount call');
1234
- }
1235
- }
1236
- if (this.props.devicebroker.getCount.body) {
1237
- try {
1238
- callBody = this.props.devicebroker.getCount.body;
1239
- } catch (e) {
1240
- log.warn('Could not parse body for getCount call');
1241
- }
1242
- }
1243
- if (this.props.devicebroker.getCount.headers) {
1244
- try {
1245
- callHeaders = this.props.devicebroker.getCount.headers;
1246
- } catch (e) {
1247
- log.warn('Could not parse headers for getCount call');
1248
- }
888
+ const callPromises = [];
889
+ for (let i = 0; i < this.allProps.devicebroker.getCount.length; i += 1) {
890
+ // Perform component calls here.
891
+ callPromises.push(
892
+ new Promise((resolve, reject) => {
893
+ this.iapMakeBrokerCall('getCount', this.allProps.devicebroker.getCount[i], null, null, (callRet, callErr) => {
894
+ // return an error
895
+ if (callErr) {
896
+ reject(callErr);
897
+ } else {
898
+ // return the data
899
+ resolve(callRet);
900
+ }
901
+ });
902
+ })
903
+ );
1249
904
  }
1250
905
 
1251
- return this.genericAdapterRequest(uriPath, uriMethod, callQuery, callBody, callHeaders, (result, error) => {
1252
- // if we received an error or their is no response on the results return an error
1253
- if (error) {
1254
- return callback(null, error);
1255
- }
906
+ // return an array of repsonses
907
+ return Promise.all(callPromises).then((results) => {
908
+ let myResult = {};
909
+ results.forEach((result) => {
910
+ myResult = { ...myResult, ...result };
911
+ });
1256
912
 
1257
913
  // return the result
1258
- return callback({ count: result.response });
1259
- });
914
+ return callback({ count: myResult.length });
915
+ })
916
+ .catch((error) => {
917
+ log.debug(`Caught ${JSON.stringify(error)}`);
918
+ return callback(null, error);
919
+ });
1260
920
  } catch (ex) {
1261
921
  const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Caught Exception', null, null, null, ex);
1262
922
  log.error(`${origin}: ${errorObj.IAPerror.displayString}`);