Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lgfa29 committed Aug 14, 2023
0 parents commit 4fc1f88
Show file tree
Hide file tree
Showing 12 changed files with 298 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: "Terraform Valida Modulo"

on:
push:
branches:
- main
pull_request:

jobs:
terraform:
uses: "mentoriaiac/cicd_centralizado/.github/workflows/terraform_valida_modulo.yaml@v1"
secrets:
token: ${{ secrets.TOKEN }}

34 changes: 34 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Local .terraform directories
**/.terraform/*
.terraform*

#terraform inc files
terraform.inc

# .tfstate files
*.tfstate
*.tfstate.*

# Crash log files
crash.log

# Ignore any .tfvars files that are generated automatically for each Terraform run. Most
# .tfvars files are managed as part of configuration and so should be included in
# version control.
#
# example.tfvars

# Ignore override files as they are usually used to override resources locally and so
# are not checked in
override.tf
override.tf.json
*_override.tf
*_override.tf.json

# Include override files you do wish to add to version control using negated pattern
#
# !example_override.tf

# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
# example: *tfplan*
tfplan
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0.0 |
| <a name="requirement_hashicups"></a> [hashicups](#requirement\_hashicups) | ~> 0.3.1 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_hashicups"></a> [hashicups](#provider\_hashicups) | 0.3.1 |

## Modules

No modules.

## Resources

| Name | Type |
|------|------|
| [hashicups_order.order](https://registry.terraform.io/providers/hashicorp/hashicups/latest/docs/resources/order) | resource |
| [hashicups_coffees.all](https://registry.terraform.io/providers/hashicorp/hashicups/latest/docs/data-sources/coffees) | data source |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_order"></a> [order](#input\_order) | Mapa de cafe e quantidade | `map(number)` | n/a | yes |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_order_id"></a> [order\_id](#output\_order\_id) | n/a |

## Como usar esse módulo
[Acesse o passo-a-passo](how-to-use-this-module/README.md)
6 changes: 6 additions & 0 deletions how-to-use-this-module/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
INCLUDE_MAKEFILE=terraform.inc
VERSION_MAKEFILE=v1.0.0
REMOTE_MAKEFILE=https://raw.githubusercontent.com/mentoriaiac/Makefiles/${VERSION_MAKEFILE}/${INCLUDE_MAKEFILE}
TARGET_FOLDER=how-to-use-this-module
TARGET_ENV=.target.env
TERRAFORM_VERSION=1.0.0
Empty file.
24 changes: 24 additions & 0 deletions how-to-use-this-module/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
SHELL = /bin/bash

cnf ?= .env
ifneq ($(shell test -e $(cnf) && echo -n yes),yes)
ERROR := $(error $(cnf) file not defined in current directory)
endif

include $(cnf)
export $(shell sed 's/=.*//' $(cnf))

ifneq ($(shell test -e $(INCLUDE_MAKEFILE) && echo -n yes),yes)
ifdef REMOTE_MAKEFILE
REMOTE_MAKEFILE_RESULT := $(shell curl ${REMOTE_MAKEFILE} -o ${INCLUDE_MAKEFILE})
else
ERROR := $(error REMOTE_MAKEFILE not provided, look for your .env file)
endif
endif

ifdef INCLUDE_MAKEFILE
include ${INCLUDE_MAKEFILE}
endif

build: plan
deploy: apply
132 changes: 132 additions & 0 deletions how-to-use-this-module/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# Template de módulo Terraform

## Dependências

Para realizar os testes localmente é necessário:

| Ferramentas | Versão | Instalação |
| ----------- | ------ | ---------- |
| Terraform | >= 1.0.0 | [Acesse](https://learn.hashicorp.com/tutorials/terraform/install-cli) |
| Docker | >= 20.10 | [Acesse](https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-20-04-pt) |
| Shipyard.run | >= 0.3.36 | [Acesse](https://shipyard.run/docs/install) |
| Git | >= 2.30.2 | [Acesse](https://git-scm.com/downloads) |

# Criando o ambiente para a API do Hashicups

Instalação do Shipyard.run

```
curl https://shipyard.run/install | bash
```

Clone o repositório e acesse a pasta blueprint

```
git clone https://github.com/hashicorp-demoapp/product-api-go
cd product-api-go/blueprint/
```

Inicie a aplicação executando o comando a baixo

```
shipyard run
```

Saída Esperada

```bash

The API can be accessed at
[http://localhost:19090/coffees](http://localhost:19090/coffees)

```

Crie um usuário para a aplicação

```
curl -X POST localhost:19090/signup -d '{"username":"mentoriaiac", "password":"2021@mentoria"}'
```

# Utilizando o módulo

### Primeiro Passo:

Acesse o repósitorio do módulo :

<pre>

├── product-api-go
│   ├── blueprint
│   ├── client
│   ├── config
│   ├── data
│   │   └── model
│   ├── database
│   ├── docker_compose
│   ├── functional_tests
│   │   └── features
│   ├── handlers
│   └── telemetry
<b>└── template-modulo-terraform </b>
└── how-to-use-this-module

</pre>

Depois acesse a pasta how-to-use-this-module

```
cd ./how-to-use-this-module/
```

Inicialize o Terraform

```
terraform init
```

### Segundo Passo:

Personalize o `terrafile.tf`:

```
order = {
Terraspresso = 4,
Nomadicano = 10,
"Vagrante espresso" = 4,
Packer Spiced Latte = 6,
Vaulatte = 8,
Connectaccino = 2
}
```

Tente criar o primeiro plan:
```
terraform plan
```

Obs.: Caso retorne erro 401, verifique o usuário e a senha.

### Terceiro Passo:

Aplique suas mudanças:

```
terraform apply
```
16 changes: 16 additions & 0 deletions how-to-use-this-module/terrafile.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
provider "hashicups" {
username = "mentoriaiac"
password = "2021@mentoria"
}

module "hashicups_order" {
source = "../"
order = {
Terraspresso = 4,
Nomadicano = 10
}
}

output "hashicups_order_id" {
value = module.hashicups_order.order_id
}
18 changes: 18 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
data "hashicups_coffees" "all" {}

locals {
coffee_name_id_map = { for coffee in data.hashicups_coffees.all.coffees : coffee.name => coffee.id }
}

resource "hashicups_order" "order" {
dynamic "items" {
for_each = var.order
content {
coffee {
id = local.coffee_name_id_map[items.key]
}
quantity = items.value
}
}
}

3 changes: 3 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "order_id" {
value = resource.hashicups_order.order.id
}
4 changes: 4 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
variable "order" {
description = "Mapa de cafe e quantidade"
type = map(number)
}
9 changes: 9 additions & 0 deletions versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
required_version = ">= 1.0.0"
required_providers {
hashicups = {
version = "~> 0.3.1"
}
}
}

0 comments on commit 4fc1f88

Please sign in to comment.