Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor with terrafrom and defaultCredential for storage and cosmos #125

Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
9b19242
add terrafrom support for storage-blob
backwind1233 Dec 17, 2021
eaf485d
update spring-cloud-azure-cosmos-sample with terraform
backwind1233 Dec 19, 2021
07713e4
refactor spring-cloud-azure-data-cosmos-sample with terraform
backwind1233 Dec 19, 2021
6532f40
update terraform script for storage-blob-sample
backwind1233 Dec 19, 2021
b4d94a5
update README.md
backwind1233 Dec 19, 2021
50e4603
update README.md
backwind1233 Dec 24, 2021
229b247
update README.md
backwind1233 Dec 24, 2021
529fb14
rename variables.tf
backwind1233 Dec 24, 2021
5106802
fix error in readme
backwind1233 Dec 24, 2021
2751865
fix README.md
backwind1233 Dec 24, 2021
60b487e
Merge branch 'spring-cloud-azure_4.0' into terraform_credential_cosmo…
backwind1233 Dec 24, 2021
6a1a247
update aztfmod/azurecaf from 1.2.6 to 1.2.10
backwind1233 Dec 24, 2021
dbc1a71
update codes for cosmos
backwind1233 Dec 24, 2021
f79e89f
update README.md
backwind1233 Dec 24, 2021
5276abc
replace links
backwind1233 Dec 24, 2021
99f7781
update #what you need
backwind1233 Dec 24, 2021
89d35d8
Merge branch 'terraform_credential_cosmos_storage' of https://github.…
backwind1233 Dec 24, 2021
a787a44
fix comments
backwind1233 Dec 24, 2021
11bc10d
fix doc errors
backwind1233 Dec 24, 2021
e7b26f0
fix doc
backwind1233 Dec 24, 2021
6bd39e6
Merge branch 'spring-cloud-azure_4.0' into terraform_credential_cosmo…
backwind1233 Dec 25, 2021
f44f5de
renmae local Environment > local environment
backwind1233 Dec 25, 2021
1f4e190
fix doc
backwind1233 Dec 25, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,46 +1,134 @@
# Sample project for Spring Cloud Azure Starter Cosmos
# Using CosmosDB With cosmosClient
saragluna marked this conversation as resolved.
Show resolved Hide resolved
saragluna marked this conversation as resolved.
Show resolved Hide resolved

## Key concepts
This guide walks you through the process of accessing Azure CosmosDB with cosmosClient.
saragluna marked this conversation as resolved.
Show resolved Hide resolved

## What You Will build
You will build an application that read and write data with Azure CosmosDB using cosmosClient.
saragluna marked this conversation as resolved.
Show resolved Hide resolved

## Getting started
## What You Need

- [An Azure subscription](https://azure.microsoft.com/free/)
- [Terraform](https://www.terraform.io/)
- [IntelliJ IDEA](https://www.jetbrains.com/idea/download/#section=mac)
saragluna marked this conversation as resolved.
Show resolved Hide resolved
- [Azure CLI](https://docs.microsoft.com/cli/azure/install-azure-cli)
- JDK8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- Maven

## Prevision Azure Resources required to run this sample.

### Create an Azure Cosmos DB on Azure
### Authenticate using the Azure CLI
Terraform must authenticate to Azure to create infrastructure.

1. Go to [Azure portal](https://portal.azure.com/) and click +New .
2. Click Databases, and then click Azure Cosmos DB to create your database.
3. Navigate to the database you have created, and click Access keys and copy your URI and access keys for your database.

## Examples
In your terminal, use the Azure CLI tool to setup your account permissions locally.

### Config the sample
```shell
az login
```

Your browser window will open and you will be prompted to enter your Azure login credentials. After successful authentication, your terminal will display your subscription information. You do not need to save this output as it is saved in your system for Terraform to use.

```shell
You have logged in. Now let us find all the subscriptions to which you have access...

[
{
"cloudName": "AzureCloud",
"homeTenantId": "home-Tenant-Id",
"id": "subscription-id",
"isDefault": true,
"managedByTenants": [],
"name": "Subscription-Name",
"state": "Enabled",
"tenantId": "0envbwi39-TenantId",
"user": {
"name": "[email protected]",
"type": "user"
}
}
]
```

If you have more than one subscription, specify the subscription-id you want to use with command below:
```shell
az account set --subscription <your-subscription-id>
```

### Provision the Resources

After login Azure CLI with your account, now you can use the terraform script to create Azure Resources.

```shell
# In the root directory of the sample
# Initialize your Terraform configuration
terraform -chdir=./terraform init

# Apply your Terraform Configuration
# Type `yes` at the confirmation prompt to proceed.
terraform -chdir=./terraform apply

1. Navigate to `src/main/resources` and open `application.yml`.
2. replace below properties in `application.yml` with information of your database.
```

```yaml
spring:
cloud:
azure:
cosmos:
key: [your-cosmos-key]
endpoint: [your-cosmos-endpoint]
It may take a few minutes to run the script. After successful running, you will see prompt information like below:

```shell
azurecaf_name.cosmos: Creating...
azurecaf_name.resource_group: Creating...
azurerm_cosmosdb_account.application: Creating...
...
...
azurerm_cosmosdb_account.application: Creation complete after 2m23s ...
azurerm_cosmosdb_sql_database.db: Creating...
...
azurerm_cosmosdb_sql_database.db: Creation complete after 40s ...
azurerm_cosmosdb_sql_container.application: Creating...
azurerm_cosmosdb_sql_container.application: Still creating... [10s elapsed]
...
azurerm_cosmosdb_sql_container.application: Creation complete after 39s ...
...
...
Apply complete! Resources: 6 added, 0 changed, 0 destroyed.
...
```

### Run with Maven
You can go to [Azure portal](https://ms.portal.azure.com/) in your web browser to check the resources you created.

### Export output to your local Environment
Running the command below to export environment values:

```shell
source ./terraform/setup_env.sh
```
cd azure-spring-boot-samples/cosmos/spring-cloud-azure-starter-cosmos/spring-cloud-azure-cosmos-sample
mvn spring-boot:run

## Run locally

In your terminal, run `mvn clean spring-boot:run`.


```shell
mvn clean spring-boot:run
```

## Troubleshooting
## Verify this sample
Verify in your app’s logs that similar messages were posted:
```shell
...
Exec getDatabase() is Done.
...
Exec createContainerIfNotExists() is Done.
...
Exec createDocument() is Done.
...
Exec executeQueryPrintSingleResult() is Done.

```

## Next steps

## Clean up Resources
After running the sample, if you don't want to run the sample, remember to destroy the Azure resources you created to avoid unnecessary billing.

## Contributing
The terraform destroy command terminates resources managed by your terraform project.
To destroy the resources you created.

<!-- LINKS -->
```shell
terraform -chdir=./terraform destroy
```
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public class CosmosSampleApplication implements CommandLineRunner {
@Autowired
private CosmosClient client;

private final String databaseName = "AzureCosmosSampleDB";
private final String containerName = "AzureCosmosSampleContainer";
private final String databaseName = "products";
private final String containerName = "users";
private final String documentId = UUID.randomUUID().toString();
private final String documentLastName = "Peterson";

Expand All @@ -55,20 +55,20 @@ public void run(String... var1) throws Exception {
* Create Database
*/
private void createDatabaseIfNotExists() throws Exception {
logger.info("Create database " + databaseName + " if not exists...");
logger.info("Create database " + databaseName + " if not exists.........");

// Create database if not exists
CosmosDatabaseResponse databaseResponse = client.createDatabaseIfNotExists(databaseName);
database = client.getDatabase(databaseResponse.getProperties().getId());

logger.info("Done.");
logger.info("Exec getDatabase() is Done.");
}

/**
* Create container
*/
private void createContainerIfNotExists() throws Exception {
logger.info("Create container " + containerName + " if not exists.");
logger.info("Create container " + containerName + " if not exists.........");

// Create container if not exists
CosmosContainerProperties containerProperties =
Expand All @@ -81,7 +81,7 @@ private void createContainerIfNotExists() throws Exception {
CosmosContainerResponse containerResponse = database.createContainerIfNotExists(containerProperties, throughputProperties);
container = database.getContainer(containerResponse.getProperties().getId());

logger.info("Done.");
logger.info("Exec createContainerIfNotExists() is Done.");
}

/**
Expand All @@ -98,9 +98,9 @@ private void createDocument() throws Exception {

// Insert this item as a document
// Explicitly specifying the /pk value improves performance.
container.createItem(family,new PartitionKey(family.getLastName()),new CosmosItemRequestOptions());
container.createItem(family,new PartitionKey(family.getId()),new CosmosItemRequestOptions());

logger.info("Done.");
logger.info("Exec createDocument() is Done.");
}

private void queryAllDocuments() throws Exception {
Expand All @@ -120,6 +120,6 @@ private void executeQueryPrintSingleResult(String sql) {
logger.info(String.format("First query result: Family with (/id, partition key) = (%s,%s)",family.getId(),family.getLastName()));
}

logger.info("Done.");
logger.info("Exec executeQueryPrintSingleResult() is Done.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ spring:
cloud:
azure:
cosmos:
key: [your-cosmos-key]
endpoint: [your-cosmos-endpoint]
endpoint: ${AZURE_COSMOS_ENDPOINT}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = ">= 2.75"
}
azurecaf = {
source = "aztfmod/azurecaf"
version = "1.2.6"
}
}
}

provider "azurerm" {
features {}
}

resource "azurecaf_name" "resource_group" {
name = var.application_name
resource_type = "azurerm_resource_group"
random_length = 5
clean_input = true
}

resource "azurerm_resource_group" "main" {
name = azurecaf_name.resource_group.result
location = var.location

tags = {
"spring-cloud-azure-sample" = var.sample_tag_value
}
}

resource "azurecaf_name" "cosmos" {
name = var.application_name
resource_type = "azurerm_cosmosdb_account"
random_length = 5
clean_input = true
}

resource "azurerm_cosmosdb_account" "application" {
name = azurecaf_name.cosmos.result
location = var.location
resource_group_name = azurerm_resource_group.main.name
offer_type = "Standard"
kind = "GlobalDocumentDB"

enable_automatic_failover = true

consistency_policy {
consistency_level = "Session"
}

geo_location {
location = var.location
failover_priority = 0
}

tags = {
"spring-cloud-azure-sample" = var.sample_tag_value
}
}

resource "azurerm_cosmosdb_sql_database" "db" {
name = "products"
resource_group_name = azurerm_resource_group.main.name
account_name = azurerm_cosmosdb_account.application.name
throughput = 400
}

resource "azurerm_cosmosdb_sql_container" "application" {
name = "users"
resource_group_name = azurerm_cosmosdb_account.application.resource_group_name
account_name = azurerm_cosmosdb_account.application.name
database_name = azurerm_cosmosdb_sql_database.db.name
partition_key_path = "/id"
partition_key_version = 1
throughput = 400
}

# Used to get object_id
data "azurerm_client_config" "current" {
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
output "azure_cosmos_endpoint" {
value = azurerm_cosmosdb_account.application.endpoint
description = "Azure Storage endpoint."
saragluna marked this conversation as resolved.
Show resolved Hide resolved
}

output "azure_cosmos_account" {
value = azurerm_cosmosdb_account.application.name
description = "Azure Storage account created."
saragluna marked this conversation as resolved.
Show resolved Hide resolved
}

output "resource_group_name" {
value = azurerm_resource_group.main.name
description = "The Azure resource group name."
}

output "cosmos_application_id" {
value = azurerm_cosmosdb_account.application.id
description = "Cosmos account application id."
}

output "object_id" {
value = data.azurerm_client_config.current.object_id
description = "Current user's object_id."
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export AZURE_COSMOS_ENDPOINT=$(terraform -chdir=./terraform output -raw azure_cosmos_endpoint)
azure_cosmos_account=$(terraform -chdir=./terraform output -raw azure_cosmos_account)
# resourceGroupName
resourceGroupName=$(terraform -chdir=./terraform output -raw resource_group_name)
principalId=$(terraform -chdir=./terraform output -raw object_id)
readOnlyRoleDefinitionId=$(terraform -chdir=./terraform output -raw cosmos_application_id)/sqlRoleDefinitions/00000000-0000-0000-0000-000000000001
writeOnlyRoleDefinitionId=$(terraform -chdir=./terraform output -raw cosmos_application_id)/sqlRoleDefinitions/00000000-0000-0000-0000-000000000002
# assign current Cosmos DB Built-in Data Reader
# https://docs.microsoft.com/azure/cosmos-db/how-to-setup-rbac#using-the-azure-cli-1
az cosmosdb sql role assignment create --account-name $azure_cosmos_account --resource-group $resourceGroupName --scope "/" --principal-id $principalId --role-definition-id $readOnlyRoleDefinitionId
# assign current Cosmos DB Built-in Data Contributor
az cosmosdb sql role assignment create --account-name $azure_cosmos_account --resource-group $resourceGroupName --scope "/" --principal-id $principalId --role-definition-id $writeOnlyRoleDefinitionId
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
variable "application_name" {
type = string
description = "The name of your application"
default = "cosmos-sample"
}

variable "location" {
type = string
description = "The Azure region where all resources in this example should be created"
default = "eastus"
}

variable "sample_tag_value" {
type = string
description = "The value of spring-cloud-azure-sample tag"
default = "true"
}
Loading