Skip to content

Commit b6ea889

Browse files
authored
chore: multiple optimize (#136)
1 parent fc7509c commit b6ea889

33 files changed

+700
-397
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.8.4
1+
3.8.5

api/client.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ type UserFilter struct {
5555
type Client interface {
5656
// GetCaller returns the API caller.
5757
GetCaller() *v1pb.User
58+
// CheckResourceExist check if the resource exists.
59+
CheckResourceExist(ctx context.Context, name string) error
60+
// DeleteResource force delete the resource by name.
61+
DeleteResource(ctx context.Context, name string) error
5862

5963
// Instance
6064
// ListInstance will return instances.
@@ -65,8 +69,6 @@ type Client interface {
6569
CreateInstance(ctx context.Context, instanceID string, instance *v1pb.Instance) (*v1pb.Instance, error)
6670
// UpdateInstance updates the instance.
6771
UpdateInstance(ctx context.Context, patch *v1pb.Instance, updateMasks []string) (*v1pb.Instance, error)
68-
// DeleteInstance deletes the instance.
69-
DeleteInstance(ctx context.Context, instanceName string) error
7072
// UndeleteInstance undeletes the instance.
7173
UndeleteInstance(ctx context.Context, instanceName string) (*v1pb.Instance, error)
7274
// SyncInstanceSchema will trigger the schema sync for an instance.
@@ -105,8 +107,6 @@ type Client interface {
105107
CreateProject(ctx context.Context, projectID string, project *v1pb.Project) (*v1pb.Project, error)
106108
// UpdateProject updates the project.
107109
UpdateProject(ctx context.Context, patch *v1pb.Project, updateMask []string) (*v1pb.Project, error)
108-
// DeleteProject deletes the project.
109-
DeleteProject(ctx context.Context, projectName string) error
110110
// UndeleteProject undeletes the project.
111111
UndeleteProject(ctx context.Context, projectName string) (*v1pb.Project, error)
112112
// GetProjectIAMPolicy gets the project IAM policy by project full name.
@@ -141,16 +141,12 @@ type Client interface {
141141
GetUser(ctx context.Context, userName string) (*v1pb.User, error)
142142
// UpdateUser updates the user.
143143
UpdateUser(ctx context.Context, patch *v1pb.User, updateMasks []string) (*v1pb.User, error)
144-
// DeleteUser deletes the user by name.
145-
DeleteUser(ctx context.Context, userName string) error
146144
// UndeleteUser undeletes the user by name.
147145
UndeleteUser(ctx context.Context, userName string) (*v1pb.User, error)
148146

149147
// Role
150148
// ListRole will returns all roles.
151149
ListRole(ctx context.Context) (*v1pb.ListRolesResponse, error)
152-
// DeleteRole deletes the role by name.
153-
DeleteRole(ctx context.Context, name string) error
154150
// CreateRole creates the role.
155151
CreateRole(ctx context.Context, roleID string, role *v1pb.Role) (*v1pb.Role, error)
156152
// GetRole gets the role by full name.
@@ -167,8 +163,6 @@ type Client interface {
167163
GetGroup(ctx context.Context, name string) (*v1pb.Group, error)
168164
// UpdateGroup updates the group.
169165
UpdateGroup(ctx context.Context, patch *v1pb.Group, updateMasks []string) (*v1pb.Group, error)
170-
// DeleteGroup deletes the group by name.
171-
DeleteGroup(ctx context.Context, name string) error
172166

173167
// Workspace
174168
// GetWorkspaceIAMPolicy gets the workspace IAM policy.
@@ -183,8 +177,6 @@ type Client interface {
183177
GetReviewConfig(ctx context.Context, reviewName string) (*v1pb.ReviewConfig, error)
184178
// UpsertReviewConfig updates or creates the review config.
185179
UpsertReviewConfig(ctx context.Context, patch *v1pb.ReviewConfig, updateMasks []string) (*v1pb.ReviewConfig, error)
186-
// DeleteReviewConfig deletes the review config.
187-
DeleteReviewConfig(ctx context.Context, reviewName string) error
188180

189181
// Risk
190182
// ListRisk lists the risk.
@@ -195,8 +187,6 @@ type Client interface {
195187
CreateRisk(ctx context.Context, risk *v1pb.Risk) (*v1pb.Risk, error)
196188
// UpdateRisk updates the risk.
197189
UpdateRisk(ctx context.Context, patch *v1pb.Risk, updateMasks []string) (*v1pb.Risk, error)
198-
// DeleteRisk deletes the risk by name.
199-
DeleteRisk(ctx context.Context, name string) error
200190

201191
// ListDatabaseGroup list all database groups in a project.
202192
ListDatabaseGroup(ctx context.Context, project string) (*v1pb.ListDatabaseGroupsResponse, error)
@@ -206,6 +196,4 @@ type Client interface {
206196
GetDatabaseGroup(ctx context.Context, name string, view v1pb.DatabaseGroupView) (*v1pb.DatabaseGroup, error)
207197
// UpdateDatabaseGroup updates the database group.
208198
UpdateDatabaseGroup(ctx context.Context, patch *v1pb.DatabaseGroup, updateMasks []string) (*v1pb.DatabaseGroup, error)
209-
// DeleteDatabaseGroup deletes the database group by name.
210-
DeleteDatabaseGroup(ctx context.Context, name string) error
211199
}

client/client.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package client
33

44
import (
5+
"context"
56
"fmt"
67
"io"
78
"net/http"
@@ -72,3 +73,16 @@ func (c *client) doRequest(req *http.Request) ([]byte, error) {
7273
func (c *client) GetCaller() *v1pb.User {
7374
return c.caller
7475
}
76+
77+
// CheckResourceExist check if the resource exists.
78+
func (c *client) CheckResourceExist(ctx context.Context, name string) error {
79+
if _, err := c.getResource(ctx, name, ""); err != nil {
80+
return err
81+
}
82+
return nil
83+
}
84+
85+
// DeleteResource force delete the resource by name.
86+
func (c *client) DeleteResource(ctx context.Context, name string) error {
87+
return c.execDelete(ctx, name)
88+
}

client/common.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import (
1414
// ProtojsonUnmarshaler is the unmarshal for protocol.
1515
var ProtojsonUnmarshaler = protojson.UnmarshalOptions{DiscardUnknown: true}
1616

17-
// deleteResource deletes the resource by name.
18-
func (c *client) deleteResource(ctx context.Context, name string) error {
19-
req, err := http.NewRequestWithContext(ctx, "DELETE", fmt.Sprintf("%s/%s/%s", c.url, c.version, url.QueryEscape(name)), nil)
17+
// execDelete deletes the resource by name.
18+
func (c *client) execDelete(ctx context.Context, name string) error {
19+
req, err := http.NewRequestWithContext(ctx, "DELETE", fmt.Sprintf("%s/%s/%s?force=true", c.url, c.version, url.QueryEscape(name)), nil)
2020
if err != nil {
2121
return err
2222
}
@@ -42,7 +42,7 @@ func (c *client) undeleteResource(ctx context.Context, name string) ([]byte, err
4242
return body, nil
4343
}
4444

45-
// deleteResource deletes the resource by name.
45+
// updateResource update the resource.
4646
func (c *client) updateResource(ctx context.Context, name string, patch protoreflect.ProtoMessage, updateMasks []string, allowMissing bool) ([]byte, error) {
4747
payload, err := protojson.Marshal(patch)
4848
if err != nil {

client/database_group.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,3 @@ func (c *client) UpdateDatabaseGroup(ctx context.Context, patch *v1pb.DatabaseGr
8686

8787
return &res, nil
8888
}
89-
90-
// DeleteDatabaseGroup deletes the database group by name.
91-
func (c *client) DeleteDatabaseGroup(ctx context.Context, name string) error {
92-
return c.deleteResource(ctx, name)
93-
}

client/group.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,3 @@ func (c *client) UpdateGroup(ctx context.Context, patch *v1pb.Group, updateMasks
8686

8787
return &res, nil
8888
}
89-
90-
// DeleteGroup deletes the group by name.
91-
func (c *client) DeleteGroup(ctx context.Context, name string) error {
92-
return c.deleteResource(ctx, name)
93-
}

client/instance.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,6 @@ func (c *client) UpdateInstance(ctx context.Context, patch *v1pb.Instance, updat
170170
return &res, nil
171171
}
172172

173-
// DeleteInstance deletes the instance.
174-
func (c *client) DeleteInstance(ctx context.Context, instanceName string) error {
175-
return c.deleteResource(ctx, instanceName)
176-
}
177-
178173
// UndeleteInstance undeletes the instance.
179174
func (c *client) UndeleteInstance(ctx context.Context, instanceName string) (*v1pb.Instance, error) {
180175
body, err := c.undeleteResource(ctx, instanceName)

client/policy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,5 @@ func (c *client) UpsertPolicy(ctx context.Context, policy *v1pb.Policy, updateMa
6666

6767
// DeletePolicy deletes the policy.
6868
func (c *client) DeletePolicy(ctx context.Context, policyName string) error {
69-
return c.deleteResource(ctx, policyName)
69+
return c.execDelete(ctx, policyName)
7070
}

client/project.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,6 @@ func (c *client) UpdateProject(ctx context.Context, patch *v1pb.Project, updateM
268268
return &res, nil
269269
}
270270

271-
// DeleteProject deletes the project.
272-
func (c *client) DeleteProject(ctx context.Context, projectName string) error {
273-
return c.deleteResource(ctx, projectName)
274-
}
275-
276271
// UndeleteProject undeletes the project.
277272
func (c *client) UndeleteProject(ctx context.Context, projectName string) (*v1pb.Project, error) {
278273
body, err := c.undeleteResource(ctx, projectName)

client/review_config.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,3 @@ func (c *client) UpsertReviewConfig(ctx context.Context, patch *v1pb.ReviewConfi
5757

5858
return &res, nil
5959
}
60-
61-
// DeleteReviewConfig deletes the review config.
62-
func (c *client) DeleteReviewConfig(ctx context.Context, reviewName string) error {
63-
return c.deleteResource(ctx, reviewName)
64-
}

0 commit comments

Comments
 (0)