/
table_gcp_sql_database.go
209 lines (179 loc) · 5.87 KB
/
table_gcp_sql_database.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package gcp
import (
"context"
"strings"
"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"
sqladmin "google.golang.org/api/sqladmin/v1beta4"
)
type sqlDatabaseInfo = struct {
Database *sqladmin.Database
Region string
}
//// TABLE DEFINITION
func tableGcpSQLDatabase(ctx context.Context) *plugin.Table {
return &plugin.Table{
Name: "gcp_sql_database",
Description: "GCP SQL Database",
Get: &plugin.GetConfig{
KeyColumns: plugin.AllColumns([]string{"name", "instance_name"}),
Hydrate: getSQLDatabase,
},
List: &plugin.ListConfig{
Hydrate: listSQLDatabases,
ParentHydrate: listSQLDatabaseInstances,
},
Columns: []*plugin.Column{
{
Name: "name",
Description: "A friendly name that identifies the resource.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Database.Name"),
},
{
Name: "instance_name",
Description: "The name of the Cloud SQL instance.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Database.Instance"),
},
{
Name: "kind",
Description: "The type of the resource.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Database.Kind"),
},
{
Name: "charset",
Description: "Specifies the MySQL charset value.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Database.Charset"),
},
{
Name: "collation",
Description: "Specifies the MySQL collation value.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Database.Collation"),
},
{
Name: "self_link",
Description: "The server-defined URL for the resource.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Database.SelfLink"),
},
{
Name: "sql_server_database_compatibility_level",
Description: "The version of SQL Server with which the database is to be made compatible.",
Type: proto.ColumnType_INT,
Transform: transform.FromField("Database.SqlserverDatabaseDetails.CompatibilityLevel"),
},
{
Name: "sql_server_database_recovery_model",
Description: "The recovery model of a SQL Server database.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Database.SqlserverDatabaseDetails.RecoveryModel"),
},
// standard steampipe columns
{
Name: "title",
Description: ColumnDescriptionTitle,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Database.Name"),
},
{
Name: "akas",
Description: ColumnDescriptionAkas,
Type: proto.ColumnType_JSON,
Transform: transform.FromP(sqlInstanceSelfLinkToTurbotData, "Akas"),
},
// standard gcp columns
{
Name: "location",
Description: ColumnDescriptionLocation,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Region"),
},
{
Name: "project",
Description: ColumnDescriptionProject,
Type: proto.ColumnType_STRING,
Transform: transform.FromP(sqlInstanceSelfLinkToTurbotData, "Project"),
},
},
}
}
//// LIST FUNCTION
func listSQLDatabases(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
plugin.Logger(ctx).Trace("listSQLDatabases")
// Get the details of Cloud SQL instance
instance := h.Item.(*sqladmin.DatabaseInstance)
// ERROR: rpc error: code = Unknown desc = googleapi: Error 400: Invalid request: Invalid request since instance is not running., invalid
// Return nil, if the instance not in running state
if instance.State != "RUNNABLE" {
return nil, nil
}
// Create service connection
service, err := CloudSQLAdminService(ctx, d)
if err != nil {
return nil, err
}
// Get project details
getProjectCached := plugin.HydrateFunc(getProject).WithCache()
projectId, err := getProjectCached(ctx, d, h)
if err != nil {
return nil, err
}
project := projectId.(string)
resp, err := service.Databases.List(project, instance.Name).Do()
if err != nil {
return nil, err
}
for _, database := range resp.Items {
d.StreamLeafListItem(ctx, sqlDatabaseInfo{database, instance.Region})
}
return nil, nil
}
//// HYDRATE FUNCTIONS
func getSQLDatabase(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
plugin.Logger(ctx).Trace("getSQLDatabase")
// Create service connection
service, err := CloudSQLAdminService(ctx, d)
if err != nil {
return nil, err
}
// Get project details
getProjectCached := plugin.HydrateFunc(getProject).WithCache()
projectId, err := getProjectCached(ctx, d, h)
if err != nil {
return nil, err
}
project := projectId.(string)
name := d.EqualsQuals["name"].GetStringValue()
instanceName := d.EqualsQuals["instance_name"].GetStringValue()
// Return nil, if no input provided
if name == "" || instanceName == "" {
return nil, nil
}
// Get the region where the specified instance is created
instanceData, err := service.Instances.Get(project, instanceName).Do()
if err != nil {
return nil, err
}
region := instanceData.Region
resp, err := service.Databases.Get(project, instanceName, name).Do()
if err != nil {
return nil, err
}
return sqlDatabaseInfo{resp, region}, nil
}
//// TRANSFORM FUNCTIONS
func sqlInstanceSelfLinkToTurbotData(_ context.Context, d *transform.TransformData) (interface{}, error) {
data := d.HydrateItem.(sqlDatabaseInfo)
param := d.Param.(string)
project := strings.Split(data.Database.SelfLink, "/")[6]
turbotData := map[string]interface{}{
"Project": project,
"Akas": []string{"gcp://cloudsql.googleapis.com/projects/" + project + "/instances/" + data.Database.Instance + "/databases/" + data.Database.Name},
}
return turbotData[param], nil
}