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 samples with terraform and defaultCredential for Storage Queue samples #139

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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,75 +1,134 @@
# Spring Cloud Azure Storage Queue Integration Code Sample shared library for Java

## Key concepts
# Using Spring Integration for Azure Storage Queue

This code sample demonstrates how to use Spring Integration for Azure Storage Queue.

## Getting started
## What You Will Build
You will build an application using Spring Integration to send and receive messages for Azure Storage Queue.

## What You Need

- [An Azure subscription](https://azure.microsoft.com/free/)
- [Terraform](https://www.terraform.io/)
- [Azure CLI](https://docs.microsoft.com/cli/azure/install-azure-cli)
- [JDK8](https://www.oracle.com/java/technologies/downloads/) or later
- Maven
- You can also import the code straight into your IDE:
- [IntelliJ IDEA](https://www.jetbrains.com/idea/download)

Running this sample will be charged by Azure. You can check the usage and bill at
[this link][azure-account].
## Provision Azure Resources Required to Run This Sample
This sample will create Azure resources using Terraform. If you choose to run it without using Terraform to provision resources, please pay attention to:
> [!IMPORTANT]
> If you choose to use a security principal to authenticate and authorize with Azure Active Directory for accessing an Azure resource
> please refer to [Authorize access with Azure AD](https://microsoft.github.io/spring-cloud-azure/docs/current/reference/html/index.html#authorize-access-with-azure-active-directory) to make sure the security principal has been granted the sufficient permission to access the Azure resource.

### Authenticate Using the Azure CLI
Terraform must authenticate to Azure to create infrastructure.

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

### Create Azure resources
```shell
az login
```

1. Create [Azure Storage][create-azure-storage].

1. **[Optional]** if you want to use service principal, please follow
[create service principal from Azure CLI][create-sp-using-azure-cli] to create one.
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.

1. **[Optional]** if you want to use managed identity, please follow
[create managed identity][create-managed-identity] to set up managed identity.
```shell
You have logged in. Now let us find all the subscriptions to which you have access...

## Examples
[
{
"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"
}
}
]
```

1. Update stream binding related properties in
`application.yaml`. If you choose to use
service principal or managed identity, update the `application-sp.yaml` or
`application-mi.yaml` respectively.
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>
```

```yaml
spring:
cloud:
azure:
storage:
account: [storage-account-name]
access-key: [storage-account-access-key]
```
### Provision the Resources

2. Update queue name in
**SendController.java** and
**ReceiveController.java**.
After login Azure CLI with your account, now you can use the terraform script to create Azure Resources.

1. Run the `mvn spring-boot:run` in the root of the code sample to get
the app running.
```shell
# In the root directory of the sample
# Initialize your Terraform configuration
terraform -chdir=./terraform init

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

$ curl -X POST localhost:8080/messages?message=hello
```

1. Receive the message you posted
It may take a few minutes to run the script. After successful running, you will see prompt information like below:

$ curl -X GET localhost:8080/messages
```shell
azurecaf_name.azurecaf_name_storage_account: Creating...
azurecaf_name.resource_group: Creating...
azurecaf_name.azurecaf_name_storage_account: Creation complete after 0s
azurecaf_name.resource_group: Creation complete after 0s
azurerm_resource_group.main: Creating...
azurerm_resource_group.main: Creation complete after 1s ...
azurerm_storage_account.storage_account: Creating...
azurerm_storage_account.storage_account: Still creating... [10s elapsed]
...
azurerm_role_assignment.role_storage_queue_data_contributor: Still creating...
...
azurerm_role_assignment.role_storage_queue_data_contributor: Creation complete ...

1. Verify in your app’s logs that a similar message was posted:
Apply complete! Resources: 6 added, 0 changed, 0 destroyed.

New message received: 'hello'
Message 'hello' successfully checkpointed
Outputs:
...
```

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
```

1. Delete the resources on [Azure Portal][azure-portal] to avoid unexpected charges.
## Run Locally

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

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

## Next steps
## Verify This Sample

1. Send a POST request

curl -X POST http://localhost:8080/messages?message=hello

2. Verify in your app’s logs that similar messages were posted:

New message received: 'hello'
Message 'hello' successfully checkpointed

## Contributing
## 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.

<!-- LINKS -->
The terraform destroy command terminates resources managed by your Terraform project.
To destroy the resources you created.

[azure-account]: https://azure.microsoft.com/account/
[azure-portal]: https://ms.portal.azure.com/
[create-azure-storage]: https://docs.microsoft.com/azure/storage/
[create-managed-identity]: https://github.com/Azure-Samples/azure-spring-boot-samples/blob/main/create-managed-identity.md
[create-sp-using-azure-cli]: https://github.com/Azure-Samples/azure-spring-boot-samples/blob/main/create-sp-using-azure-cli.md
```shell
terraform -chdir=./terraform destroy
```

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ spring:
azure:
storage:
queue:
account-name: [account-name]
account-key: [account-key]
account-name: ${ACCOUNT_NAME}

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = ">= 2.75"
}
azurecaf = {
source = "aztfmod/azurecaf"
version = "1.2.10"
}
}
}

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 = {
terraform = "true"
application-name = var.application_name
spring-cloud-azure-sample = var.sample_tag_value
}
}

resource "azurecaf_name" "azurecaf_name_storage_account" {
name = var.application_name
resource_type = "azurerm_storage_account"
random_length = 5
clean_input = true
}

# storage
resource "azurerm_storage_account" "storage_account" {
name = azurecaf_name.azurecaf_name_storage_account.result
resource_group_name = azurerm_resource_group.main.name
location = var.location
account_tier = "Standard"
account_replication_type = "LRS"
}

resource "azurerm_storage_queue" "queue" {
name = "example"
storage_account_name = azurerm_storage_account.storage_account.name
}

# assign roles
data "azurerm_client_config" "current" {
}

resource "azurerm_role_assignment" "role_storage_queue_data_contributor" {
scope = azurerm_storage_account.storage_account.id
role_definition_name = "Storage Queue Data Contributor"
principal_id = data.azurerm_client_config.current.object_id
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "ACCOUNT_NAME" {
value = azurerm_storage_account.storage_account.name
description = "The name of storage account."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export ACCOUNT_NAME=$(terraform -chdir=./terraform output -raw ACCOUNT_NAME)
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 = "integration"
}

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