-
Notifications
You must be signed in to change notification settings - Fork 27
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ebe10e6
Add table gcp_compute_machine_type. CLoses #219
rajeshbal65 43364d5
made review chages
rajeshbal65 9ecdee4
update
rajeshbal65 2e5dd9a
Merge branch 'main' of github.com:turbot/steampipe-plugin-gcp into is…
Subhajit97 2d74e5c
Fixed column type and added check to return nil, if no input provided
Subhajit97 e624999
Resolved conflicts
Subhajit97 a9b4407
Update gcp_compute_machine_type.md
cbruno10 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
```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
14
gcp-test/tests/gcp_compute_machine_type/test-get-expected.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }}" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }}'; |
7 changes: 7 additions & 0 deletions
7
gcp-test/tests/gcp_compute_machine_type/test-list-expected.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }}" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }}"]'; |
1 change: 1 addition & 0 deletions
1
gcp-test/tests/gcp_compute_machine_type/test-notfound-expected.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
null |
3 changes: 3 additions & 0 deletions
3
gcp-test/tests/gcp_compute_machine_type/test-notfound-query.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
6 changes: 6 additions & 0 deletions
6
gcp-test/tests/gcp_compute_machine_type/test-turbot-expected.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
3
gcp-test/tests/gcp_compute_machine_type/test-turbot-query.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }}'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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) ?