@@ -2,27 +2,21 @@ package client
22
33import (
44 "context"
5- "encoding/json"
65 "fmt"
76 "net/http"
87 "strings"
98
10- "github.com/pkg/errors"
11-
12- "github.com/bytebase/terraform-provider-bytebase/api"
9+ v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
10+ "google.golang.org/protobuf/encoding/protojson"
1311)
1412
1513// ListPolicies lists policies in a specific resource.
16- func (c * client ) ListPolicies (ctx context.Context , find * api.PolicyFindMessage ) (* api.ListPolicyMessage , error ) {
17- if find .Type != nil {
18- return nil , errors .Errorf ("invalid request, list policies cannot specific the policy type" )
19- }
20-
14+ func (c * client ) ListPolicies (ctx context.Context , parent string ) (* v1pb.ListPoliciesResponse , error ) {
2115 var url string
22- if find . Parent == "" {
16+ if parent == "" {
2317 url = fmt .Sprintf ("%s/%s/policies" , c .url , c .version )
2418 } else {
25- url = fmt .Sprintf ("%s/%s/%s/policies" , c .url , c .version , find . Parent )
19+ url = fmt .Sprintf ("%s/%s/%s/policies" , c .url , c .version , parent )
2620 }
2721 req , err := http .NewRequestWithContext (ctx , "GET" , url , nil )
2822 if err != nil {
@@ -34,16 +28,16 @@ func (c *client) ListPolicies(ctx context.Context, find *api.PolicyFindMessage)
3428 return nil , err
3529 }
3630
37- var res api. ListPolicyMessage
38- if err := json .Unmarshal (body , & res ); err != nil {
31+ var res v1pb. ListPoliciesResponse
32+ if err := ProtojsonUnmarshaler .Unmarshal (body , & res ); err != nil {
3933 return nil , err
4034 }
4135
4236 return & res , nil
4337}
4438
4539// GetPolicy gets a policy in a specific resource.
46- func (c * client ) GetPolicy (ctx context.Context , policyName string ) (* api. PolicyMessage , error ) {
40+ func (c * client ) GetPolicy (ctx context.Context , policyName string ) (* v1pb. Policy , error ) {
4741 req , err := http .NewRequestWithContext (ctx , "GET" , fmt .Sprintf ("%s/%s/%s" , c .url , c .version , policyName ), nil )
4842 if err != nil {
4943 return nil , err
@@ -54,33 +48,22 @@ func (c *client) GetPolicy(ctx context.Context, policyName string) (*api.PolicyM
5448 return nil , err
5549 }
5650
57- var res api. PolicyMessage
58- if err := json .Unmarshal (body , & res ); err != nil {
51+ var res v1pb. Policy
52+ if err := ProtojsonUnmarshaler .Unmarshal (body , & res ); err != nil {
5953 return nil , err
6054 }
6155
6256 return & res , nil
6357}
6458
6559// UpsertPolicy creates or updates the policy.
66- func (c * client ) UpsertPolicy (ctx context.Context , patch * api. PolicyPatchMessage ) (* api. PolicyMessage , error ) {
67- payload , err := json .Marshal (patch )
60+ func (c * client ) UpsertPolicy (ctx context.Context , policy * v1pb. Policy , updateMasks [] string ) (* v1pb. Policy , error ) {
61+ payload , err := protojson .Marshal (policy )
6862 if err != nil {
6963 return nil , err
7064 }
7165
72- paths := []string {}
73- if patch .InheritFromParent != nil {
74- paths = append (paths , "inherit_from_parent" )
75- }
76- if patch .DeploymentApprovalPolicy != nil ||
77- patch .BackupPlanPolicy != nil ||
78- patch .SensitiveDataPolicy != nil ||
79- patch .AccessControlPolicy != nil {
80- paths = append (paths , "payload" )
81- }
82-
83- 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 )))
66+ 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 )))
8467 if err != nil {
8568 return nil , err
8669 }
@@ -90,8 +73,8 @@ func (c *client) UpsertPolicy(ctx context.Context, patch *api.PolicyPatchMessage
9073 return nil , err
9174 }
9275
93- var res api. PolicyMessage
94- if err := json .Unmarshal (body , & res ); err != nil {
76+ var res v1pb. Policy
77+ if err := ProtojsonUnmarshaler .Unmarshal (body , & res ); err != nil {
9578 return nil , err
9679 }
9780
0 commit comments