Skip to content

Commit 3d6183d

Browse files
authored
Merge pull request microsoft#3 from jcorioland/contrib
Update contribution guide
2 parents 649503b + 8b65cc2 commit 3d6183d

File tree

1 file changed

+131
-2
lines changed

1 file changed

+131
-2
lines changed

provider/CONTRIBUTE.md

Lines changed: 131 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Contribute to Terraform AzureRM provider
22

33
This document describe how you can get ready to contribute to the [AzureRM Terraform provider](https://github.com/terraform-providers/terraform-provider-azurerm).
4+
We also recommend that you read the [README](https://github.com/terraform-providers/terraform-provider-azurerm/blob/master/README.md) on the AzureRM Terraform Provider repository.
45

56
## Setup your system
67

@@ -65,9 +66,9 @@ More information [here](https://github.com/terraform-providers/terraform-provide
6566
Once you have built a new version of the AzureRM Terraform provider, you can use it locally.
6667
To use your local version, the first thing to do is a `terraform init`, as usual, to inialize your terraform working directory.
6768

68-
The init operation will download the AzureRM Provider for you. You can just remove it, and replace it with your local copy.
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.
6970

70-
Do a `terraform init` again and you're done ! :-)
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 ! :-)
7172

7273
## Debug the AzureRM provider using Visual Studio Code and Delve
7374

@@ -135,6 +136,134 @@ Once done, you can just press F5 and the debug will start! You can place breakpo
135136

136137
*Note: the first time your start the debug, it can take a while, you need to be patient :-)*
137138

139+
## How to contribute to the Azure RM provider
140+
141+
*Note: we also invite you to read the official documentation about developing a provider [here](https://www.terraform.io/docs/plugins/basics.html#developing-a-plugin)*
142+
143+
First you need to pick [an issue on the provider](https://github.com/terraform-providers/terraform-provider-azurerm/issues) by commenting the issue and saying that you're going to work on that, to make sure that the repo maintainers are aware that you are going to work on this issue.
144+
We also strongly advise that you describe the work you are planning to do, like explaining your implementation, submitting the TF schema for new resources, to make sure the discussion start early as possible with the reviewers.
145+
146+
*Note: if there is no issue for the problem you are trying to solve, you can create one.*
147+
148+
If you are a new contributor, there is a [good first issue](https://github.com/terraform-providers/terraform-provider-azurerm/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22) label that may help you to filter the issue and start with a simple one.
149+
150+
For each piece of code that you write into the provider, you need to make sure that you have:
151+
152+
- the implementations of the `data source` and/or `resource` definition
153+
- acceptance test created or updated
154+
- documentation created or updated
155+
- example created or updated (optional)
156+
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.
158+
159+
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.
160+
161+
Finally, examples are located in the [examples](https://github.com/terraform-providers/terraform-provider-azurerm/tree/master/examples) folder.
162+
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.
164+
165+
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.
166+
167+
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.
168+
169+
### Working with Azure SDK for Go
170+
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`:
177+
178+
```go
179+
batchAccountClient batch.AccountClient
180+
```
181+
182+
3. Add a function that registers the Azure Batch client:
183+
184+
```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
189+
}
190+
```
191+
192+
4. Call the function that registers the Batch client:
193+
194+
```go
195+
client.registerBatchClients(endpoint, c.SubscriptionID, auth)
196+
```
197+
198+
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.
199+
200+
### Developing a Data Source
201+
202+
The file for a data source is named following the convention: data_source_*NAME_OF_THE_DATA_SOURCE*.go.
203+
204+
A data source is composed of two relevant functions:
205+
206+
- The function that creates a `schema.Resource` object that returns the schema (definition) of the data source,i.e. the property that compose the data source, and some rules about those properties. You only need to provide required properties to locate that resource, and mark all other properties returned from the service as `Computed`. This function is named by convention dataSourceArm*NAME_OF_THE_DATA_SOURCE*, for example `dataSourceArmBatchAccount` for a Batch Account.
207+
- The function that retrieves the data from Azure using the Azure SDK for Go client and set all the values related to the data source. This function is named by convention dataSourceArm*NAME_OF_THE_DATA_SOURCE*Read, for example `dataSourceArmBatchAccountRead` for a Batch Account.
208+
209+
This function takes a `schema.ResourceData` in parameter. You can get the name of the object to retrieve and any property that is defined by the user on that object:
210+
211+
```go
212+
resourceGroup := d.Get("resource_group_name").(string)
213+
name := d.Get("name").(string)
214+
```
215+
216+
You can get a reference on any Azure SDK for Go client registered in the `client.go` using:
217+
218+
```go
219+
client := meta.(*ArmClient).batchAccountClient
220+
```
221+
222+
Finally, you can set values retrieve from Azure on the same object, using the `Set` function:
223+
224+
```go
225+
d.Set("account_endpoint", resp.AccountEndpoint)
226+
```
227+
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).
229+
230+
Once your data source is defined, you need to register it into data sources map in the `provider.go` file:
231+
232+
```go
233+
DataSourcesMap: map[string]*schema.Resource{
234+
"azurerm_api_management": dataSourceApiManagementService(),
235+
```
236+
237+
### Developing a resource
238+
239+
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:
240+
241+
- The file is named using the convention: resource_arm_*NAME_OF_RESOURCE*.go
242+
- One function to define the schema of the resource, named by convention resourceArm*NAME_OF_RESOURCE*, for example `resourceArmBatchAccount`.
243+
- One function to create the resource, named by convention resourceArm*NAME_OF_RESOURCE*Create, for example `resourceArmBatchAccountCreate`.
244+
- One function to read the resource, named by convention resourceArm*NAME_OF_RESOURCE*Read, for example `resourceArmBatchAccountRead`.
245+
- One function to update the resource, named by convention resourceArm*NAME_OF_RESOURCE*Update, for example `resourceArmBatchAccountUpdate`.
246+
- One function to delete the resource, named by convention resourceArm*NAME_OF_RESOURCE*Delete, for example `resourceArmBatchAccountDelete`.
247+
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.
253+
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:
261+
262+
```go
263+
ResourcesMap: map[string]*schema.Resource{
264+
"azurerm_api_management": resourceArmApiManagementService(),
265+
```
266+
138267
## Other
139268
140269
### Slack

0 commit comments

Comments
 (0)