Skip to content

Commit

Permalink
chore: support masking/masking exception policy (#79)
Browse files Browse the repository at this point in the history
* refactor: use protocol api

* fix: go mod

* chore: update

* chore: update

* chore: go version

* fix: test

* chore: golang lint

* chore: support approval setting

* chore: support approval flow

* chore: update

* fix: go mod tidy

* fix: lint

* chore: update

* chore: update

* chore: update

* fix: lint

* chore: policy and setting

* chore: update

* chore: update docs
  • Loading branch information
ecmadao authored Dec 7, 2024
1 parent cb288ca commit 2ac2bae
Show file tree
Hide file tree
Showing 29 changed files with 971 additions and 936 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ terraform destory

> This will generate the doc template in the `docs` folder
>
> Check https://github.com/hashicorp/terraform-plugin-docs for details.
> Check https://github.com/hashicorp/terraform-plugin-docs and https://github.com/hashicorp/terraform-plugin-docs/issues/141 for details.
```bash
go run github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs --provider-name=terraform-provider-bytebase
GOOS=darwin GOARCH=amd64 go run github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs --provider-name=terraform-provider-bytebase
```

## Release
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.3
1.0.4
6 changes: 3 additions & 3 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ type Client interface {

// Policy
// ListPolicies lists policies in a specific resource.
ListPolicies(ctx context.Context, find *PolicyFindMessage) (*ListPolicyMessage, error)
ListPolicies(ctx context.Context, parent string) (*v1pb.ListPoliciesResponse, error)
// GetPolicy gets a policy in a specific resource.
GetPolicy(ctx context.Context, policyName string) (*PolicyMessage, error)
GetPolicy(ctx context.Context, policyName string) (*v1pb.Policy, error)
// UpsertPolicy creates or updates the policy.
UpsertPolicy(ctx context.Context, patch *PolicyPatchMessage) (*PolicyMessage, error)
UpsertPolicy(ctx context.Context, patch *v1pb.Policy, updateMasks []string) (*v1pb.Policy, error)
// DeletePolicy deletes the policy.
DeletePolicy(ctx context.Context, policyName string) error

Expand Down
19 changes: 0 additions & 19 deletions api/deployment.go

This file was deleted.

130 changes: 0 additions & 130 deletions api/policy.go

This file was deleted.

47 changes: 15 additions & 32 deletions client/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,21 @@ package client

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

"github.com/pkg/errors"

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

// ListPolicies lists policies in a specific resource.
func (c *client) ListPolicies(ctx context.Context, find *api.PolicyFindMessage) (*api.ListPolicyMessage, error) {
if find.Type != nil {
return nil, errors.Errorf("invalid request, list policies cannot specific the policy type")
}

func (c *client) ListPolicies(ctx context.Context, parent string) (*v1pb.ListPoliciesResponse, error) {
var url string
if find.Parent == "" {
if parent == "" {
url = fmt.Sprintf("%s/%s/policies", c.url, c.version)
} else {
url = fmt.Sprintf("%s/%s/%s/policies", c.url, c.version, find.Parent)
url = fmt.Sprintf("%s/%s/%s/policies", c.url, c.version, parent)
}
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
Expand All @@ -34,16 +28,16 @@ func (c *client) ListPolicies(ctx context.Context, find *api.PolicyFindMessage)
return nil, err
}

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

return &res, nil
}

// GetPolicy gets a policy in a specific resource.
func (c *client) GetPolicy(ctx context.Context, policyName string) (*api.PolicyMessage, error) {
func (c *client) GetPolicy(ctx context.Context, policyName string) (*v1pb.Policy, error) {
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/%s/%s", c.url, c.version, policyName), nil)
if err != nil {
return nil, err
Expand All @@ -54,33 +48,22 @@ func (c *client) GetPolicy(ctx context.Context, policyName string) (*api.PolicyM
return nil, err
}

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

return &res, nil
}

// UpsertPolicy creates or updates the policy.
func (c *client) UpsertPolicy(ctx context.Context, patch *api.PolicyPatchMessage) (*api.PolicyMessage, error) {
payload, err := json.Marshal(patch)
func (c *client) UpsertPolicy(ctx context.Context, policy *v1pb.Policy, updateMasks []string) (*v1pb.Policy, error) {
payload, err := protojson.Marshal(policy)
if err != nil {
return nil, err
}

paths := []string{}
if patch.InheritFromParent != nil {
paths = append(paths, "inherit_from_parent")
}
if patch.DeploymentApprovalPolicy != nil ||
patch.BackupPlanPolicy != nil ||
patch.SensitiveDataPolicy != nil ||
patch.AccessControlPolicy != nil {
paths = append(paths, "payload")
}

req, err := http.NewRequestWithContext(ctx, "PATCH", fmt.Sprintf("%s/%s/%s?allow_missing=true&update_mask=%s", c.url, c.version, patch.Name, strings.Join(paths, ",")), strings.NewReader(string(payload)))
req, err := http.NewRequestWithContext(ctx, "PATCH", fmt.Sprintf("%s/%s/%s?allow_missing=true&update_mask=%s", c.url, c.version, policy.Name, strings.Join(updateMasks, ",")), strings.NewReader(string(payload)))
if err != nil {
return nil, err
}
Expand All @@ -90,8 +73,8 @@ func (c *client) UpsertPolicy(ctx context.Context, patch *api.PolicyPatchMessage
return nil, err
}

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

Expand Down
4 changes: 4 additions & 0 deletions docs/data-sources/instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ Read-Only:
- `database` (String)
- `host` (String)
- `id` (String)
- `password` (String)
- `port` (String)
- `ssl_ca` (String)
- `ssl_cert` (String)
- `ssl_key` (String)
- `type` (String)
- `username` (String)

Expand Down
4 changes: 4 additions & 0 deletions docs/data-sources/instance_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ Read-Only:
- `database` (String)
- `host` (String)
- `id` (String)
- `password` (String)
- `port` (String)
- `ssl_ca` (String)
- `ssl_cert` (String)
- `ssl_key` (String)
- `type` (String)
- `username` (String)

Expand Down
Loading

0 comments on commit 2ac2bae

Please sign in to comment.