Terraform 自定义provider 开发

内容来自官方文档,主要是进行学习自定义provider 开发的流程

开发说明

我们需要开发的有provider 以及resource 对于resource 我们需要进行crud 的处理,同时还需要进行状态的
处理

项目初始化

  • dep
    使用dep 进行包管理
dep init
  • provider
package main

import (
 "github.com/hashicorp/terraform/helper/schema"
)

func Provider() *schema.Provider {
        return &schema.Provider{
                ResourcesMap: map[string]*schema.Resource{
                        "example_server": resourceServer(),
                },
        }
}
  • resource_server.go
package main

import (
        "github.com/hashicorp/terraform/helper/schema"
)
// 简单状态处理
func resourceServerCreate(d *schema.ResourceData, m interface{}) error {
    address := d.Get("address").(string)
        d.SetId(address)
        return resourceServerRead(d, m)    
}

func resourceServerRead(d *schema.ResourceData, m interface{}) error {
        return nil
}

func resourceServerUpdate(d *schema.ResourceData, m interface{}) error {
        return resourceServerRead(d, m)
}

func resourceServerDelete(d *schema.ResourceData, m interface{}) error {
        return nil
}

// resource 的curd 操作
func resourceServer() *schema.Resource {
        return &schema.Resource{
                Create: resourceServerCreate,
                Read: resourceServerRead,
                Update: resourceServerUpdate,
                Delete: resourceServerDelete,

                Schema: map[string]*schema.Schema{
                        "address": &schema.Schema{
                                Type: schema.TypeString,
                                Required: true,
                        },
                },
        }
}
  • main.go
package main

import (
    "github.com/hashicorp/terraform/plugin"
    "github.com/hashicorp/terraform/terraform"
)

func main() {
    plugin.Serve(&plugin.ServeOpts{
        ProviderFunc: func() terraform.ResourceProvider {
            return Provider()
        },
    })
}

测试

  • 构建插件
go build -o terraform-provider-example
  • 添加tf 文件

    使用插件 main.tf

resource "example_server" "my-server" {

 address = "110.2.3.4"
}
  • init terraform
terraform init

效果

Initializing provider plugins...

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
  • 查看plan
terraform plan

效果

Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.


------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  + example_server.my-server
      id: <computed>
      address: "110.2.3.4"


Plan: 1 to add, 0 to change, 0 to destroy.

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.
  • apply
terraform apply

效果

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  + example_server.my-server
      id: <computed>
      address: "110.2.3.4"


Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

example_server.my-server: Creating...
  address: "" => "110.2.3.4"
example_server.my-server: Creation complete after 0s (ID: 110.2.3.4)

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
  • destroy
terraform destroy

效果

example_server.my-server: Refreshing state... (ID: 110.2.3.4)

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  - destroy

Terraform will perform the following actions:

  - example_server.my-server


Plan: 0 to add, 0 to change, 1 to destroy.

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

example_server.my-server: Destroying... (ID: 110.2.3.4)
example_server.my-server: Destruction complete after 0s

Destroy complete! Resources: 1 destroyed.

说明

这篇文章很简单,只是参考官方内容做了一个简单的demo,官方的那篇文章很不错,包含了很多内容
后边也有写一些简单的实践。

参考资料

https://github.com/rongfengliang/myterraform-plugin
https://www.terraform.io/docs/extend/writing-custom-providers.html

posted on 2019-03-25 11:28  荣锋亮  阅读(1716)  评论(0编辑  收藏  举报

导航