Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: Initial examples #346

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions activity-tracking/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Generic Activity Tracking

## Use Case: Pass Activity Tracking

If you just want to include `pass` placeholder within a lab, use the following link.

| Service | Method | Action | Link |
|---------|--------|--------|------|
| N/A | N/A | Demo a `pass` activity within a lab | [Link](https://github.com/CloudVLab/terraform-lab-foundation/tree/main/activity-tracking/pass_step_check) |


## Use Case: Service Exists Activity Tracking

If you are spinning up a resource, it can be helpful to check the existence within a project.
Use the Terraform scripts below to validate a resource exists with generic activity tracking code.

| Service | Method | Action | Link |
|---------|--------|--------|------|
| BigQuery | get_dataset | Check if a `dataset` is defined within a project | [Link](https://github.com/CloudVLab/terraform-lab-foundation/tree/main/activity-tracking/bq_dataset_check) |
| Cloud Storage | get_bucket | Check if a `storage bucket` is defined within a project | [Link](https://github.com/CloudVLab/terraform-lab-foundation/tree/main/activity-tracking/gcs_bucket_check) |
| Firewall | get_firewall | Check if a `firewall rule` is defined within a project | [Link](https://github.com/CloudVLab/terraform-lab-foundation/tree/main/activity-tracking/fw_rule_check) |
| Logging | get_sink | Check if a `log sink` is defined within a project | [Link](https://github.com/CloudVLab/terraform-lab-foundation/tree/main/activity-tracking/log_sink_check) |
| PubSub | get_topic | Check if a `Topic` is defined within a project | [Link](https://github.com/CloudVLab/terraform-lab-foundation/tree/main/activity-tracking/pubsub_topic_check) |
| PubSub | get_subscription | Check if a `Subscription` is defined within a project | [Link](https://github.com/CloudVLab/terraform-lab-foundation/tree/main/activity-tracking/pubsub_sub_check) |
| Source Repo | get_repo | Check if a `Repository` is defined within a project | [Link](https://github.com/CloudVLab/terraform-lab-foundation/tree/main/activity-tracking/source_repo_check) |
| Compute | get_network | Check if a `network` is defined within a project | [Link](https://github.com/CloudVLab/terraform-lab-foundation/tree/main/activity-tracking/vpc_network_check) |
| Compute | get_subnetwork | Check if a `subnetwork` is defined within a project | [Link](https://github.com/CloudVLab/terraform-lab-foundation/tree/main/activity-tracking/vpc_subnet_check) |


## Setup

1. Amend the `variables.tf` file to set the name the `step` to be generated.


```terraform
variable "step_name" {
type = string
description = "The name of the step to be created"
default = "bigquery_dataset_check"
}
```

__Note:__ The Terraform `default` value will determine the name of:

- [ ] The file to be generated
- [ ] The name of the method to be created

## Generate

To generate the file, use Terraform to create the output file.

1. Initialise the folder
```
terraform init
```

2. Validate the code
```
terraform validate
```

3. Create the activity tracking code file
```
terraform apply -auto-approve
```

4. Optional: To remove the file created
```
terraform destroy -auto-approve
```
21 changes: 21 additions & 0 deletions activity-tracking/bq_dataset_create/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions activity-tracking/bq_dataset_create/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# BigQuery Dataset Create

Checks if a dataset exists within a project.

```
bigquery = handles['project_0.BigqueryV2']
dataset_name = resources['project_0']['startup_script.bq_dataset_name']
```

In the above code, the resources definition accesses a startup script value.

## Googleapi

-[ ] BigqueryV2

37 changes: 37 additions & 0 deletions activity-tracking/bq_dataset_create/stable/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
resource "local_file" "activity_tracking_step" {
content = <<-EOF
# Objective: Verifies the creation of the bucket
def ${var.step_name}(handles:, maximum_score:, resources:)

# Service handle initialization
bigquery = handles['project_0.BigqueryV2']
dataset_name = resources['project_0']['startup_script.bq_dataset_name']

# Assessment score and status checker hash variable
ret_hash = { :score => 0, :message => "", :student_message => ""}

# Assessment specific variables
isAvailable = false

# Get the object information
dataset = bigquery.get_dataset(dataset_name.to_s) || []

# Check the object exists
if dataset
isAvailable = true
end

if isAvailable
ret_hash = { :score => maximum_score, :message => "Assessment completed!", :student_message => "Assessment completed!"}
else
error_message = 'Please create the BigQuery dataset.'
ret_hash[:message] = error_message
ret_hash[:student_message] = error_message
end

return ret_hash
end
EOF
filename = "${var.step_name}.rb"
}

5 changes: 5 additions & 0 deletions activity-tracking/bq_dataset_create/stable/variable.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
variable "step_name" {
type = string
description = "The name of the step to be created"
default = "bigquery_dataset_check"
}
21 changes: 21 additions & 0 deletions activity-tracking/fw_rule_create/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions activity-tracking/fw_rule_create/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Firewall Rule Exists

Checks if a Firewall Rule exists within a project.

```
compute = handles['project_0.ComputeV1']
fw_rule_name = resources['project_0']['startup_script.fw_rule_name']
```

In the above code, the resources definition accesses a startup script value.

## Googleapi

-[ ] ComputeV1

35 changes: 35 additions & 0 deletions activity-tracking/fw_rule_create/stable/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
resource "local_file" "activity_tracking_step" {
content = <<-EOF
# Objective: Check if a firewall rule has been defined within the project
def ${var.step_name}(handles:, maximum_score:, resources:)

compute = handles['project_0.ComputeV1']
fw_rule_name = resources['project_0']['startup_script.fw_rule_name']

# Assessment specific variables
isAvailable = false

# Assessment score and status checker hash variable
ret_hash = { :score => 0, :message => "", :student_message => ""}

# Get the object information
resource_object = compute.get_firewall(fw_rule_name.to_s) || []

# Check the object exists
if resource_object
isAvailable = true
end

if isAvailable
ret_hash = { :score => maximum_score, :message => "Assessment completed!", :student_message => "Assessment completed!"}
else
error_message = 'Please create a firewall rule.'
ret_hash[:message] = error_message
ret_hash[:student_message] = error_message
end
return ret_hash
end
EOF
filename = "${var.step_name}.rb"
}

5 changes: 5 additions & 0 deletions activity-tracking/fw_rule_create/stable/variable.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
variable "step_name" {
type = string
description = "The name of the step to be created"
default = "fw_rule_check"
}
21 changes: 21 additions & 0 deletions activity-tracking/gcs_bucket_create/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions activity-tracking/gcs_bucket_create/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Cloud Storage Bucket Create

Checks if a Bucket exists within a project.

```
storage = handles['project_0.StorageV1']
bucket_name = resources['project_0']['startup_script.bucket']
```

In the above code, the resources definition accesses a startup script value.

## Googleapi

-[ ] StorageV1

35 changes: 35 additions & 0 deletions activity-tracking/gcs_bucket_create/stable/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
resource "local_file" "activity_tracking_step" {
content = <<-EOF
# Objective: Verifies the creation of the bucket
def ${var.step_name}(handles:, maximum_score:, resources:)

storage = handles['project_0.StorageV1']
bucket_name = resources['project_0']['startup_script.bucket']

# Assessment specific variables
isAvailable = false

# Assessment score and status checker hash variable
ret_hash = { :score => 0, :message => "", :student_message => ""}

# Get the object information
resource_object = storage.get_bucket(bucket_name.to_s) || []

# Validate object exists
if resource_object
isAvailable = true
end

if isAvailable
ret_hash = { :score => maximum_score, :message => "Assessment completed!", :student_message => "Assessment completed!"}
else
error_message = 'Please create a storage bucket.'
ret_hash[:message] = error_message
ret_hash[:student_message] = error_message
end
return ret_hash
end
EOF
filename = "${var.step_name}.rb"
}

5 changes: 5 additions & 0 deletions activity-tracking/gcs_bucket_create/stable/variable.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
variable "step_name" {
type = string
description = "The name of the step to be created"
default = "storage_bucket_check"
}
21 changes: 21 additions & 0 deletions activity-tracking/log_sink_create/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions activity-tracking/log_sink_create/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Log Sink Create

Checks if a Log Sink exists within a project.

```
log_sink = handles['project_0.LoggingV2']
log_sink_name = 'projects/' + log_sink.project + '/sinks/' + resources['project_0']['startup_script.log_sink_name']
```

In the above code, the resources definition accesses a startup script value.

## Googleapi

-[ ] LoggingV2

35 changes: 35 additions & 0 deletions activity-tracking/log_sink_create/stable/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
resource "local_file" "activity_tracking_step" {
content = <<-EOF
# Objective: Check if a log sink has been created
def ${var.step_name}(handles:, maximum_score:, resources:)

log_sink = handles['project_0.LoggingV2']
log_sink_name = 'projects/' + log_sink.project + '/sinks/' + resources['project_0']['startup_script.log_sink_name']

# Assessment specific variables
isAvailable = false

# Assessment score and status checker hash variable
ret_hash = { :score => 0, :message => "", :student_message => ""}

# Get the object information
resource_object = log_sink.get_sink(log_sink_name.to_s) || []

# Validate object exists
if resource_object
isAvailable = true
end

if isAvailable
ret_hash = { :score => maximum_score, :message => "Assessment completed!", :student_message => "Assessment completed!"}
else
error_message = 'Please create a log sink.'
ret_hash[:message] = error_message
ret_hash[:student_message] = error_message
end
return ret_hash
end
EOF
filename = "${var.step_name}.rb"
}

5 changes: 5 additions & 0 deletions activity-tracking/log_sink_create/stable/variable.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
variable "step_name" {
type = string
description = "The name of the step to be created"
default = "log_sink_check"
}
Loading
Loading