-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add gcp_compute_instance_group_manager table
- Loading branch information
Showing
5 changed files
with
337 additions
and
7 deletions.
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
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,71 @@ | ||
--- | ||
title: "Steampipe Table: gcp_compute_instance_group_manager - Query Google Cloud Compute Engine Instance Group Managers using SQL" | ||
description: "Allows users to query Google Cloud Compute Engine Instance Group Managers, providing insights into the configuration, status, and properties of these group managers." | ||
--- | ||
|
||
# Table: gcp_compute_instance_group_manager - Query Google Cloud Compute Engine Instance Group Managers using SQL | ||
|
||
Google Cloud Compute Engine Instance Group Managers manage [Managed Instance Groups (MIG)](https://cloud.google.com/compute/docs/instance-groups#managed_instance_groups). are ideal for highly available applications that require a lot of computing power and need to scale rapidly to meet demand. They offer a range of features including autoscaling, autohealing, regional (multiple zone) deployment, and automatic updating. | ||
|
||
## Table Usage Guide | ||
|
||
The `gcp_compute_instance_group_manager` table provides insights into instance group managers within Google Cloud Compute Engine. As a system administrator, you can explore group-specific details through this table, including configuration, associated instances, and autoscaling policies. Utilize it to monitor the status of your instance groups, manage load balancing, and plan for capacity adjustments. | ||
|
||
## Examples | ||
|
||
### Basic Info | ||
Discover the segments of your Google Cloud Platform (GCP) that contain instance group managers, gaining insights into aspects like size and location. This can help in project management and resource allocation within the GCP infrastructure. | ||
|
||
```sql+postgres | ||
select | ||
name, | ||
description, | ||
self_link, | ||
instance_group, | ||
location, | ||
akas, | ||
project | ||
from | ||
gcp_compute_instance_group_manager; | ||
``` | ||
|
||
```sql+sqlite | ||
select | ||
name, | ||
description, | ||
self_link, | ||
instance_group, | ||
location, | ||
akas, | ||
project | ||
from | ||
gcp_compute_instance_group_manager; | ||
``` | ||
|
||
|
||
### Get instance group details of each instance group manager | ||
Get the size of the instance groups managed by instance group managers. | ||
|
||
```sql+postgres | ||
select | ||
m.name, | ||
g.name as group_name, | ||
g.size as group_size | ||
from | ||
gcp_compute_instance_group_manager as m, | ||
gcp_compute_instance_group as g | ||
where | ||
m.instance_group ->> 'name' = g.name; | ||
``` | ||
|
||
```sql+sqlite | ||
select | ||
m.name, | ||
g.name as group_name, | ||
g.size as group_size | ||
from | ||
gcp_compute_instance_group_manager as m, | ||
gcp_compute_instance_group as g | ||
where | ||
m.instance_group -> 'name' = g.name; | ||
``` |
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
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,261 @@ | ||
package gcp | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
|
||
"github.com/turbot/go-kit/types" | ||
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto" | ||
"github.com/turbot/steampipe-plugin-sdk/v5/plugin" | ||
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform" | ||
"google.golang.org/api/compute/v1" | ||
) | ||
|
||
func tableGcpComputeInstanceGroupManager(ctx context.Context) *plugin.Table { | ||
return &plugin.Table{ | ||
Name: "gcp_compute_instance_group_manager", | ||
Description: "GCP Compute Instance Group Manager", | ||
Get: &plugin.GetConfig{ | ||
KeyColumns: plugin.SingleColumn("name"), | ||
Hydrate: getComputeInstanceGroupManager, | ||
}, | ||
List: &plugin.ListConfig{ | ||
Hydrate: listComputeInstanceGroupManager, | ||
}, | ||
Columns: []*plugin.Column{ | ||
{ | ||
Name: "name", | ||
Description: "The name of the instance group manager.", | ||
Type: proto.ColumnType_STRING, | ||
}, | ||
{ | ||
Name: "id", | ||
Description: "The unique identifier for this instance group manager. This identifier is defined by the server.", | ||
Type: proto.ColumnType_INT, | ||
}, | ||
{ | ||
Name: "creation_timestamp", | ||
Description: "The timestamp when the instance group manager was created.", | ||
Type: proto.ColumnType_TIMESTAMP, | ||
}, | ||
{ | ||
Name: "description", | ||
Description: "An optional description of this resource. Provide this property when you create the resource.", | ||
Type: proto.ColumnType_STRING, | ||
}, | ||
{ | ||
Name: "fingerprint", | ||
Description: "The fingerprint of the instance group manager.", | ||
Type: proto.ColumnType_STRING, | ||
}, | ||
{ | ||
Name: "kind", | ||
Description: "The type of the resource. Always compute#instanceGroupManager for instance group managers.", | ||
Type: proto.ColumnType_STRING, | ||
}, | ||
{ | ||
Name: "region", | ||
Description: "The URL of the region where the instance group manager resides. Only applicable for regional resources.", | ||
Type: proto.ColumnType_STRING, | ||
}, | ||
{ | ||
Name: "region_name", | ||
Description: "The name of the region where the instance group manager resides. Only applicable for regional resources.", | ||
Type: proto.ColumnType_STRING, | ||
Transform: transform.FromField("Region").Transform(lastPathElement), | ||
}, | ||
{ | ||
Name: "self_link", | ||
Description: "The server-defined fully-qualified URL for this resource.", | ||
Type: proto.ColumnType_STRING, | ||
}, | ||
{ | ||
Name: "zone", | ||
Description: "The URL of the zone where the instance group manager resides.", | ||
Type: proto.ColumnType_STRING, | ||
}, | ||
{ | ||
Name: "named_ports", | ||
Description: "The named ports configured for the Instance Groups complementary to this Instance Group Manager.", | ||
Type: proto.ColumnType_JSON, | ||
Transform: transform.FromGo().NullIfZero(), | ||
}, | ||
{ | ||
Name: "instance_group", | ||
Description: "The instance group that is managed by this group manager.", | ||
Type: proto.ColumnType_JSON, | ||
Transform: transform.FromValue(), | ||
}, | ||
|
||
// zone_name is a simpler view of the zone, without the full path | ||
{ | ||
Name: "zone_name", | ||
Description: "The zone name in which the instance group manager resides.", | ||
Type: proto.ColumnType_STRING, | ||
Transform: transform.FromField("Zone").Transform(lastPathElement), | ||
}, | ||
|
||
// 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.From(instanceGroupManagerAka), | ||
}, | ||
|
||
// GCP standard columns | ||
{ | ||
Name: "location", | ||
Description: ColumnDescriptionLocation, | ||
Type: proto.ColumnType_STRING, | ||
Transform: transform.FromP(instanceGroupManagerLocation, "Location"), | ||
}, | ||
{ | ||
Name: "project", | ||
Description: ColumnDescriptionProject, | ||
Type: proto.ColumnType_STRING, | ||
Transform: transform.FromP(instanceGroupManagerLocation, "Project"), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
//// LIST FUNCTIONS | ||
|
||
func listComputeInstanceGroupManager(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { | ||
// Max limit is set as per documentation | ||
pageSize := types.Int64(500) | ||
limit := d.QueryContext.Limit | ||
if d.QueryContext.Limit != nil { | ||
if *limit < *pageSize { | ||
pageSize = limit | ||
} | ||
} | ||
|
||
// Get project details | ||
|
||
projectId, err := getProject(ctx, d, h) | ||
if err != nil { | ||
return nil, err | ||
} | ||
project := projectId.(string) | ||
|
||
// Create Service Connection | ||
service, err := ComputeService(ctx, d) | ||
if err != nil { | ||
plugin.Logger(ctx).Error("gcp_compute_instance_group_manager.listComputeInstanceGroupManager", "service_creation_err", err) | ||
return nil, err | ||
} | ||
|
||
resp := service.InstanceGroupManagers.AggregatedList(project).MaxResults(*pageSize) | ||
if err := resp.Pages(ctx, func(page *compute.InstanceGroupManagerAggregatedList) error { | ||
for _, item := range page.Items { | ||
for _, group := range item.InstanceGroupManagers { | ||
d.StreamListItem(ctx, group) | ||
|
||
// Check if context has been cancelled or if the limit has been hit (if specified) | ||
// if there is a limit, it will return the number of rows required to reach this limit | ||
if d.RowsRemaining(ctx) == 0 { | ||
page.NextPageToken = "" | ||
return nil | ||
} | ||
} | ||
} | ||
return nil | ||
}); err != nil { | ||
plugin.Logger(ctx).Error("gcp_compute_instance_group_manager.listComputeInstanceGroupManager", "api_err", err) | ||
return nil, err | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
//// HYDRATE FUNCTIONS | ||
|
||
func getComputeInstanceGroupManager(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { | ||
// Get project details | ||
projectId, err := getProject(ctx, d, h) | ||
if err != nil { | ||
return nil, err | ||
} | ||
project := projectId.(string) | ||
|
||
var group compute.InstanceGroupManager | ||
name := d.EqualsQuals["name"].GetStringValue() | ||
|
||
// Create Service Connection | ||
service, err := ComputeService(ctx, d) | ||
if err != nil { | ||
plugin.Logger(ctx).Error("gcp_compute_instance_group_manager.getComputeInstanceGroupManager", "service_creation_err", err) | ||
return nil, err | ||
} | ||
|
||
resp := service.InstanceGroupManagers.AggregatedList(project).Filter("name=" + name) | ||
if err := resp.Pages(ctx, func(page *compute.InstanceGroupManagerAggregatedList) error { | ||
for _, item := range page.Items { | ||
for _, i := range item.InstanceGroupManagers { | ||
group = *i | ||
} | ||
} | ||
return nil | ||
}, | ||
); err != nil { | ||
plugin.Logger(ctx).Error("gcp_compute_instance_group_manager.getComputeInstanceGroupManager", "api_err", err) | ||
return nil, err | ||
} | ||
|
||
// If the specified resource is not present, API does not return any not found errors | ||
if len(group.Name) < 1 { | ||
return nil, nil | ||
} | ||
|
||
return &group, nil | ||
} | ||
|
||
//// TRANSFORM FUNCTIONS | ||
|
||
func instanceGroupManagerAka(_ context.Context, d *transform.TransformData) (interface{}, error) { | ||
i := d.HydrateItem.(*compute.InstanceGroupManager) | ||
|
||
zoneName := getLastPathElement(types.SafeString(i.Zone)) | ||
regionName := getLastPathElement(types.SafeString(i.Region)) | ||
project := strings.Split(i.SelfLink, "/")[6] | ||
instanceGroupManagerName := types.SafeString(i.Name) | ||
|
||
var akas []string | ||
if zoneName == "" { | ||
akas = []string{"gcp://compute.googleapis.com/projects/" + project + "/regions/" + regionName + "/instanceGroupManagers/" + instanceGroupManagerName} | ||
} else { | ||
akas = []string{"gcp://compute.googleapis.com/projects/" + project + "/zones/" + zoneName + "/instanceGroupManagers/" + instanceGroupManagerName} | ||
} | ||
|
||
return akas, nil | ||
} | ||
|
||
func instanceGroupManagerLocation(_ context.Context, d *transform.TransformData) (interface{}, error) { | ||
i := d.HydrateItem.(*compute.InstanceGroupManager) | ||
param := d.Param.(string) | ||
|
||
zoneName := getLastPathElement(types.SafeString(i.Zone)) | ||
regionName := getLastPathElement(types.SafeString(i.Region)) | ||
project := strings.Split(i.SelfLink, "/")[6] | ||
|
||
locationData := map[string]string{ | ||
"Type": "ZONAL", | ||
"Location": zoneName, | ||
"Project": project, | ||
} | ||
|
||
if zoneName == "" { | ||
locationData["Type"] = "REGIONAL" | ||
locationData["Location"] = regionName | ||
} | ||
|
||
return locationData[param], nil | ||
} |