Skip to content

Commit

Permalink
New cosmosdb cli samples
Browse files Browse the repository at this point in the history
  • Loading branch information
markjbrown committed Sep 17, 2019
1 parent 39e89e9 commit 1ae39a9
Show file tree
Hide file tree
Showing 15 changed files with 799 additions and 0 deletions.
63 changes: 63 additions & 0 deletions cosmosdb/cassandra/create.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
!/bin/bash

# Create a Cassandra keyspace and table

# Generate a unique 10 character alphanumeric string to ensure unique resource names
uniqueId=$(env LC_CTYPE=C tr -dc 'a-z0-9' < /dev/urandom | fold -w 10 | head -n 1)

# Variables for Cassandra API resources
resourceGroupName="Group-$uniqueId"
location='westus2'
accountName="cosmos-$uniqueId" #needs to be lower case
keySpaceName='keyspace1'
tableName='table1'

# Create a resource group
az group create -n $resourceGroupName -l $location

# Create a Cosmos account for Cassandra API
az cosmosdb create \
-n $accountName \
-g $resourceGroupName \
--capabilities EnableCassandra \
--default-consistency-level Eventual \
--locations regionName='West US 2' failoverPriority=0 isZoneRedundant=False \
--locations regionName='East US 2' failoverPriority=1 isZoneRedundant=False

# Create a Cassandra Keyspace
az cosmosdb cassandra keyspace create \
-a $accountName \
-g $resourceGroupName \
-n $keySpaceName

# Define the schema for the table
schema=$(cat << EOF
{
"columns": [
{"name": "columnA","type": "uuid"},
{"name": "columnB","type": "int"},
{"name": "columnC","type": "text"}
],
"partitionKeys": [
{"name": "columnA"}
],
"clusterKeys": [
{ "name": "columnB", "orderBy": "asc" }
]
}
EOF
)
# Persist schema to json file
echo "$schema" > "schema-$uniqueId.json"

# Create the Cassandra table
az cosmosdb cassandra table create \
-a $accountName \
-g $resourceGroupName \
-k $keySpaceName \
-n $tableName \
--throughput 400 \
--schema @schema-$uniqueId.json

# Clean up temporary schema file
rm -f "schema-$uniqueId.json"
72 changes: 72 additions & 0 deletions cosmosdb/cassandra/throughput.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
!/bin/bash

# Update throughput for Cassandra keyspace and table

# Generate a unique 10 character alphanumeric string to ensure unique resource names
uniqueId=$(env LC_CTYPE=C tr -dc 'a-z0-9' < /dev/urandom | fold -w 10 | head -n 1)

# Variables for Cassandra API resources
resourceGroupName="Group-$uniqueId"
location='westus2'
accountName="cosmos-$uniqueId" #needs to be lower case
keySpaceName='keyspace1'
tableName='table1'

# Create a resource group
az group create -n $resourceGroupName -l $location

# Create a new Cosmos account for Cassandra API
az cosmosdb create \
-n $accountName \
-g $resourceGroupName \
--capabilities EnableCassandra

# Create a new Cassandra Keyspace with shared throughput
az cosmosdb cassandra keyspace create \
-a $accountName \
-g $resourceGroupName \
-n $keySpaceName \
--throughput 400

# Define the schema for the Table
schema=$(cat << EOF
{
"columns": [
{"name": "columnA","type": "uuid"},
{"name": "columnB","type": "text"}
],
"partitionKeys": [{"name": "columnA"}]
}
EOF
)
# Persist schema to json file
echo "$schema" > "schema-$uniqueId.json"

# Create a Cassandra table with dedicated throughput
az cosmosdb cassandra table create \
-a $accountName \
-g $resourceGroupName \
-k $keySpaceName \
-n $tableName \
--throughput 400 \
--schema @schema-$uniqueId.json

# Delete schema file
rm -f "schema-$uniqueId.json"

read -p 'Press any key to increase Keyspace throughput to 500'

az cosmosdb cassandra keyspace throughput update \
-a $accountName \
-g $resourceGroupName \
-n $keySpaceName \
--throughput 500

read -p 'Press any key to increase Table throughput to 500'

az cosmosdb cassandra table throughput update \
-a $accountName \
-g $resourceGroupName \
-k $keySpaceName \
-n $tableName \
--throughput 500
21 changes: 21 additions & 0 deletions cosmosdb/common/ipfirewall.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

# Create an Azure Cosmos Account with IP Firewall

# Generate a unique 10 character alphanumeric string to ensure unique resource names
uniqueId=$(env LC_CTYPE=C tr -dc 'a-z0-9' < /dev/urandom | fold -w 10 | head -n 1)

# Resource group and Cosmos account variables
resourceGroupName="Group-$uniqueId"
location='westus2'
accountName="cosmos-$uniqueId" #needs to be lower case

# Create a resource group
az group create -n $resourceGroupName -l $location

# Create a Cosmos DB account with default values and IP Firewall enabled
# Use appropriate values for --kind or --capabilities for other APIs
az cosmosdb create \
-n $accountName \
-g $resourceGroupName \
--ip-range-filter '192.168.221.17'
48 changes: 48 additions & 0 deletions cosmosdb/common/keys.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/bash

# This sample shows the following:
# List all account keys
# List read only account keys
# List connection strings
# Regenerate account keys

# Generate a unique 10 character alphanumeric string to ensure unique resource names
uniqueId=$(env LC_CTYPE=C tr -dc 'a-z0-9' < /dev/urandom | fold -w 10 | head -n 1)

# Resource group and Cosmos account variables
resourceGroupName="Group-$uniqueId"
location='westus2'
accountName="cosmos-$uniqueId" #needs to be lower case

# Create a resource group
az group create -n $resourceGroupName -l $location

# Create a Cosmos DB account with default values
# Use appropriate values for --kind or --capabilities for other APIs
az cosmosdb create -n $accountName -g $resourceGroupName

read -p "Press any key to list account keys"
# List all account keys
az cosmosdb keys list \
-n $accountName \
-g $resourceGroupName

read -p "Press any key to list read only account keys"
# List read-only keys
az cosmosdb list-read-only-keys \
-n $accountName \
-g $resourceGroupName

read -p "Press any key to list connection strings"
# List connection strings
az cosmosdb list-connection-strings \
-n $accountName \
-g $resourceGroupName

read -p "Press any key to regenerate secondary account keys"
# Regenerate secondary account keys
# key-kind values: primary, primaryReadonly, secondary, secondaryReadonly
az cosmosdb regenerate-key \
-n $accountName \
-g $resourceGroupName \
--key-kind secondary
46 changes: 46 additions & 0 deletions cosmosdb/common/regions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
!/bin/bash

# This sample shows the following:
# Add regions to an existing Cosmos account
# Change regional failover priority (applies to accounts using automatic failover)
# Trigger a manual failover from primary to secondary region (applies to accounts with manual failover)

# Note: Azure Comos accounts cannot include updates to regions with changes to other properties in the same operation

# Generate a unique 10 character alphanumeric string to ensure unique resource names
uniqueId=$(env LC_CTYPE=C tr -dc 'a-z0-9' < /dev/urandom | fold -w 10 | head -n 1)

# Resource group and Cosmos account variables
resourceGroupName="Group-$uniqueId"
location='westus2'
accountName="cosmos-$uniqueId" #needs to be lower case

# Create a resource group
az group create -n $resourceGroupName -l $location

# Create a Cosmos DB account with default values
# Use appropriate values for --kind or --capabilities for other APIs
az cosmosdb create -n $accountName -g $resourceGroupName

read -p "Press any key to add additional regions to this account"
az cosmosdb update \
-n $accountName \
-g $resourceGroupName \
--locations regionName='West US 2' failoverPriority=0 isZoneRedundant=False \
--locations regionName='East US 2' failoverPriority=1 isZoneRedundant=False \
--locations regionName='South Central US' failoverPriority=2 isZoneRedundant=False

read -p "Press any key to change the failover priority"
# Flip East US 2 and South Central US regions
az cosmosdb failover-priority-change \
-n $accountName \
-g $resourceGroupName \
--failover-policies 'West US 2'=0 'South Central US'=1 'East US 2'=2


read -p "Press any key to trigger a manual failover by changing region 0"
# Initiate a manual failover and promote East US 2 as primary write region
az cosmosdb failover-priority-change \
-n $accountName \
-g $resourceGroupName \
--failover-policies 'East US 2'=0 'West US 2'=1 'South Central US'=2
61 changes: 61 additions & 0 deletions cosmosdb/common/service-endpoints-ignore-missing-vnet.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/bash

# Create an Azure Cosmos Account with a service endpoint connected to a backend subnet
# that is not yet enabled for service endpoints.

# This sample demonstrates how to configure service endpoints for existing Cosmos account where
# the connected subnet is not yet configured for service endpoints.
# This sample will then configure the subnet for service endpoints.

# Generate a unique 10 character alphanumeric string to ensure unique resource names
uniqueId=$(env LC_CTYPE=C tr -dc 'a-z0-9' < /dev/urandom | fold -w 10 | head -n 1)

# Resource group and Cosmos account variables
resourceGroupName="Group-$uniqueId"
location='westus2'
accountName="cosmos-$uniqueId" #needs to be lower case

# Variables for a new Virtual Network with two subnets
vnetName='myVnet'
frontEnd='FrontEnd'
backEnd='BackEnd'

# Create a resource group
az group create -n $resourceGroupName -l $location

# Create a virtual network with a front-end subnet
az network vnet create \
-n $vnetName \
-g $resourceGroupName \
--address-prefix 10.0.0.0/16 \
--subnet-name $frontEnd \
--subnet-prefix 10.0.1.0/24

# Create a back-end subnet but without specifying --service-endpoints Microsoft.AzureCosmosDB
az network vnet subnet create \
-n $backEnd \
-g $resourceGroupName \
--address-prefix 10.0.2.0/24 \
--vnet-name $vnetName

svcEndpoint=$(az network vnet subnet show -g $resourceGroupName -n $backEnd --vnet-name $vnetName --query 'id' -o tsv)

# Create a Cosmos DB account with default values
# Use appropriate values for --kind or --capabilities for other APIs
az cosmosdb create -n $accountName -g $resourceGroupName

# Add the virtual network rule but ignore the missing service endpoint on the subnet
az cosmosdb networ-rule add \
-n $accountName \
-g $resourceGroupName \
--virtual-network $vnetName \
--subnet svcEndpoint \
--ignore-missing-vnet-service-endpoint true

read -p'Press any key to configure the subnet for service endpoints'

az network vnet subnet update \
-n $backEnd \
-g $resourceGroupName \
--vnet-name $vnetName \
--service-endpoints Microsoft.AzureCosmosDB
45 changes: 45 additions & 0 deletions cosmosdb/common/service-endpoints.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/bash

# Create an Azure Cosmos Account with a service endpoint connected to a backend subnet

# Generate a unique 10 character alphanumeric string to ensure unique resource names
uniqueId=$(env LC_CTYPE=C tr -dc 'a-z0-9' < /dev/urandom | fold -w 10 | head -n 1)

# Resource group and Cosmos account variables
resourceGroupName="Group-$uniqueId"
location='westus2'
accountName="cosmos-$uniqueId" #needs to be lower case

# Variables for a new Virtual Network with two subnets
vnetName='myVnet'
frontEnd='FrontEnd'
backEnd='BackEnd'

# Create a resource group
az group create -n $resourceGroupName -l $location

# Create a virtual network with a front-end subnet
az network vnet create \
-n $vnetName \
-g $resourceGroupName \
--address-prefix 10.0.0.0/16 \
--subnet-name $frontEnd \
--subnet-prefix 10.0.1.0/24

# Create a back-end subnet
az network vnet subnet create \
-n $backEnd \
-g $resourceGroupName \
--address-prefix 10.0.2.0/24 \
--vnet-name $vnetName \
--service-endpoints Microsoft.AzureCosmosDB

svcEndpoint=$(az network vnet subnet show -g $resourceGroupName -n $backEnd --vnet-name $vnetName --query 'id' -o tsv)

# Create a Cosmos DB account with default values and service endpoints
# Use appropriate values for --kind or --capabilities for other APIs
az cosmosdb create \
-n $accountName \
-g $resourceGroupName \
--enable-virtual-network true \
--virtual-network-rules $svcEndpoint
Loading

0 comments on commit 1ae39a9

Please sign in to comment.