Write your own Terraform provider: Part 1
转自:https://container-solutions.com/write-terraform-provider-part-1/
This is the first part of a series of blog posts that explain how to write Terraform providers.
Before we start I would like to state that this article asumes a couple of things from you:
- You have (some) experience with Terraform, the different provisioners and providers that come out of the box,
its configuration files, tfstate files, etc. - You are comfortable with the Go language and its code organization.
Because bootstrapping a Terraform provider can take some effort feel free to clone this Github repository to use it as your Terraform provider/plugin skeleton. It’ll also help you go along with all the steps that we will mention later on.
Let’s say that you want to write a Terraform provider for your awesome (cloud) provider. In practice, your Terraform configuration file would look like this:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
provider"awesome"{
api_key ="securetoken=="
endpoint ="https://api.example.org/v1"
timeout =60
max_retries=5
}
resource"awesome_machine""speedy-server"{
name="speedracer"
cpus=4
ram =16384
}
|
So, your provider called awesome supports four different fields:
api_keyendpointtimeoutmax_retries
You also want to have your own resource called machine (notice here that because of the way Terraform works your resource name is prefixed with the name of your provider, hence awesome_machine and not just machine) which supports the following fields:
namecpusram
Where to start?
Start by calling plugin.Serve, passing along a “provider” function that returns a terraform.ResourceProvider.
|
1
2
3
4
5
6
7
|
func main(){
opts:=plugin.ServeOpts{
ProviderFunc:Provider,// Read on to find the definition of this "Provider" function.
}
plugin.Serve(&opts)
}
|
Then define a function that returns an object that implements the terraform.ResourceProvider interface, specifically a schema.Provider:
|
1
2
3
4
5
6
7
8
|
func Provider()terraform.ResourceProvider{
return&schema.Provider{
Schema: map[string]*schema.Schema{...},
ResourcesMap: map[string]*schema.Resource,
ConfigureFunc:func(*schema.ResourceData)(interface{},error){...},
}
}
|
This schema.Provider struct has three fields:
Schema: List of all the fields for your provider to work. Things like access tokens, log levels, endpoints, region, etc.
The value of this field is amap[string]*schema.Schema, or in Spanish: a linked list where the key is astringand the value is a pointer to aschema.Schema.
A minimalistic example schema would look like this:12345678map[string]*schema.Schema{"api_key":&schema.Schema{Type: schema.TypeString,Required: true,Description:"Some short description here."}}Here we are saying that api_key is our configuration field in our configuration file; we are also specifying its type (
schema.TypeStringand not juststringas this is required for Terraform to perform some validations when parsing the configuration file); we are also saying that is a required field: if the user does not specify a value for this field in the configuration file Terraform will throw an error and stop execution. Finally we add a short description to the field. There are more configuration options that can be specified for a schema field. You can see the complete list of fields of this struct here.ResourcesMap: List of resources that you want to support in your Terraform configuration file. For example, if we were writing a Terraform provider for AWS and we wanted to support S3 buckets, Elastic Balancers and EC2 instances this is the place where you want to declare those resources.
The value for this field is amap[string]*schema.Resource, similar to the one of theSchemafield, the difference being that this list points toschema.Resource. Let’s take a look at one of the resources from the skeleton:12345678910111213141516map[string]*schema.Resource{"awesome_machine":&schema.Resource{Schema:map[string]*schema.Schema{"name":&schema.Schema{Type: schema.TypeString,Required:true,},},SchemaVersion:1,Create: func(d *schema.ResourceData,metainterface{}){},Read: func(d *schema.ResourceData,metainterface{}){},Update: func(d *schema.ResourceData,metainterface{}){},Delete: func(d *schema.ResourceData,metainterface{}){},},}What we are doing here is until now pretty straight forward: we are declaring a list of resources. Each resource declaration has its own structure which is made out of a
schema.Schema(we saw this already in the previous example when configuring theschema.Provider) and you probably also noticed that there are also a couple more fields like theSchemaVersionbut I want to draw your attention specially towards theCreate,Read,Update&Deleteones. These are the four operations that Terraform will perform over the resources of your infrastructure and they will be called according to the case for each resource. This means that if you are creating four resources theCreatefunction will be called four times. The same applies for the rest of the cases.
The signature for these functions isfunc(*ResourceData, interface{}).
TheResourceDatatype will provide you with some goodies for getting the values from the configuration file:Get(key string): fetches the value for the given key. If the given key is not defined in the structure it will returnnil. If the key has not been set in the configuration file then it will return the key’s type’s default value (0 for integers, “” for strings and so on).GetChange(key string): Returns the old and new value for the given key.HasChange(key string): Returns whether or not the given key has been changed.SetId(): Sets the id for the given resource. If set to blank then the resource will be marked for deletion.
It also offers a couple more methods (
GetOk,Set,ConnInfo,SetPartial) that we won’t cover on this post.The second argument passed to the CRUD functions will be the value returned by your
ConfigureFuncof yourschema.Provider.
Following our example,metain this case can be safely casted to ourExampleClientlike this:12client:=meta.(*ExampleClient)Let’s now take a look at the
createFuncsource:123456789101112131415161718func createFunc(d *schema.ResourceData,metainterface{})error{client:=meta.(*ExampleClient)machine:=Machine{Name:d.Get("name").(string),CPUs:d.Get("cpus").(int),RAM: d.Get("ram").(int),}err:=client.CreateMachine(&machine)iferr!=nil{returnerr}d.SetId(machine.Id())returnnil}As mentioned before we know that
metais indeed a pointer to ourExampleClientso we cast it. The client offers aCreateMachinemethod which receives a pointer to aMachineobject, so we initialize that object populating its fields with the values that the user put in the configuration file using theGetmethod of theResourceDatathat has been passed to our function. Then we perform theclient.CreateMachinecall, passing along themachinethat we declared before. After that we check for errors and make an early return in case that something went wrong with the creation of the machine. Finally, if everything went fine we will make a call toSetId. This not only sets the resource ID in the tfstate file but also tells Terraform that the resource was successfully created.
For updating resources leverage theHasChangeandGetChangefunctions. I will leave the implementation to your imagination and awesome software development capabilities.
It is important also to mention that if at any point you set your resource id to blank Terraform will understand that the resource no longer exists. This is convenient, for example, when you want to synchronize your remote state with your local state (when a resource has been removed remotely). This is a common task for thereadFuncfunction.ConfigureFunc: Make use of this function when you need to initialize some client with the credentials defined in theSchemapart. You can find its signature here.
Any other example?
Check the skeleton project. I recommend you use it for when you’re starting fresh with a new Terraform provider. Another good place to look for examples of complex use cases is the builtin providers that come along with Terraform.
Unit tests
When it comes to unit testing I suggest that you leave your Terraform provider as lightweight as possible. In the cases that we have worked on here at Container Solutions we have all the business logic in the client libraries (check for instance this Cobbler clientlibrary that we wrote) and has so far worked charms for us. Perhaps your use case is different. Perhaps not. Drop me a line either in the comments sections or on Twitter (@mongrelion). I would love to hear from you regarding this specific matter.
Final notes
This is not a full-grown Terraform provider. Far from it. But it will help you get started. Most of the documentation is in Terraform’s source code which can be tricky at first to browse around. This is a small effort to gather some of the basic concepts to reduce the barrier and help other developers get started as quick as possible. And again, as stated in the beginning of this article, this is only the first part of a series of upcoming blog posts that will talk more about Terraform providers.
Some of the things that we want to talk about in the future are:
- Partial state (or how to recover from faulty resource modification)
- More complex schema definitions
- How to run callbacks once all your resources have been created/updated/deleted
And possibly much more. Leave a comment if there is anything else that you would like us to cover on these series and thanks for reading!
Write your own Terraform provider: Part 1的更多相关文章
- 京东云携手HashiCorp,宣布推出Terraform Provider
2019年4月23日消息,京东云携手云基础设施自动化软件的领导者HashiCorp,宣布推出Terraform Provider for JD Cloud,这意味着用户能够在京东云上轻松使用简单模板语 ...
- terraform plugin 版本以及changlog 规范
文章来自官方文章,转自:https://www.terraform.io/docs/extend/best-practices/versioning.html 里面包含了版本命名的规范,以及chang ...
- Developer Friendly | 基础设施即代码的事实标准Terraform已支持京东云!
Developer Friendly | 基础设施即代码的事实标准Terraform已支持京东云! Chef.Puppet.Ansible.SaltStack 都可以称为配置管理工具,这些工具的主要目 ...
- 网易云terraform实践
此文已由作者王慎为授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 一.terraform介绍 随着应用上云的常态化,资源栈动态管理的需求对用户也变得更加急切.资源编排(Res ...
- 零基础教程!一文教你使用Rancher 2.3和Terraform运行Windows容器
本文来自Rancher Labs 介 绍 在Kubernetes 1.14版本中已经GA了对Windows的支持.这一结果凝结了一群优秀的工程师的努力,他们来自微软.Pivotal.VMware.红帽 ...
- 干货 | 运维福音——Terraform自动化管理京东云
干货 | 运维福音--Terraform自动化管理京东云 原创: 张宏伟 京东云开发者社区 昨天 Terraform是一个高度可扩展的IT基础架构自动化编排工具,主张基础设施即代码,可通过代码集中管 ...
- Writing Custom Providers
转自:https://www.terraform.io/docs/extend/writing-custom-providers.html 很详细,做为一个记录 In Terraform, a Pro ...
- 云原生之旅 - 6)不能错过的一款 Kubernetes 应用编排管理神器 Kustomize
前言 相信经过前一篇文章的学习,大家已经对Helm有所了解,本篇文章介绍另一款工具 Kustomize,为什么Helm如此流行,还会出现 Kustomize?而且 Kustomize 自 kubect ...
- Terraform 自定义provider 开发
内容来自官方文档,主要是进行学习自定义provider 开发的流程 开发说明 我们需要开发的有provider 以及resource 对于resource 我们需要进行crud 的处理,同时还需要进行 ...
随机推荐
- IO多路复用,select、poll、epoll 编程主要步骤
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- Android知识补充(Android学习笔记)
Android知识补充 ●国际化 所谓的国际化,就是指软件在开发时就应该具备支持多种语言和地区的功能,也就是说开发的软件能同时应对不同国家和地区的用户访问,并针对不同国家和地区的用户,提供相应的.符合 ...
- (C/C++学习笔记) 一. 基础知识
一. 基础知识 ● 程序和C/C++ 程序: 根据Wirth (1976), Algorithms + Data Structures = Programs. Whence C: 1972, Denn ...
- VSTO杂项拾零(持续更新中……)
环境:win 7+visual basic 2008 侧重:VSTO 界面:sheetbook工作簿 1.创建一个过程并调用(2017.6.3) Public Class Sheet1 ...
- Cracking The Coding Interview 4.1
//Implement a function to check if a tree is balanced. For the purposes of this question, a balanced ...
- SQL-17 获取当前(to_date='9999-01-01')薪水第二多的员工的emp_no以及其对应的薪水salary
题目描述 获取当前(to_date='9999-01-01')薪水第二多的员工的emp_no以及其对应的薪水salaryCREATE TABLE `salaries` (`emp_no` int(11 ...
- C++11智能指针 share_ptr,unique_ptr,weak_ptr用法
0x01 智能指针简介 所谓智能指针(smart pointer)就是智能/自动化的管理指针所指向的动态资源的释放.它是存储指向动态分配(堆)对象指针的类,用于生存期控制,能够确保自动正确的销毁动 ...
- Array.apply(null, {length: 20})和Array(20)的理解
话说今晚在学习Vue.js教程里:Render函数,这一章节是发现了一个问题,就是利用下面的这个render函数可以渲染20个重复的段落: render: function (createElemen ...
- Beta阶段冲刺---Day5
一.Daily Scrum Meeting照片 二.今天冲刺情况反馈 昨天已完成的工作: (1)闯关模式界面设计: (2)主界面做了相应修改: (3)RankActivity修改. (4)RANKli ...
- Day10作业及默写
1,继续整理函数相关知识点,写博客. 2,写函数,接收n个数字,求这些参数数字的和.(动态传参) def func(*number): sum=0 for num in number: sum+=nu ...