{
    "metadata": {
        "kernelspec": {
            "name": "python3",
            "display_name": "Python 3"
        },
        "language_info": {
            "name": "python",
            "version": "3.6.6",
            "mimetype": "text/x-python",
            "codemirror_mode": {
                "name": "ipython",
                "version": 3
            },
            "pygments_lexer": "ipython3",
            "nbconvert_exporter": "python",
            "file_extension": ".py"
        }
    },
    "nbformat_minor": 2,
    "nbformat": 4,
    "cells": [
        {
            "cell_type": "markdown",
            "source": [
                "![Microsoft](https://raw.githubusercontent.com/microsoft/azuredatastudio/master/src/sql/media/microsoft-small-logo.png)\n",
                " \n",
                "## Create Azure Kubernetes Service cluster and deploy SQL Server 2019 Big Data Cluster\n",
                " \n",
                "This notebook walks through the process of creating a new Azure Kubernetes Service cluster first, and then deploys a <a href=\"https://docs.microsoft.com/sql/big-data-cluster/big-data-cluster-overview?view=sqlallproducts-allversions\">SQL Server 2019 Big Data Cluster</a> on the newly created AKS cluster."
            ],
            "metadata": {
                "azdata_cell_guid": "4f6bc3bc-3592-420a-b534-384011189005"
            }
        },
        {
            "cell_type": "code",
            "source": [
                "import json,sys,os\n",
                "def run_command(command):\n",
                "    print(\"Executing: \" + command)\n",
                "    !{command}\n",
                "    if _exit_code != 0:\n",
                "        sys.exit(f'Command execution failed with exit code: {str(_exit_code)}.\\n\\t{command}\\n')\n",
                "    print(f'Successfully executed: {command}')"
            ],
            "metadata": {
                "azdata_cell_guid": "326645cf-022a-47f2-8aff-37de71da8955",
                "tags": [
                    "hide_input"
                ]
            },
            "outputs": [],
            "execution_count": 1
        },
        {
            "cell_type": "markdown",
            "source": [
                "### **Set variables**\n",
                "Generated by Azure Data Studio using the values collected in the Deploy Big Data Cluster wizard"
            ],
            "metadata": {
                "azdata_cell_guid": "8716915b-1439-431b-ab0a-0221ef94cb7f"
            }
        },
        {
            "cell_type": "markdown",
            "source": [
                "### **Set password**"
            ],
            "metadata": {
                "azdata_cell_guid": "b083aa8d-990c-4170-ba1d-247ba5c6ae76"
            }
        },
        {
            "cell_type": "code",
            "source": [
                "mssql_password = os.environ[\"AZDATA_NB_VAR_BDC_ADMIN_PASSWORD\"]"
            ],
            "metadata": {
                "azdata_cell_guid": "de256ddd-b835-4eb6-8cfc-c1a6239b0726"
            },
            "outputs": [],
            "execution_count": 0
        },
        {
            "cell_type": "markdown",
            "source": [
                "### **Login to Azure**\n",
                "\n",
                "This will open a web browser window to enable credentials to be entered. If this cells is hanging forever, it might be because your Web browser windows is waiting for you to enter your Azure credentials!\n",
                ""
            ],
            "metadata": {
                "azdata_cell_guid": "baddf2d9-93ee-4c42-aaf1-b42116bb1912"
            }
        },
        {
            "cell_type": "code",
            "source": [
                "run_command(f'az login')"
            ],
            "metadata": {
                "azdata_cell_guid": "8f1404a6-216d-49fb-b6ad-81beeea50083",
                "tags": [
                    "hide_input"
                ]
            },
            "outputs": [],
            "execution_count": 5
        },
        {
            "cell_type": "markdown",
            "source": [
                "\n",
                "### **Set active Azure subscription**"
            ],
            "metadata": {
                "azdata_cell_guid": "230dc0f1-bf6e-474a-bfaa-aae6f8aad12e"
            }
        },
        {
            "cell_type": "code",
            "source": [
                "if azure_subscription_id != \"\":\n",
                "    run_command(f'az account set --subscription {azure_subscription_id}')\n",
                "else:\n",
                "    print('Using the default Azure subscription', {azure_subscription_id})\n",
                "run_command(f'az account show')"
            ],
            "metadata": {
                "azdata_cell_guid": "ab230931-2e99-483b-a229-3847684a8c1c",
                "tags": [
                    "hide_input"
                ]
            },
            "outputs": [],
            "execution_count": 6
        },
        {
            "cell_type": "markdown",
            "source": [
                "### **Create Azure resource group**"
            ],
            "metadata": {
                "azdata_cell_guid": "d51db914-f484-489f-990d-72edb3065068"
            }
        },
        {
            "cell_type": "code",
            "source": [
                "run_command(f'az group create --name {azure_resource_group} --location {azure_region}')"
            ],
            "metadata": {
                "azdata_cell_guid": "7c53eb23-c327-41bf-8936-bd34a02ebdd5",
                "tags": [
                    "hide_input"
                ]
            },
            "outputs": [],
            "execution_count": 7
        },
        {
            "cell_type": "markdown",
            "source": [
                "### **Create AKS cluster**"
            ],
            "metadata": {
                "azdata_cell_guid": "818eb705-71e2-4013-8420-44886a5468b2"
            }
        },
        {
            "cell_type": "code",
            "source": [
                "run_command(f'az aks create --name {aks_cluster_name} --resource-group {azure_resource_group} --generate-ssh-keys --node-vm-size {azure_vm_size} --node-count {azure_vm_count}')"
            ],
            "metadata": {
                "azdata_cell_guid": "3cea1da0-0c18-4030-a5aa-79bc98a5a14d",
                "tags": [
                    "hide_input"
                ]
            },
            "outputs": [],
            "execution_count": 8
        },
        {
            "cell_type": "markdown",
            "source": [
                "### **Set the new AKS cluster as current context**"
            ],
            "metadata": {
                "azdata_cell_guid": "5ade8453-5e71-478f-b6b6-83c55626243d"
            }
        },
        {
            "cell_type": "code",
            "source": [
                "run_command(f'az aks get-credentials --resource-group {azure_resource_group} --name {aks_cluster_name} --admin --overwrite-existing')"
            ],
            "metadata": {
                "azdata_cell_guid": "9ccb9adf-1cf6-4dcb-8bd9-7ae9a85c2437",
                "tags": [
                    "hide_input"
                ]
            },
            "outputs": [],
            "execution_count": 9
        },
        {
            "cell_type": "markdown",
            "source": [
                "### **Create deployment configuration files**"
            ],
            "metadata": {
                "azdata_cell_guid": "57eb69fb-c68f-4ba8-818d-ffbaa0bc7aec"
            }
        },
        {
            "cell_type": "code",
            "source": [
                "mssql_target_profile = 'ads-bdc-custom-profile'\n",
                "if not os.path.exists(mssql_target_profile):\n",
                "    os.mkdir(mssql_target_profile)\n",
                "bdcJsonObj = json.loads(bdc_json)\n",
                "controlJsonObj = json.loads(control_json)\n",
                "bdcJsonFile = open(f'{mssql_target_profile}/bdc.json', 'w')\n",
                "bdcJsonFile.write(json.dumps(bdcJsonObj, indent = 4))\n",
                "bdcJsonFile.close()\n",
                "controlJsonFile = open(f'{mssql_target_profile}/control.json', 'w')\n",
                "controlJsonFile.write(json.dumps(controlJsonObj, indent = 4))\n",
                "controlJsonFile.close()\n",
                "print(f'Created deployment configuration folder: {mssql_target_profile}')"
            ],
            "metadata": {
                "azdata_cell_guid": "3fd73c04-8a79-4d08-9049-1dad30265558",
                "tags": [
                    "hide_input"
                ]
            },
            "outputs": [],
            "execution_count": 10
        },
        {
            "cell_type": "markdown",
            "source": [
                "### **Create SQL Server 2019 Big Data Cluster**"
            ],
            "metadata": {
                "azdata_cell_guid": "6e82fad8-0fd0-4952-87ce-3fea1edd98cb"
            }
        },
        {
            "cell_type": "code",
            "source": [
                "print (f'Creating SQL Server 2019 Big Data Cluster: {mssql_cluster_name} using configuration {mssql_target_profile}')\n",
                "os.environ[\"ACCEPT_EULA\"] = 'yes'\n",
                "os.environ[\"AZDATA_USERNAME\"] = mssql_username\n",
                "os.environ[\"AZDATA_PASSWORD\"] = mssql_password\n",
                "if os.name == 'nt':\n",
                "    print(f'If you don\\'t see output produced by azdata, you can run the following command in a terminal window to check the deployment status:\\n\\tkubectl get pods -n {mssql_cluster_name} ')\n",
                "run_command(f'azdata bdc create -c {mssql_target_profile}')"
            ],
            "metadata": {
                "azdata_cell_guid": "c43ea026-ca5e-4e2a-8602-fcc786354168",
                "tags": [
                    "hide_input"
                ]
            },
            "outputs": [],
            "execution_count": 11
        }
    ]
}