Skip to content
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_project.Closes #199 #205

Merged
merged 7 commits into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/tables/gcp_project.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Table: gcp_project

A project organizes all your Google Cloud resources. A project consists of a set of users, a set of APIs and billing, authentication and monitoring settings for those APIs.

## Examples

### Basic info

```sql
select
name,
project_id,
project_number,
lifecycle_state,
create_time
from
gcp_project;
```
Empty file.
7 changes: 7 additions & 0 deletions gcp-test/tests/gcp_project/test-list-expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"name": "{{ output.current_project_name.value }}",
"project_id": "{{ output.project_id.value }}",
"project_number": {{ output.project_number.value }}
}
]
8 changes: 8 additions & 0 deletions gcp-test/tests/gcp_project/test-list-query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
select
name,
project_id,
project_number
from
gcp_project
where
project_id = '{{ output.project_id.value }}';
1 change: 1 addition & 0 deletions gcp-test/tests/gcp_project/test-notfound-expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
null
7 changes: 7 additions & 0 deletions gcp-test/tests/gcp_project/test-notfound-query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
select
name,
title
from
gcp_project
where
name = 'dummy-{{ resourceName }}';
7 changes: 7 additions & 0 deletions gcp-test/tests/gcp_project/test-turbot-expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"akas": ["{{ output.resource_aka.value }}"],
"name": "{{ output.current_project_name.value }}",
"title": "{{ output.current_project_name.value }}"
}
]
8 changes: 8 additions & 0 deletions gcp-test/tests/gcp_project/test-turbot-query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
select
akas,
name,
title
from
gcp_project
where
name = '{{ output.current_project_name.value }}';
1 change: 1 addition & 0 deletions gcp-test/tests/gcp_project/variables.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
55 changes: 55 additions & 0 deletions gcp-test/tests/gcp_project/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

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}"
}
}

data "google_project" "current" {}

output "resource_aka" {
value = data.null_data_source.resource.inputs.scope
}

Subhajit97 marked this conversation as resolved.
Show resolved Hide resolved
output "current_project_name" {
value = data.google_project.current.name
}

output "project_id" {
value = data.google_project.current.project_id
}

output "project_number" {
value = data.google_project.current.number
}
1 change: 1 addition & 0 deletions gcp/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func Plugin(ctx context.Context) *plugin.Plugin {
"gcp_monitoring_alert_policy": tableGcpMonitoringAlert(ctx),
"gcp_monitoring_group": tableGcpMonitoringGroup(ctx),
"gcp_monitoring_notification_channel": tableGcpMonitoringNotificationChannel(ctx),
"gcp_project": tableGcpProject(ctx),
"gcp_project_service": tableGcpProjectService(ctx),
"gcp_pubsub_snapshot": tableGcpPubSubSnapshot(ctx),
"gcp_pubsub_subscription": tableGcpPubSubSubscription(ctx),
Expand Down
123 changes: 123 additions & 0 deletions gcp/table_gcp_project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package gcp

import (
"context"

"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/cloudresourcemanager/v1"
)

//// TABLE DEFINITION

func tableGcpProject(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "gcp_project",
Description: "GCP Project",
List: &plugin.ListConfig{
Hydrate: listGCPProjects,
},
Columns: []*plugin.Column{
{
Name: "name",
Description: "The name of the project.",
Type: proto.ColumnType_STRING,
},
{
Name: "project_id",
Description: "An unique, user-assigned ID of the Project.",
Type: proto.ColumnType_STRING,
},
{
Name: "project_number",
Description: "The number uniquely identifying the project.",
Type: proto.ColumnType_INT,
},
{
Name: "lifecycle_state",
Description: "Specifies the project lifecycle state.",
Type: proto.ColumnType_STRING,
},
{
Name: "create_time",
Description: "Creation time of the project.",
Type: proto.ColumnType_TIMESTAMP,
},
{
Name: "parent",
Description: "An optional reference to a parent Resource.",
Type: proto.ColumnType_JSON,
},
{
Name: "labels",
Description: "A list of labels attached to this project.",
Type: proto.ColumnType_JSON,
},

// Steampipe standard columns
{
Name: "title",
Description: ColumnDescriptionTitle,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Name"),
},
{
Name: "tags",
Description: ColumnDescriptionTags,
Type: proto.ColumnType_JSON,
Transform: transform.FromField("Labels"),
},
{
Name: "akas",
Description: ColumnDescriptionAkas,
Type: proto.ColumnType_JSON,
Hydrate: getProjectAka,
Transform: transform.FromValue(),
},
},
}
}

//// LIST FUNCTION

func listGCPProjects(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
plugin.Logger(ctx).Trace("listGCPProjects")

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

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

resp, err := service.Projects.List().Filter("name=" + project).Do()
for _, project := range resp.Projects {
d.StreamListItem(ctx, project)
}
if err != nil {
return nil, err
}

return nil, nil
}

Subhajit97 marked this conversation as resolved.
Show resolved Hide resolved
//// HYDRATE FUNCTIONS

func getProjectAka(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
plugin.Logger(ctx).Trace("getProjectAka")

// Get project details
project := h.Item.(*cloudresourcemanager.Project)

// Build resource aka
Subhajit97 marked this conversation as resolved.
Show resolved Hide resolved
akas := []string{"gcp://cloudresourcemanager.googleapis.com/projects/" + project.ProjectId}

return akas, nil
}