terraform v0.12 发布了,有好多新功能的添加,包括语法的增强,新函数的引入,更好的开发提示
只是当前对于一些老版本的provider 暂时还不兼容,但是大部分官方的provider 都是可以使用的

这片文章只是一个简单的demo,使用sqler 提供rest api,集合tf 0.12 的新功能,使用模版函数以及 json 解码函数
实现一个部署资源的动态生成

环境准备

tf 使用本地软件,sqler 以及数据库使用docker-compose 运行

  • 软件下载
 
tf 下载 https://www.terraform.io/downloads.html
  • 项目结构
├── main.tf
├── restapi
│ ├── config
│ │ └── config-example.hcl
│ ├── docker-compose.yaml
│ └── user-app.sql
├── terraform.tfstate
├── tf-result
└── user_apps.tmpl
 
 
  • sqler 环境
    docker-compose 文件
 
version: "3"
services:
  sqler:
    image: dalongrong/sqler:2.0
    volumes:
    - "./config/config-example.hcl:/app/config.example.hcl"
    environment:
    - "DRIVER=postgres" 
    - "DSN=postgresql://postgres:dalong@postgres:5432/postgres?sslmode=disable"
    ports:
    - "3678:3678"
    - "8025:8025"
  postgres:
    image: postgres:9.6.11
    ports:
    - "5432:5432"
    environment:
    - "POSTGRES_PASSWORD:dalong"
 

代码说明

  • sqler 数据库查询dsl

    config/config-example.hcl

apps {
    exec = <<SQL
    select * from apps
    SQL
}
users {
    exec = <<SQL
     select * from users;
    SQL
}
user_apps {
    aggregate = ["users","apps"]
}
  • pg sql

    几个测试的数据表

CREATE TABLE apps (
    id SERIAL PRIMARY KEY,
    appname text,
    appversion text,
    content text
);
CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    username text,
    age integer,
    content text
);
INSERT INTO "public"."apps"("appname", "appversion", "content") VALUES('login', 'v1.0', 'login') RETURNING "id", "appname", "appversion", "content";
INSERT INTO "public"."apps"("appname", "appversion") VALUES('logo', 'v2.0') RETURNING "id", "appname", "appversion", "content";
INSERT INTO "public"."users"("username", "age", "content") VALUES('dalong', 22, 'demo') RETURNING "id", "username", "age", "content";
INSERT INTO "public"."users"("username", "age") VALUES('rong', 29) RETURNING "id", "username", "age", "content";
 
  • tf main.tf

    主要是集成模版函数以及http provider 还有jsondecode 函数

main.tf

variable filename {
  type = string
  default = "tf-result/main.tf"
}
data "http" "user_apps" {
  url = "http://localhost:8025/user_apps"
}
resource "local_file" "main_tf" {
    content = templatefile("user_apps.tmpl", jsondecode(data.http.user_apps.body))
    filename = var.filename
}

user_apps.tmpl

%{ for item in data.apps ~}
resource "users" "${item.appname}" {
    appname = "${item.appname}"
    version = "${item.appversion}"
    content = "${item.content != null ? item.content : ""}"
}
%{ endfor ~}
%{ for item in data.users ~}
resource "apps" "${item.username}" {
    username = "${item.username}"
    age = "${item.age}"
    content = "${item.content != null ? item.content : ""}"
}
%{ endfor ~}
 

运行&&测试

  • 启动sqler
cd restapi && docker-compose up -d
  • 运行tf

init :

下载依赖的provider

terraform init 
 

plan:

查询tf 的任务

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.
data.http.user_apps: Refreshing state...
------------------------------------------------------------------------
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:
  # local_file.main_tf will be created
  + resource "local_file" "main_tf" {
      + content = "resource \"users\" \"login\" {\n appname = \"login\"\n version = \"v1.0\"\n content = \"login\"\n}\nresource \"users\" \"logo\" {\n appname = \"logo\"\n version = \"v2.0\"\n content = \"\"\n}\n\nresource \"apps\" \"dalong\" {\n username = \"dalong\"\n age = \"22\"\n content = \"demo\"\n}\nresource \"apps\" \"rong\" {\n username = \"rong\"\n age = \"29\"\n content = \"\"\n}\n"
      + filename = "tf-result/main.tf"
      + id = (known after apply)
    }
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:

data.http.user_apps: Refreshing state...
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:
  # local_file.main_tf will be created
  + resource "local_file" "main_tf" {
      + content = "resource \"users\" \"login\" {\n appname = \"login\"\n version = \"v1.0\"\n content = \"login\"\n}\nresource \"users\" \
"logo\" {\n appname = \"logo\"\n version = \"v2.0\"\n content = \"\"\n}\n\nresource \"apps\" \"dalong\" {\n username = \"dalong\"\n ag
e = \"22\"\n content = \"demo\"\n}\nresource \"apps\" \"rong\" {\n username = \"rong\"\n age = \"29\"\n content = \"\"\n}\n"
      + filename = "tf-result/main.tf"
      + id = (known after apply)
    }
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
local_file.main_tf: Creating...
local_file.main_tf: Creation complete after 0s [id=ee0a2ad2f12e1e21723c46248af40d5cc53e46cd]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
 
  • 生成的模版内容
resource "users" "login" {
    appname = "login"
    version = "v1.0"
    content = "login"
}
resource "users" "logo" {
    appname = "logo"
    version = "v2.0"
    content = ""
}
resource "apps" "dalong" {
    username = "dalong"
    age = "22"
    content = "demo"
}
resource "apps" "rong" {
    username = "rong"
    age = "29"
    content = ""
}
 

说明

以上只是一个简单的集成,实际上我们可以集合数据库的管理,做为配置管理,同时集成安全认证,使用slqer 快速提供数据接口
使用tf 进行资源部署,同时可以使用tf 的状态管理,实现技术设施资源的快速部署以及管理,还是很不错的

参考资料

https://github.com/rongfengliang/sqler-docker-compose
https://www.terraform.io/docs/cli-index.html
https://www.terraform.io/docs/configuration/functions/templatefile.html
https://www.terraform.io/docs/providers/http/index.html
https://github.com/rongfengliang/sqler-terraform-resource-demo

sqler 集成 terraform v0.12 生成资源部署文件的更多相关文章

  1. 使用terraform v0.12 生成gitlab repo 创建部署tf 文件

      以前写过一个使用模版引擎+ rest 接口的模式,生成tf 文件,v0.12 直接提供了方便的json 处理函数 我们可以直接结合http 以及templatefile providers 方便的 ...

  2. terraform v0.12.0 发布了

    v0.12.0 相比以前的有好多新的特性,包括语法,以及函数增强,昨天还在折腾的一个json解码的问题,直接使用 v0.12.0 就可以解决了,同时也包含了for 操作处理同时官方文档对于v0.12. ...

  3. 最简单的 K8S 部署文件编写姿势,没有之一!

    1. 头疼编写K8S部署文件? K8S yaml 参数很多,需要边写边查? 保留回滚版本数怎么设? 如何探测启动成功,如何探活? 如何分配和限制资源? 如何设置时区?否则打印日志是GMT标准时间 如何 ...

  4. 【ZZ】谈谈持续集成,持续交付,持续部署之间的区别

    谈谈持续集成,持续交付,持续部署之间的区别 http://blog.flow.ci/cicd_difference/ 谈谈持续集成,持续交付,持续部署之间的区别 2016年08月03日 标签:beta ...

  5. 浅谈<持续集成、持续交付、持续部署>(一)

    谈谈持续集成,持续交付,持续部署之间的区别 经常会听到持续集成,持续交付,持续部署,三者究竟是什么,有何联系和区别呢?   假如把开发工作流程分为以下几个阶段: 编码 -> 构建 -> 集 ...

  6. Jenkins学习总结(4)——持续集成,持续交付,持续部署之间的区别

    经常会听到持续集成,持续交付,持续部署,三者究竟是什么,有何联系和区别呢? 假如把开发工作流程分为以下几个阶段: 编码 -> 构建 -> 集成 -> 测试 -> 交付 -> ...

  7. 用持续集成工具Travis进行构建和部署

    用持续集成工具Travis进行构建和部署 用持续集成工具Travis进行构建和部署 摘要:本文简单说明了如何使用持续集成工具Travis进行构建和部署的过程. 1. 概述 持续集成(Continuou ...

  8. .NET基础 (03)生成、部署和管理

    生成.部署和管理1 如何生成强签名的程序集2 如何把程序集放入GAC中3 延迟签名及其作用4 程序集的版本分哪几部分 1 如何生成强签名的程序集在生成程序集时,CLR提供了两种可选类型:强签名程序集. ...

  9. API生命周期第三阶段:API实施:使用swagger codegen生成可部署工程,择取一个作为mock service

    在分享培训了swagger对于API的设计之后,有一些人问我说:你看,现在咱们前端使用web_API做为mock data在进行测试,后端也有mock 测试.然后我们再进行联调,这之中肯定会出现一些偏 ...

随机推荐

  1. 【Linux】Ubuntu替换阿里源

    --------------------------------------------------------- 参考文章:https://www.jianshu.com/p/97c35d569aa ...

  2. linux上文件的上传和下载

    现整理一篇linux上文件的上传和下载 第一种方式就是在windos上安装工具 如: 工具如何使用我就不赘述了,easy 第二种方式就是使用liux的命令(首先是文件上传) 上传文件(首先创建文件夹如 ...

  3. linux 释放系统内存命令

    1.sync 因为系统在操作的过程当中,会把你的操作到的文件资料先保存到buffer中去,因为怕你在操作的过程中因为断电等原因遗失数据,所以在你操作过程中会把文件资料先缓存.所以我们执行sync命令, ...

  4. Java子类方法签名相同,返回类型不同

    2019年7月27日15:04:20 Java子类覆盖父类的方法,方法名字相同,参数列表相同,返回类型不同的情况: 如果子类方法返回类型是父类方法返回类型的子类,这是没问题的,否则报错. 在JAVA ...

  5. 未能加载文件或程序集 Microsoft.ReportViewer.ProcessingObjectModel, Version=10.0.0.0

    写在前面 整理错误集.某一天在启动项目的时候,出现了未能加载文件或程序集 Microsoft.ReportViewer.ProcessingObjectModel, Version=10.0.0.0错 ...

  6. 最全的 pip 使用指南,50% 你可能没用过

    所有的 Python 开发者都清楚,Python 之所以如此受欢迎,能够在众多高级语言中,脱颖而出,除了语法简单,上手容易之外,更多还要归功于 Python 生态的完备,有数以万计的 Python 爱 ...

  7. pandas-05 map和replace操作

    # pandas-05 map和replace操作 map可以做一个映射,对于操作大型的dataframe来说就非常方便了,而且也不容易出错.replace的作用是替换,这个很好理解. import ...

  8. 【转载】 C#中使用decimal.Parse方法将字符串转换为十进制decimal类型

    在C#编程过程中,很多时候涉及到数据类型的转换,例如将字符串类型的变量转换为十进制decimal类型就是一个常见的类型转换操作,decimal.Parse方法是C#中专门用来将字符串转换为decima ...

  9. js函数(续)

    一.全局变量和局部变量全局变量:当前js页面中均可使用的变量[声明在函数外面的变量],整个js页面中均可以使用.局部变量:声明在函数内部的变量,只能在函数内部使用.eg: var a = 1; con ...

  10. 实战AudioToolbox--在iOS平台上播放音频

    上午看了关于AudioToolbox.framework相关的资料,结合网上的资料对AudioToolbox的基本使用有了整体上的认识,上一篇文章 笔谈AudioToolbox(一) 中提到使用Aud ...