Skip to content

Commit 6583f84

Browse files
authored
feat: support risk (#108)
* chore: support single environment resource * chore: update version * feat: support risk * fix: lint
1 parent f64b094 commit 6583f84

18 files changed

+713
-8
lines changed

api/client.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,16 @@ type Client interface {
179179
UpsertReviewConfig(ctx context.Context, patch *v1pb.ReviewConfig, updateMasks []string) (*v1pb.ReviewConfig, error)
180180
// DeleteReviewConfig deletes the review config.
181181
DeleteReviewConfig(ctx context.Context, reviewName string) error
182+
183+
// Risk
184+
// ListRisk lists the risk.
185+
ListRisk(ctx context.Context) ([]*v1pb.Risk, error)
186+
// GetRisk gets the risk by full name.
187+
GetRisk(ctx context.Context, name string) (*v1pb.Risk, error)
188+
// CreateRisk creates the risk.
189+
CreateRisk(ctx context.Context, risk *v1pb.Risk) (*v1pb.Risk, error)
190+
// UpdateRisk updates the risk.
191+
UpdateRisk(ctx context.Context, patch *v1pb.Risk, updateMasks []string) (*v1pb.Risk, error)
192+
// DeleteRisk deletes the risk by name.
193+
DeleteRisk(ctx context.Context, name string) error
182194
}

client/risk.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package client
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"strings"
8+
9+
v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
10+
"google.golang.org/protobuf/encoding/protojson"
11+
)
12+
13+
// ListRisk lists the risk.
14+
func (c *client) ListRisk(ctx context.Context) ([]*v1pb.Risk, error) {
15+
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/%s/risks", c.url, c.version), nil)
16+
if err != nil {
17+
return nil, err
18+
}
19+
20+
body, err := c.doRequest(req)
21+
if err != nil {
22+
return nil, err
23+
}
24+
25+
var res v1pb.ListRisksResponse
26+
if err := ProtojsonUnmarshaler.Unmarshal(body, &res); err != nil {
27+
return nil, err
28+
}
29+
30+
return res.Risks, nil
31+
}
32+
33+
// GetRisk gets the risk by full name.
34+
func (c *client) GetRisk(ctx context.Context, name string) (*v1pb.Risk, error) {
35+
body, err := c.getResource(ctx, name)
36+
if err != nil {
37+
return nil, err
38+
}
39+
40+
var res v1pb.Risk
41+
if err := ProtojsonUnmarshaler.Unmarshal(body, &res); err != nil {
42+
return nil, err
43+
}
44+
45+
return &res, nil
46+
}
47+
48+
// CreateRisk creates the risk.
49+
func (c *client) CreateRisk(ctx context.Context, risk *v1pb.Risk) (*v1pb.Risk, error) {
50+
payload, err := protojson.Marshal(risk)
51+
if err != nil {
52+
return nil, err
53+
}
54+
55+
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/%s/risks", c.url, c.version), strings.NewReader(string(payload)))
56+
57+
if err != nil {
58+
return nil, err
59+
}
60+
61+
body, err := c.doRequest(req)
62+
if err != nil {
63+
return nil, err
64+
}
65+
66+
var res v1pb.Risk
67+
if err := ProtojsonUnmarshaler.Unmarshal(body, &res); err != nil {
68+
return nil, err
69+
}
70+
71+
return &res, nil
72+
}
73+
74+
// UpdateRisk updates the risk.
75+
func (c *client) UpdateRisk(ctx context.Context, patch *v1pb.Risk, updateMasks []string) (*v1pb.Risk, error) {
76+
body, err := c.updateResource(ctx, patch.Name, patch, updateMasks, false /* allow missing = false*/)
77+
if err != nil {
78+
return nil, err
79+
}
80+
81+
var res v1pb.Risk
82+
if err := ProtojsonUnmarshaler.Unmarshal(body, &res); err != nil {
83+
return nil, err
84+
}
85+
86+
return &res, nil
87+
}
88+
89+
// DeleteRisk deletes the risk by name.
90+
func (c *client) DeleteRisk(ctx context.Context, name string) error {
91+
return c.deleteResource(ctx, name)
92+
}

client/role.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (c *client) DeleteRole(ctx context.Context, name string) error {
5858

5959
// UpdateRole updates the role.
6060
func (c *client) UpdateRole(ctx context.Context, patch *v1pb.Role, updateMasks []string) (*v1pb.Role, error) {
61-
body, err := c.updateResource(ctx, patch.Name, patch, updateMasks, false /* allow missing = false*/)
61+
body, err := c.updateResource(ctx, patch.Name, patch, updateMasks, true /* allow missing = true*/)
6262
if err != nil {
6363
return nil, err
6464
}

docs/data-sources/environment.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "bytebase_environment Data Source - terraform-provider-bytebase"
4+
subcategory: ""
5+
description: |-
6+
The environment data source.
7+
---
8+
9+
# bytebase_environment (Data Source)
10+
11+
The environment data source.
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `resource_id` (String) The environment unique resource id.
21+
22+
### Optional
23+
24+
- `color` (String) The environment color.
25+
- `protected` (Boolean) The environment is protected or not.
26+
27+
### Read-Only
28+
29+
- `id` (String) The ID of this resource.
30+
- `name` (String) The environment full name in environments/{resource id} format.
31+
- `order` (Number) The environment sorting order.
32+
- `title` (String) The environment unique name.
33+
34+

docs/data-sources/risk.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "bytebase_risk Data Source - terraform-provider-bytebase"
4+
subcategory: ""
5+
description: |-
6+
The risk data source.
7+
---
8+
9+
# bytebase_risk (Data Source)
10+
11+
The risk data source.
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `name` (String) The risk full name in risks/{uid} format.
21+
22+
### Read-Only
23+
24+
- `active` (Boolean) The risk active.
25+
- `condition` (String) The risk condition.
26+
- `id` (String) The ID of this resource.
27+
- `level` (Number) The risk level.
28+
- `source` (String) The risk source.
29+
- `title` (String) The risk title.
30+
31+

docs/data-sources/risk_list.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "bytebase_risk_list Data Source - terraform-provider-bytebase"
4+
subcategory: ""
5+
description: |-
6+
The risk data source list.
7+
---
8+
9+
# bytebase_risk_list (Data Source)
10+
11+
The risk data source list.
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Read-Only
19+
20+
- `id` (String) The ID of this resource.
21+
- `risks` (List of Object) (see [below for nested schema](#nestedatt--risks))
22+
23+
<a id="nestedatt--risks"></a>
24+
### Nested Schema for `risks`
25+
26+
Read-Only:
27+
28+
- `active` (Boolean)
29+
- `condition` (String)
30+
- `level` (Number)
31+
- `name` (String)
32+
- `source` (String)
33+
- `title` (String)
34+
35+

docs/resources/environment.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "bytebase_environment Resource - terraform-provider-bytebase"
4+
subcategory: ""
5+
description: |-
6+
The environment resource.
7+
---
8+
9+
# bytebase_environment (Resource)
10+
11+
The environment resource.
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `order` (Number) The environment sorting order.
21+
- `resource_id` (String) The environment unique id.
22+
- `title` (String) The environment display name.
23+
24+
### Optional
25+
26+
- `color` (String) The environment color.
27+
- `protected` (Boolean) The environment is protected or not.
28+
29+
### Read-Only
30+
31+
- `id` (String) The ID of this resource.
32+
- `name` (String) The environment readonly name in environments/{id} format.
33+
34+

docs/resources/risk.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "bytebase_risk Resource - terraform-provider-bytebase"
4+
subcategory: ""
5+
description: |-
6+
The risk resource. Require ENTERPRISE subscription. Check the docs https://www.bytebase.com/docs/administration/risk-center?source=terraform for more information.
7+
---
8+
9+
# bytebase_risk (Resource)
10+
11+
The risk resource. Require ENTERPRISE subscription. Check the docs https://www.bytebase.com/docs/administration/risk-center?source=terraform for more information.
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `condition` (String) The risk condition.
21+
- `level` (Number) The risk level.
22+
- `source` (String) The risk source.
23+
- `title` (String) The risk title.
24+
25+
### Optional
26+
27+
- `active` (Boolean) If the risk is active.
28+
29+
### Read-Only
30+
31+
- `id` (String) The ID of this resource.
32+
- `name` (String) The risk full name in risks/{uid} format.
33+
34+

examples/risk/main.tf

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
terraform {
2+
required_providers {
3+
bytebase = {
4+
version = "3.6.0"
5+
# For local development, please use "terraform.local/bytebase/bytebase" instead
6+
source = "registry.terraform.io/bytebase/bytebase"
7+
}
8+
}
9+
}
10+
11+
provider "bytebase" {
12+
# You need to replace the account and key with your Bytebase service account.
13+
service_account = "[email protected]"
14+
service_key = "bbs_BxVIp7uQsARl8nR92ZZV"
15+
# The Bytebase service URL. You can use the external URL in production.
16+
# Check the docs about external URL: https://www.bytebase.com/docs/get-started/install/external-url
17+
url = "https://bytebase.example.com"
18+
}
19+
20+
data "bytebase_risk_list" "all" {
21+
}
22+
23+
output "all_risks" {
24+
value = data.bytebase_risk_list.all
25+
}

examples/setup/risk.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
resource "bytebase_risk" "risk" {
2+
title = "Risk for prod environment"
3+
source = "DML"
4+
level = 300
5+
active = true
6+
condition = "environment_id == \"prod\" && affected_rows >= 100"
7+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/bytebase/terraform-provider-bytebase
33
go 1.24.2
44

55
require (
6-
github.com/bytebase/bytebase v0.0.0-20250424073126-d57cbba37d61
6+
github.com/bytebase/bytebase v0.0.0-20250513033606-5479107aeeb3
77
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
88
github.com/hashicorp/terraform-plugin-docs v0.13.0
99
github.com/hashicorp/terraform-plugin-log v0.7.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgI
3030
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
3131
github.com/bgentry/speakeasy v0.1.0 h1:ByYyxL9InA1OWqxJqqp2A5pYHUrCiAL6K3J+LKSsQkY=
3232
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
33-
github.com/bytebase/bytebase v0.0.0-20250424073126-d57cbba37d61 h1:+ptgPqM2aSzlPjeauecETOwbuGcoSKd5wUsNLLtMuCQ=
34-
github.com/bytebase/bytebase v0.0.0-20250424073126-d57cbba37d61/go.mod h1:Gu5A9lSsc8OMJ5nUbKAxOn5X8gDj1Rxuzy0NwxVt90k=
33+
github.com/bytebase/bytebase v0.0.0-20250513033606-5479107aeeb3 h1:D60aM1+dFti+VTqhmjt6zDXSwStc99OUAUowmfFY5uI=
34+
github.com/bytebase/bytebase v0.0.0-20250513033606-5479107aeeb3/go.mod h1:HHRGRkJYb2FKv2Iyl1feFzaadoXc14t2iMk4z+Xc/Kg=
3535
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
3636
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3737
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

0 commit comments

Comments
 (0)