github.com/pdecat/terraform@v0.11.9-beta1/website/docs/plugins/provider.html.md (about) 1 --- 2 layout: "docs" 3 page_title: "Provider Plugins" 4 sidebar_current: "docs-plugins-provider" 5 description: |- 6 A provider in Terraform is responsible for the lifecycle of a resource: create, read, update, delete. An example of a provider is AWS, which can manage resources of type `aws_instance`, `aws_eip`, `aws_elb`, etc. 7 --- 8 9 # Provider Plugins 10 11 ~> **Advanced topic!** Plugin development is a highly advanced 12 topic in Terraform, and is not required knowledge for day-to-day usage. 13 If you don't plan on writing any plugins, this section of the documentation is 14 not necessary to read. For general use of Terraform, please see our 15 [Intro to Terraform](/intro/index.html) and [Getting 16 Started](/intro/getting-started/install.html) guides. 17 18 A provider in Terraform is responsible for the lifecycle of a resource: 19 create, read, update, delete. An example of a provider is AWS, which 20 can manage resources of type `aws_instance`, `aws_eip`, `aws_elb`, etc. 21 22 The primary reasons to care about provider plugins are: 23 24 * You want to add a new resource type to an existing provider. 25 26 * You want to write a completely new provider for managing resource 27 types in a system not yet supported. 28 29 * You want to write a completely new provider for custom, internal 30 systems such as a private inventory management system. 31 32 If you're interested in provider development, then read on. The remainder 33 of this page will assume you're familiar with 34 [plugin basics](/docs/plugins/basics.html) and that you already have 35 a basic development environment setup. 36 37 ## Provider Plugin Codebases 38 39 Provider plugins live outside of the Terraform core codebase in their own 40 source code repositories. The official set of provider plugins released by 41 HashiCorp (developed by both HashiCorp staff and community contributors) 42 all live in repositories in 43 [the `terraform-providers` organization](https://github.com/terraform-providers) 44 on GitHub, but third-party plugins can be maintained in any source code 45 repository. 46 47 When developing a provider plugin, it is recommended to use a common `GOPATH` 48 that includes both the core Terraform repository and the repositories of any 49 providers being changed. This makes it easier to use a locally-built 50 `terraform` executable and a set of locally-built provider plugins together 51 without further configuration. 52 53 For example, to download both Terraform and the `template` provider into 54 `GOPATH`: 55 56 ``` 57 $ go get github.com/hashicorp/terraform 58 $ go get github.com/terraform-providers/terraform-provider-template 59 ``` 60 61 These two packages are both "main" packages that can be built into separate 62 executables with `go install`: 63 64 ``` 65 $ go install github.com/hashicorp/terraform 66 $ go install github.com/terraform-providers/terraform-provider-template 67 ``` 68 69 After running the above commands, both Terraform core and the `template` 70 provider will both be installed in the current `GOPATH` and `$GOPATH/bin` 71 will contain both `terraform` and `terraform-provider-template` executables. 72 This `terraform` executable will find and use the `template` provider plugin 73 alongside it in the `bin` directory in preference to downloading and installing 74 an official release. 75 76 When constructing a new provider from scratch, it's recommended to follow 77 a similar repository structure as for the existing providers, with the main 78 package in the repository root and a library package in a subdirectory named 79 after the provider. For more information, see 80 [the custom providers guide](/guides/writing-custom-terraform-providers.html). 81 82 When making changes only to files within the provider repository, it is _not_ 83 necessary to re-build the main Terraform executable. Note that some packages 84 from the Terraform repository are used as library dependencies by providers, 85 such as `github.com/hashicorp/terraform/helper/schema`; it is recommended to 86 use `govendor` to create a local vendor copy of the relevant packages in the 87 provider repository, as can be seen in the repositories within the 88 `terraform-providers` GitHub organization. 89 90 ## Low-Level Interface 91 92 The interface you must implement for providers is 93 [ResourceProvider](https://github.com/hashicorp/terraform/blob/master/terraform/resource_provider.go). 94 95 This interface is extremely low level, however, and we don't recommend 96 you implement it directly. Implementing the interface directly is error 97 prone, complicated, and difficult. 98 99 Instead, we've developed some higher level libraries to help you out 100 with developing providers. These are the same libraries we use in our 101 own core providers. 102 103 ## helper/schema 104 105 The `helper/schema` library is a framework we've built to make creating 106 providers extremely easy. This is the same library we use to build most 107 of the core providers. 108 109 To give you an idea of how productive you can become with this framework: 110 we implemented the Google Cloud provider in about 6 hours of coding work. 111 This isn't a simple provider, and we did have knowledge of 112 the framework beforehand, but it goes to show how expressive the framework 113 can be. 114 115 The GoDoc for `helper/schema` can be 116 [found here](https://godoc.org/github.com/hashicorp/terraform/helper/schema). 117 This is API-level documentation but will be extremely important 118 for you going forward. 119 120 ## Provider 121 122 The first thing to do in your plugin is to create the 123 [schema.Provider](https://godoc.org/github.com/hashicorp/terraform/helper/schema#Provider) structure. 124 This structure implements the `ResourceProvider` interface. We 125 recommend creating this structure in a function to make testing easier 126 later. Example: 127 128 ```golang 129 func Provider() *schema.Provider { 130 return &schema.Provider{ 131 ... 132 } 133 } 134 ``` 135 136 Within the `schema.Provider`, you should initialize all the fields. They 137 are documented within the godoc, but a brief overview is here as well: 138 139 * `Schema` - This is the configuration schema for the provider itself. 140 You should define any API keys, etc. here. Schemas are covered below. 141 142 * `ResourcesMap` - The map of resources that this provider supports. 143 All keys are resource names and the values are the 144 [schema.Resource](https://godoc.org/github.com/hashicorp/terraform/helper/schema#Resource) structures implementing this resource. 145 146 * `ConfigureFunc` - This function callback is used to configure the 147 provider. This function should do things such as initialize any API 148 clients, validate API keys, etc. The `interface{}` return value of 149 this function is the `meta` parameter that will be passed into all 150 resource [CRUD](https://en.wikipedia.org/wiki/Create,_read,_update_and_delete) 151 functions. In general, the returned value is a configuration structure 152 or a client. 153 154 As part of the unit tests, you should call `InternalValidate`. This is used 155 to verify the structure of the provider and all of the resources, and reports 156 an error if it is invalid. An example test is shown below: 157 158 ```golang 159 func TestProvider(t *testing.T) { 160 if err := Provider().(*schema.Provider).InternalValidate(); err != nil { 161 t.Fatalf("err: %s", err) 162 } 163 } 164 ``` 165 166 Having this unit test will catch a lot of beginner mistakes as you build 167 your provider. 168 169 ## Resources 170 171 Next, you'll want to create the resources that the provider can manage. 172 These resources are put into the `ResourcesMap` field of the provider 173 structure. Again, we recommend creating functions to instantiate these. 174 An example is shown below. 175 176 ```golang 177 func resourceComputeAddress() *schema.Resource { 178 return &schema.Resource { 179 ... 180 } 181 } 182 ``` 183 184 Resources are described using the 185 [schema.Resource](https://godoc.org/github.com/hashicorp/terraform/helper/schema#Resource) 186 structure. This structure has the following fields: 187 188 * `Schema` - The configuration schema for this resource. Schemas are 189 covered in more detail below. 190 191 * `Create`, `Read`, `Update`, and `Delete` - These are the callback 192 functions that implement CRUD operations for the resource. The only 193 optional field is `Update`. If your resource doesn't support update, then 194 you may keep that field nil. 195 196 * `Importer` - If this is non-nil, then this resource is 197 [importable](/docs/import/importability.html). It is recommended to 198 implement this. 199 200 The CRUD operations in more detail, along with their contracts: 201 202 * `Create` - This is called to create a new instance of the resource. 203 Terraform guarantees that an existing ID is not set on the resource 204 data. That is, you're working with a new resource. Therefore, you are 205 responsible for calling `SetId` on your `schema.ResourceData` using a 206 value suitable for your resource. This ensures whatever resource 207 state you set on `schema.ResourceData` will be persisted in local state. 208 If you neglect to `SetId`, no resource state will be persisted. 209 210 * `Read` - This is called to resync the local state with the remote state. 211 Terraform guarantees that an existing ID will be set. This ID should be 212 used to look up the resource. Any remote data should be updated into 213 the local data. **No changes to the remote resource are to be made.** 214 If the resource is no longer present, calling `SetId` 215 with an empty string will signal its removal. 216 217 * `Update` - This is called to update properties of an existing resource. 218 Terraform guarantees that an existing ID will be set. Additionally, 219 the only changed attributes are guaranteed to be those that support 220 update, as specified by the schema. Be careful to read about partial 221 states below. 222 223 * `Delete` - This is called to delete the resource. Terraform guarantees 224 an existing ID will be set. 225 226 * `Exists` - This is called to verify a resource still exists. It is 227 called prior to `Read`, and lowers the burden of `Read` to be able 228 to assume the resource exists. `false` should be returned if 229 the resources is no longer present, which has the same effect 230 as calling `SetId("")` from `Read` (i.e. removal of the resource data 231 from state). 232 233 ## Schemas 234 235 Both providers and resources require a schema to be specified. The schema 236 is used to define the structure of the configuration, the types, etc. It is 237 very important to get correct. 238 239 In both provider and resource, the schema is a `map[string]*schema.Schema`. 240 The key of this map is the configuration key, and the value is a schema for 241 the value of that key. 242 243 Schemas are incredibly powerful, so this documentation page won't attempt 244 to cover the full power of them. Instead, the API docs should be referenced 245 which cover all available settings. 246 247 We recommend viewing schemas of existing or similar providers to learn 248 best practices. A good starting place is the 249 [core Terraform providers](https://github.com/terraform-providers). 250 251 ## Resource Data 252 253 The parameter to provider configuration as well as all the CRUD operations 254 on a resource is a 255 [schema.ResourceData](https://godoc.org/github.com/hashicorp/terraform/helper/schema#ResourceData). 256 This structure is used to query configurations as well as to set information 257 about the resource such as its ID, connection information, and computed 258 attributes. 259 260 The API documentation covers ResourceData well, as well as the core providers 261 in Terraform. 262 263 **Partial state** deserves a special mention. Occasionally in Terraform, create or 264 update operations are not atomic; they can fail halfway through. As an example, 265 when creating an AWS security group, creating the group may succeed, 266 but creating all the initial rules may fail. In this case, it is incredibly 267 important that Terraform record the correct _partial state_ so that a 268 subsequent `terraform apply` fixes this resource. 269 270 Most of the time, partial state is not required. When it is, it must be 271 specifically enabled. An example is shown below: 272 273 ```golang 274 func resourceUpdate(d *schema.ResourceData, meta interface{}) error { 275 // Enable partial state mode 276 d.Partial(true) 277 278 if d.HasChange("tags") { 279 // If an error occurs, return with an error, 280 // we didn't finish updating 281 if err := updateTags(d, meta); err != nil { 282 return err 283 } 284 285 d.SetPartial("tags") 286 } 287 288 if d.HasChange("name") { 289 if err := updateName(d, meta); err != nil { 290 return err 291 } 292 293 d.SetPartial("name") 294 } 295 296 // We succeeded, disable partial mode 297 d.Partial(false) 298 299 return nil 300 } 301 ``` 302 303 In the example above, it is possible that setting the `tags` succeeds, 304 but setting the `name` fails. In this scenario, we want to make sure 305 that only the state of the `tags` is updated. To do this the 306 `Partial` and `SetPartial` functions are used. 307 308 `Partial` toggles partial-state mode. When disabled, all changes are merged 309 into the state upon result of the operation. When enabled, only changes 310 enabled with `SetPartial` are merged in. 311 312 `SetPartial` tells Terraform what state changes to adopt upon completion 313 of an operation. You should call `SetPartial` with every key that is safe 314 to merge into the state. The parameter to `SetPartial` is a prefix, so 315 if you have a nested structure and want to accept the whole thing, 316 you can just specify the prefix.