Skip to content

Commit 51d8e11

Browse files
Docs: add sql review api tuts (#461)
* docs: first draft * docs: update
1 parent 56f4ad3 commit 51d8e11

File tree

6 files changed

+135
-2
lines changed

6 files changed

+135
-2
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
1. Log in as the admin user, and go to **Security & Policy > Users & Groups**. Click **+ Add User**, fill in with `api-example`, choose the `DBA` role that is sufficient for this tutorial and click **Confirm**.
2-
![service-account-create](/content/docs/share/tutorials/add-service-account.webp)
2+
![service-account-create](/content/docs/tutorials/share/add-service-account.webp)
33

44
1. Find the newly created service account and click on **Copy Service Key**. We will use this token to authenticate the API calls.
5-
![service-account-key](/content/docs/share/tutorials/service-account-key.webp)
5+
![service-account-key](/content/docs/tutorials/share/service-account-key.webp)
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
---
2+
title: Configure SQL Review Policies with Bytebase API
3+
author: Ningjing
4+
updated_at: 2024/11/12 18:00
5+
tags: Tutorial
6+
integrations: General, API
7+
level: Advanced
8+
estimated_time: '40 mins'
9+
description: 'Learn how to use the Bytebase API to configure SQL review rules in Bytebase'
10+
---
11+
12+
Bytebase is a database DevOps and CI/CD tool designed for developers, DBAs, and platform engineering teams. While it offers an intuitive GUI for managing database schema changes and access control, some teams may want to integrate Bytebase into their existing DevOps platforms using the [Bytebase API](/docs/api/overview/).
13+
14+
This tutorial will guide you through configuring SQL review rules using the Bytebase API. This approach allows you to manage SQL review rules as code within your repository, enabling DBAs or platform engineering teams to apply them to Bytebase as needed.
15+
16+
<HintBlock type="info">
17+
18+
This tutorial code repository is at https://github.com/bytebase/api-example/tree/main/sql-review
19+
20+
</HintBlock>
21+
22+
## Prerequisites
23+
24+
1. [Docker](https://www.docker.com/) installed
25+
1. Node.js >= v18
26+
27+
## Start Bytebase
28+
29+
<IncludeBlock url="/docs/share/tutorials/start-bytebase"></IncludeBlock>
30+
31+
## Create Service Account
32+
33+
<IncludeBlock url="/docs/share/tutorials/create-service-account"></IncludeBlock>
34+
35+
## Run Scripts in Terminal
36+
37+
### Generate the Token
38+
39+
1. Go to [Bytebase API Example
40+
repo](https://github.com/bytebase/api-example) and clone it.
41+
42+
1. Go to subfolder `sql-review`, and follow the instructions in the `README.md` to run the scripts. replace the `bytebase_url`, `bytebase_account`, `bytebase_password` with your own values. Then you will get a `bytebase_token` looks like `ey....9V8s`.
43+
44+
```bash
45+
export bytebase_url=http://localhost:8080
46+
47+
export bytebase_password=bbs_xxxxxxxxxxxxxilcLVG
48+
bytebase_token=$(curl -v ${bytebase_url}/v1/auth/login \
49+
--data-raw '{"email":"'${bytebase_account}'","password":"'${bytebase_password}'","web":true}' \
50+
--compressed 2>&1 | grep token | grep -o 'access-token=[^;]*;' | grep -o '[^;]*' | sed 's/access-token=//g; s/;//g')
51+
echo $bytebase_token
52+
```
53+
54+
### Configure SQL Review Policies
55+
56+
1. Continue following the `README.md` to run the scripts.
57+
58+
```bash
59+
curl --request PATCH ${bytebase_url}/v1/reviewConfigs/basic \
60+
--header 'Authorization: Bearer '${bytebase_token} \
61+
--data @policy/basic.json
62+
63+
curl --request PATCH ${bytebase_url}/v1/reviewConfigs/advanced \
64+
--header 'Authorization: Bearer '${bytebase_token} \
65+
--data @policy/advanced.json
66+
```
67+
68+
1. In the Bytebase console, navigate to **CI/CD > SQL Review** to see the applied SQL review rules. You may click **Edit** to change the rules.
69+
![bb-sql-review-config](/content/docs/tutorials/api-sql-review/bb-sql-review-config.webp)
70+
71+
1. To delete the SQL review rules, use the following commands:
72+
73+
```bash
74+
curl --request PATCH "${bytebase_url}/v1/reviewConfigs/basic?allow_missing=true&update_mask=rules" \
75+
--header 'Authorization: Bearer '${bytebase_token} \
76+
--data @policy/basic.json
77+
78+
curl --request PATCH "${bytebase_url}/v1/reviewConfigs/advanced?allow_missing=true&update_mask=rules" \
79+
--header 'Authorization: Bearer '${bytebase_token} \
80+
--data @policy/advanced.json
81+
```
82+
83+
### Apply SQL Review Policies to Resources
84+
85+
You may notice that the SQL review rules are not applied to any resources yet from the above screenshot. In Bytebase, the SQL review rules can be applied to the `environments` or `projects`. Project-level rules take precedence over environment-level rules.
86+
87+
1. Follow the `README.md` to run the scripts to apply the SQL review rules to environments.
88+
89+
```bash
90+
curl --request PATCH "${bytebase_url}/v1/environments/test/policies/tag?allow_missing=true&update_mask=payload" \
91+
--header 'Authorization: Bearer '${bytebase_token} \
92+
--data @binding/environments/test.json
93+
94+
curl --request PATCH "${bytebase_url}/v1/environments/prod/policies/tag?allow_missing=true&update_mask=payload" \
95+
--header 'Authorization: Bearer '${bytebase_token} \
96+
--data @binding/environments/prod.json
97+
```
98+
99+
1. Continue with the `README.md` to apply the SQL review rules to projects.
100+
101+
```bash
102+
curl --request PATCH "${bytebase_url}/v1/projects/project-sample/policies/tag?allow_missing=true&update_mask=payload" \
103+
--header 'Authorization: Bearer '${bytebase_token} \
104+
--data @binding/projects/project-sample.json
105+
```
106+
107+
1. On the **CI/CD > SQL Review** page, you will see the SQL review rules are applied to environments and projects.
108+
![bb-sql-review-config-rsc](/content/docs/tutorials/api-sql-review/bb-sql-review-config-rsc.webp)
109+
110+
1. Go to **Environments** page, click **Test** environment to see the applied SQL review rules.
111+
![bb-env](/content/docs/tutorials/api-sql-review/bb-env.webp)
112+
113+
1. Go to `Sample Project` page, click **Setting** on the left sidebar to see the applied the SQL review rules.
114+
![bb-project-setting](/content/docs/tutorials/api-sql-review/bb-project-setting.webp)
115+
116+
1. To delete SQL review rules from environments and projects, use the following commands:
117+
118+
```bash
119+
curl --request DELETE ${bytebase_url}/v1/environments/test/policies/tag \
120+
--header 'Authorization: Bearer '${bytebase_token}
121+
122+
curl --request DELETE ${bytebase_url}/v1/environments/prod/policies/tag \
123+
--header 'Authorization: Bearer '${bytebase_token}
124+
```
125+
126+
```bash
127+
curl --request DELETE ${bytebase_url}/v1/projects/project-sample/policies/tag \
128+
--header 'Authorization: Bearer '${bytebase_token}
129+
```
130+
131+
## Summary
132+
133+
Congratulations! You can now configure SQL review rules using the Bytebase API, in addition to the Bytebase GUI, making SQL review policy as code a reality.
80.9 KB
Loading
61.3 KB
Loading
66.2 KB
Loading
61.9 KB
Loading

0 commit comments

Comments
 (0)