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 table gcp_compute_machine_type. Closes #219 #238

Merged
merged 7 commits into from
Jun 30, 2021
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
64 changes: 64 additions & 0 deletions docs/tables/gcp_compute_machine_type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Table: gcp_compute_machine_type

A machine type is a set of virtualized hardware resources available to a virtual machine (VM) instance, including the system memory size, virtual CPU (vCPU) count, and persistent disk limits.

In Compute Engine, machine types are grouped and curated by families for different workloads. Compute Engine offers general-purpose, memory-optimized, compute-optimized, and accelerated-optimized families.

## Examples

### Basic info

```sql
select
name,
id,
description,
guest_cpus,
maximum_persistent_disks,
maximum_persistent_disks_size_gb
from
gcp_compute_machine_type;
```


### List machine types with more than 48 cores

```sql
select
name,
id,
description,
guest_cpus
from
gcp_compute_machine_type
where
guest_cpus >= 48;
```


### List machine types with shared CPUs

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be CPU ? or virtual CPU (vCPU) ?


```sql
select
name,
id,
is_shared_cpu
from
gcp_compute_machine_type
where
is_shared_cpu;
```


### Get accelerator configurations assigned to each machine type

```sql
select
name,
id,
a -> 'guestAcceleratorCount' as guest_accelerator_count,
a ->> 'guestAcceleratorType' as guest_accelerator_type
from
gcp_compute_machine_type,
jsonb_array_elements(accelerators) as a;
```
Empty file.
14 changes: 14 additions & 0 deletions gcp-test/tests/gcp_compute_machine_type/test-get-expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"akas": ["{{ output.resource_aka.value }}"],
"guest_cpus": 48,
"image_space_gb": 0,
"is_shared_cpu": false,
"kind": "compute#machineType",
"maximum_persistent_disks": 128,
"maximum_persistent_disks_size_gb": 263168,
"memory_mb": 348160,
"name": "{{ output.machine_type.value }}",
"title": "{{ output.machine_type.value }}"
}
]
3 changes: 3 additions & 0 deletions gcp-test/tests/gcp_compute_machine_type/test-get-query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
select name, title, akas, kind, guest_cpus, memory_mb, image_space_gb, maximum_persistent_disks, maximum_persistent_disks_size_gb, is_shared_cpu
from gcp.gcp_compute_machine_type
where name = '{{ output.machine_type.value }}';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"akas": ["{{ output.resource_aka.value }}"],
"name": "{{ output.machine_type.value }}",
"title": "{{ output.machine_type.value }}"
}
]
3 changes: 3 additions & 0 deletions gcp-test/tests/gcp_compute_machine_type/test-list-query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
select name, title, akas
from gcp.gcp_compute_machine_type
where akas::text = '["{{ output.resource_aka.value }}"]';
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
from gcp.gcp_compute_machine_type
where name = '{{ output.machine_type.value }}.dd';
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"akas": ["{{ output.resource_aka.value }}"],
"title": "{{ output.machine_type.value }}"
}
]
3 changes: 3 additions & 0 deletions gcp-test/tests/gcp_compute_machine_type/test-turbot-query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
select title, akas
from gcp.gcp_compute_machine_type
where name = '{{ output.machine_type.value }}';
1 change: 1 addition & 0 deletions gcp-test/tests/gcp_compute_machine_type/variables.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
49 changes: 49 additions & 0 deletions gcp-test/tests/gcp_compute_machine_type/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

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}"
}
}

output "machine_type" {
value = "a2-highgpu-4g"
}

output "zone" {
value = "us-central1-c"
}

output "resource_aka" {
value = "gcp://compute.googleapis.com/projects/${var.gcp_project}/machineTypes/a2-highgpu-4g"
}
1 change: 1 addition & 0 deletions gcp/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func Plugin(ctx context.Context) *plugin.Plugin {
"gcp_compute_image": tableGcpComputeImage(ctx),
"gcp_compute_instance": tableGcpComputeInstance(ctx),
"gcp_compute_instance_template": tableGcpComputeInstanceTemplate(ctx),
"gcp_compute_machine_type": tableGcpComputeMachineType(ctx),
"gcp_compute_network": tableGcpComputeNetwork(ctx),
"gcp_compute_node_group": tableGcpComputeNodeGroup(ctx),
"gcp_compute_node_template": tableGcpComputeNodeTemplate(ctx),
Expand Down
201 changes: 201 additions & 0 deletions gcp/table_gcp_compute_machine_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
package gcp

import (
"context"
"strings"

"github.com/turbot/steampipe-plugin-sdk/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/plugin"
"github.com/turbot/steampipe-plugin-sdk/plugin/transform"
"google.golang.org/api/compute/v1"
)

//// TABLE DEFINITION

func tableGcpComputeMachineType(ctx context.Context) *plugin.Table {
return &plugin.Table{
Name: "gcp_compute_machine_type",
Description: "GCP Compute Machine Type",
Get: &plugin.GetConfig{
KeyColumns: plugin.SingleColumn("name"),
Hydrate: getComputeMachineType,
},
List: &plugin.ListConfig{
Hydrate: listComputeMachineTypes,
},
Columns: []*plugin.Column{
{
Name: "name",
Description: "Name of the resource.",
Type: proto.ColumnType_STRING,
},
{
Name: "id",
Description: "An unique identifier for the resource. This identifier is defined by the server.",
Type: proto.ColumnType_INT,
},
{
Name: "self_link",
Description: "Server-defined URL for the resource.",
Type: proto.ColumnType_STRING,
},
{
Name: "creation_timestamp",
Description: "Creation timestamp in RFC3339 text format.",
Type: proto.ColumnType_TIMESTAMP,
Transform: transform.FromGo().NullIfZero(),
},
{
Name: "description",
Description: "An optional textual description of the resource.",
Type: proto.ColumnType_STRING,
},
{
Name: "guest_cpus",
Description: "The number of virtual CPUs that are available to the instance.",
Type: proto.ColumnType_INT,
},
{
Name: "memory_mb",
Description: "The amount of physical memory available to the instance, defined in MB.",
Type: proto.ColumnType_INT,
},
{
Name: "image_space_gb",
Description: "The amount of memory available for image ig GB.",
Type: proto.ColumnType_INT,
},
{
Name: "maximum_persistent_disks",
Description: "Maximum persistent disks allowed.",
Type: proto.ColumnType_INT,
},
{
Name: "maximum_persistent_disks_size_gb",
Description: "Maximum total persistent disks size (GB) allowed.",
Type: proto.ColumnType_INT,
},
{
Name: "is_shared_cpu",
Description: "Whether this machine type has a shared CPU.",
Type: proto.ColumnType_BOOL,
},
{
Name: "kind",
Description: "The type of the resource. Always compute#machineType for machine types.",
Type: proto.ColumnType_STRING,
},
{
Name: "accelerators",
Description: "A list of accelerator configurations assigned to this machine type.",
Type: proto.ColumnType_JSON,
},
{
Name: "scratch_disks",
Description: "A list of extended scratch disks assigned to the instance.",
Type: proto.ColumnType_JSON,
},

// Steampipe standard columns
{
Name: "title",
Description: ColumnDescriptionTitle,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Name"),
},
{
Name: "akas",
Description: ColumnDescriptionAkas,
Type: proto.ColumnType_JSON,
Transform: transform.FromP(machineTypeTurbotData, "Akas"),
},

// GCP standard columns
{
Name: "project",
Description: ColumnDescriptionProject,
Type: proto.ColumnType_STRING,
Transform: transform.FromP(machineTypeTurbotData, "Project"),
},
},
}
}

//// LIST FUNCTION

func listComputeMachineTypes(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
plugin.Logger(ctx).Trace("listComputeMachineTypes")

// Create Service Connection
service, err := ComputeService(ctx, d)
if err != nil {
return nil, err
}

// Get project details
projectData, err := activeProject(ctx, d)
if err != nil {
return nil, err
}
project := projectData.Project
zone := "us-central1-c"

resp := service.MachineTypes.List(project, zone)
if err := resp.Pages(ctx, func(page *compute.MachineTypeList) error {
for _, machineType := range page.Items {
d.StreamListItem(ctx, machineType)
}
return nil
}); err != nil {
return nil, err
}

return nil, nil
}

//// HYDRATE FUNCTIONS

func getComputeMachineType(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
plugin.Logger(ctx).Trace("getComputeMachineType")
// Create Service Connection
service, err := ComputeService(ctx, d)
if err != nil {
return nil, err
}

// Get project details
projectData, err := activeProject(ctx, d)
if err != nil {
return nil, err
}
project := projectData.Project
zone := "us-central1-c"
machineTypeName := d.KeyColumnQuals["name"].GetStringValue()

// Return nil, if no input provided
if machineTypeName == "" {
return nil, nil
}

resp, err := service.MachineTypes.Get(project, zone, machineTypeName).Do()
if err != nil {
return nil, err
}
return resp, nil
}

//// TRANSFORM FUNCTIONS

func machineTypeTurbotData(_ context.Context, d *transform.TransformData) (interface{}, error) {
data := d.HydrateItem.(*compute.MachineType)
param := d.Param.(string)

project := strings.Split(data.SelfLink, "/")[6]

turbotData := map[string]interface{}{
"Project": project,
"Akas": []string{"gcp://compute.googleapis.com/projects/" + project + "/machineTypes/" + data.Name},
}

return turbotData[param], nil
}