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 ad84b45  feat(pitr):cli-agent api integration (#246)
ad84b45 is described below

commit ad84b45007ce207940a4559a3298c84ec9ec6381
Author: lltgo <[email protected]>
AuthorDate: Mon Mar 6 18:58:28 2023 +0800

    feat(pitr):cli-agent api integration (#246)
    
    * feat(pitr):cli-agent api integration,restore
    
    * feat(pitr):cli-agent api integration,showDetail
    
    * feat(pitr):cli-agent api integration,showList
---
 pitr/cli/internal/pkg/agent-server.go              | 84 ++++++++++++++++++++++
 pitr/cli/internal/pkg/agent-server_test.go         | 70 ++++++++++++++++++
 .../{agent-server_test.go => model/as_restore.go}  | 40 ++++-------
 pitr/cli/internal/pkg/model/as_show.go             | 61 ++++++++++++++++
 4 files changed, 230 insertions(+), 25 deletions(-)

diff --git a/pitr/cli/internal/pkg/agent-server.go 
b/pitr/cli/internal/pkg/agent-server.go
index 5531b12..7af91b7 100644
--- a/pitr/cli/internal/pkg/agent-server.go
+++ b/pitr/cli/internal/pkg/agent-server.go
@@ -74,3 +74,87 @@ func (as *agentServer) Backup(in *model.BackupIn) (string, 
error) {
 
        return out.Data.ID, nil
 }
+
+func (as *agentServer) Restore(in *model.RestoreIn) error {
+       url := fmt.Sprintf("%s%s", as.addr, as._apiRestore)
+
+       out := &model.RestoreResp{}
+       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 nil
+}
+
+func (as *agentServer) ShowDetail(in *model.ShowDetailIn) (*model.BackupInfo, 
error) {
+       url := fmt.Sprintf("%s%s", as.addr, as._apiShowDetail)
+
+       out := &model.BackupDetailResp{}
+       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 nil, fmt.Errorf(efmt, url, in, out, err, 
xerr.NewCliErr(xerr.Unknown))
+       }
+
+       if httpCode != http.StatusOK {
+               return nil, 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 nil, fmt.Errorf("agent server 
error[code=%d,msg=%s],err=%w", out.Code, out.Msg, asErr)
+       }
+
+       return &out.Data, nil
+}
+
+func (as *agentServer) ShowList(in *model.ShowListIn) ([]model.BackupInfo, 
error) {
+       url := fmt.Sprintf("%s%s", as.addr, as._apiShowList)
+
+       out := &model.BackupListResp{}
+       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 nil, fmt.Errorf(efmt, url, in, out, err, 
xerr.NewCliErr(xerr.Unknown))
+       }
+
+       if httpCode != http.StatusOK {
+               return nil, 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 nil, fmt.Errorf("agent server 
error[code=%d,msg=%s],err=%w", out.Code, out.Msg, asErr)
+       }
+
+       return out.Data, nil
+}
diff --git a/pitr/cli/internal/pkg/agent-server_test.go 
b/pitr/cli/internal/pkg/agent-server_test.go
index 218bd31..46c7668 100644
--- a/pitr/cli/internal/pkg/agent-server_test.go
+++ b/pitr/cli/internal/pkg/agent-server_test.go
@@ -18,6 +18,7 @@
 package pkg
 
 import (
+       "encoding/json"
        "fmt"
        "github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg/model"
        "testing"
@@ -43,3 +44,72 @@ func TestAgentServer_Backup(t *testing.T) {
        }
        fmt.Println(backupID)
 }
+
+func TestAgentServer_Restore(t *testing.T) {
+       t.SkipNow()
+       //Note:just for test api,you need map you own host.
+       as := NewAgentServer("http://agent-server:18080";)
+
+       err := as.Restore(&model.RestoreIn{
+               DbPort:       5432,
+               DbName:       "omm",
+               Username:     "og",
+               Password:     "1234567890@SphereEx",
+               DnBackupPath: "/home/omm/data",
+               Instance:     "ins-default-0",
+               DnBackupId:   "RR3FIC",
+       })
+       if err != nil {
+               panic(err)
+       }
+       fmt.Println("Success")
+}
+
+func TestAgentServer_ShowDetail(t *testing.T) {
+       t.SkipNow()
+       //Note:just for test api,you need map you own host.
+       as := NewAgentServer("http://agent-server:18080";)
+
+       backupInfo, err := as.ShowDetail(&model.ShowDetailIn{
+               DbPort:       5432,
+               DbName:       "omm",
+               Username:     "og",
+               Password:     "1234567890@SphereEx",
+               DnBackupPath: "/home/omm/data",
+               Instance:     "ins-default-0",
+               DnBackupId:   "RR3FIC",
+       })
+       if err != nil {
+               panic(err)
+       }
+
+       indent, err := json.MarshalIndent(backupInfo, "", "  ")
+       if err != nil {
+               panic(err)
+       }
+       fmt.Println(string(indent))
+}
+
+func TestAgentServer_ShowList(t *testing.T) {
+       t.SkipNow()
+       //Note:just for test api,you need map you own host.
+       as := NewAgentServer("http://agent-server:18080";)
+
+       list, err := as.ShowList(&model.ShowListIn{
+               DbPort:       5432,
+               DbName:       "omm",
+               Username:     "og",
+               Password:     "1234567890@SphereEx",
+               DnBackupPath: "/home/omm/data",
+               Instance:     "ins-default-0",
+       })
+       if err != nil {
+               panic(err)
+       }
+
+       indent, err := json.MarshalIndent(list, "", "  ")
+       if err != nil {
+               panic(err)
+       }
+       fmt.Println(string(indent))
+}
diff --git a/pitr/cli/internal/pkg/agent-server_test.go 
b/pitr/cli/internal/pkg/model/as_restore.go
similarity index 57%
copy from pitr/cli/internal/pkg/agent-server_test.go
copy to pitr/cli/internal/pkg/model/as_restore.go
index 218bd31..015c871 100644
--- a/pitr/cli/internal/pkg/agent-server_test.go
+++ b/pitr/cli/internal/pkg/model/as_restore.go
@@ -15,31 +15,21 @@
 * limitations under the License.
  */
 
-package pkg
+package model
 
-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";)
+type (
+       RestoreIn struct {
+               DbPort       uint16 `json:"db_port"`
+               DbName       string `json:"db_name"`
+               Username     string `json:"username"`
+               Password     string `json:"password"`
+               Instance     string `json:"instance"`
+               DnBackupPath string `json:"dn_backup_path"`
+               DnBackupId   string `json:"dn_backup_id"`
+       }
 
-       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)
+       RestoreResp struct {
+               Code int    `json:"code" validate:"required"`
+               Msg  string `json:"msg" validate:"required"`
        }
-       fmt.Println(backupID)
-}
+)
diff --git a/pitr/cli/internal/pkg/model/as_show.go 
b/pitr/cli/internal/pkg/model/as_show.go
new file mode 100644
index 0000000..231db16
--- /dev/null
+++ b/pitr/cli/internal/pkg/model/as_show.go
@@ -0,0 +1,61 @@
+/*
+* 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 model
+
+type (
+       ShowDetailIn struct {
+               DbPort       uint16 `json:"db_port"`
+               DbName       string `json:"db_name"`
+               Username     string `json:"username"`
+               Password     string `json:"password"`
+               DnBackupId   string `json:"dn_backup_id"`
+               DnBackupPath string `json:"dn_backup_path"`
+               Instance     string `json:"instance"`
+       }
+
+       ShowListIn 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"`
+               Instance     string `json:"instance"`
+       }
+
+       BackupInfo struct {
+               Id        string `json:"dn_backup_id"`
+               Path      string `json:"dn_backup_path"`
+               Mode      string `json:"db_backup_mode"`
+               Instance  string `json:"instance"`
+               StartTime string `json:"start_time"`
+               EndTime   string `json:"end_time"`
+               Status    string `json:"status"`
+       }
+
+       BackupDetailResp struct {
+               Code int        `json:"code" validate:"required"`
+               Msg  string     `json:"msg" validate:"required"`
+               Data BackupInfo `json:"data"`
+       }
+
+       BackupListResp struct {
+               Code int          `json:"code" validate:"required"`
+               Msg  string       `json:"msg" validate:"required"`
+               Data []BackupInfo `json:"data"`
+       }
+)

Reply via email to