Skip to content

Commit cf00c37

Browse files
authored
feat(gcp): Add SQL users (#5622)
#### Summary Fixes #5492 <!--
1 parent 285b9cd commit cf00c37

9 files changed

Lines changed: 202 additions & 8 deletions

File tree

plugins/source/gcp/codegen/recipes/sqladmin.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import (
66
sqladmin "google.golang.org/api/sqladmin/v1beta4"
77
)
88

9-
10-
11-
func init(){
9+
func init() {
1210
resources := []*Resource{
1311
{
1412
SubService: "instances",
@@ -24,6 +22,34 @@ func init(){
2422
},
2523
},
2624
NameTransformer: CreateReplaceTransformer(map[string]string{"ipv_6": "ipv6"}),
25+
Relations: []string{"Users()"},
26+
},
27+
{
28+
SubService: "users",
29+
Struct: &sqladmin.User{},
30+
SkipMock: true,
31+
SkipFetch: true,
32+
ChildTable: true,
33+
ExtraColumns: []codegen.ColumnDefinition{
34+
{
35+
Name: "project_id",
36+
Type: schema.TypeString,
37+
Options: schema.ColumnCreationOptions{PrimaryKey: true},
38+
Resolver: "client.ResolveProject",
39+
},
40+
{
41+
Name: "instance",
42+
Type: schema.TypeString,
43+
Options: schema.ColumnCreationOptions{PrimaryKey: true},
44+
Resolver: `schema.PathResolver("Instance")`,
45+
},
46+
{
47+
Name: "name",
48+
Type: schema.TypeString,
49+
Options: schema.ColumnCreationOptions{PrimaryKey: true},
50+
Resolver: `schema.PathResolver("Name")`,
51+
},
52+
},
2753
},
2854
}
2955

plugins/source/gcp/docs/tables/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,6 @@
4747
- [gcp_secretmanager_secrets](gcp_secretmanager_secrets.md)
4848
- [gcp_serviceusage_services](gcp_serviceusage_services.md)
4949
- [gcp_sql_instances](gcp_sql_instances.md)
50+
- [gcp_sql_users](gcp_sql_users.md)
5051
- [gcp_storage_buckets](gcp_storage_buckets.md)
5152
- [gcp_storage_bucket_policies](gcp_storage_bucket_policies.md)

plugins/source/gcp/docs/tables/gcp_sql_instances.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
The primary key for this table is **self_link**.
66

7+
## Relations
78

9+
The following tables depend on gcp_sql_instances:
10+
- [gcp_sql_users](gcp_sql_users.md)
811

912
## Columns
1013
| Name | Type |
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Table: gcp_sql_users
2+
3+
4+
5+
The composite primary key for this table is (**project_id**, **instance**, **name**).
6+
7+
## Relations
8+
This table depends on [gcp_sql_instances](gcp_sql_instances.md).
9+
10+
11+
## Columns
12+
| Name | Type |
13+
| ------------- | ------------- |
14+
|_cq_source_name|String|
15+
|_cq_sync_time|Timestamp|
16+
|_cq_id|UUID|
17+
|_cq_parent_id|UUID|
18+
|project_id (PK)|String|
19+
|instance (PK)|String|
20+
|name (PK)|String|
21+
|dual_password_type|String|
22+
|etag|String|
23+
|host|String|
24+
|kind|String|
25+
|password|String|
26+
|password_policy|JSON|
27+
|project|String|
28+
|sqlserver_user_details|JSON|
29+
|type|String|

plugins/source/gcp/resources/services/sql/instances.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/source/gcp/resources/services/sql/instances_mock_test.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ import (
1212
)
1313

1414
func createInstances(mux *httprouter.Router) error {
15-
var item sql.InstancesListResponse
16-
if err := faker.FakeObject(&item); err != nil {
15+
var instanceResponse sql.InstancesListResponse
16+
if err := faker.FakeObject(&instanceResponse); err != nil {
1717
return err
1818
}
19-
item.NextPageToken = ""
19+
instanceResponse.NextPageToken = ""
2020

21-
mux.GET("/*filepath", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
22-
b, err := json.Marshal(item)
21+
mux.GET("/sql/v1beta4/projects/testProject/instances", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
22+
b, err := json.Marshal(instanceResponse)
2323
if err != nil {
2424
http.Error(w, "unable to marshal request: "+err.Error(), http.StatusBadRequest)
2525
return
@@ -29,6 +29,25 @@ func createInstances(mux *httprouter.Router) error {
2929
return
3030
}
3131
})
32+
33+
var usersResponse sql.UsersListResponse
34+
if err := faker.FakeObject(&usersResponse); err != nil {
35+
return err
36+
}
37+
usersResponse.NextPageToken = ""
38+
39+
mux.GET("/sql/v1beta4/projects/testProject/instances/test string/users", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
40+
b, err := json.Marshal(&usersResponse)
41+
if err != nil {
42+
http.Error(w, "unable to marshal request: "+err.Error(), http.StatusBadRequest)
43+
return
44+
}
45+
if _, err := w.Write(b); err != nil {
46+
http.Error(w, "failed to write", http.StatusBadRequest)
47+
return
48+
}
49+
})
50+
3251
return nil
3352
}
3453

plugins/source/gcp/resources/services/sql/users.go

Lines changed: 87 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package sql
2+
3+
import (
4+
"context"
5+
6+
"github.com/cloudquery/plugin-sdk/schema"
7+
"github.com/cloudquery/plugins/source/gcp/client"
8+
sql "google.golang.org/api/sqladmin/v1beta4"
9+
)
10+
11+
func fetchUsers(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
12+
c := meta.(*client.Client)
13+
sqlClient, err := sql.NewService(ctx, c.ClientOptions...)
14+
if err != nil {
15+
return err
16+
}
17+
instance := parent.Item.(*sql.DatabaseInstance)
18+
output, err := sqlClient.Users.List(c.ProjectId, instance.Name).Do()
19+
if err != nil {
20+
return err
21+
}
22+
res <- output.Items
23+
return nil
24+
}

website/pages/docs/plugins/sources/gcp/tables.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,6 @@
4747
- [gcp_secretmanager_secrets](https://github.com/cloudquery/cloudquery/blob/main/plugins/source/gcp/docs/tables/gcp_secretmanager_secrets.md)
4848
- [gcp_serviceusage_services](https://github.com/cloudquery/cloudquery/blob/main/plugins/source/gcp/docs/tables/gcp_serviceusage_services.md)
4949
- [gcp_sql_instances](https://github.com/cloudquery/cloudquery/blob/main/plugins/source/gcp/docs/tables/gcp_sql_instances.md)
50+
- [gcp_sql_users](https://github.com/cloudquery/cloudquery/blob/main/plugins/source/gcp/docs/tables/gcp_sql_users.md)
5051
- [gcp_storage_buckets](https://github.com/cloudquery/cloudquery/blob/main/plugins/source/gcp/docs/tables/gcp_storage_buckets.md)
5152
- [gcp_storage_bucket_policies](https://github.com/cloudquery/cloudquery/blob/main/plugins/source/gcp/docs/tables/gcp_storage_bucket_policies.md)

0 commit comments

Comments
 (0)