Skip to content

Commit cc6b61a

Browse files
authored
Merge pull request microsoft#51 from yupwei68/wyp-20200622
Update the Terraform Azurerm Provider Resource Contribute.md
2 parents 24455a9 + ef75783 commit cc6b61a

File tree

1 file changed

+55
-55
lines changed

1 file changed

+55
-55
lines changed

provider/CONTRIBUTE.md

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@ We also recommend that you read the [README](https://github.com/terraform-provid
77

88
### Terraform
99

10-
You need to install Terraform on your dev environment. You can downlaod it from [this page](https://www.terraform.io/downloads.html).
10+
You need to install Terraform on your dev environment. You can downlaod it from [terraform](https://www.terraform.io/downloads.html).
1111

1212
### Go tools
1313

14-
Terraform is developed using Go Programming Language. You need to install Go Programming Language **1.11.x** to be able to build and debug the provider locally.
15-
You can download it from [this page](https://golang.org/dl/) and find the installation instructions for your system [here](https://golang.org/doc/install#install)
14+
Terraform is developed using Go Programming Language. You need to install Go Programming Language **1.x.x** to be able to build and debug the provider locally. You can check to see what version of Go is currently being used in the project by checking the [go.mod](https://github.com/terraform-providers/terraform-provider-azurerm/blob/master/go.mod) file in the root directory.
15+
You can download it from [golang](https://golang.org/dl/) and find the installation instructions for your system [golang install](https://golang.org/doc/install#install)
1616

17-
Then you can test your environment following the instructions on [this page](https://golang.org/doc/install#testing).
17+
Then you can test your environment following the instructions on [golang testing](https://golang.org/doc/install#testing).
1818

1919
### Check your environment variables
2020

21-
As with many Go project, AzureRM Terraform provider rely on your GOPATH environment variable. You may want to make sure it is well configured for your system, reading [this page](https://github.com/golang/go/wiki/SettingGOPATH).
21+
As with many Go project, AzureRM Terraform provider rely on your GOPATH environment variable. You may want to make sure it is well configured for your system, reading [golang setting go path](https://github.com/golang/go/wiki/SettingGOPATH).
2222

2323
### Visual Studio Code
2424

25-
You can use the IDE you love, but in this documentation we will describe how to contribute to the Terraform AzureRM provider using Visual Studio Code. You can download it for your system from [this page](https://code.visualstudio.com/Download).
25+
You can use the IDE you love, but in this documentation we will describe how to contribute to the Terraform AzureRM provider using Visual Studio Code. You can download it for your system from [visual studio](https://code.visualstudio.com/Download).
2626

2727
Once installed, download the Go extension for VS Code:
2828

@@ -38,7 +38,7 @@ Once installed, open VS Code and look for the `Go: Install/Update Tools` in the
3838

3939
### Specific requirements for Windows users
4040

41-
If you are running Windows, then you need to install Git Bash and Make for Windows. Check the dedicated section on Terraform on Azure repository [here](https://github.com/terraform-providers/terraform-provider-azurerm#windows-specific-requirements).
41+
If you are running Windows, then you need to install Git Bash and Make for Windows. Check the dedicated section on Terraform on Azure repository [terraform developer requirements](https://github.com/terraform-providers/terraform-provider-azurerm#developer-requirements).
4242

4343
## Get the sources
4444

@@ -64,11 +64,14 @@ More information [here](https://github.com/terraform-providers/terraform-provide
6464
## Work with your local build
6565

6666
Once you have built a new version of the AzureRM Terraform provider, you can use it locally.
67-
To use your local version, the first thing to do is a `terraform init`, as usual, to inialize your terraform working directory.
6867

69-
If your `terraform` binary is in the `$GOPATH/bin` folder on your machine, then the terraform init operation will use the locally built provider.
68+
First, you need to move your `terraform` binary which is in the `$GOPATH/bin` folder on your machine, to the third-party plugins folder which is located at `~/.terraform.d/plugins` on most operating systems and `%APPDATA%\terraform.d\plugins` on Windows.
7069

71-
If not, the init operation will download the AzureRM Provider for you. You can just remove it, and replace it with your local copy. Do a `terraform init` again and you're done ! :-)
70+
Then you will need to go to your terraform test folder where the `main.tf` configuration file exists and initialize your terraform working directory by using the `terraform init` command.
71+
72+
If the third-party plugin folder is empty, the init operation will download the AzureRM Provider for you.
73+
74+
More information [here](https://www.terraform.io/docs/extend/how-terraform-works.html#plugin-locations).
7275

7376
## Debug the AzureRM provider using Visual Studio Code and Delve
7477

@@ -154,52 +157,53 @@ For each piece of code that you write into the provider, you need to make sure t
154157
- documentation created or updated
155158
- example created or updated (optional)
156159

157-
Data sources, resources and tests are defined in the [azurerm](https://github.com/terraform-providers/terraform-provider-azurerm/tree/master/azurerm) folder of the repository.
160+
Data sources, resources and tests are defined in the [azurerm/internal/services](https://github.com/terraform-providers/terraform-provider-azurerm/tree/master/azurerm/internal/services) folder of the repository.
158161

159162
Documentation for data sources is in the [website/docs/d](https://github.com/terraform-providers/terraform-provider-azurerm/tree/master/website/docs/d) folder and documentation for resources is in the [website/docs/r](https://github.com/terraform-providers/terraform-provider-azurerm/tree/master/website/docs/r) folder.
160163

161164
Finally, examples are located in the [examples](https://github.com/terraform-providers/terraform-provider-azurerm/tree/master/examples) folder.
162165

163-
AzureRM Terraform provider uses the [Azure SDK for Go](https://github.com/Azure/azure-sdk-for-go) to interact with Azure. It's important that you respect that rule. If you are trying to do something that is not available in the Azure SDK for Go, then you should open an issue on that repository first.
166+
AzureRM Terraform provider uses the [Azure SDK for Go](https://github.com/Azure/azure-sdk-for-go) to interact with Azure. It's important that you respect that rule. If you are trying to do something that is not available in the Azure SDK for Go, then you should check if the API is availble in [Azure Service Repository](https://github.com/Azure/azure-rest-api-specs) and open an issue in the **Azure SDK for Go** repository to have it added to a future release of the Azure SDK for Go.
164167

165168
We recommand that you start with fixing issues/patching an existing data source or resource to understand how it works in details before trying to implement a brand new one.
166169

167170
Don't forget that you are working on your own fork of the repository and that you need to open a pull request to the main repository to bring your update to the HashiCorp team, for review.
168171

169172
### Working with Azure SDK for Go
170173

171-
All the imports for the Azure Go SDK services have to be done in the [config.go](https://github.com/terraform-providers/terraform-provider-azurerm/blob/master/azurerm/config.go) file. It exposes a top level struct, `ArmClient` that exposes all the client that you may use in a data source or resource to interact with Azure.
172-
173-
For example, if you need to work on implementing Azure Batch Account support to the provider:
174-
175-
1. Add import for `"github.com/Azure/azure-sdk-for-go/services/batch/mgmt/2018-12-01/batch"` in the imports list
176-
2. Add a field for the `batchAccountClient`:
174+
All the imports for the Azure for Go SDK services have to be done in the [client.go](https://github.com/terraform-providers/terraform-provider-azurerm/blob/master/azurerm/internal/clients/client.go) file. It exposes a top level struct, `Client` that exposes all the service clients that you may use in a data source or resource to interact with Azure. If the service already exist and you are just modifying an existing resource you can skip this step.
177175

178-
```go
179-
batchAccountClient batch.AccountClient
180-
```
176+
Then you need to register your subservice in `client.go` in your corresponding service folder.
181177

182-
3. Add a function that registers the Azure Batch client:
178+
For example, if you need to work on implementing Azure Batch Account support to the provider:
183179

180+
1. Check if the service `Batch` registered in service [client.go](https://github.com/terraform-providers/terraform-provider-azurerm/blob/master/azurerm/internal/clients/client.go). If not, you need to register your service.
181+
2. Go to your corresponding service folder [batch](https://github.com/terraform-providers/terraform-provider-azurerm/tree/master/azurerm/internal/services/batch) and write a `client.go` in client folder. If the service folder does not exist, you will need to create one.
182+
3. Add a variable `AccountClient` which is created by the service api `NewAccountClientWithBaseURI` from `"github.com/Azure/azure-sdk-for-go/services/batch/mgmt/2019-08-01/batch"`.
184183
```go
185-
func (c *ArmClient) registerBatchClients(endpoint, subscriptionId string, auth autorest.Authorizer) {
186-
batchAccount := batch.NewAccountClientWithBaseURI(endpoint, subscriptionId)
187-
c.configureClient(&batchAccount.Client, auth)
188-
c.batchAccountClient = batchAccount
184+
type Client struct {
185+
AccountClient *batch.AccountClient
189186
}
190187
```
191188

192-
4. Call the function that registers the Batch client:
189+
4. Register your client in the Terraform ResourceManagerAuthorizer:
193190

194191
```go
195-
client.registerBatchClients(endpoint, c.SubscriptionID, auth)
196-
```
192+
func NewClient(o *common.ClientOptions) *Client {
193+
accountClient := batch.NewAccountClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
194+
o.ConfigureClient(&accountClient.Client, o.ResourceManagerAuthorizer)
197195

196+
return &Client{
197+
AccountClient: &accountClient,
198+
}
199+
}
200+
```
198201
By doing the four steps above, you will make sure that when developping the data source or the resource, you can access the Azure SDK for Go clients that you need.
199202

200203
### Developing a Data Source
201204

202-
The file for a data source is named following the convention: data_source_*NAME_OF_THE_DATA_SOURCE*.go.
205+
When naming a new data source file it should follow the below naming convention:
206+
**name_of_the_data_source**_data_source.go
203207

204208
A data source is composed of two relevant functions:
205209

@@ -216,7 +220,7 @@ name := d.Get("name").(string)
216220
You can get a reference on any Azure SDK for Go client registered in the `client.go` using:
217221

218222
```go
219-
client := meta.(*ArmClient).batchAccountClient
223+
client := meta.(*clients.Client).Batch.AccountClient
220224
```
221225

222226
Finally, you can set values retrieve from Azure on the same object, using the `Set` function:
@@ -225,52 +229,48 @@ Finally, you can set values retrieve from Azure on the same object, using the `S
225229
d.Set("account_endpoint", resp.AccountEndpoint)
226230
```
227231

228-
You can check the whole definition of the Azure Batch Account data source [here](https://github.com/terraform-providers/terraform-provider-azurerm/blob/master/azurerm/data_source_batch_account.go).
232+
You can check the whole definition of the Azure Batch Account data source [here](https://github.com/terraform-providers/terraform-provider-azurerm/blob/master/azurerm/internal/services/batch/batch_account_data_source.go).
229233

230-
Once your data source is defined, you need to register it into data sources map in the `provider.go` file:
234+
Once your data source is defined, you need to register it into the data sources map in the `registration.go` file:
231235

232236
```go
233-
DataSourcesMap: map[string]*schema.Resource{
234-
"azurerm_api_management": dataSourceApiManagementService(),
237+
func (r Registration) SupportedDataSources() map[string]*schema.Resource {
238+
return map[string]*schema.Resource{
239+
"azurerm_batch_account": dataSourceArmBatchAccount(),
240+
}
241+
}
235242
```
236243

237244
### Developing a resource
238245

239246
Developing a resource is really similar to developing a data source. Instead of having only a function to read the data from Azure, it also offers the possibility to write functions to create, update and delete the resource. Apart from that, concepts are the same:
240247

241-
- The file is named using the convention: resource_arm_*NAME_OF_RESOURCE*.go
248+
- The file is named using the convention: *name_of_resource*_resource.go
242249
- One function to define the schema of the resource, named by convention resourceArm*NAME_OF_RESOURCE*, for example `resourceArmBatchAccount`.
243250
- One function to create the resource, named by convention resourceArm*NAME_OF_RESOURCE*Create, for example `resourceArmBatchAccountCreate`.
244251
- One function to read the resource, named by convention resourceArm*NAME_OF_RESOURCE*Read, for example `resourceArmBatchAccountRead`.
245252
- One function to update the resource, named by convention resourceArm*NAME_OF_RESOURCE*Update, for example `resourceArmBatchAccountUpdate`.
246253
- One function to delete the resource, named by convention resourceArm*NAME_OF_RESOURCE*Delete, for example `resourceArmBatchAccountDelete`.
247254

248-
The [Azure Batch Account resource](https://github.com/terraform-providers/terraform-provider-azurerm/blob/master/azurerm/resource_arm_batch_account.go) is a good and simple example to understand the flow.
249-
250-
### Acceptance tests
251-
252-
Acceptance tests are test that will run live on your Azure subscription to validate that your data source or resource is working well. Each data source should have its acceptance tests and each resource should have its acceptance test.
255+
The [Azure Batch Account resource](https://github.com/terraform-providers/terraform-provider-azurerm/blob/master/azurerm/internal/services/batch/batch_account_resource.go) is a good and simple example to understand the flow.
253256

254-
Tests are definied in the `azurerm` directory, aside with data sources and resources. The file name is the same than the one for the data source or resource, with the `_test` suffix.
255-
256-
You can find examples of tests for Azure Batch Account data source [here](https://github.com/terraform-providers/terraform-provider-azurerm/blob/master/azurerm/data_source_batch_account_test.go) and Azure Batch Account resource [here](https://github.com/terraform-providers/terraform-provider-azurerm/blob/master/azurerm/resource_arm_batch_account_test.go).
257-
258-
Please refer to the above section to learn on to run the acceptance tests on your laptop.
259-
260-
Once your resource is defined, you need to register it into the resources map in the `provider.go` file:
257+
Once your resource is defined, you need to register it into the resources map in the `registration.go` file:
261258

262259
```go
263-
ResourcesMap: map[string]*schema.Resource{
264-
"azurerm_api_management": resourceArmApiManagementService(),
260+
func (r Registration) SupportedResources() map[string]*schema.Resource {
261+
return map[string]*schema.Resource{
262+
"azurerm_batch_account": resourceArmBatchAccount(),
263+
}
264+
}
265265
```
266266

267-
## Other
267+
### Acceptance tests
268268

269-
### Slack
269+
Acceptance tests are tests that will run live on your Azure subscription to validate that your resource/data source is working as expected. Each resource/data source should have its own set of acceptance tests that will run independently of each other.
270270

271-
You can request an invite to access the Terraform on Azure Slack [here](https://join.slack.com/t/terraform-azure/shared_invite/enQtNDMzNjQ5NzcxMDc3LTJkZTJhNTg3NTE5ZTdjZjFhMThmMTVmOTg5YWJkMDU1YTMzN2YyOWJmZGM3MGI4OTQ0ODQxNTEyNjdjMDAxMjM).
271+
All tests for a given resource/data source are located in the `test` folder which is a sub folder of the service folder. The file name for the tests will be exactly the same as the resource/data source with the addition of the `_test.go` suffix. For example, if you created a data source for `batch account`, the data source file would be named **batch_account_data_source.go** and the test file would have the name **batch_account_data_source_test.go**.
272272

273-
### Configuring Terraform on Windows 10 Linux Sub-System
273+
You can find examples of tests for Azure Batch Account data source [here](https://github.com/terraform-providers/terraform-provider-azurerm/blob/master/azurerm/internal/services/batch/batch_account_data_source_test.go) and Azure Batch Account resource [here](https://github.com/terraform-providers/terraform-provider-azurerm/blob/master/azurerm/internal/services/batch/batch_account_resource_test.go).
274274

275-
It is also possible to configure Terraform to be run from the Windows 10 Linux Sub-System (WSL). Check [this blog post](https://techcommunity.microsoft.com/t5/Azure-Developer-Community-Blog/Configuring-Terraform-on-Windows-10-Linux-Sub-System/ba-p/393845) for more information.
275+
Please refer to the above section to learn how to run the acceptance tests on your computer.
276276

0 commit comments

Comments
 (0)