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. pytest_assert断言

    前言 断言是写自动化测试基本最重要的一步,一个用例没有断言,就失去了自动化测试的意义了.什么是断言呢? 简单来讲就是实际结果和期望结果去对比,符合预期那就测试pass,不符合预期那就测试 failed ...

  2. SQL注入获取Sa账号密码

    漏洞位置:http://168.1.1.81/Information/Search?Keyword=1111 漏洞利用: MSSQL 2000 http://168.1.1.81/Informatio ...

  3. 异或序列 [set优化DP]

    也许更好的阅读体验 \(\mathcal{Description}\) 有一个长度为 \(n\)的自然数序列 \(a\),要求将这个序列分成至少 \(m\) 个连续子段 每个子段的价值为该子段的所有数 ...

  4. C# 快捷键(总结)

    C# 展开和折叠代码的快捷键 VS2005代码编辑器的展开和折叠代码确实很方便和实用.以下是展开代码和折叠代码所用到的快捷键,很常用: Ctrl + M + O: 折叠所有方法 Ctrl + M +  ...

  5. Oracle解决锁表语句与批量生成解锁语句

    --以下几个为相关表SELECT * FROM v$lock;SELECT * FROM v$sqlarea;SELECT * FROM v$session;SELECT * FROM v$proce ...

  6. Mybatis中使用collection进行多对多双向关联示例(含XML版与注解版)

    Mybatis中使用collection进行多对多双向关联示例(含XML版与注解版) XML版本: 实体类: @Data @NoArgsConstructor public class Course ...

  7. spring boot 规范json返回值

    spring boot 规范json返回值 spring boot 接口返回配置 @ResponseBody ,则返回自定义的对象,解析成json. 但是,部分字段能友好的展示出来.如 Date,Lo ...

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

    在C#编程过程中,将字符串string转换为decimal类型过程中,时常使用decimal.Parse方法,但decimal.Parse在无法转换的时候,会抛出程序异常,其实还有个decimal.T ...

  9. WC_Project

    个人项目:WC_Project 一.GitHub项目地址 GitHub项目地址:https://github.com/ting9500/WC_GNIT.git 二.PSP表格 PSP2.1 Perso ...

  10. Linux负载模拟

    转载:https://blog.csdn.net/F8qG7f9YD02Pe/article/details/79063392 CPU 下面命令会创建 CPU 负荷,方法是通过压缩随机数据并将结果发送 ...