From eafa28287e15b51a2aa20a227056f0af43d20275 Mon Sep 17 00:00:00 2001
From: Paul Yu <pauyu@microsoft.com>
Date: Mon, 14 Nov 2022 12:22:02 -0800
Subject: Add new option to install azure-cli extensions (#165)

* Add new option for azure-cli extensions

* Update to install az extensions as $_REMOTE_USER

* Skip unknown extensions

* Change azure cli extension option type to string

* Adding test for azure-cli extensions
---
 src/azure-cli/devcontainer-feature.json | 19 ++++++++++++-------
 src/azure-cli/install.sh                | 12 ++++++++++++
 test/azure-cli/install_extensions.sh    | 17 +++++++++++++++++
 test/azure-cli/scenarios.json           | 12 ++++++++++++
 4 files changed, 53 insertions(+), 7 deletions(-)
 create mode 100644 test/azure-cli/install_extensions.sh
 create mode 100644 test/azure-cli/scenarios.json

diff --git a/src/azure-cli/devcontainer-feature.json b/src/azure-cli/devcontainer-feature.json
index 12a4092..012e287 100644
--- a/src/azure-cli/devcontainer-feature.json
+++ b/src/azure-cli/devcontainer-feature.json
@@ -1,6 +1,6 @@
 {
   "id": "azure-cli",
-  "version": "1.0.5",
+  "version": "1.0.6",
   "name": "Azure CLI",
   "documentationURL": "https://github.com/devcontainers/features/tree/main/src/azure-cli",
   "description": "Installs the Azure CLI along with needed dependencies. Useful for base Dockerfiles that often are missing required install dependencies like gpg.",
@@ -12,16 +12,21 @@
       ],
       "default": "latest",
       "description": "Select or enter an Azure CLI version. (Available versions may vary by Linux distribution.)"
+    },
+    "extensions": {
+      "type": "string",
+      "default": "",
+      "description": "Optional comma separated list of Azure CLI extensions to install in profile."
     }
   },
   "customizations": {
-      "vscode": {
-          "extensions": [
-            "ms-vscode.azurecli"
-          ]
-      }
+    "vscode": {
+      "extensions": [
+        "ms-vscode.azurecli"
+      ]
+    }
   },
   "installsAfter": [
     "ghcr.io/devcontainers/features/common-utils"
   ]
-}
+}
\ No newline at end of file
diff --git a/src/azure-cli/install.sh b/src/azure-cli/install.sh
index de5d65d..99b4af5 100755
--- a/src/azure-cli/install.sh
+++ b/src/azure-cli/install.sh
@@ -13,6 +13,7 @@ set -e
 rm -rf /var/lib/apt/lists/*
 
 AZ_VERSION=${VERSION:-"latest"}
+AZ_EXTENSIONS=${EXTENSIONS}
 
 MICROSOFT_GPG_KEYS_URI="https://packages.microsoft.com/keys/microsoft.asc"
 AZCLI_ARCHIVE_ARCHITECTURES="amd64"
@@ -185,6 +186,17 @@ if [ "${use_pip}" = "true" ]; then
     fi
 fi
 
+# If Azure CLI extensions are requested, loop through and install 
+if [ ${#AZ_EXTENSIONS[@]} -gt 0 ]; then
+    echo "Installing Azure CLI extensions: ${AZ_EXTENSIONS}"
+    extensions=(`echo ${AZ_EXTENSIONS} | tr ',' ' '`)
+    for i in "${extensions[@]}"
+    do
+        echo "Installing ${i}"
+        su ${_REMOTE_USER} -c "az extension add --name ${i} -y" || continue
+    done
+fi
+
 # Clean up
 rm -rf /var/lib/apt/lists/*
 
diff --git a/test/azure-cli/install_extensions.sh b/test/azure-cli/install_extensions.sh
new file mode 100644
index 0000000..1e66e26
--- /dev/null
+++ b/test/azure-cli/install_extensions.sh
@@ -0,0 +1,17 @@
+#!/bin/bash
+
+set -e
+
+# Import test library for `check` command
+source dev-container-features-test-lib
+
+# Check to make sure the user is vscode
+check "user is vscode" whoami | grep vscode
+
+# Extension-specific tests
+check "aks-preview" az extension show --name aks-preview
+check "amg" az extension show --name amg
+check "containerapp" az extension show --name containerapp
+
+# Report result
+reportResults
\ No newline at end of file
diff --git a/test/azure-cli/scenarios.json b/test/azure-cli/scenarios.json
new file mode 100644
index 0000000..aa21f72
--- /dev/null
+++ b/test/azure-cli/scenarios.json
@@ -0,0 +1,12 @@
+{
+  "install_extensions": {
+    "image": "ubuntu:focal",
+    "user": "vscode",
+    "features": {
+      "azure-cli": {
+        "version": "latest",
+        "extensions": "aks-preview,amg,containerapp"
+      }
+    }
+  }
+}
\ No newline at end of file
-- 
cgit v1.2.3