【AZ-204】Developing Solutions for Microsoft Azure - 試験対策

## Azure Key Vault
### Create a Key vault
```PowerShell
New-AzKeyVault -Name $keyVaultName `
               -ResourceGroupName $resourceGroupName `
               -Location $location `
               -EnabledForDiskEncryption `
               -Sku $sku
```

## Azure VM
### Encrypt a running VM
```PowerShell
$keyVault = Get-AzKeyVault -VaultName $keyVaultName -ResourceGroupName $resourceGroupName
$diskEncryptionKeyVaultUrl = $keyVault.VaultUri
$keyVaultResourceId = $keyVault.ResourceId
$keyEncryptionKeyUrl = (Get-AzKeyVaultKey -VaultName $keyVaultName -Name CrypKey).Key.Kid
 
Set-AzVMDiskEncryptionExtension -ResourceGroupName $resourceGroupName `
                                -VMName $vm `
                                -DiskEncryptionKeyVaultUrl $diskEncryptionKeyVaultUrl `
                                -DiskEncryptionKeyVaultId $keyVaultResourceId `
                                -KeyEncryptionKeyUrl $keyEncryptionKeyUrl `
                                -KeyEncryptionKeyVaultId $keyVaultResourceId
```

## API Management
```PowerShell
New-AzApiManagement -Name "myapim" -ResourceGroupName "myResourceGroup" `
  -Location "West US" -Organization "Contoso" -AdminEmail "admin@contoso.com"
```

## Azure SQL Database
[dynamic data masking for your database](https://docs.microsoft.com/en-us/azure/azure-sql/database/dynamic-data-masking-configure-portal)

## How to use Azure Search
- Provision Service
- Create Index
- Load Data
    - Create a `SearchIndexClient` object to connect to your search index
    - Create an `IndexBatch` containing the documents to be added, modified, or deleted
    - Call the `Documents.Index` method of your `SearchIndexClient` to send the `IndexBatch` to your search index
- Search
  
## Azure Cosmos DB

### Create a SQL API Cosmos DB account with session consistency and multi-master enabled
```PowerShell
az cosmosdb create \
    --resource-group $resourceGroupName \
    --name $accountName \
    --kind GlobalDocumentDB
    --locations "South Central US=0" "North Central US"=1 \
    --default-consistency-level "Session" \
    --enable-multiple-write-locations true
```

### [Consistency levels](https://docs.microsoft.com/ja-jp/azure/cosmos-db/consistency-levels)
- **Strong**
    - The reads are guaranteed to return the most recent committed version of an item.
    - After the data is written to the "West US 2" region, when you read the data from other regions, you get the most recent value.
- **Bounded staleness**
    - The number of versions ( K ) of the item
    - The time interval ( T ) reads might lag behind the writes
    - **single region account**, the minimum value of K and T is **10 write operations** or **5 seconds**
    - **multi-region accounts** the minimum value of K and T is **100,000 write** operations or **300 seconds**
- **Session**
    - Within a single client session reads are guaranteed to honor the consistent-prefix, monotonic reads, monotonic writes, read-your-writes, and write-follows-reads guarantees. This assumes **a single "writer" session** or sharing the **session token for multiple writers**.
- **Consistent prefix**
    - Updates that are returned contain some prefix of all the updates, with no gaps. Consistent prefix consistency level guarantees that reads never see out-of-order writes.
- **Eventual**
    - **no ordering guarantee for reads**. In the absence of any further writes, the replicas eventually converge.

![img](https://docs.microsoft.com/en-us/azure/cosmos-db/media/consistency-levels/five-consistency-levels.png)

### Create a table
```c#:C#
// Retrieve the stroage account from the connection string
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));

// Create the table client
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

// Retrieve a reference to the table
CloudTable table = tableClient.GetTableReference("people");

// Create the table if it doesn't exist
table.CreateIfNotExists();
```

### Retrieve a single entity
```c#:C#
// Retrieve the stroage account from the connection string
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));

// Create the table client
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

// Retrieve a reference to the table
CloudTable table = tableClient.GetTableReference("people");

// Create a retrieve operation that takes a customer entity
TableOperation retrieveOperation = TableOperation.Retrieve<CustomerEntity>("Smith", "Ben");

// Execute the retrieve operation
TableResult result = await table.Execute(retrieveOperation);

```

### Batch Operation
```c#:C#
// Retrieve the stroage account from the connection string
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));

// Create the table client
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

// Create the CloudTable object that represents the people table.
CloudTable table = tableClient.GetTableReference("people");

// Create the batch operation
TableBatchOperation batchOperation = new TableBatchOperation();

// Create a customer entity and add it to the table
CustomerEntity customer1 = new CustomerEntity("Smith", "Jeff");
customer1.Email = "Jeff@contoso.com";
customer1.PhoneNumber = "425-555-0104";

CustomerEntity customer2 = new CustomerEntity("Smith", "Ben");
customer2.Email = "Ben@contoso.com";
customer2.PhoneNumber = "425-555-0102";

// Add both customer entities to the batch insert operation
batchOperation.Insert(customer1);
batchOperation.Insert(customer2);

// Execute the batch operation
table.ExecuteBatch(batchOperation);
```

## Service Bus
### [Topic filters and actions](https://docs.microsoft.com/en-us/azure/service-bus-messaging/topic-filters)
- Boolean filters
    - The TrueFilter and FalseFilter either cause all arriving messages (true) or none of the arriving messages (false) to be selected for the subscription. 
- SQL Filters
    - Holds a SQL-like conditional expression that is evaluated in the broker against the arriving messages' user-defined properties and system properties. 
- Correlation Filters
    - holds a set of conditions that are matched against one or more of an arriving message's user and system properties

## Azure Application Insights

###  Usage Analysis features
- Funnels
- Cohorts
- Impact
- User Retention
- User Flows

### Types of sampling
- **Adaptive sampling** automatically adjusts the volume of telemetry **sent from the SDK** in your ASP.NET/ASP.NET Core app, and from Azure Functions. This is the default sampling when you use the ASP.NET or ASP.NET Core SDK. Adaptive sampling is currently only available for ASP.NET server-side telemetry, and for Azure Functions.
- **Fixed-rate sampling** reduces the volume of telemetry sent from both your ASP.NET or ASP.NET Core or Java server and from your users' browsers. You set the rate. The client and server will synchronize their sampling so that, in Search, you can navigate between related page views and requests.
- **Ingestion sampling** happens **at the Application Insights service endpoint**. It discards some of the telemetry that arrives from your app, at a sampling rate that you set. It doesn't reduce telemetry traffic sent from your app, but helps you keep within your monthly quota. The main advantage of ingestion sampling is that you can set the sampling rate without redeploying your app. Ingestion sampling works uniformly for all servers and clients, but it does not apply when any other types of sampling are in operation.