This is an automated email from the ASF dual-hosted git repository.

miaoliyao pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/shardingsphere-on-cloud.git


The following commit(s) were added to refs/heads/main by this push:
     new 96a3bb7  feat(pitr):cli-agent api integration and add 
httputils,custome err (#245)
96a3bb7 is described below

commit 96a3bb74b1ca6531d8b38c7a1a3a48320f2d9ed6
Author: lltgo <[email protected]>
AuthorDate: Mon Mar 6 18:00:36 2023 +0800

    feat(pitr):cli-agent api integration and add httputils,custome err (#245)
---
 pitr/cli/go.mod                                    |   1 +
 pitr/cli/go.sum                                    |   2 +
 pitr/cli/internal/pkg/agent-server.go              |  58 ++++++++++-
 .../pkg/{agent-server.go => agent-server_test.go}  |  27 +++++-
 .../pkg/{agent-server.go => model/as_backup.go}    |  26 ++++-
 .../internal/pkg/{agent-server.go => xerr/err.go}  |  32 +++++-
 pitr/cli/pkg/httputils/req.go                      | 107 +++++++++++++++++++++
 7 files changed, 247 insertions(+), 6 deletions(-)

diff --git a/pitr/cli/go.mod b/pitr/cli/go.mod
index a2234a3..72119b0 100644
--- a/pitr/cli/go.mod
+++ b/pitr/cli/go.mod
@@ -12,6 +12,7 @@ require (
        github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // 
indirect
        github.com/google/go-cmp v0.5.9 // indirect
        github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
+       github.com/google/uuid v1.3.0 // indirect
        github.com/inconshreveable/mousetrap v1.0.1 // indirect
        github.com/onsi/ginkgo/v2 v2.8.3 // indirect
        github.com/onsi/gomega v1.27.1 // indirect
diff --git a/pitr/cli/go.sum b/pitr/cli/go.sum
index 831a320..1928d7f 100644
--- a/pitr/cli/go.sum
+++ b/pitr/cli/go.sum
@@ -15,6 +15,8 @@ github.com/google/go-cmp v0.5.9 
h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
 github.com/google/go-cmp v0.5.9/go.mod 
h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
 github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 
h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE=
 github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod 
h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
+github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
+github.com/google/uuid v1.3.0/go.mod 
h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
 github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod 
h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
 github.com/inconshreveable/mousetrap v1.0.1 
h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
 github.com/inconshreveable/mousetrap v1.0.1/go.mod 
h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
diff --git a/pitr/cli/internal/pkg/agent-server.go 
b/pitr/cli/internal/pkg/agent-server.go
index a21fd42..5531b12 100644
--- a/pitr/cli/internal/pkg/agent-server.go
+++ b/pitr/cli/internal/pkg/agent-server.go
@@ -17,4 +17,60 @@
 
 package pkg
 
-//TODO
+import (
+       "context"
+       "fmt"
+       "github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg/model"
+       "github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg/xerr"
+       "github.com/apache/shardingsphere-on-cloud/pitr/cli/pkg/httputils"
+       "github.com/google/uuid"
+       "net/http"
+)
+
+type agentServer struct {
+       addr string
+
+       _apiBackup     string
+       _apiRestore    string
+       _apiShowDetail string
+       _apiShowList   string
+}
+
+func NewAgentServer(addr string) *agentServer {
+       return &agentServer{
+               addr: addr,
+
+               _apiBackup:     "/api/backup",
+               _apiRestore:    "/api/restore",
+               _apiShowDetail: "/api/show",
+               _apiShowList:   "/api/show/list",
+       }
+}
+
+func (as *agentServer) Backup(in *model.BackupIn) (string, error) {
+       url := fmt.Sprintf("%s%s", as.addr, as._apiBackup)
+
+       out := &model.BackupOutResp{}
+       httpCode, err := httputils.NewRequest(context.Background(), 
http.MethodPost, url).
+               Header(map[string]string{
+                       "x-request-id": uuid.New().String(),
+                       "content-type": "application/json",
+               }).
+               Body(in).
+               Send(out)
+       if err != nil {
+               efmt := "httputils.NewRequest[url=%s,body=%v,out=%v] return 
err=%s,wrap=%w"
+               return "", fmt.Errorf(efmt, url, in, out, err, 
xerr.NewCliErr(xerr.Unknown))
+       }
+
+       if httpCode != http.StatusOK {
+               return "", fmt.Errorf("unknown http status[code=%d],err=%w", 
httpCode, xerr.NewCliErr(xerr.InvalidHttpStatus))
+       }
+
+       if out.Code != 0 {
+               asErr := xerr.NewAgentServerErr(out.Code, out.Msg)
+               return "", fmt.Errorf("agent server 
error[code=%d,msg=%s],err=%w", out.Code, out.Msg, asErr)
+       }
+
+       return out.Data.ID, nil
+}
diff --git a/pitr/cli/internal/pkg/agent-server.go 
b/pitr/cli/internal/pkg/agent-server_test.go
similarity index 57%
copy from pitr/cli/internal/pkg/agent-server.go
copy to pitr/cli/internal/pkg/agent-server_test.go
index a21fd42..218bd31 100644
--- a/pitr/cli/internal/pkg/agent-server.go
+++ b/pitr/cli/internal/pkg/agent-server_test.go
@@ -17,4 +17,29 @@
 
 package pkg
 
-//TODO
+import (
+       "fmt"
+       "github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg/model"
+       "testing"
+)
+
+func TestAgentServer_Backup(t *testing.T) {
+       t.SkipNow()
+       //Note:just for test api,you need map you own host.
+       as := NewAgentServer("http://agent-server:18080";)
+
+       backupID, err := as.Backup(&model.BackupIn{
+               DbPort:       5432,
+               DbName:       "omm",
+               Username:     "og",
+               Password:     "1234567890@SphereEx",
+               DnBackupPath: "/home/omm/data",
+               DnThreadsNum: 1,
+               DnBackupMode: "FULL",
+               Instance:     "ins-default-0",
+       })
+       if err != nil {
+               panic(err)
+       }
+       fmt.Println(backupID)
+}
diff --git a/pitr/cli/internal/pkg/agent-server.go 
b/pitr/cli/internal/pkg/model/as_backup.go
similarity index 57%
copy from pitr/cli/internal/pkg/agent-server.go
copy to pitr/cli/internal/pkg/model/as_backup.go
index a21fd42..e0cc1cf 100644
--- a/pitr/cli/internal/pkg/agent-server.go
+++ b/pitr/cli/internal/pkg/model/as_backup.go
@@ -15,6 +15,28 @@
 * limitations under the License.
  */
 
-package pkg
+package model
 
-//TODO
+type (
+       BackupIn struct {
+               DbPort   uint16 `json:"db_port"`
+               DbName   string `json:"db_name"`
+               Username string `json:"username"`
+               Password string `json:"password"`
+
+               DnBackupPath string `json:"dn_backup_path"`
+               DnThreadsNum uint8  `json:"dn_threads_num"`
+               DnBackupMode string `json:"dn_backup_mode"`
+               Instance     string `json:"instance"`
+       }
+
+       BackupOut struct {
+               ID string `json:"backup_id"`
+       }
+
+       BackupOutResp struct {
+               Code int       `json:"code" validate:"required"`
+               Msg  string    `json:"msg" validate:"required"`
+               Data BackupOut `json:"data"`
+       }
+)
diff --git a/pitr/cli/internal/pkg/agent-server.go 
b/pitr/cli/internal/pkg/xerr/err.go
similarity index 64%
copy from pitr/cli/internal/pkg/agent-server.go
copy to pitr/cli/internal/pkg/xerr/err.go
index a21fd42..d9302f8 100644
--- a/pitr/cli/internal/pkg/agent-server.go
+++ b/pitr/cli/internal/pkg/xerr/err.go
@@ -15,6 +15,34 @@
 * limitations under the License.
  */
 
-package pkg
+package xerr
 
-//TODO
+import "fmt"
+
+type (
+       service string
+       err     struct {
+               msg string
+       }
+)
+
+const (
+       Unknown           = "Unknown error"
+       InvalidHttpStatus = "Invalid http status"
+)
+
+func (e *err) Error() string {
+       return e.msg
+}
+
+func NewCliErr(msg string) error {
+       return &err{
+               msg: msg,
+       }
+}
+
+func NewAgentServerErr(code int, msg string) error {
+       return &err{
+               msg: fmt.Sprintf("agent server err[code=%d,msg=%s]", code, msg),
+       }
+}
diff --git a/pitr/cli/pkg/httputils/req.go b/pitr/cli/pkg/httputils/req.go
new file mode 100644
index 0000000..fc93c6c
--- /dev/null
+++ b/pitr/cli/pkg/httputils/req.go
@@ -0,0 +1,107 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You under the Apache License, Version 2.0
+* (the "License"); you may not use this file except in compliance with
+* the License.  You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+ */
+
+package httputils
+
+import (
+       "bytes"
+       "context"
+       "encoding/json"
+       "fmt"
+       "io"
+       "net/http"
+)
+
+type req struct {
+       ctx    context.Context
+       method string
+       header map[string]string
+       url    string
+       body   any
+       query  map[string]string
+}
+
+func NewRequest(ctx context.Context, method, url string) *req {
+       r := &req{
+               ctx:    ctx,
+               method: method,
+               url:    url,
+       }
+       return r
+}
+
+func (r *req) Header(h map[string]string) *req {
+       r.header = h
+       return r
+}
+
+func (r *req) Body(b any) *req {
+       r.body = b
+       return r
+}
+
+func (r *req) Query(m map[string]string) *req {
+       r.query = m
+       return r
+}
+
+func (r *req) Send(body any) (int, error) {
+       var (
+               bs  []byte
+               err error
+       )
+
+       if r.body != nil {
+               bs, err = json.Marshal(r.body)
+               if err != nil {
+                       return -1, fmt.Errorf("json.Marshal return err=%w", err)
+               }
+       }
+
+       _req, err := http.NewRequestWithContext(r.ctx, r.method, r.url, 
bytes.NewReader(bs))
+       if err != nil {
+               return -1, fmt.Errorf("new request failure,err=%w", err)
+       }
+
+       for k, v := range r.header {
+               _req.Header.Set(k, v)
+       }
+
+       for k, v := range r.query {
+               values := _req.URL.Query()
+               values.Add(k, v)
+               _req.URL.RawQuery = values.Encode()
+       }
+
+       c := &http.Client{}
+       resp, err := c.Do(_req)
+       if err != nil {
+               return -1, fmt.Errorf("http request err=%w", err)
+       }
+
+       all, err := io.ReadAll(resp.Body)
+       if err != nil {
+               return -1, fmt.Errorf("invalid response,err=%w", err)
+       }
+       if body != nil {
+               if err = json.Unmarshal(all, body); err != nil {
+                       return -1, fmt.Errorf("json unmarshal return err=%w", 
err)
+               }
+       }
+
+       return resp.StatusCode, nil
+}

Reply via email to