Added DiskInfo capability
Change-Id: Ie98a890d2e8adf57d16b000de748317b31d2943c
diff --git a/src/main/resources/Documentation/rest-api.md b/src/main/resources/Documentation/rest-api.md
index c75730a..abad4b6 100644
--- a/src/main/resources/Documentation/rest-api.md
+++ b/src/main/resources/Documentation/rest-api.md
@@ -21,6 +21,16 @@
- gerritVersion - JSON String with the version of running Gerrit server
- cpuInfo - JSON Object with all the CPU information collected by [jHardware](https://github.com/profesorfalken/jHardware)
- memInfo - JSON Object with all the Memory information collected by [jHardware](https://github.com/profesorfalken/jHardware)
+- diskInfo - JSON Object describing the disk information. Here a possible output:
+```
+{
+ "diskFree": 106969321472,
+ "diskTotal": 235089907712,
+ "diskUsable": 95003811840,
+ "path": "/home/pakkio/g2.14-stable/data"
+}
+
+```
NOTE: API must be authenticated with the credentials of a user with the
'Collect Server Data' capability.
diff --git a/src/main/scala/com/googlesource/gerrit/plugins/support/SitePathsWrapper.scala b/src/main/scala/com/googlesource/gerrit/plugins/support/SitePathsWrapper.scala
new file mode 100644
index 0000000..a74118a
--- /dev/null
+++ b/src/main/scala/com/googlesource/gerrit/plugins/support/SitePathsWrapper.scala
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.googlesource.gerrit.plugins.support
+
+import java.io.File
+import java.nio.file.Path
+
+import com.google.gerrit.server.config.SitePaths
+import com.google.inject.{Inject, Injector, Key, Provider}
+
+class SitePathsWrapper @Inject()(val injector: Injector) {
+ def getAsPath(folder: String): Path = {
+ val sitePaths = injector.getInstance(Key.get(classOf[SitePaths]))
+ val field = sitePaths.getClass.getDeclaredField(folder)
+ val value = field.get(sitePaths)
+ if (value.isInstanceOf[File]) {
+ value.asInstanceOf[File].toPath
+ } else {
+ value.asInstanceOf[Path]
+ }
+ }
+}
diff --git a/src/main/scala/com/googlesource/gerrit/plugins/support/commands/DiskInfoCommand.scala b/src/main/scala/com/googlesource/gerrit/plugins/support/commands/DiskInfoCommand.scala
new file mode 100644
index 0000000..78f3b68
--- /dev/null
+++ b/src/main/scala/com/googlesource/gerrit/plugins/support/commands/DiskInfoCommand.scala
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.googlesource.gerrit.plugins.support.commands
+
+import java.nio.file.Files
+
+import com.google.inject.Inject
+import com.googlesource.gerrit.plugins.support.{SitePathsWrapper, GerritSupportCommand}
+
+class DiskInfoCommand @Inject()(val sitePathsFolder: SitePathsWrapper) extends GerritSupportCommand {
+
+ case class DiskInfo(path: String, diskFree: Long, diskUsable: Long, diskTotal: Long)
+
+ def getResult = {
+ val dataPath = sitePathsFolder.getAsPath("data_dir")
+ val store = Files.getFileStore(dataPath)
+ DiskInfo(dataPath.toString, store.getUnallocatedSpace,
+ store.getUsableSpace, store.getTotalSpace
+ )
+ }
+}
\ No newline at end of file
diff --git a/src/test/scala/com/googlesource/gerrit/plugins/support/GerritSupportTest.scala b/src/test/scala/com/googlesource/gerrit/plugins/support/GerritSupportTest.scala
index dddabcf..9331c7b 100644
--- a/src/test/scala/com/googlesource/gerrit/plugins/support/GerritSupportTest.scala
+++ b/src/test/scala/com/googlesource/gerrit/plugins/support/GerritSupportTest.scala
@@ -17,11 +17,14 @@
package com.googlesource.gerrit.plugins.support
import java.io.File
+import java.nio.file.Paths
import java.util.zip.ZipFile
import com.google.gson.{Gson, JsonPrimitive}
import com.googlesource.gerrit.plugins.support.FileMatchers._
import com.googlesource.gerrit.plugins.support.commands._
+import org.mockito.Matchers.any
+import org.mockito.Mockito
import org.scalatest.{FlatSpec, Matchers}
import scala.collection.JavaConverters._
@@ -55,6 +58,16 @@
memInfo.getAsJsonObject should haveValidFields
}
+ "disk-info command" should "return a json object with some fields" in {
+ val wrapper = Mockito.mock(classOf[SitePathsWrapper])
+ Mockito.when(wrapper.getAsPath(any[String])).thenReturn(Paths.get("/tmp"))
+
+ val diskInfo = new DiskInfoCommand(wrapper).execute.content
+
+ diskInfo should not be null
+ diskInfo.getAsJsonObject should haveValidFields
+ }
+
"Bundle builder" should "create an output zip file" in {
val zipFile = new SupportBundleBuilder(tmpPath.toPath, new Gson).build(Seq())