diff --git a/docs/tables/gcp_compute_machine_type.md b/docs/tables/gcp_compute_machine_type.md index 3f0ccd13..4d083a02 100644 --- a/docs/tables/gcp_compute_machine_type.md +++ b/docs/tables/gcp_compute_machine_type.md @@ -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; +``` \ No newline at end of file diff --git a/gcp-test/tests/gcp_compute_machine_type/test-get-query.sql b/gcp-test/tests/gcp_compute_machine_type/test-get-query.sql index 1fd70a31..ca3c0cb3 100644 --- a/gcp-test/tests/gcp_compute_machine_type/test-get-query.sql +++ b/gcp-test/tests/gcp_compute_machine_type/test-get-query.sql @@ -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 }}'; \ No newline at end of file +where name = '{{ output.machine_type.value }}' and zone = 'us-east1-b'; \ No newline at end of file diff --git a/gcp-test/tests/gcp_compute_machine_type/test-list-query.sql b/gcp-test/tests/gcp_compute_machine_type/test-list-query.sql index dab2b851..949f7cc5 100644 --- a/gcp-test/tests/gcp_compute_machine_type/test-list-query.sql +++ b/gcp-test/tests/gcp_compute_machine_type/test-list-query.sql @@ -1,3 +1,3 @@ select name, title, akas from gcp.gcp_compute_machine_type -where akas::text = '["{{ output.resource_aka.value }}"]'; \ No newline at end of file +where akas::text = '["{{ output.resource_aka.value }}"]' and zone = 'us-east1-b'; \ No newline at end of file diff --git a/gcp-test/tests/gcp_compute_machine_type/test-turbot-query.sql b/gcp-test/tests/gcp_compute_machine_type/test-turbot-query.sql index f3976c75..dfc4d0af 100644 --- a/gcp-test/tests/gcp_compute_machine_type/test-turbot-query.sql +++ b/gcp-test/tests/gcp_compute_machine_type/test-turbot-query.sql @@ -1,3 +1,3 @@ select title, akas from gcp.gcp_compute_machine_type -where name = '{{ output.machine_type.value }}'; \ No newline at end of file +where name = '{{ output.machine_type.value }}' and zone = 'us-east1-b'; \ No newline at end of file diff --git a/gcp-test/tests/gcp_compute_machine_type/variables.tf b/gcp-test/tests/gcp_compute_machine_type/variables.tf index 63153964..07dc9059 100644 --- a/gcp-test/tests/gcp_compute_machine_type/variables.tf +++ b/gcp-test/tests/gcp_compute_machine_type/variables.tf @@ -41,7 +41,7 @@ output "machine_type" { } output "zone" { - value = "us-central1-c" + value = "us-east1-b" } output "resource_aka" { diff --git a/gcp/table_gcp_compute_machine_type.go b/gcp/table_gcp_compute_machine_type.go index e9e9abe6..01e8b794 100644 --- a/gcp/table_gcp_compute_machine_type.go +++ b/gcp/table_gcp_compute_machine_type.go @@ -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{ { @@ -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 @@ -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 { @@ -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 { @@ -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 }