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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2017-03-25 luarocks 安装
2017-03-25 几个中文排版web 类库
2017-03-25 几个开源ssg 技术方案
2017-03-25 saas 系统租户自助网站
2017-03-25 安装 Ruby, Rails 运行环境