From 666b07017207b8b092ba34773fc8d81c20816eef Mon Sep 17 00:00:00 2001
From: Mihir Soni <mihson@amazon.com>
Date: Wed, 26 Jun 2019 13:00:59 -0700
Subject: [PATCH] Fix update monitor from moniotr list (#64)

Pass seqNo and PrimaryTerm for updateMonitor API.
---
 .../pages/Monitors/containers/Monitors/Monitors.js   |  7 +++++--
 .../Monitors/containers/Monitors/Monitors.test.js    | 12 +++++++++---
 server/services/MonitorService.js                    |  5 +++--
 3 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/public/pages/Monitors/containers/Monitors/Monitors.js b/public/pages/Monitors/containers/Monitors/Monitors.js
index 1864fb77..ed70c74a 100644
--- a/public/pages/Monitors/containers/Monitors/Monitors.js
+++ b/public/pages/Monitors/containers/Monitors/Monitors.js
@@ -180,9 +180,12 @@ export default class Monitors extends Component {
 
   updateMonitor(item, update) {
     const { httpClient } = this.props;
-    const { id, version, monitor } = item;
+    const { id, ifSeqNo, ifPrimaryTerm, monitor } = item;
     return httpClient
-      .put(`../api/alerting/monitors/${id}?version=${version}`, { ...monitor, ...update })
+      .put(`../api/alerting/monitors/${id}?ifSeqNo=${ifSeqNo}&ifPrimaryTerm=${ifPrimaryTerm}`, {
+        ...monitor,
+        ...update,
+      })
       .then(resp => resp)
       .catch(err => err);
   }
diff --git a/public/pages/Monitors/containers/Monitors/Monitors.test.js b/public/pages/Monitors/containers/Monitors/Monitors.test.js
index 30f34d92..f4eb3937 100644
--- a/public/pages/Monitors/containers/Monitors/Monitors.test.js
+++ b/public/pages/Monitors/containers/Monitors/Monitors.test.js
@@ -152,19 +152,25 @@ describe('Monitors', () => {
     const monitor = alertingFakes.randomMonitor();
     const response = await mountWrapper
       .instance()
-      .updateMonitor({ id: 'random_id', version: 17, monitor }, { name: 'UNIQUE_NAME' });
+      .updateMonitor(
+        { id: 'random_id', ifSeqNo: 17, ifPrimaryTerm: 20, monitor },
+        { name: 'UNIQUE_NAME' }
+      );
     mountWrapper.update();
 
     expect(updateMonitor).toHaveBeenCalled();
     expect(httpClientMock.put).toHaveBeenCalled();
     expect(httpClientMock.put).toHaveBeenCalledWith(
-      `../api/alerting/monitors/random_id?version=17`,
+      `../api/alerting/monitors/random_id?ifSeqNo=17&ifPrimaryTerm=20`,
       { ...monitor, name: 'UNIQUE_NAME' }
     );
     expect(response).toEqual({ data: { ok: true } });
     const error = await mountWrapper
       .instance()
-      .updateMonitor({ id: 'random_id', version: 17, monitor }, { name: 'UNIQUE_NAME' });
+      .updateMonitor(
+        { id: 'random_id', ifSeqNo: 17, ifPrimaryTerm: 20, monitor },
+        { name: 'UNIQUE_NAME' }
+      );
     expect(httpClientMock.put).toHaveBeenCalledTimes(2);
     expect(error.message).toBe('random error');
   });
diff --git a/server/services/MonitorService.js b/server/services/MonitorService.js
index d179d2df..f67f6cb9 100644
--- a/server/services/MonitorService.js
+++ b/server/services/MonitorService.js
@@ -156,6 +156,7 @@ export default class MonitorService {
 
       const params = {
         body: JSON.stringify({
+          seq_no_primary_term: true,
           version: true,
           ...monitorSortPageData,
           query: {
@@ -174,9 +175,9 @@ export default class MonitorService {
 
       const totalMonitors = _.get(getResponse, 'hits.total.value', 0);
       const monitorKeyValueTuples = _.get(getResponse, 'hits.hits', []).map(result => {
-        const { _id: id, _version: version, _source: monitor } = result;
+        const { _id: id, _version: version, _seq_no: ifSeqNo, _primary_term: ifPrimaryTerm, _source: monitor } = result;
         const { name, enabled } = monitor;
-        return [id, { id, version, name, enabled, monitor }];
+        return [id, { id, version, ifSeqNo, ifPrimaryTerm, name, enabled, monitor }];
       }, {});
       const monitorMap = new Map(monitorKeyValueTuples);
       const monitorIds = [...monitorMap.keys()];