Skip to content

Commit

Permalink
Fixed the table gcp_compute_machine_type not support t2a machine type…
Browse files Browse the repository at this point in the history
… query Closes #466 (#480)
  • Loading branch information
ParthaI authored Aug 17, 2023
1 parent 1b20fe4 commit 75e0de1
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 9 deletions.
14 changes: 14 additions & 0 deletions docs/tables/gcp_compute_machine_type.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,17 @@ from
gcp_compute_machine_type,
jsonb_array_elements(accelerators) as a;
```

### Display the categorization of machine types by zone

```sql
select
name,
zone,
count(name) as numbers_of_machine_type
from
gcp_compute_machine_type
group by
name,
zone;
```
2 changes: 1 addition & 1 deletion gcp-test/tests/gcp_compute_machine_type/test-get-query.sql
Original file line number Diff line number Diff line change
@@ -1,3 +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 }}';
where name = '{{ output.machine_type.value }}' and zone = 'us-east1-b';
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
select name, title, akas
from gcp.gcp_compute_machine_type
where akas::text = '["{{ output.resource_aka.value }}"]';
where akas::text = '["{{ output.resource_aka.value }}"]' and zone = 'us-east1-b';
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
select title, akas
from gcp.gcp_compute_machine_type
where name = '{{ output.machine_type.value }}';
where name = '{{ output.machine_type.value }}' and zone = 'us-east1-b';
2 changes: 1 addition & 1 deletion gcp-test/tests/gcp_compute_machine_type/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ output "machine_type" {
}

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

output "resource_aka" {
Expand Down
36 changes: 31 additions & 5 deletions gcp/table_gcp_compute_machine_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,18 @@ func tableGcpComputeMachineType(ctx context.Context) *plugin.Table {
Name: "gcp_compute_machine_type",
Description: "GCP Compute Machine Type",
Get: &plugin.GetConfig{
KeyColumns: plugin.SingleColumn("name"),
KeyColumns: plugin.AllColumns([]string{"name", "zone"}),
Hydrate: getComputeMachineType,
},
List: &plugin.ListConfig{
Hydrate: listComputeMachineTypes,
ParentHydrate: listComputeZones,
Hydrate: listComputeMachineTypes,
KeyColumns: plugin.KeyColumnSlice{
{
Name: "zone",
Require: plugin.Optional,
},
},
},
Columns: []*plugin.Column{
{
Expand Down Expand Up @@ -86,15 +93,26 @@ func tableGcpComputeMachineType(ctx context.Context) *plugin.Table {
Description: "The type of the resource. Always compute#machineType for machine types.",
Type: proto.ColumnType_STRING,
},
{
Name: "zone",
Description: "The name of the zone where the machine type resides, such as us-central1-a.",
Type: proto.ColumnType_STRING,
},
{
Name: "accelerators",
Description: "A list of accelerator configurations assigned to this machine type.",
Type: proto.ColumnType_JSON,
},
{
Name: "deprecated",
Description: "The deprecation status associated with this machine type. Only applicable if the machine type is unavailable.",
Type: proto.ColumnType_JSON,
},
{
Name: "scratch_disks",
Description: "A list of extended scratch disks assigned to the instance.",
Type: proto.ColumnType_JSON,
Transform: transform.FromField("MachineType.Accelerators"),
},

// Steampipe standard columns
Expand Down Expand Up @@ -127,6 +145,14 @@ func tableGcpComputeMachineType(ctx context.Context) *plugin.Table {
func listComputeMachineTypes(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
plugin.Logger(ctx).Trace("listComputeMachineTypes")

zoneDetails := h.Item.(*compute.Zone)
zoneName := d.EqualsQualString("zone")

// Restrict API call for other zones
if zoneName != "" && zoneName != zoneDetails.Name {
return nil, nil
}

// Create Service Connection
service, err := ComputeService(ctx, d)
if err != nil {
Expand All @@ -150,7 +176,7 @@ func listComputeMachineTypes(ctx context.Context, d *plugin.QueryData, h *plugin
return nil, err
}
project := projectId.(string)
zone := "us-central1-c"
zone := zoneDetails.Name

resp := service.MachineTypes.List(project, zone).MaxResults(*pageSize)
if err := resp.Pages(ctx, func(page *compute.MachineTypeList) error {
Expand Down Expand Up @@ -189,11 +215,11 @@ func getComputeMachineType(ctx context.Context, d *plugin.QueryData, h *plugin.H
return nil, err
}
project := projectId.(string)
zone := "us-central1-c"
machineTypeName := d.EqualsQuals["name"].GetStringValue()
zone := d.EqualsQuals["zone"].GetStringValue()

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

Expand Down

0 comments on commit 75e0de1

Please sign in to comment.