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. UI单据按钮点击事件校验

    一.按钮点击前事务处理<BeforeEventProcess> public override void BeforeEventProcess(IPart part, string eve ...

  2. Calico网络模型

    由于两台物理机的容器网段不同,我们完全可以将两台物理机配置成为路由器,并按照容器的网段配置路由表. 在物理机A中,我们可以这样配置:要想访问网段172.17.9.0/24,下一跳是192.168.10 ...

  3. C# 简单的定时器使用

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  4. MSP---企业上云需要考虑的问题

    一.评估 1.应用是否可以上云: 2.时间:规划时间,迁移时间 2.成本:人力成本,资源成本 二.上云 1.如何上云:选择云厂商,选择MSP 2.云选择:公有云,私有云,混合云,多云(不要最贵的,也不 ...

  5. ssm(spring+springmvc+mybatis)整合之环境配置

    1-1.导包 导入SpringMVC.Spring.MyBatis.mybatis-spring.mysql.druid.json.上传和下载.验证的包 1-2.创建并配置web.xml文件 配置sp ...

  6. Bean named 'XXX' is expected to be of type [XXX] but was actually of type [com.sun.proxy.$Proxy7

    AOP原理 <aop:aspectj-autoproxy />声明自动为spring容器中那些配置@aspectJ切面的bean创建代理,织入切面. <aop:aspectj-aut ...

  7. Vue.js详解

    vuejs介绍 Vue.js是当下很火的一个JavaScript MVVM库,它是以数据驱动和组件化的思想构建的.相比于Angular.js,Vue.js提供了更加简洁.更易于理解的API,使得我们能 ...

  8. 英语foteball足球foteball单词

    现代足球起源地是在英格兰.传说在11世纪,英格兰与丹麦之间有过一场战争,战争结束后,英格兰人在清理战争废墟时发现一个丹麦入侵者的头骨,出于愤恨,他们便用脚去踢这个头骨,一群小孩见了便也来踢,不过他们发 ...

  9. 一道经典面试题,atoi函数的实现

    参考资料 (1)atoi函数的实现 (2)<剑指offer> 题目分析 本题需要注意的有几个方面: (1)检查输入参数,指针是否为NULL: (2)去除字符串前面的空格 (3)处理正负符号 ...

  10. SQL Server 用一张表的数据更新另一张表的数据(转载)

    文章一:SQL Server中如何基于一个表的数据更新另一个表的对应数据的SQL语句脚本 https://codedefault.com/2017/sql-server-update-from-a-s ...