Script to delete the resource group created by DeployDSVM.

This commit is contained in:
Graham Williams 2017-02-23 18:19:46 +08:00
Родитель dd92d8721f
Коммит ab21234b88
1 изменённых файлов: 113 добавлений и 0 удалений

113
vignettes/DeleteRG.Rmd Normal file
Просмотреть файл

@ -0,0 +1,113 @@
---
title = "Using Azure Data Science Resources: Delete a Resource Group"
author= "Graham Williams"
---
# Use Case
A sample deployment of a Linux Data Science Virtual Machine (DSVM) is
presented in the
[AzureDSR's Deploy DSMV](https://github.com/Azure/AzureDSR/blob/master/vignettes/DeployDSVM.Rmd)
document. All of the resources (the virtual machine, public IP
address, network interface, storage account, network security group,
and virtual network) will be hosted within a single resource
group. Thus it is convenient to simply delete the resource group to
remove the deployment of the DSVM.
This script can be run after the
[AzureDSR's Deploy DSMV](https://github.com/Azure/AzureDSR/blob/master/vignettes/DeployDSVM.Rmd)
script to delete all resources created in that script.
# Preparation
We assume the user already has an Azure subscription and we have
obtained the credentials required. See
[AzureSMR's Authentication Guide](https://github.com/Microsoft/AzureSMR/blob/master/vignettes/Authentication.Rmd)
for details. We will then ensure the resource group exists and then
delete it.
# Setup
To get started we need to load our Azure credentials.
```{r credentials, eval=FALSE}
# Credentials come from app creation in Active Directory within Azure.
TID <- "72f9....db47" # Tenant ID
CID <- "9c52....074a" # Client ID
KEY <- "9Efb....4nwV....ASa8=" # User key
```
We might be able to load such information from the file
<USER>_credentials.R where <USER> is replaced with your username.
```{r setup}
# Load the required subscription resources: TID, CID, and KEY.
USER <- Sys.getenv("USER")
source(paste0(USER, "_credentials.R"))
# Install the packages if required.
## devtools::install_github("Microsoft/AzureSMR")
## devtools::install_github("Azure/AzureDSR", auth_token=GIT_TOKEN)
```
```{r packages}
# Load the required packages.
library(AzureSMR) # Support for managing Azure resources.
library(AzureDSR) # Further support for the Data Scientist.
library(magrittr)
library(dplyr)
```
```{r tuning}
# Parameters for this script: the name for the new resource group and
# its location across the Azure cloud. The resource name is used to
# name the resource group that we will create transiently for the
# purposes of this script.
RG <- "my_dsvm_rg_sea" # The resource group to be deleted.
```
```{r connect}
# Connect to the Azure subscription and use this as the context for
# our activities.
context <- createAzureContext(tenantID=TID, clientID=CID, authKey=KEY)
# Check if the resource group already exists. Take note this script
# will not remove the resource group if it pre-existed.
context %>%
azureListRG() %>%
filter(name == RG) %>%
select(name, location) %T>%
print() %>%
nrow() %>%
equals(0) %>%
not() %T>%
print() ->
rg_pre_exists
```
# Delete the Resource Group
Delete the resource group within which all resources were created.
```{r create resource group}
if (rg_pre_exists)
{
# Delete the resource group RG.
# Note that to delete a resource group can take some time, like 10 minutes.
azureDeleteResourceGroup(context, RG)
}
```
Once deleted we are consuming no more.