Skip to content

Commit

Permalink
docs: update example add one to the readme
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasZeissner committed Sep 13, 2024
1 parent 4fe866b commit 8c5560b
Show file tree
Hide file tree
Showing 2 changed files with 204 additions and 18 deletions.
142 changes: 142 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,148 @@ The Cosmo Terraform provider includes the following resources and data sources:

Each resource and data source allows you to define and manage specific aspects of your Cosmo infrastructure seamlessly within Terraform.

## Example Usage

The provider can be used as follows:

```hcl
// your stages represented by different namespaces
locals {
stages = {
dev = {},
stg = {},
prod = {}
}
}
// your subgraphs, which are deployed to each stage
locals {
subgraphs = {
"product-api" = {
routing_url = "http://product-api:3000/graphql"
labels = {
"team" = "backend"
}
},
"employees-api" = {
routing_url = "http://employees-api:3000/graphql"
labels = {
"team" = "backend"
}
},
"family-api" = {
routing_url = "http://family-api:3000/graphql"
labels = {
"team" = "backend"
}
},
"hobbies-api" = {
routing_url = "http://hobbies-api:3000/graphql"
labels = {
"team" = "backend"
}
},
"availability-api" = {
routing_url = "http://availability-api:3000/graphql"
labels = {
"team" = "backend"
}
}
}
// Helper, used to make the subgraphs above staged
// {
// "dev-product-api" = {
// "stage" = "dev"
// "subgraph" = "product-api"
// "routing_url" = "http://product-api:3000/graphql"
// "labels" = {
// "team" = "backend"
// }
// }
// }
stage_subgrahs = merge(flatten([
for key, value in local.stages : {
for subgraph, subgraph_value in local.subgraphs :
"${key}-${subgraph}" => {
"stage" = key
"subgraph" = subgraph
"routing_url" = subgraph_value.routing_url
"labels" = subgraph_value.labels
}
}])...)
}
// create a namespace for each stage
// dev-namespace, stg-namespace, prod-namespace
resource "cosmo_namespace" "namespace" {
for_each = local.stages
name = "${each.key}-namespace"
}
// create a federated graph for each stage
// dev-federated-graph, stg-federated-graph, prod-federated-graph
resource "cosmo_federated_graph" "federated_graph" {
for_each = local.stages
name = "${each.key}-federated-graph"
routing_url = "http://${each.key}.localhost:3000"
namespace = cosmo_namespace.namespace[each.key].name
label_matchers = ["team=backend", "stage=${each.key}"]
depends_on = [cosmo_subgraph.subgraph]
}
// create each stages subgraph
resource "cosmo_subgraph" "subgraph" {
for_each = local.stage_subgrahs
name = "${each.value.subgraph}-${each.value.stage}-subgraph"
namespace = cosmo_namespace.namespace[each.value.stage].name
routing_url = each.value.routing_url
// merge graph labels with the stage label
labels = merge(each.value.labels, {
"stage" = each.value.stage
})
}
// create a router token for each stage
resource "cosmo_router_token" "router_token" {
for_each = local.stages
name = "${each.key}-router-token"
namespace = cosmo_namespace.namespace[each.key].name
graph_name = cosmo_federated_graph.federated_graph[each.key].name
}
output "dev_router_token" {
value = cosmo_router_token.router_token["dev"].token
sensitive = true
}
output "stg_router_token" {
value = cosmo_router_token.router_token["stg"].token
sensitive = true
}
output "prod_router_token" {
value = cosmo_router_token.router_token["prod"].token
sensitive = true
}
// used to debug sensitive values
// resource "local_file" "router_tokens" {
// content = jsonencode(cosmo_router_token.router_token)
// filename = "router_tokens.json"
// }
```

Further in depth examples can be found in the [examples](examples) directory.

## Building The Provider

To build the provider, clone the repository, enter the directory, and run `make install` to compile and install the provider binary. Note that the `install` command will first build the provider to ensure the binary is up to date.
Expand Down
80 changes: 62 additions & 18 deletions examples/cosmo/main.tf
Original file line number Diff line number Diff line change
@@ -1,46 +1,58 @@
// your stages represented by different namespaces
locals {
stages = {
dev = {},
stg = {},
prod = {}
}
}

// your subgraphs, which are deployed to each stage
locals {
subgraphs = {
"product-api" = {
routing_url = "http://product-api:3000/graphql"
labels = {
"team" = "backend"
"stage" = "dev"
"team" = "backend"
}
},
"employees-api" = {
routing_url = "http://employees-api:3000/graphql"
labels = {
"team" = "backend"
"stage" = "dev"
"team" = "backend"
}
},
"family-api" = {
routing_url = "http://family-api:3000/graphql"
labels = {
"team" = "backend"
"stage" = "dev"
"team" = "backend"
}
},
"hobbies-api" = {
routing_url = "http://hobbies-api:3000/graphql"
labels = {
"team" = "backend"
"stage" = "dev"
"team" = "backend"
}
},
"availability-api" = {
routing_url = "http://availability-api:3000/graphql"
labels = {
"team" = "qa"
"stage" = "int"
"team" = "backend"
}
},
}
}

// Helper, used to make the subgraphs above staged
// {
// "dev-product-api" = {
// "stage" = "dev"
// "subgraph" = "product-api"
// "routing_url" = "http://product-api:3000/graphql"
// "labels" = {
// "team" = "backend"
// }
// }
// }
stage_subgrahs = merge(flatten([
for key, value in local.stages : {
for subgraph, subgraph_value in local.subgraphs :
Expand All @@ -54,36 +66,68 @@ locals {
}

// create a namespace for each stage
// e.g. dev-namespace, stg-namespace, prod-namespace
// dev-namespace, stg-namespace, prod-namespace
resource "cosmo_namespace" "namespace" {
for_each = local.stages

name = "${each.key}-namespace"
}

// create a federated graph for each stage
// e.g. dev-federated-graph, stg-federated-graph, prod-federated-graph
// dev-federated-graph, stg-federated-graph, prod-federated-graph
resource "cosmo_federated_graph" "federated_graph" {
for_each = local.stages

name = "${each.key}-federated-graph"
routing_url = "http://${each.key}.localhost:3000"
namespace = cosmo_namespace.namespace[each.key].name

label_matchers = ["team=backend"]
label_matchers = ["team=backend", "stage=${each.key}"]

depends_on = [cosmo_subgraph.subgraph]
}

// create each stages subgraph
// e.g. dev-subgraph, stg-subgraph, prod-subgraph
resource "cosmo_subgraph" "subgraph" {
for_each = local.stage_subgrahs

name = "${each.key}-subgraph"
name = "${each.value.subgraph}-${each.value.stage}-subgraph"
namespace = cosmo_namespace.namespace[each.value.stage].name

routing_url = each.value.routing_url

labels = each.value.labels
}
// merge graph labels with the stage label
labels = merge(each.value.labels, {
"stage" = each.value.stage
})
}

// create a router token for each stage
resource "cosmo_router_token" "router_token" {
for_each = local.stages

name = "${each.key}-router-token"
namespace = cosmo_namespace.namespace[each.key].name
graph_name = cosmo_federated_graph.federated_graph[each.key].name
}

output "dev_router_token" {
value = cosmo_router_token.router_token["dev"].token
sensitive = true
}

output "stg_router_token" {
value = cosmo_router_token.router_token["stg"].token
sensitive = true
}

output "prod_router_token" {
value = cosmo_router_token.router_token["prod"].token
sensitive = true
}

// used to debug sensitive values
// resource "local_file" "router_tokens" {
// content = jsonencode(cosmo_router_token.router_token)
// filename = "router_tokens.json"
// }

0 comments on commit 8c5560b

Please sign in to comment.