Skip to content

Commit dd92728

Browse files
committed
add how to for debugging azurerm tf provider
1 parent 8004c17 commit dd92728

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

provider/CONTRIBUTE.md

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,66 @@ Do a `terraform init` again and you're done ! :-)
7272
## Debug the AzureRM provider using Visual Studio Code and Delve
7373

7474
It is possible to use Visual Studio Code and Delve (the Golang debugger) to debug the AzureRM provider.
75+
The easiest way to debug Terraform AzureRM Provider is to execute the acceptances unit test with the Delve debugger attached. Acceptance tests are tests that are written for every resources and data sources and that will really execute the code to an Azure subscription, to validate everything is working well.
7576

76-
**TODO**
77+
First, to be able to connect to Azure, you need to create a service principal using the following command:
78+
79+
```bash
80+
az ad sp create-for-rbac --role=Contributor --scope=/subscriptions/<YOUR_SUBSCRIPTION_ID>
81+
```
82+
83+
Then, you need to create a `.launch.json` file inside the `.vscode` folder at the root of the Terraform AzureRM provider directory (create the `.vscode` folder if it does not exist).
84+
85+
Copy the following content into the file:
86+
87+
```json
88+
{
89+
// Use IntelliSense to learn about possible attributes.
90+
// Hover to view descriptions of existing attributes.
91+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
92+
"version": "0.2.0",
93+
"configurations": [
94+
{
95+
"name": "Launch test function",
96+
"type": "go",
97+
"request": "launch",
98+
"mode": "test",
99+
"program": "${workspaceRoot}/azurerm/resource_arm_container_registry_test.go",
100+
"args": [
101+
"-test.v",
102+
"-test.run",
103+
"TestAccAzureRMContainerRegistry_geoReplication"
104+
],
105+
"envFile": "${workspaceRoot}/.vscode/private.env",
106+
"showLog": true
107+
},
108+
]
109+
}
110+
```
111+
112+
The configuration above allows to start debugging a Terraform resource, by launching one or more acceptance test:
113+
114+
- The `program` property indicates the file you want to debug
115+
- The last entry of the `args` property, here `TestAccAzureRMContainerRegistry_geoReplication` represents th test to launch. You can use regex to run multiple tests (ex: `TestAccAzureRMContainerRegistry_*`)
116+
- The `envFile` property defines the path to get the environment variables file (mainly Azure credentials) that needs to be used to run the acceptance test.
117+
118+
Create the `private.env` file into the `.vscode` folder and fill it with the following environment variables:
119+
120+
```
121+
ARM_CLIENT_ID=<YOUR_SERVICE_PRINCIPAL_CLIENT_ID>
122+
ARM_CLIENT_SECRET=<YOUR_SERVICE_PRINCIPAL_CLIENT_SECRET>
123+
ARM_SUBSCRIPTION_ID=<YOUR_AZURE_SUBSCRIPTION_ID>
124+
ARM_TENANT_ID=<YOUR_AZURE_TENANT_ID>
125+
ARM_TEST_LOCATION=<AZURE_LOCATION_1>
126+
ARM_TEST_LOCATION_ALT=<AZURE_LOCATION_2>
127+
TF_ACC=1
128+
```
129+
130+
Once done, you can just press F5 and the debug will start! You can place breakpoints in your code to do step by step debugging:
131+
132+
![Install Go Tools - Wait](assets/code-debug-breakpoint.png)
133+
134+
*Note: the first time your start the debug, it can take a while, you need to be patient :-)*
77135

78136
## Other
79137

208 KB
Loading

0 commit comments

Comments
 (0)