{
    "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",
                "## Deploy SQL Server 2019 Big Data Cluster on an existing Azure Kubernetes Service (AKS) cluster\n",
                " \n",
                "This notebook walks through the process of deploying 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 an existing AKS cluster."
            ],
            "metadata": {
                "azdata_cell_guid": "82e60c1a-7acf-47ee-877f-9e85e92e11da"
            }
        },
        {
            "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": "d973d5b4-7f0a-4a9d-b204-a16480f3940d",
                "tags": []
            },
            "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": "4b266b2d-bd1b-4565-92c9-3fc146cdce6d"
            }
        },
        {
            "cell_type": "markdown",
            "source": [
                "### **Set password**"
            ],
            "metadata": {
                "azdata_cell_guid": "7c37d248-b9ac-4ad6-be56-158cd70443b1"
            }
        },
        {
            "cell_type": "code",
            "source": [
                "mssql_password = os.environ[\"AZDATA_NB_VAR_BDC_ADMIN_PASSWORD\"]"
            ],
            "metadata": {
                "azdata_cell_guid": "83d455f3-db10-48bb-bb81-78a6b4e5f2fd"
            },
            "outputs": [],
            "execution_count": 0
        },
        {
            "cell_type": "markdown",
            "source": [
                "### **Set and show current context**"
            ],
            "metadata": {
                "azdata_cell_guid": "127c8042-181f-4862-a390-96e59c181d09"
            }
        },
        {
            "cell_type": "code",
            "source": [
                "run_command(f'kubectl config use-context {mssql_cluster_context}')\n",
                "run_command('kubectl config current-context')"
            ],
            "metadata": {
                "azdata_cell_guid": "7d1a03d4-1df8-48eb-bff0-0042603b95b1",
                "tags": [
                    "hide_input"
                ]
            },
            "outputs": [],
            "execution_count": 0
        },
        {
            "cell_type": "markdown",
            "source": [
                "### **Create deployment configuration files**"
            ],
            "metadata": {
                "azdata_cell_guid": "138536c3-1db6-428f-9e5c-8269a02fb52e"
            }
        },
        {
            "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": "2ff82c8a-4bce-449c-9d91-3ac7dd272021",
                "tags": [
                    "hide_input"
                ]
            },
            "outputs": [],
            "execution_count": 6
        },
        {
            "cell_type": "markdown",
            "source": [
                "### **Create SQL Server 2019 Big Data Cluster**"
            ],
            "metadata": {
                "azdata_cell_guid": "efe78cd3-ed73-4c9b-b586-fdd6c07dd37f"
            }
        },
        {
            "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",
                "run_command(f'azdata bdc create -c {mssql_target_profile}')"
            ],
            "metadata": {
                "azdata_cell_guid": "373947a1-90b9-49ee-86f4-17a4c7d4ca76",
                "tags": [
                    "hide_input"
                ]
            },
            "outputs": [],
            "execution_count": 7
        }
    ]
}