Skip to content

Commit

Permalink
chore: iterate
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneswuerbach committed Mar 28, 2024
1 parent 2033e22 commit efc11a7
Show file tree
Hide file tree
Showing 29 changed files with 543 additions and 70 deletions.
2 changes: 2 additions & 0 deletions examples/mysql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ graph LR;
| Name | Source | Version |
|------|--------|---------|
| mysql | ../../humanitec-resource-defs/mysql/basic | n/a |
| mysql\_instance | ../../humanitec-resource-defs/mysql-instance/basic | n/a |

## Resources

Expand All @@ -77,6 +78,7 @@ graph LR;
| [humanitec_application.example](https://registry.terraform.io/providers/humanitec/humanitec/latest/docs/resources/application) | resource |
| [humanitec_resource_account.humanitec_provisioner](https://registry.terraform.io/providers/humanitec/humanitec/latest/docs/resources/resource_account) | resource |
| [humanitec_resource_definition_criteria.mysql](https://registry.terraform.io/providers/humanitec/humanitec/latest/docs/resources/resource_definition_criteria) | resource |
| [humanitec_resource_definition_criteria.mysql_instance](https://registry.terraform.io/providers/humanitec/humanitec/latest/docs/resources/resource_definition_criteria) | resource |
| [google_compute_network.network](https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/compute_network) | data source |

## Inputs
Expand Down
35 changes: 33 additions & 2 deletions examples/mysql/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ resource "humanitec_application" "example" {
name = var.name
}

# MySQL instance

locals {
# Define the shared mysql-instance resource id and class
mysql_instance_res_id = "main-mysql"
mysql_instance_class = "default"
}

data "google_compute_network" "network" {
name = var.private_network
}
Expand Down Expand Up @@ -64,8 +72,8 @@ resource "google_service_networking_connection" "private_vpc_connection" {
depends_on = [google_project_service.servicenetworking]
}

module "mysql" {
source = "../../humanitec-resource-defs/mysql/basic"
module "mysql_instance" {
source = "../../humanitec-resource-defs/mysql-instance/basic"

prefix = var.prefix
resource_packs_gcp_rev = var.resource_packs_gcp_rev
Expand All @@ -82,6 +90,29 @@ module "mysql" {
depends_on = [google_service_networking_connection.private_vpc_connection]
}

resource "humanitec_resource_definition_criteria" "mysql_instance" {
resource_definition_id = module.mysql.id
app_id = humanitec_application.example.id
class = local.mysql_instance_class
res_id = local.mysql_instance_res_id
force_delete = true
}


module "mysql" {
source = "../../humanitec-resource-defs/mysql/basic"

prefix = var.prefix
resource_packs_gcp_rev = var.resource_packs_gcp_rev
resource_packs_gcp_url = var.resource_packs_gcp_url
append_logs_to_error = true
driver_account = humanitec_resource_account.humanitec_provisioner.id
project = var.project
region = var.region

instance_resource = "mysql-instance.${local.mysql_instance_class}#${local.mysql_instance_res_id}"
}

resource "humanitec_resource_definition_criteria" "mysql" {
resource_definition_id = module.mysql.id
app_id = humanitec_application.example.id
Expand Down
12 changes: 11 additions & 1 deletion examples/postgres/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ graph LR;

| Name | Source | Version |
|------|--------|---------|
| postgres | ../../humanitec-resource-defs/postgres/basic | n/a |
| gcp\_service\_account\_workload | ../../humanitec-resource-defs/gcp-service-account/workload | n/a |
| iam\_role\_binding\_service\_account\_workload\_identity | ../../humanitec-resource-defs/gcp-iam-policy-binding/basic | n/a |
| k8s\_service\_account | ../../humanitec-resource-defs/k8s/service-account | n/a |
| postgres | ../../humanitec-resource-defs/postgres/workload-identity | n/a |
| postgres\_instance | ../../humanitec-resource-defs/postgres-instance/basic | n/a |
| workload | ../../humanitec-resource-defs/workload/service-account | n/a |

## Resources

Expand All @@ -76,7 +81,12 @@ graph LR;
| [google_service_networking_connection.private_vpc_connection](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/service_networking_connection) | resource |
| [humanitec_application.example](https://registry.terraform.io/providers/humanitec/humanitec/latest/docs/resources/application) | resource |
| [humanitec_resource_account.humanitec_provisioner](https://registry.terraform.io/providers/humanitec/humanitec/latest/docs/resources/resource_account) | resource |
| [humanitec_resource_definition_criteria.gcp_service_account_workload](https://registry.terraform.io/providers/humanitec/humanitec/latest/docs/resources/resource_definition_criteria) | resource |
| [humanitec_resource_definition_criteria.iam_role_binding_service_account_workload_identity](https://registry.terraform.io/providers/humanitec/humanitec/latest/docs/resources/resource_definition_criteria) | resource |
| [humanitec_resource_definition_criteria.k8s_service_account](https://registry.terraform.io/providers/humanitec/humanitec/latest/docs/resources/resource_definition_criteria) | resource |
| [humanitec_resource_definition_criteria.postgres](https://registry.terraform.io/providers/humanitec/humanitec/latest/docs/resources/resource_definition_criteria) | resource |
| [humanitec_resource_definition_criteria.postgres_instance](https://registry.terraform.io/providers/humanitec/humanitec/latest/docs/resources/resource_definition_criteria) | resource |
| [humanitec_resource_definition_criteria.workload](https://registry.terraform.io/providers/humanitec/humanitec/latest/docs/resources/resource_definition_criteria) | resource |
| [google_compute_network.network](https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/compute_network) | data source |

## Inputs
Expand Down
2 changes: 1 addition & 1 deletion examples/postgres/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ module "iam_role_binding_service_account_workload_identity" {
prefix = var.prefix
name = "gcp-iam-policy-binding-sa-workload-identity"

type = "storage_account"
type = "service_account"
scope_key = "service_account_id"
scope_value = "$${resources['postgres.${local.postgres_basic_class}'].outputs.service_account_id}"
role = "roles/iam.workloadIdentityUser"
Expand Down
42 changes: 42 additions & 0 deletions humanitec-resource-defs/mysql-instance/basic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!-- BEGIN_TF_DOCS -->
## Requirements

| Name | Version |
|------|---------|
| terraform | >= 1.3.0 |
| humanitec | ~> 1.0 |

## Providers

| Name | Version |
|------|---------|
| humanitec | ~> 1.0 |

## Resources

| Name | Type |
|------|------|
| [humanitec_resource_definition.main](https://registry.terraform.io/providers/humanitec/humanitec/latest/docs/resources/resource_definition) | resource |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| database\_version | The MySQL, PostgreSQL or SQL Server version to use. | `string` | n/a | yes |
| driver\_account | The ID of the Resource Account which should be used. | `string` | n/a | yes |
| prefix | Prefix for all resources | `string` | n/a | yes |
| private\_network | The VPC network from which the Cloud SQL instance is accessible for private IP. | `string` | n/a | yes |
| project | n/a | `string` | n/a | yes |
| region | GCP region | `string` | n/a | yes |
| tier | The machine type to use. | `string` | n/a | yes |
| append\_logs\_to\_error | Append Terraform logs to error messages. | `bool` | `false` | no |
| name | Resource name (can contain placeholders like ${context.app.id}) | `string` | `""` | no |
| resource\_packs\_gcp\_rev | n/a | `string` | `"ref/heads/main"` | no |
| resource\_packs\_gcp\_url | n/a | `string` | `"https://github.com/humanitec-architecture/resource-packs-gcp.git"` | no |

## Outputs

| Name | Description |
|------|-------------|
| id | n/a |
<!-- END_TF_DOCS -->
40 changes: 40 additions & 0 deletions humanitec-resource-defs/mysql-instance/basic/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
resource "humanitec_resource_definition" "main" {
driver_type = "humanitec/terraform"
id = "${var.prefix}cloudsql-mysql-instance-basic"
name = "${var.prefix}cloudsql-mysql-instance-basic"
type = "mysql-instance"

driver_account = var.driver_account
driver_inputs = {
values_string = jsonencode({
source = {
path = "modules/cloudsql-instance/basic"
rev = var.resource_packs_gcp_rev
url = var.resource_packs_gcp_url
}

append_logs_to_error = var.append_logs_to_error

credentials_config = {
environment = {
GOOGLE_CREDENTIALS = "*"
}
}

variables = {
name = var.name
project = var.project
region = var.region
prefix = var.prefix
app_id = "$${context.app.id}"
env_id = "$${context.env.id}"
res_id = "$${context.res.id}"

database_version = var.database_version
tier = var.tier
private_network = var.private_network
port = 3306
}
})
}
}
3 changes: 3 additions & 0 deletions humanitec-resource-defs/mysql-instance/basic/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "id" {
value = humanitec_resource_definition.main.id
}
10 changes: 10 additions & 0 deletions humanitec-resource-defs/mysql-instance/basic/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
terraform {
required_providers {
humanitec = {
source = "humanitec/humanitec"
version = "~> 1.0"
}
}

required_version = ">= 1.3.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

# Append Terraform logs to error messages.
append_logs_to_error = false

# The MySQL, PostgreSQL or SQL Server version to use.
database_version = ""

# The ID of the Resource Account which should be used.
driver_account = ""

# Resource name (can contain placeholders like ${context.app.id})
name = ""

# Prefix for all resources
prefix = ""

# The VPC network from which the Cloud SQL instance is accessible for private IP.
private_network = ""

project = ""

# GCP region
region = ""

resource_packs_gcp_rev = "ref/heads/main"
resource_packs_gcp_url = "https://github.com/humanitec-architecture/resource-packs-gcp.git"

# The machine type to use.
tier = ""
55 changes: 55 additions & 0 deletions humanitec-resource-defs/mysql-instance/basic/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
variable "prefix" {
description = "Prefix for all resources"
type = string
}

variable "resource_packs_gcp_rev" {
type = string
default = "ref/heads/main"
}

variable "resource_packs_gcp_url" {
type = string
default = "https://github.com/humanitec-architecture/resource-packs-gcp.git"
}

variable "append_logs_to_error" {
description = "Append Terraform logs to error messages."
type = bool
default = false
}

variable "driver_account" {
description = "The ID of the Resource Account which should be used."
type = string
}

variable "project" {
type = string
}

variable "region" {
description = "GCP region"
type = string
}

variable "name" {
type = string
description = "Resource name (can contain placeholders like $${context.app.id})"
default = ""
}

variable "database_version" {
type = string
description = "The MySQL, PostgreSQL or SQL Server version to use."
}

variable "tier" {
type = string
description = "The machine type to use."
}

variable "private_network" {
type = string
description = "The VPC network from which the Cloud SQL instance is accessible for private IP."
}
4 changes: 1 addition & 3 deletions humanitec-resource-defs/mysql/basic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| database\_version | The MySQL, PostgreSQL or SQL Server version to use. | `string` | n/a | yes |
| driver\_account | The ID of the Resource Account which should be used. | `string` | n/a | yes |
| instance\_resource | Identifier of the PostgreSQL instance resource. | `string` | n/a | yes |
| prefix | Prefix for all resources | `string` | n/a | yes |
| private\_network | The VPC network from which the Cloud SQL instance is accessible for private IP. | `string` | n/a | yes |
| project | n/a | `string` | n/a | yes |
| region | GCP region | `string` | n/a | yes |
| tier | The machine type to use. | `string` | n/a | yes |
| append\_logs\_to\_error | Append Terraform logs to error messages. | `bool` | `false` | no |
| name | Resource name (can contain placeholders like ${context.app.id}) | `string` | `""` | no |
| resource\_packs\_gcp\_rev | n/a | `string` | `"ref/heads/main"` | no |
Expand Down
12 changes: 5 additions & 7 deletions humanitec-resource-defs/mysql/basic/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
resource "humanitec_resource_definition" "main" {
driver_type = "humanitec/terraform"
id = "${var.prefix}cloudsql-mysql"
name = "${var.prefix}cloudsql-mysql"
id = "${var.prefix}cloudsql-mysql-basic"
name = "${var.prefix}cloudsql-mysql-basic"
type = "mysql"

driver_account = var.driver_account
Expand All @@ -25,15 +25,13 @@ resource "humanitec_resource_definition" "main" {
name = var.name
project = var.project
region = var.region
prefix = var.prefix
app_id = "$${context.app.id}"
env_id = "$${context.env.id}"
res_id = "$${context.res.id}"

database_version = var.database_version
tier = var.tier
private_network = var.private_network
port = 3306
host = "$${resources['${var.instance_resource}'].outputs.host}"
port = "$${resources['${var.instance_resource}'].outputs.port}"
instance = "$${resources['${var.instance_resource}'].outputs.instance}"
}
})
}
Expand Down
14 changes: 4 additions & 10 deletions humanitec-resource-defs/mysql/basic/terraform.tfvars.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,22 @@
# Append Terraform logs to error messages.
append_logs_to_error = false

# The MySQL, PostgreSQL or SQL Server version to use.
database_version = ""

# The ID of the Resource Account which should be used.
driver_account = ""

# Identifier of the PostgreSQL instance resource.
instance_resource = ""

# Resource name (can contain placeholders like ${context.app.id})
name = ""

# Prefix for all resources
prefix = ""

# The VPC network from which the Cloud SQL instance is accessible for private IP.
private_network = ""

project = ""

# GCP region
region = ""

resource_packs_gcp_rev = "ref/heads/main"
resource_packs_gcp_url = "https://github.com/humanitec-architecture/resource-packs-gcp.git"

# The machine type to use.
tier = ""
resource_packs_gcp_url = "https://github.com/humanitec-architecture/resource-packs-gcp.git"
14 changes: 2 additions & 12 deletions humanitec-resource-defs/mysql/basic/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,7 @@ variable "name" {
default = ""
}

variable "database_version" {
variable "instance_resource" {
description = "Identifier of the PostgreSQL instance resource."
type = string
description = "The MySQL, PostgreSQL or SQL Server version to use."
}

variable "tier" {
type = string
description = "The machine type to use."
}

variable "private_network" {
type = string
description = "The VPC network from which the Cloud SQL instance is accessible for private IP."
}
Loading

0 comments on commit efc11a7

Please sign in to comment.