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
Terraform 自定义provider 开发的更多相关文章
- asp.net core mcroservices 架构之 分布式日志(二)之自定义日志开发
netcore日志原理 netcore的日志是作为一个扩展库存在的,每个组件都有它的入口,那么作为研究这个组件的入口是最好的,首先看两种方式: 这个是源码例子提供的. var loggingConfi ...
- Terraform插件Provider管理,搜索、定义、下载
我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 简介 最近工作中用到了Terraform,权当学习记录一下,希望能帮助到其它人. Terraform系列文章如下: Ter ...
- Java自定义注解开发
一.背景 最近在自己搞一个项目时,遇到可需要开发自定义注解的需求,对于没有怎么关注这些java新特性的来说,比较尴尬,索性就拿出一些时间,来进行研究下自定义注解开发的步骤以及使用方式.今天在这里记下, ...
- 【JSP】自定义标签开发入门
JSP 自定义标签 自定义标签是用户定义的JSP语言元素.当JSP页面包含一个自定义标签时将被转化为servlet,标签转化为对被 称为tag handler的对象的操作,即当servlet执行时We ...
- javaweb学习总结(二十三)——jsp自定义标签开发入门
一.自定义标签的作用 自定义标签主要用于移除Jsp页面中的java代码. 二.自定义标签开发和使用 2.1.自定义标签开发步骤 1.编写一个实现Tag接口的Java类(标签处理器类) 1 packag ...
- MS CRM 2011的自定义和开发(11)——插件(plugin)开发(三)
http://www.cnblogs.com/StoneGarden/archive/2012/02/06/2340661.html MS CRM 2011的自定义和开发(11)——插件(plugin ...
- MS CRM 2011的自定义和开发(11)——插件(plugin)开发(四)
http://www.cnblogs.com/StoneGarden/archive/2012/02/08/2343294.html MS CRM 2011的自定义和开发(11)——插件(plugin ...
- MS CRM 2011的自定义和开发(11)——插件(plugin)开发(一)
http://www.cnblogs.com/StoneGarden/archive/2012/02/02/2336147.html MS CRM 2011的自定义和开发(11)——插件(plugin ...
- MS CRM 2011的自定义和开发(11)——插件(plugin)开发(二)
http://www.cnblogs.com/StoneGarden/archive/2012/02/06/2339490.html MS CRM 2011的自定义和开发(11)——插件(plugin ...
随机推荐
- C++ STL string对象操作汇总
string对象 C语言只提供了一个char类型用来处理字符,而对于字符串,只能通过字符串数组来处理,显得十分不便.C++STL提供了string基本字符系列容器来处理字符串,可以把string理解为 ...
- [Linux]Linux下Apache服务器配置
Linux下Apache服务器配置 相关包: httpd-2.2.3-29.e15.i386.rpm //主程序包 httpd-devel-2.2.3-29.e15.i ...
- 十三. Python基础(13)--生成器进阶
十三. Python基础(13)--生成器进阶 1 ● send()方法 generator.send(value) Resumes the execution, and "sends&qu ...
- Cracking The Coding Interview 9.6
//原文: // // Given a matrix in which each row and each column is sorted, write a method to find an el ...
- 在MATLAB中安装MinGW-w64 C/C++ 编译器的方法
reference:http://blog.sina.com.cn/s/blog_167bbdec10102x113.html 在MATLAB中编译C/C++ 文件时出现以下情况: 说明缺少MinGW ...
- Install SharePoint 2013 with SP1 on Windows Server 2012 R2 error - This Product requires .NF 4.5
博客地址:http://blog.csdn.net/FoxDave 最近因为项目需要要搭建SharePoint 2013的开发环境. 准备了Windows Server 2012 R2系统和Sha ...
- day 54 jQuery 的初步基础
jQuery介绍 jQuery是一个轻量级的.兼容多浏览器的JavaScript库. jQuery使用户能够更方便地处理HTML Document.Events.实现动画效果.方便地进行Ajax交互, ...
- mysql主从复制-读写分离-原理
Mysql主从复制和读写分离 在实际的生产环境中,如果对mysql数据库的读和写都在一台数据库服务器中操作,无论是在安全性.高可用性,还是高并发等各个方面都是不能满足实际需求的.因此,一般通过主从复制 ...
- OneinStack 安装
安装步骤 注意 如果有单独数据盘,建议您先挂载数据盘,建议将网站内容.数据库放在数据盘中.如何挂载数据盘,请参考(支持阿里云.腾讯云):<如何利用脚本自动化挂载数据盘?> yum -y i ...
- 2--linux命令--查看磁盘空间
du命令用来查看目录或文件所占用磁盘空间的大小.常用选项组合为:du -sh 二.du常用的选项: -h:以人类可读的方式显示 -a:显示目录占用的磁盘空间大小,还要显示其下目录和文件占用磁盘空间的大 ...