Skip to content

Commit

Permalink
🎉 add complete example to create spaces and provide non-expiring token
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Münch <[email protected]>
  • Loading branch information
atomic111 committed Nov 4, 2023
1 parent 8761286 commit da2686c
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 1 deletion.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,16 @@ Then commit the changes to `go.mod` and `go.sum`.

## Using the provider

Fill this in for each provider
After building the provider please install the terraform provider:

```
# ARCH = 'linux_amd64'
export ARCH='darwin_arm64'
export VERSION='1.0.0'
mkdir -p ~/.terraform.d/plugins/registry.terraform.io/mondoo/mondoo/$VERSION/$ARCH
cp $GOPATH/bin/terraform-provider-mondoo ~/.terraform.d/plugins/registry.terraform.io/mondoo/mondoo/$VERSION/$ARCH/
```

## Developing the Provider

Expand Down
191 changes: 191 additions & 0 deletions docs/create_spaces/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
# Example to create spaces + get registration token

This example creates 3 different Mondoo Spaces in a given Mondoo Organisation and provides the user for each Space a non-expiring Mondoo Registration Token.

## Prereqs

- [Mondoo Platform account](https://mondoo.com/docs/platform/start/plat-start-acct/)
- [Mondoo Organisation](https://mondoo.com/docs/platform/start/organize/overview/)
- [Mondoo API Token](https://mondoo.com/docs/platform/maintain/access/api-tokens/)

## Usage

Adjust the variables `space_names` and `org_id` in `terraform.tfvars`:

```coffee
space_names = ["Terraform Mondoo1", "Terraform Mondoo2", "Terraform Mondoo3"]
org_id = "love-mondoo-131514041515"
```

Set the Mondoo API token

```bash
export MONDOO_API_TOKEN="InsertTokenHere"
```

Initialize a working directory containing Terraform configuration files.

```bash
terraform init

Initializing the backend...

Initializing provider plugins...
- Finding latest version of mondoo/mondoo...
- Installing mondoo/mondoo v1.0.0...
- Installed mondoo/mondoo v1.0.0 (unauthenticated)

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

â•·
│ Warning: Incomplete lock file information for providers
│
│ Due to your customized provider installation methods, Terraform was forced to calculate lock file checksums locally for the following providers:
│ - mondoo/mondoo
│
│ The current .terraform.lock.hcl file only includes checksums for darwin_arm64, so Terraform running on another platform will fail to install these providers.
│
│ To calculate additional checksums for another platform, run:
│ terraform providers lock -platform=linux_amd64
│ (where linux_amd64 is the platform to generate)
╵

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
```
Create an execution plan, which lets you preview the changes that the Terraform plan makes to your Mondoo Organisation:
```bash
terraform plan -out plan.out

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create

Terraform will perform the following actions:

# mondoo_registration_token.token[0] will be created
+ resource "mondoo_registration_token" "token" {
+ description = "Get a mondoo registration token"
+ expires_at = (known after apply)
+ mrn = (known after apply)
+ no_exipration = true
+ result = (sensitive value)
+ revoked = (known after apply)
+ space_id = (known after apply)
}

# mondoo_registration_token.token[1] will be created
+ resource "mondoo_registration_token" "token" {
+ description = "Get a mondoo registration token"
+ expires_at = (known after apply)
+ mrn = (known after apply)
+ no_exipration = true
+ result = (sensitive value)
+ revoked = (known after apply)
+ space_id = (known after apply)
}

# mondoo_registration_token.token[2] will be created
+ resource "mondoo_registration_token" "token" {
+ description = "Get a mondoo registration token"
+ expires_at = (known after apply)
+ mrn = (known after apply)
+ no_exipration = true
+ result = (sensitive value)
+ revoked = (known after apply)
+ space_id = (known after apply)
}

# mondoo_space.my_space[0] will be created
+ resource "mondoo_space" "my_space" {
+ id = (known after apply)
+ name = "Terraform Mondoo1"
+ org_id = "love-mondoo-131514041515"
}

# mondoo_space.my_space[1] will be created
+ resource "mondoo_space" "my_space" {
+ id = (known after apply)
+ name = "Terraform Mondoo2"
+ org_id = "love-mondoo-131514041515"
}

# mondoo_space.my_space[2] will be created
+ resource "mondoo_space" "my_space" {
+ id = (known after apply)
+ name = "Terraform Mondoo3"
+ org_id = "love-mondoo-131514041515"
}

Plan: 6 to add, 0 to change, 0 to destroy.

Changes to Outputs:
+ complete_space_setup = (sensitive value)

────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Saved the plan to: plan.out

To perform exactly these actions, run the following command to apply:
terraform apply "plan.out"
```
Execute the actions proposed in the Terraform plan
```bash
terraform apply -auto-approve plan.out

mondoo_space.my_space[2]: Creating...
mondoo_space.my_space[1]: Creating...
mondoo_space.my_space[0]: Creating...
mondoo_space.my_space[1]: Creation complete after 1s [id=admiring-wiles-299863]
mondoo_space.my_space[2]: Creation complete after 1s [id=inspiring-tesla-178593]
mondoo_space.my_space[0]: Creation complete after 1s [id=sad-wescoff-418523]
mondoo_registration_token.token[2]: Creating...
mondoo_registration_token.token[0]: Creating...
mondoo_registration_token.token[1]: Creating...
mondoo_registration_token.token[0]: Creation complete after 0s
mondoo_registration_token.token[1]: Creation complete after 0s
mondoo_registration_token.token[2]: Creation complete after 0s

Apply complete! Resources: 6 added, 0 changed, 0 destroyed.

Outputs:

complete_space_setup = <sensitive>
```
Extract the value of the output variable `complete_space_setup` from the state file.
```bash
terraform output -json complete_space_setup | jq

[
{
"space-id": "sad-wescoff-418523",
"space-name": "Terraform Mondoo1",
"token": "eyJhbGciOiJFUzM4NCIsInR5cCI6IkpXVCJ9.eyJhcGlfZW5kcG9pbnQiOiJodHRwczovL3VzLmFwaS5tb25kb28uY29tIiwiYXVkIjpbIm1vbmRvbyJdLCJjZXJ0X3ZhbGlkX3VudGlsIjoiOTk5OS0xMi0zMVQyMzo1OTo1OVoiLCJkZXNjIjoiR2V0IGEgbW9uZG9vIHJlZ2lzdHJhdGlvbiB0b2tlbiIsImlhdCI6MTY5OTA5NDA3MiwiaXNzIjoibW9uZG9vL2FtcyIsImxhYmVscyI6bnVsbCwibmJmIjoxNjk5MDk0MDcyLCJvd25lciI6IiIsInNjb3BlIjoiLy4NTI1Iiwic3ViIjoiLy9hZ2VudHMuYXBpLm1vbmRvby5hcHAvb3JnYW5pemF0aW9ucy9zdHVwZWZpZWQtam9obnNvbi02MzExNTUvc2VydmljZWFjY291bnRzLzJYZmxFU3NJN3VPbHc2VVhUMXlsbXdhUGRrciJ9.ajcJeYC5WTX7TwJdIO8wBITXwIGHuhxp2qGVgAWKaRgKTUlbEUkua898PBJWpseDDUpRZVKMBZpQjd78xglJtd0nUiBvg2b4py3XIPlutxBAhNHar"
},
{
"space-id": "admiring-wiles-299863",
"space-name": "Terraform Mondoo2",
"token": "eyJhbGciOiJFUzM4NCIsInR5cCI6IkpXVCJ9.eyJhcGlfZW5kcG9pbnQiOiJodHRwczovL3VzHJhdGlvbiB0b2tlbiIsImlhdCI6MTY5OTA5NDA3MiwiaXNzIjoibW9uZG9vL2FtcyIsImxhYmVscyI6bnVsbCwibmJmIjoxNjk5MDk0MDcyLCJvd25lciI6IiIsInNjb3BlIjoiLy9jYXB0YWluLmFwaS5tb25kb28uYXBwL3NwYWNlcy9hZG1pcmluZy13aWxlcy0yOTk4NjQiLCJzcGFjZSI6Ii8vY2FwdGFpbi5hcGkubW9uZG9vLmFwcC9zcGFjZXMvYWRtaXJpbmctd2lsZXMtMjk5ODY0Iiwic3ViIjoiLy9hZ2VudHMuYXBpLm1vbmRvby5hcHAvb3JnYW5pemF0aW9ucy9zdHVwZWZpZWQtam9obnNvbi02MzExNTUvc2VydmljZWFjY291bnRzLzJYZmxFU3NJN3VPbHc2VVhUMXlsbXdhUGRrciJ9.Dq98j1sWXShNxhWXJC0aqZsbcqcOyDH3SQdwU7S67bh_qQMgYS8WSQgM_0QmbVNOBYg3mNVEr2lwB45w105zXkvADk_KBpXgfIHS3rXQXJIK"
},
{
"space-id": "inspiring-tesla-178593",
"space-name": "Terraform Mondoo3",
"token": "eyJhbGciOiJFUzM4NCIsInR5cCI6IkpXVCJ9.eyJhcGlfZW5kcG9pbnQiOiJodHRwczovL3VzLmFwaS5tb25kb28uY29tIiwiYXVkIjpbIm1vbmRvbyJdLCJjZXJ0X3ZhbGlkX3VudGlsIjoiOTk5OS0xMi0zMVQyMzo1OTo1OVoiLCJkZXNjIjoiR2V0IGEgbW9uZG9vIHJlZ2lzdHJhdGlvbiB0b2tlbiIsImlhdCI6MTY5OTA5NDA3MiwiaXNzIjoibW9uZG9vL2FtcyIsImxhYmVscyI6bnVsbCwibmJmIjoxNjk5MDk0MDcyLCJvd25lciI6IiIsInNjb3BlIjopcmluZy10ZXNsYS0xNzg1OTIiLCJzdWIiOiIvL2FnZW50cy5hcGkubW9uZG9vLmFwcC9vcmdhbml6YXRpb25zL3N0dXBlZmllZC1qb2huc29uLTYzMTE1NS9zZXJ2aWNlYWNjb3VudHMvMlhmbEVTc0k3dUmaFeCIKxr6xbSDqNRIzEwSDVlx7TO2AVQm9w-k0hy8jCkfjXk6VBGwFOtz9TiWHeoQZz8igh5pOoeQwc-TjglUZx"
}
]
```
25 changes: 25 additions & 0 deletions docs/create_spaces/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
terraform {
required_providers {
mondoo = {
source = "mondoo/mondoo"
}
}
}

resource "mondoo_space" "my_space" {
count = length(var.space_names)
name = var.space_names[count.index]
org_id = var.org_id
}

resource "mondoo_registration_token" "token" {
description = "Get a mondoo registration token"
count = length(var.space_names)
space_id = mondoo_space.my_space[count.index].id
no_exipration = true
// expires_in = "1h"
depends_on = [
mondoo_space.my_space
]
}

4 changes: 4 additions & 0 deletions docs/create_spaces/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "complete_space_setup" {
value = [for count, space in mondoo_space.my_space : { "space-name" : space.name, "space-id" : space.id, "token" : mondoo_registration_token.token[count].result }]
sensitive = true
}
4 changes: 4 additions & 0 deletions docs/create_spaces/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
provider "mondoo" {
//region = "us" # if you use the share platform, possible values us and eu
endpoint = "https://us.api.mondoo.com" # if you use your own mondoo hosted platform
}
11 changes: 11 additions & 0 deletions docs/create_spaces/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
variable "space_names" {
description = "Create Spaces with these names"
type = list(string)
default = []
}

variable "org_id" {
description = "The organization id to create the spaces in"
type = string
default = ""
}

0 comments on commit da2686c

Please sign in to comment.