From d655502ec055c11a25eac6a0440c8c8fdca7a261 Mon Sep 17 00:00:00 2001
From: Severin Neumann <severin.neumann@altmuehlnet.de>
Date: Tue, 26 Mar 2019 11:03:44 +0100
Subject: [PATCH] Add dashboard get ; add recipe to update dashboard

---
 USAGE.md                                    |  3 +-
 act.sh                                      |  8 ++++--
 commands.yml                                | 10 +++++--
 examples/dashboard_empty_update_format.json | 27 +++++++++++++++++
 postman-collection.json                     | 32 ++++++++++++++++++++-
 recipes/.json                               |  1 -
 recipes/update_dashboard.sh                 | 16 +++++++++++
 7 files changed, 89 insertions(+), 8 deletions(-)
 create mode 100644 examples/dashboard_empty_update_format.json
 delete mode 100644 recipes/.json
 create mode 100755 recipes/update_dashboard.sh

diff --git a/USAGE.md b/USAGE.md
index 047e242..a9ba639 100644
--- a/USAGE.md
+++ b/USAGE.md
@@ -164,9 +164,10 @@ Import and export custom dashboards in the AppDynamics controller
 | ------- | ----------- | ------- |
 | delete | Delete a dashboard. Provide a dashboard id (-i) as parameter | `act.sh dashboard delete -i 2` |
 | export | Export a dashboard. Provide a dashboard id (-i) as parameter | `act.sh dashboard export -i 2` |
+| get | Get a dashboard. Provide a dashboard id (-i) as parameter. | `act.sh dashboard get -i 2` |
 | import | Import a dashboard. Provide a dashboard file or json (-d) as parameter. | `act.sh dashboard import -d @examples/dashboard.json` |
 | list | List all dashboards. This command requires no further arguments. | `act.sh dashboard list ` |
-| update | Update a dashboard. Provide a dashboard file or json (-f) as parameter. Please not that the json you need to provide is not compatible with the export format. | `act.sh dashboard update -d @dashboardUpdate.json` |
+| update | Update a dashboard. Provide a dashboard file or json (-d) as parameter. Use the `dashboard get` command to retrieve the correct format for updating. | `act.sh dashboard update -d @dashboardUpdate.json` |
 
 
 ## dbmon
diff --git a/act.sh b/act.sh
index 350c1e4..8013f7c 100755
--- a/act.sh
+++ b/act.sh
@@ -1,6 +1,6 @@
 #!/bin/bash
 ACT_VERSION="v0.4.0"
-ACT_LAST_COMMIT="375ea8f52adfb5b7d708ae8614103c7586fd519d"
+ACT_LAST_COMMIT="cdd8b4bff4df64e2eae56ccb4bc0eb1e5b7f0639"
 USER_CONFIG="$HOME/.appdynamics/act/config.sh"
 GLOBAL_CONFIG="/etc/appdynamics/act/config.sh"
 CONFIG_CONTROLLER_COOKIE_LOCATION="/tmp/appdynamics-controller-cookie.txt"
@@ -162,12 +162,14 @@ function dashboard_delete { apiCall -X POST -d '[{{i:dashboard_id}}]' '/controll
 rde dashboard_delete "Delete a dashboard." "Provide a dashboard id (-i) as parameter" "-i 2"
 function dashboard_export { apiCall '/controller/CustomDashboardImportExportServlet?dashboardId={{i:dashboard_id}}' "$@" ; }
 rde dashboard_export "Export a dashboard." "Provide a dashboard id (-i) as parameter" "-i 2"
+function dashboard_get { apiCall '/controller/restui/dashboards/dashboardIfUpdated/{{i:dashboard_id}}/-1' "$@" ; }
+rde dashboard_get "Get a dashboard." "Provide a dashboard id (-i) as parameter." "-i 2"
 function dashboard_import { apiCallExpand -X POST -F 'file={{d:dashboard}}' '/controller/CustomDashboardImportExportServlet' "$@" ; }
 rde dashboard_import "Import a dashboard." "Provide a dashboard file or json (-d) as parameter." "-d @examples/dashboard.json"
 function dashboard_list { apiCall '/controller/restui/dashboards/getAllDashboardsByType/false' "$@" ; }
 rde dashboard_list "List all dashboards." "This command requires no further arguments." ""
-function dashboard_update { apiCall -X POST -d '{{f:dashboard_definition}}' '/controller/restui/dashboards/updateDashboard' "$@" ; }
-rde dashboard_update "Update a dashboard." "Provide a dashboard file or json (-f) as parameter. Please not that the json you need to provide is not compatible with the export format." "-d @dashboardUpdate.json"
+function dashboard_update { apiCall -X POST -d '{{d:dashboard_definition}}' '/controller/restui/dashboards/updateDashboard' "$@" ; }
+rde dashboard_update "Update a dashboard." "Provide a dashboard file or json (-d) as parameter. Use the \`dashboard get\` command to retrieve the correct format for updating." "-d @dashboardUpdate.json"
 doc dbmon << EOF
 Use the Database Visibility API to get, create, update, and delete Database Visibility Collectors.
 EOF
diff --git a/commands.yml b/commands.yml
index c786b6f..9a1bba5 100644
--- a/commands.yml
+++ b/commands.yml
@@ -286,11 +286,11 @@ dashboard:
     payload: [{{i:dashboard_id}}]
   update:
     title: Update a dashboard.
-    description: Provide a dashboard file or json (-f) as parameter. Please not that the json you need to provide is not compatible with the export format.
+    description: Provide a dashboard file or json (-d) as parameter. Use the \\\`dashboard get\\\` command to retrieve the correct format for updating.
     example: -d @dashboardUpdate.json
     method: POST
     endpoint: /controller/restui/dashboards/updateDashboard
-    payload: {{f:dashboard_definition}}
+    payload: {{d:dashboard_definition}}
   import:
     title: Import a dashboard.
     description: Provide a dashboard file or json (-d) as parameter.
@@ -299,6 +299,12 @@ dashboard:
     endpoint: /controller/CustomDashboardImportExportServlet
     form: file={{d:dashboard}}
     expand: true
+  get:
+    title: Get a dashboard.
+    description: Provide a dashboard id (-i) as parameter.
+    example: -i 2
+    method: GET
+    endpoint: /controller/restui/dashboards/dashboardIfUpdated/{{i:dashboard_id}}/-1
 event:
   title: Events
   description: Create and list events in your business applications.
diff --git a/examples/dashboard_empty_update_format.json b/examples/dashboard_empty_update_format.json
new file mode 100644
index 0000000..243c1fe
--- /dev/null
+++ b/examples/dashboard_empty_update_format.json
@@ -0,0 +1,27 @@
+{
+  "id" : 3166,
+  "version" : 2,
+  "name" : "Empty Dashboard",
+  "nameUnique" : false,
+  "builtIn" : false,
+  "description" : null,
+  "missingAssociatedEntities" : null,
+  "widgets" : [ ],
+  "securityToken" : null,
+  "sharingRevoked" : false,
+  "warRoom" : false,
+  "template" : false,
+  "templateEntityType" : "APPLICATION_COMPONENT_NODE",
+  "minutesBeforeAnchorTime" : -1,
+  "startTime" : -1,
+  "endTime" : -1,
+  "refreshInterval" : 120000,
+  "backgroundColor" : 5856629,
+  "color" : 15856629,
+  "height" : 1024,
+  "width" : 50,
+  "disabled" : false,
+  "canvasType" : "CANVAS_TYPE_GRID",
+  "layoutType" : "",
+  "properties" : [ ]
+}
diff --git a/postman-collection.json b/postman-collection.json
index 42ead49..07f8d20 100644
--- a/postman-collection.json
+++ b/postman-collection.json
@@ -958,6 +958,36 @@
 						},
             "description": "Provide a dashboard id (-i) as parameter"
 					}
+				},{
+					"name": "Get a dashboard.",
+					"request": {
+						"method": "GET",
+						"header": [
+              {
+								"key": "Content-Type",
+								"value": "application/json;charset=UTF-8",
+								"type": "text"
+							},
+              {
+								"key": "X-CSRF-TOKEN",
+								"value": "{{X-CSRF-TOKEN}}",
+								"type": "text"
+							}
+            ],
+						"body": {
+							"mode": "raw",
+							"raw": ""
+						},
+						"url": {
+							"raw": "{{controller_host}}/controller/restui/dashboards/dashboardIfUpdated/{{i:dashboard_id}}/-1",
+              "host": [
+								"{{controller_host}}"
+							],
+							"path": ["controller","restui","dashboards","dashboardIfUpdated","{{dashboard_id}}","-1"],
+              "query": []
+						},
+            "description": "Provide a dashboard id (-i) as parameter."
+					}
 				},{
 					"name": "Import a dashboard.",
 					"request": {
@@ -1046,7 +1076,7 @@
 							"path": ["controller","restui","dashboards","updateDashboard"],
               "query": []
 						},
-            "description": "Provide a dashboard file or json (-f) as parameter. Please not that the json you need to provide is not compatible with the export format."
+            "description": "Provide a dashboard file or json (-d) as parameter. Use the \`dashboard get\` command to retrieve the correct format for updating."
 					}
 				}]},{"name": "dbmon","item": [{
 					"name": "Delete multiple collectors.",
diff --git a/recipes/.json b/recipes/.json
deleted file mode 100644
index 8b13789..0000000
--- a/recipes/.json
+++ /dev/null
@@ -1 +0,0 @@
-
diff --git a/recipes/update_dashboard.sh b/recipes/update_dashboard.sh
new file mode 100755
index 0000000..67c924c
--- /dev/null
+++ b/recipes/update_dashboard.sh
@@ -0,0 +1,16 @@
+#!/bin/bash
+# SINCE: yesterday, 0:00, UNTIL today, 0:00
+TODAY_MIDNIGHT=$(date -r $(((`date +%s`/86400*86400))) +%s)
+declare -i TODAY_MIDNIGHT
+DEFAULT_SINCE="$((${TODAY_MIDNIGHT}-86400))000"
+DEFAULT_UNTIL="${TODAY_MIDNIGHT}000"
+
+ENVIRONMENT=$1
+DASHBOARD_ID=$2
+SINCE=${3:-${DEFAULT_SINCE}}
+UNTIL=${4:-${DEFAULT_UNTIL}}
+
+
+JSON=$(../act.sh -E ${ENVIRONMENT} dashboard get -i ${DASHBOARD_ID} | sed -e "s/SINCE[[:space:]]*[0-9]*[[:space:]]*UNTIL[[:space:]]*[0-9]*/SINCE ${SINCE} UNTIL ${UNTIL}/g")
+
+../act.sh -E ${ENVIRONMENT} dashboard update -d "${JSON}"