Skip to content

Commit

Permalink
Add compute instance template table. Closes #84 (#93)
Browse files Browse the repository at this point in the history
* Add gcp_compute_instance_template template table. Closes #84
* Add tests to verify the table
* Add docs

Co-authored-by: Subhajit Kumar Mondal <subhajit@turbot.com>
Co-authored-by: rajeshbal65 <rajesh.bal@turbot.com>
  • Loading branch information
3 people committed Feb 10, 2021
1 parent 864dfda commit 44efc87
Show file tree
Hide file tree
Showing 20 changed files with 591 additions and 1 deletion.
83 changes: 83 additions & 0 deletions docs/tables/gcp_compute_instance_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Table: gcp_compute_instance_template

An instance template is a resource that use to create virtual machine (VM) instances and managed instance groups (MIGs). Instance templates define the machine type, boot disk image or container image, labels, and other instance properties.

## Examples

### List of c2-standard-4 machine type instance template

```sql
select
name,
id,
instance_machine_type
from
gcp_compute_instance_template
where
instance_machine_type = 'c2-standard-4';
```


### Boot Disk info of each instance template
```sql
select
name,
id,
disk ->> 'deviceName' as disk_device_name,
disk -> 'initializeParams' ->> 'diskType' as disk_type,
disk -> 'initializeParams' ->> 'diskSizeGb' as disk_size_gb,
split_part(
disk -> 'initializeParams' ->> 'sourceImage',
'/',
5
) as source_image,
disk ->> 'mode' as mode
from
gcp_compute_instance_template,
jsonb_array_elements(instance_disks) as disk;
```


### List of SPECIFIC_RESERVATION Instance type instance template
```sql
select
name,
id,
instance_reservation_affinity ->> 'consumeReservationType' as consume_reservation_type
from
gcp_compute_instance_template
where
instance_reservation_affinity ->> 'consumeReservationType' = 'SPECIFIC_RESERVATION';
```


### Network interface info of each instance template

```sql
select
name,
id,
i ->> 'name' as name,
split_part(i ->> 'network', '/', 10) as network_name,
p ->> 'name' as access_config_name,
p ->> 'networkTier' as access_config_network_tier,
p ->> 'type' as access_config_type
from
gcp_compute_instance_template,
jsonb_array_elements(instance_network_interfaces) as i,
jsonb_array_elements(i -> 'accessConfigs') as p;
```


### List of instance templates where instance_can_ip_forward is true

```sql
select
name,
id,
instance_can_ip_forward
from
gcp_compute_instance_template
where
instance_can_ip_forward;
```
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
[
{
"description": "Test instance template to verify the table.",
"instance_can_ip_forward": false,
"instance_description": "A dummy description",
"instance_disks": [
{
"autoDelete": true,
"boot": true,
"deviceName": "persistent-disk-0",
"initializeParams": {
"diskType": "pd-standard",
"sourceImage": "projects/debian-cloud/global/images/family/debian-9"
},
"interface": "SCSI",
"kind": "compute#attachedDisk",
"mode": "READ_WRITE",
"type": "PERSISTENT"
}
],
"instance_machine_type": "f1-micro",
"instance_metadata": {
"fingerprint": "{{ output.metadata_fingerprint.value }}",
"items": [
{
"key": "foo",
"value": "bar"
}
],
"kind": "compute#metadata"
},
"instance_network_interfaces": [
{
"kind": "compute#networkInterface",
"name": "nic0",
"network": "{{ output.network.value }}"
}
],
"instance_scheduling": {
"automaticRestart": true,
"onHostMaintenance": "MIGRATE"
},
"instance_service_accounts": [
{
"email": "{{ output.service_account.value }}",
"scopes": ["https://www.googleapis.com/auth/cloud-platform"]
}
],
"instance_tags": {
"items": ["bar", "foo"]
},
"kind": "compute#instanceTemplate",
"location": "global",
"name": "{{ resourceName }}",
"project": "{{ output.project_id.value }}",
"self_link": "https://www.googleapis.com/compute/v1/projects/{{ output.project_id.value }}/global/instanceTemplates/{{ resourceName }}"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
select name, description, instance_can_ip_forward, instance_description, instance_disks, instance_machine_type, instance_metadata, instance_network_interfaces, instance_scheduling, instance_service_accounts, instance_tags, kind, location, project, self_link
from gcp.gcp_compute_instance_template
where name = '{{ resourceName }}'
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"description": "Test instance template to verify the table.",
"instance_description": "A dummy description",
"instance_machine_type": "f1-micro",
"kind": "compute#instanceTemplate",
"name": "{{ resourceName }}",
"self_link": "https://www.googleapis.com/compute/v1/projects/{{ output.project_id.value }}/global/instanceTemplates/{{ resourceName }}"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
select name, instance_description, description, kind, instance_machine_type, self_link
from gcp.gcp_compute_instance_template
where name = '{{ resourceName }}'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
null
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
select name, id, description
from gcp.gcp_compute_instance_template
where name = ''
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"description": "Test instance template to verify the table.",
"name": "{{ resourceName }}"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
select name, description
from gcp.gcp_compute_instance_template
where title = '{{ resourceName }}'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
null
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
select name, id, description
from gcp.gcp_compute_instance_template
where name = 'dummy-{{ resourceName }}'
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"akas": ["{{ output.resource_aka.value }}"],
"tags": {
"name": "{{ resourceName }}"
},
"title": "{{ resourceName }}"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
select tags, title, akas
from gcp.gcp_compute_instance_template
where name = '{{ resourceName }}'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
124 changes: 124 additions & 0 deletions gcp-test/tests/gcp_compute_instance_template/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@

variable "resource_name" {
type = string
default = "turbot-test-20200125-create-update"
description = "Name of the resource used throughout the test."
}

variable "gcp_project" {
type = string
default = "niteowl-aaa"
description = "GCP project used for the test."
}

variable "gcp_region" {
type = string
default = "us-east1"
description = "GCP region used for the test."
}

variable "gcp_zone" {
type = string
default = "us-east1-b"
}

provider "google" {
project = var.gcp_project
region = var.gcp_region
zone = var.gcp_zone
}

data "google_client_config" "current" {}

data "null_data_source" "resource" {
inputs = {
scope = "gcp://cloudresourcemanager.googleapis.com/projects/${data.google_client_config.current.project}"
}
}

resource "google_compute_network" "named_test_resource" {
name = var.resource_name
}

resource "google_service_account" "named_test_resource" {
account_id = var.resource_name
display_name = var.resource_name
}

resource "google_compute_instance_template" "named_test_resource" {
name = var.resource_name
description = "Test instance template to verify the table."

tags = ["foo", "bar"]

labels = {
name = var.resource_name
}

instance_description = "A dummy description"
machine_type = "f1-micro"
can_ip_forward = false

scheduling {
automatic_restart = true
on_host_maintenance = "MIGRATE"
}

// Create a new boot disk from an image
disk {
source_image = "debian-cloud/debian-9"
auto_delete = true
boot = true
}

network_interface {
network = google_compute_network.named_test_resource.name
}

metadata = {
foo = "bar"
}

service_account {
# Google recommends custom service accounts that have cloud-platform scope and permissions granted via IAM Roles.
email = google_service_account.named_test_resource.email
scopes = ["cloud-platform"]
}
}

data "google_compute_image" "my_image" {
family = "debian-9"
project = "debian-cloud"
}

output "resource_aka" {
value = "gcp://compute.googleapis.com/${google_compute_instance_template.named_test_resource.id}"
}

output "resource_name" {
value = var.resource_name
}

output "resource_id" {
value = google_compute_instance_template.named_test_resource.id
}

output "self_link" {
value = google_compute_instance_template.named_test_resource.self_link
}

output "network" {
value = google_compute_network.named_test_resource.self_link
}

output "service_account" {
value = google_service_account.named_test_resource.email
}

output "metadata_fingerprint" {
value = google_compute_instance_template.named_test_resource.metadata_fingerprint
}

output "project_id" {
value = var.gcp_project
}
1 change: 1 addition & 0 deletions gcp/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func Plugin(ctx context.Context) *plugin.Plugin {
"gcp_compute_global_address": tableGcpComputeGlobalAddress(ctx),
"gcp_compute_global_forwarding_rule": tableGcpComputeGlobalForwardingRule(ctx),
"gcp_compute_instance": tableGcpComputeInstance(ctx),
"gcp_compute_instance_template": tableGcpComputeInstanceTemplate(ctx),
"gcp_compute_network": tableGcpComputeNetwork(ctx),
"gcp_compute_node_group": tableGcpComputeNodeGroup(ctx),
"gcp_compute_node_template": tableGcpComputeNodeTemplate(ctx),
Expand Down

0 comments on commit 44efc87

Please sign in to comment.