Skip to content

Commit

Permalink
Add table gcp_redis_instance. Closes #478 (#482)
Browse files Browse the repository at this point in the history
Co-authored-by: Ved misra <47312748+misraved@users.noreply.github.com>
  • Loading branch information
madhushreeray30 and misraved authored Aug 24, 2023
1 parent 1950e41 commit e931285
Show file tree
Hide file tree
Showing 7 changed files with 706 additions and 93 deletions.
131 changes: 131 additions & 0 deletions docs/tables/gcp_redis_instance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
# Table: gcp_redis_instance

In Memorystore for Redis, an instance refers to an in-memory Redis data store. Memorystore for Redis Instances are also referred to as Redis instances. When you create a new Memorystore for Redis instance, you are creating a new Redis data store.

## Examples

### Basic info

```sql
select
name,
display_name,
create_time,
location_id,
memory_size_gb,
reserved_ip_range
from
gcp_redis_instance;
```

### List instances that have authentication enabled

```sql
select
name,
display_name,
create_time,
location_id,
memory_size_gb,
reserved_ip_range
from
gcp_redis_instance
where
auth_enabled;
```

### List instances created in the last 7 days

```sql
select
name,
display_name,
create_time,
location_id,
memory_size_gb,
reserved_ip_range
from
gcp_redis_instance
where
create_time >= current_timestamp - interval '7 days';
```

### List the node details of each instance

```sql
select
name,
display_name,
create_time,
location_id,
jsonb_pretty(nodes) as instance_nodes
from
gcp_redis_instance
where
name = 'instance-test'
and location_id = 'europe-west3-c';
```

### List instances encrypted with customer-managed keys

```sql
select
name,
display_name,
create_time,
location_id,
memory_size_gb,
reserved_ip_range
from
gcp_redis_instance
where
customer_managed_key is not null;
```

### List instances that have transit mode disabled

```sql
select
name,
display_name,
create_time,
location_id,
memory_size_gb,
reserved_ip_range
from
gcp_redis_instance
where
transit_encryption_mode = 2;
```

### List the maintenance details of instances

```sql
select
name,
display_name,
create_time,
location_id,
maintenance_policy,
maintenance_schedule,
maintenance_version,
available_maintenance_versions
from
gcp_redis_instance;
```

### List instances with direct peering access

```sql
select
name,
display_name,
create_time,
location_id,
memory_size_gb,
reserved_ip_range
from
gcp_redis_instance
where
connect_mode = 1;
```
1 change: 1 addition & 0 deletions gcp/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func Plugin(ctx context.Context) *plugin.Plugin {
"gcp_pubsub_snapshot": tableGcpPubSubSnapshot(ctx),
"gcp_pubsub_subscription": tableGcpPubSubSubscription(ctx),
"gcp_pubsub_topic": tableGcpPubSubTopic(ctx),
"gcp_redis_instance": tableGcpRedisInstance(ctx),
"gcp_service_account": tableGcpServiceAccount(ctx),
"gcp_service_account_key": tableGcpServiceAccountKey(ctx),
"gcp_sql_backup": tableGcpSQLBackup(ctx),
Expand Down
59 changes: 59 additions & 0 deletions gcp/redis_location_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package gcp

import (
"context"

"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
"google.golang.org/api/iterator"
"google.golang.org/genproto/googleapis/cloud/location"
)

const matrixKeyRedisLocation = "redis-location"

// BuildRedisLocationList :: return a list of matrix items, one per region specified
func BuildRedisLocationList(ctx context.Context, d *plugin.QueryData) []map[string]interface{} {

// have we already created and cached the locations?
locationCacheKey := "RedisLocation"
if cachedData, ok := d.ConnectionManager.Cache.Get(locationCacheKey); ok {
plugin.Logger(ctx).Trace("listlocationDetails:", cachedData.([]map[string]interface{}))
return cachedData.([]map[string]interface{})
}

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

projectData, err := activeProject(ctx, d)
if err != nil {
return nil
}
project := projectData.Project
if project == "" {
return nil
}

req := &location.ListLocationsRequest{
Name: "projects/" + project,
PageSize: 100,
}

it := service.ListLocations(ctx, req)
matrix := []map[string]interface{}{}
for {
resp, err := it.Next()
if err != nil {
if err == iterator.Done {
break
}
return nil
}
obj := map[string]interface{}{matrixKeyRedisLocation: resp.LocationId}
matrix = append(matrix, obj)
}

d.ConnectionManager.Cache.Set(locationCacheKey, matrix)
return matrix
}
22 changes: 22 additions & 0 deletions gcp/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"google.golang.org/api/logging/v2"
"google.golang.org/api/monitoring/v3"
"google.golang.org/api/pubsub/v1"
"cloud.google.com/go/redis/apiv1"
"google.golang.org/api/serviceusage/v1"
"google.golang.org/api/storage/v1"

Expand Down Expand Up @@ -511,3 +512,24 @@ func KMSService(ctx context.Context, d *plugin.QueryData) (*cloudkms.Service, er
d.ConnectionManager.Cache.Set(serviceCacheKey, svc)
return svc, nil
}

// RedisService returns the service connection for GCP Redis service
func RedisService(ctx context.Context, d *plugin.QueryData) (*redis.CloudRedisClient, error) {
// have we already created and cached the service?
serviceCacheKey := "RedisService"
if cachedData, ok := d.ConnectionManager.Cache.Get(serviceCacheKey); ok {
return cachedData.(*redis.CloudRedisClient), nil
}

// To get config arguments from plugin config file
opts := setSessionConfig(ctx, d.Connection)

// so it was not in cache - create service
svc, err := redis.NewCloudRedisClient(ctx, opts...)
if err != nil {
return nil, err
}

d.ConnectionManager.Cache.Set(serviceCacheKey, svc)
return svc, nil
}
Loading

0 comments on commit e931285

Please sign in to comment.