Skip to content

feat: support risk #108

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

Merged
merged 5 commits into from
May 14, 2025
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
12 changes: 12 additions & 0 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,16 @@ type Client interface {
UpsertReviewConfig(ctx context.Context, patch *v1pb.ReviewConfig, updateMasks []string) (*v1pb.ReviewConfig, error)
// DeleteReviewConfig deletes the review config.
DeleteReviewConfig(ctx context.Context, reviewName string) error

// Risk
// ListRisk lists the risk.
ListRisk(ctx context.Context) ([]*v1pb.Risk, error)
// GetRisk gets the risk by full name.
GetRisk(ctx context.Context, name string) (*v1pb.Risk, error)
// CreateRisk creates the risk.
CreateRisk(ctx context.Context, risk *v1pb.Risk) (*v1pb.Risk, error)
// UpdateRisk updates the risk.
UpdateRisk(ctx context.Context, patch *v1pb.Risk, updateMasks []string) (*v1pb.Risk, error)
// DeleteRisk deletes the risk by name.
DeleteRisk(ctx context.Context, name string) error
}
92 changes: 92 additions & 0 deletions client/risk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package client

import (
"context"
"fmt"
"net/http"
"strings"

v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
"google.golang.org/protobuf/encoding/protojson"
)

// ListRisk lists the risk.
func (c *client) ListRisk(ctx context.Context) ([]*v1pb.Risk, error) {
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/%s/risks", c.url, c.version), nil)
if err != nil {
return nil, err
}

body, err := c.doRequest(req)
if err != nil {
return nil, err
}

var res v1pb.ListRisksResponse
if err := ProtojsonUnmarshaler.Unmarshal(body, &res); err != nil {
return nil, err
}

return res.Risks, nil
}

// GetRisk gets the risk by full name.
func (c *client) GetRisk(ctx context.Context, name string) (*v1pb.Risk, error) {
body, err := c.getResource(ctx, name)
if err != nil {
return nil, err
}

var res v1pb.Risk
if err := ProtojsonUnmarshaler.Unmarshal(body, &res); err != nil {
return nil, err
}

return &res, nil
}

// CreateRisk creates the risk.
func (c *client) CreateRisk(ctx context.Context, risk *v1pb.Risk) (*v1pb.Risk, error) {
payload, err := protojson.Marshal(risk)
if err != nil {
return nil, err
}

req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/%s/risks", c.url, c.version), strings.NewReader(string(payload)))

if err != nil {
return nil, err
}

body, err := c.doRequest(req)
if err != nil {
return nil, err
}

var res v1pb.Risk
if err := ProtojsonUnmarshaler.Unmarshal(body, &res); err != nil {
return nil, err
}

return &res, nil
}

// UpdateRisk updates the risk.
func (c *client) UpdateRisk(ctx context.Context, patch *v1pb.Risk, updateMasks []string) (*v1pb.Risk, error) {
body, err := c.updateResource(ctx, patch.Name, patch, updateMasks, false /* allow missing = false*/)
if err != nil {
return nil, err
}

var res v1pb.Risk
if err := ProtojsonUnmarshaler.Unmarshal(body, &res); err != nil {
return nil, err
}

return &res, nil
}

// DeleteRisk deletes the risk by name.
func (c *client) DeleteRisk(ctx context.Context, name string) error {
return c.deleteResource(ctx, name)
}
2 changes: 1 addition & 1 deletion client/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (c *client) DeleteRole(ctx context.Context, name string) error {

// UpdateRole updates the role.
func (c *client) UpdateRole(ctx context.Context, patch *v1pb.Role, updateMasks []string) (*v1pb.Role, error) {
body, err := c.updateResource(ctx, patch.Name, patch, updateMasks, false /* allow missing = false*/)
body, err := c.updateResource(ctx, patch.Name, patch, updateMasks, true /* allow missing = true*/)
if err != nil {
return nil, err
}
Expand Down
34 changes: 34 additions & 0 deletions docs/data-sources/environment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "bytebase_environment Data Source - terraform-provider-bytebase"
subcategory: ""
description: |-
The environment data source.
---

# bytebase_environment (Data Source)

The environment data source.



<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `resource_id` (String) The environment unique resource id.

### Optional

- `color` (String) The environment color.
- `protected` (Boolean) The environment is protected or not.

### Read-Only

- `id` (String) The ID of this resource.
- `name` (String) The environment full name in environments/{resource id} format.
- `order` (Number) The environment sorting order.
- `title` (String) The environment unique name.


31 changes: 31 additions & 0 deletions docs/data-sources/risk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "bytebase_risk Data Source - terraform-provider-bytebase"
subcategory: ""
description: |-
The risk data source.
---

# bytebase_risk (Data Source)

The risk data source.



<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `name` (String) The risk full name in risks/{uid} format.

### Read-Only

- `active` (Boolean) The risk active.
- `condition` (String) The risk condition.
- `id` (String) The ID of this resource.
- `level` (Number) The risk level.
- `source` (String) The risk source.
- `title` (String) The risk title.


35 changes: 35 additions & 0 deletions docs/data-sources/risk_list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "bytebase_risk_list Data Source - terraform-provider-bytebase"
subcategory: ""
description: |-
The risk data source list.
---

# bytebase_risk_list (Data Source)

The risk data source list.



<!-- schema generated by tfplugindocs -->
## Schema

### Read-Only

- `id` (String) The ID of this resource.
- `risks` (List of Object) (see [below for nested schema](#nestedatt--risks))

<a id="nestedatt--risks"></a>
### Nested Schema for `risks`

Read-Only:

- `active` (Boolean)
- `condition` (String)
- `level` (Number)
- `name` (String)
- `source` (String)
- `title` (String)


34 changes: 34 additions & 0 deletions docs/resources/environment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "bytebase_environment Resource - terraform-provider-bytebase"
subcategory: ""
description: |-
The environment resource.
---

# bytebase_environment (Resource)

The environment resource.



<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `order` (Number) The environment sorting order.
- `resource_id` (String) The environment unique id.
- `title` (String) The environment display name.

### Optional

- `color` (String) The environment color.
- `protected` (Boolean) The environment is protected or not.

### Read-Only

- `id` (String) The ID of this resource.
- `name` (String) The environment readonly name in environments/{id} format.


34 changes: 34 additions & 0 deletions docs/resources/risk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "bytebase_risk Resource - terraform-provider-bytebase"
subcategory: ""
description: |-
The risk resource. Require ENTERPRISE subscription. Check the docs https://www.bytebase.com/docs/administration/risk-center?source=terraform for more information.
---

# bytebase_risk (Resource)

The risk resource. Require ENTERPRISE subscription. Check the docs https://www.bytebase.com/docs/administration/risk-center?source=terraform for more information.



<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `condition` (String) The risk condition.
- `level` (Number) The risk level.
- `source` (String) The risk source.
- `title` (String) The risk title.

### Optional

- `active` (Boolean) If the risk is active.

### Read-Only

- `id` (String) The ID of this resource.
- `name` (String) The risk full name in risks/{uid} format.


25 changes: 25 additions & 0 deletions examples/risk/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
terraform {
required_providers {
bytebase = {
version = "3.6.0"
# For local development, please use "terraform.local/bytebase/bytebase" instead
source = "registry.terraform.io/bytebase/bytebase"
}
}
}

provider "bytebase" {
# You need to replace the account and key with your Bytebase service account.
service_account = "[email protected]"
service_key = "bbs_BxVIp7uQsARl8nR92ZZV"
# The Bytebase service URL. You can use the external URL in production.
# Check the docs about external URL: https://www.bytebase.com/docs/get-started/install/external-url
url = "https://bytebase.example.com"
}

data "bytebase_risk_list" "all" {
}

output "all_risks" {
value = data.bytebase_risk_list.all
}
7 changes: 7 additions & 0 deletions examples/setup/risk.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
resource "bytebase_risk" "risk" {
title = "Risk for prod environment"
source = "DML"
level = 300
active = true
condition = "environment_id == \"prod\" && affected_rows >= 100"
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/bytebase/terraform-provider-bytebase
go 1.24.2

require (
github.com/bytebase/bytebase v0.0.0-20250424073126-d57cbba37d61
github.com/bytebase/bytebase v0.0.0-20250513033606-5479107aeeb3
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/terraform-plugin-docs v0.13.0
github.com/hashicorp/terraform-plugin-log v0.7.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgI
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/bgentry/speakeasy v0.1.0 h1:ByYyxL9InA1OWqxJqqp2A5pYHUrCiAL6K3J+LKSsQkY=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bytebase/bytebase v0.0.0-20250424073126-d57cbba37d61 h1:+ptgPqM2aSzlPjeauecETOwbuGcoSKd5wUsNLLtMuCQ=
github.com/bytebase/bytebase v0.0.0-20250424073126-d57cbba37d61/go.mod h1:Gu5A9lSsc8OMJ5nUbKAxOn5X8gDj1Rxuzy0NwxVt90k=
github.com/bytebase/bytebase v0.0.0-20250513033606-5479107aeeb3 h1:D60aM1+dFti+VTqhmjt6zDXSwStc99OUAUowmfFY5uI=
github.com/bytebase/bytebase v0.0.0-20250513033606-5479107aeeb3/go.mod h1:HHRGRkJYb2FKv2Iyl1feFzaadoXc14t2iMk4z+Xc/Kg=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
Loading