Skip to content

Commit ce248d0

Browse files
committed
add instructions to develop the provider
1 parent 649503b commit ce248d0

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

provider/CONTRIBUTE.md

Lines changed: 112 additions & 0 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 recommand that your 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

@@ -135,6 +136,117 @@ 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+
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 HashiCorp people who maintain the repository and will review your contribution are aware that you are going to work on that.
142+
143+
*Note: if there is no issue for the problem you are trying to solve, you can create one.*
144+
145+
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.
146+
147+
For each piece of code that you write into the provider, you need to make sure that you have:
148+
149+
- the implementation in both `data source` and `resource` definition
150+
- acceptance test created or updated
151+
- documentation created or updated
152+
- example created or updated
153+
154+
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.
155+
156+
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.
157+
158+
Finally, examples are located in the [examples](https://github.com/terraform-providers/terraform-provider-azurerm/tree/master/examples) folder.
159+
160+
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.
161+
162+
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.
163+
164+
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.
165+
166+
### Working with Azure SDK for Go
167+
168+
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.
169+
170+
For example, if you need to work on implementing Azure Batch Account support to the provider:
171+
172+
1. Add import for `"github.com/Azure/azure-sdk-for-go/services/batch/mgmt/2018-12-01/batch"` in the imports list
173+
2. Add a field for the `batchAccountClient`:
174+
175+
```go
176+
batchAccountClient batch.AccountClient
177+
```
178+
179+
3. Add a function that registers the Azure Batch client:
180+
181+
```go
182+
func (c *ArmClient) registerBatchClients(endpoint, subscriptionId string, auth autorest.Authorizer) {
183+
batchAccount := batch.NewAccountClientWithBaseURI(endpoint, subscriptionId)
184+
c.configureClient(&batchAccount.Client, auth)
185+
c.batchAccountClient = batchAccount
186+
}
187+
```
188+
189+
4. Call the function that registers the Batch client:
190+
191+
```go
192+
client.registerBatchClients(endpoint, c.SubscriptionID, auth)
193+
```
194+
195+
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.
196+
197+
### Developping a Data Source
198+
199+
The file for a data source is named following the convention: data_source_*NAME_OF_THE_DATA_SOURCE*.go.
200+
201+
A data source is composed of two relevant functions:
202+
203+
- 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 (for example, if it is mandatory or not, computed etc.). This function is named by convention dataSourceArm*NAME_OF_THE_DATA_SOURCE*, for example `dataSourceArmBatchAccount` for a Batch Account.
204+
- 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.
205+
206+
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:
207+
208+
```go
209+
resourceGroup := d.Get("resource_group_name").(string)
210+
name := d.Get("name").(string)
211+
```
212+
213+
You can get a reference on any Azure SDK for Go client registered in the `client.go` using:
214+
215+
```go
216+
client := meta.(*ArmClient).batchAccountClient
217+
```
218+
219+
Finally, you can set values retrieve from Azure on the same object, using the `Set` function:
220+
221+
```go
222+
d.Set("account_endpoint", resp.AccountEndpoint)
223+
```
224+
225+
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).
226+
227+
### Developing a resource
228+
229+
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:
230+
231+
- The file is named using the convention: resource_arm_*NAME_OF_RESOURCE*.go
232+
- One function to define the structure of the resource, named by convention resourceArm*NAME_OF_RESOURCE*, for example `resourceArmBatchAccount`.
233+
- One function to create the resource, named by convention resourceArm*NAME_OF_RESOURCE*Create, for example `resourceArmBatchAccountCreate`.
234+
- One function to read the resource, named by convention resourceArm*NAME_OF_RESOURCE*Read, for example `resourceArmBatchAccountRead`.
235+
- One function to update the resource, named by convention resourceArm*NAME_OF_RESOURCE*Update, for example `resourceArmBatchAccountUpdate`.
236+
- One function to delete the resource, named by convention resourceArm*NAME_OF_RESOURCE*Delete, for example `resourceArmBatchAccountDelete`.
237+
238+
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.
239+
240+
### Acceptance tests
241+
242+
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.
243+
244+
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.
245+
246+
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).
247+
248+
Please refer to the above section to learn on to run the acceptance test on your laptop.
249+
138250
## Other
139251

140252
### Slack

0 commit comments

Comments
 (0)