|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "reflect" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/superfly/percona-postgresql-operator/internal/pgbackrest-server/exec" |
| 11 | +) |
| 12 | + |
| 13 | +type InfoCommandOptions struct { |
| 14 | + // General Options |
| 15 | + Stanza string `json:"stanza"` // Required |
| 16 | + Config string `json:"config,omitempty"` |
| 17 | + ConfigIncludePath string `json:"config_include_path,omitempty"` |
| 18 | + ConfigPath string `json:"config_path,omitempty"` |
| 19 | + Output string `json:"output,omitempty"` |
| 20 | + |
| 21 | + // Backup Set & Filter Options |
| 22 | + BackupSet string `json:"set,omitempty"` |
| 23 | + DBInclude []string `json:"db_include,omitempty"` |
| 24 | + Type string `json:"type,omitempty"` |
| 25 | + |
| 26 | + // Archive Options |
| 27 | + ArchiveCheck bool `json:"archive_check,omitempty"` |
| 28 | + ArchiveCopy bool `json:"archive_copy,omitempty"` |
| 29 | + |
| 30 | + // Repository Options |
| 31 | + Repo int `json:"repo,omitempty"` |
| 32 | + RepoPath string `json:"repo_path,omitempty"` |
| 33 | + RepoType string `json:"repo_type,omitempty"` |
| 34 | + RepoHardlink bool `json:"repo_hardlink,omitempty"` |
| 35 | + RepoRetentionFull int `json:"repo_retention_full,omitempty"` |
| 36 | + RepoRetentionFullType string `json:"repo_retention_full_type,omitempty"` |
| 37 | + |
| 38 | + // S3 Repository Options |
| 39 | + RepoS3Bucket string `json:"repo_s3_bucket,omitempty"` |
| 40 | + RepoS3Endpoint string `json:"repo_s3_endpoint,omitempty"` |
| 41 | + RepoS3Region string `json:"repo_s3_region,omitempty"` |
| 42 | +} |
| 43 | + |
| 44 | +func (c InfoCommandOptions) AsSlice() []string { |
| 45 | + var cmd []string |
| 46 | + |
| 47 | + val := reflect.ValueOf(c) |
| 48 | + typ := val.Type() |
| 49 | + |
| 50 | + for i := range val.NumField() { |
| 51 | + jsonTag := typ.Field(i).Tag.Get("json") |
| 52 | + if jsonTag == "" || jsonTag == "-" { |
| 53 | + continue |
| 54 | + } |
| 55 | + |
| 56 | + field := val.Field(i) |
| 57 | + parts := strings.Split(jsonTag, ",") |
| 58 | + jsonKey := parts[0] |
| 59 | + isOmitEmpty := len(parts) > 1 && parts[1] == "omitempty" |
| 60 | + |
| 61 | + if isOmitEmpty && field.IsZero() { |
| 62 | + continue |
| 63 | + } |
| 64 | + |
| 65 | + flag := "--" + strings.ReplaceAll(jsonKey, "_", "-") |
| 66 | + |
| 67 | + switch field.Kind() { |
| 68 | + case reflect.String: |
| 69 | + if strVal := field.String(); strVal != "" || !isOmitEmpty { |
| 70 | + cmd = append(cmd, fmt.Sprintf(" %s=%s", flag, strVal)) |
| 71 | + } |
| 72 | + case reflect.Int, reflect.Int64: |
| 73 | + if intVal := field.Int(); intVal != 0 || !isOmitEmpty { |
| 74 | + cmd = append(cmd, fmt.Sprintf(" %s=%d", flag, intVal)) |
| 75 | + } |
| 76 | + case reflect.Bool: |
| 77 | + if field.Bool() { |
| 78 | + cmd = append(cmd, fmt.Sprintf(" %s", flag)) |
| 79 | + } |
| 80 | + case reflect.Slice: |
| 81 | + if field.Type().Elem().Kind() == reflect.String { |
| 82 | + for j := range field.Len() { |
| 83 | + cmd = append(cmd, fmt.Sprintf(" %s=%s", flag, field.Index(j).String())) |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return cmd |
| 90 | +} |
| 91 | + |
| 92 | +func InfoCommandHandler(w http.ResponseWriter, r *http.Request) { |
| 93 | + var opts InfoCommandOptions |
| 94 | + if err := json.NewDecoder(r.Body).Decode(&opts); err != nil { |
| 95 | + http.Error(w, fmt.Sprintf("could not decode json body: %v", err), http.StatusBadRequest) |
| 96 | + return |
| 97 | + } |
| 98 | + |
| 99 | + cmd := exec.BackrestCommand{ |
| 100 | + Command: "info", |
| 101 | + Opts: opts.AsSlice(), |
| 102 | + } |
| 103 | + |
| 104 | + stdout, _, err := cmd.Run(r.Context()) |
| 105 | + if err != nil { |
| 106 | + http.Error(w, fmt.Sprintf("failed to run pgbackrest info command: %v", err), http.StatusInternalServerError) |
| 107 | + return |
| 108 | + } |
| 109 | + |
| 110 | + // var output InfoOutput |
| 111 | + // if err := json.Unmarshal(stdout.Bytes(), &output); err != nil { |
| 112 | + // // return InfoCommandOutput{}, errors.Wrap(err, "failed to unmarshal pgBackRest info output") |
| 113 | + // http.Error(w, fmt.Sprintf("could not decode output from pgbackrest info command: %v", err), http.StatusInternalServerError) |
| 114 | + // return |
| 115 | + // } |
| 116 | + |
| 117 | + w.WriteHeader(http.StatusOK) |
| 118 | + w.Write(stdout.Bytes()) |
| 119 | +} |
0 commit comments