Giter Site home page Giter Site logo

workshop-custom-terraform-provider's Introduction

Workshop: Custom Terraform Provider

2020 Ondrej Sika <[email protected]>
https://github.com/ondrejsika/workshop-custom-terraform-provider

Alias: sika.link/if20-terraform

Related Repositories

See API of our Demo Cloud

API Server - https://github.com/ondrejsika/demo-cloud-provider/blob/master/server.py

Run it locally:

docker run -p 8000:8000 ondrejsika/demo-cloud-provider

Demo cloud has two resources: Box and Text

You can create a box and put some texts into box:

You can try it by curl:

curl -X POST --data '{"name": "Hello Box"}' -H Content-Type:application/json http://127.0.0.1:8000/v1/box/
curl -X POST --data '{"text": "Hello"}' -H Content-Type:application/json http://127.0.0.1:8000/v1/box/1/text/
curl -X POST --data '{"text": "World"}' -H Content-Type:application/json http://127.0.0.1:8000/v1/box/1/text/

Check the http://127.0.0.1:8000/

See your objects? Let's update them:

curl -X PUT --data '{"text": "Ahoj"}' -H Content-Type:application/json http://127.0.0.1:8000/v1/box/1/text/1/
curl -X PUT --data '{"text": "Svete"}' -H Content-Type:application/json http://127.0.0.1:8000/v1/box/1/text/2/

Check the http://127.0.0.1:8000/ again

API

GET /v1/box/

Response:

[
  {"id": 1, "name": "Box 1"},
  {"id": 2, "name": "Box 2"}
]

POST /v1/box/

Request:

{"name": "Box 1"}

Response:

{"box_id": 1}

GET /v1/box/:box_id/

Response:

{"id": 1, "name": "Box 1"}

PUT /v1/box/:box_id/

Request:

{"name": "Box 1 Update"}

Response:

{"ok": true}

DELETE /v1/box/:box_id/

Response:

{"ok": true}

GET /v1/box/:box_id/text/

Response:

[
  {"id": 1, "box_id": 1, "text": "Text 1"},
  {"id": 2, "box_id": 1, "text": "Text 2"}
]

POST /v1/box/:box_id/text/

Request:

{"text": "Text 1"}

Response:

{"text_id": 1}

GET /v1/box/:box_id/text/:text_id/

Response:

{"id": 1, "box_id": 1, "text": "Text 1"}

PUT /v1/box/:box_id/text/:text_id/

Request:

{"text": "Text 1 Update"}

Response:

{"ok": true}

DELETE /v1/box/:box_id/text/:text_id/

Response:

{"ok": true}

Setup Go Project for terraform-provider-demo

Create a project directory

cd ~/go
mkdir -p src/github.com/ondrejsika/terraform-provider-demo
cd src/github.com/ondrejsika/terraform-provider-demo

Create gitignore:

# .gitignore
.VS_Code
.DS_Store
.terraform
terraform.tfstate*
terraform-provider-demo

Init Go module

go mod init

See go.mod:

cat go.mod

Create API Helpers

Download helpers:

wget https://raw.githubusercontent.com/ondrejsika/terraform-provider-demo/master/api.go

We use Resty library, we have to install it:

go get github.com/go-resty/resty/v2
go mod tidy
go mod vendor

See go.mod and go.sum:

cat go.mod
cat go.sum

Create an example main.go:

// main.go
package main

import "fmt"

func main(){
	res, _ := GetApi("http://127.0.0.1:8000/", "xxx", "/v1/box/")
	fmt.Println(res)
}

Try our API Helpers:

go build && ./terraform-provider-demo

Create API for our Demo Cloud

wget https://raw.githubusercontent.com/ondrejsika/terraform-provider-demo/master/box_api.go
wget https://raw.githubusercontent.com/ondrejsika/terraform-provider-demo/master/text_api.go

Create an example main.go:

// main.go
package main

import "fmt"

func main(){
	res, _ := ListBoxesApi("http://127.0.0.1:8000/", "xxx")
	fmt.Println(res)
}

Try our API:

go build && ./terraform-provider-demo

Create Provider for our Demo Cloud

Download a Terraform Provider files:

wget https://raw.githubusercontent.com/ondrejsika/terraform-provider-demo/master/resource_text.go
wget https://raw.githubusercontent.com/ondrejsika/terraform-provider-demo/master/resource_box.go
wget https://raw.githubusercontent.com/ondrejsika/terraform-provider-demo/master/provider.go
rm main.go
wget https://raw.githubusercontent.com/ondrejsika/terraform-provider-demo/master/main.go

Create Terraform Manifest

//terraform.tf
provider "demo" {
  api_origin = "https://demo-cloud-prod.panda.k8s.oxs.cz"
  token = "xxx"
}

resource "demo_box" "primary" {
  name = "Primary Box"
}

resource "demo_text" "hello" {
  box_id = demo_box.primary.id
  text = "This is my first box"
}

resource "demo_text" "foo" {
  box_id = demo_box.primary.id
  text = "Foo bar ..."
}

resource "demo_box" "secondary" {
  name = "Secondary Box"
}

resource "demo_text" "secondary" {
  box_id = demo_box.secondary.id
  text = "..."
}

Build, init, and apply Terraform configuration

go build && terraform && terraform apply

workshop-custom-terraform-provider's People

Contributors

ondrejsika avatar

Stargazers

Mirek Svoboda avatar

Watchers

James Cloos avatar Mirek Svoboda avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.