Skip to content

Commit 2134496

Browse files
committed
add fallback mechanism go version decoding
1 parent 7982eea commit 2134496

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

node-registrar/client/zos_version.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ package client
22

33
import (
44
"bytes"
5+
"encoding/base64"
56
"encoding/json"
7+
"io"
68
"net/http"
79
"net/url"
10+
"strings"
811
"time"
912

1013
"github.com/pkg/errors"
@@ -59,6 +62,27 @@ func (c *RegistrarClient) getZosVersion() (version ZosVersion, err error) {
5962
return version, err
6063
}
6164

65+
err = json.Unmarshal(bodyBytes, &version)
66+
if err != nil {
67+
// try decoding base64 version
68+
var versionString string
69+
err = json.Unmarshal(bodyBytes, &versionString)
70+
if err != nil {
71+
return version, err
72+
}
73+
74+
decodedVersion, err := base64.StdEncoding.DecodeString(versionString)
75+
if err != nil {
76+
return version, err
77+
}
78+
79+
correctedJSON := strings.ReplaceAll(string(decodedVersion), "'", "\"")
80+
81+
err = json.Unmarshal([]byte(correctedJSON), &version)
82+
83+
return version, err
84+
}
85+
6286
return
6387
}
6488

node-registrar/pkg/server/handlers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -720,8 +720,6 @@ type ZOSVersionRequest struct {
720720
SafeToUpgrade bool `json:"safe_to_upgrade" binding:"required"`
721721
}
722722

723-
var versionRegex = regexp.MustCompile(`^v\d+\.\d+\.\d+$`)
724-
725723
// @Summary Set ZOS Version
726724
// @Description Sets the ZOS version
727725
// @Tags ZOS
@@ -837,8 +835,10 @@ func validateTimestampHint(timestampHint int64) error {
837835
return nil
838836
}
839837

838+
// addVersionValidato registers a custom validator for version
840839
func addVersionValidator() error {
841-
// Register the custom validation
840+
versionRegex := regexp.MustCompile(`^v\d+\.\d+\.\d+$`)
841+
842842
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
843843
if err := v.RegisterValidation("versionfmt", func(fl validator.FieldLevel) bool {
844844
version := fl.Field().String()

0 commit comments

Comments
 (0)