44 "bytes"
55 "encoding/base64"
66 "encoding/json"
7- "io"
87 "net/http"
98 "net/url"
109 "strings"
@@ -97,6 +96,26 @@ func (c *RegistrarClient) setZosVersion(v string, safeToUpgrade bool) (err error
9796 return errors .Wrap (err , "failed to construct registrar url" )
9897 }
9998
99+ sendRequest := func (body bytes.Buffer ) (resp * http.Response , err error ) {
100+ req , err := http .NewRequest ("PUT" , url , & body )
101+ if err != nil {
102+ return resp , errors .Wrap (err , "failed to construct http request to the registrar" )
103+ }
104+
105+ authHeader , err := c .signRequest (time .Now ().Unix ())
106+ if err != nil {
107+ return resp , errors .Wrap (err , "failed to sign request" )
108+ }
109+ req .Header .Set ("X-Auth" , authHeader )
110+ req .Header .Set ("Content-Type" , "application/json" )
111+
112+ resp , err = c .httpClient .Do (req )
113+ if err != nil {
114+ return resp , errors .Wrap (err , "failed to send request to get zos version from the registrar" )
115+ }
116+ return resp , nil
117+ }
118+
100119 version := ZosVersion {
101120 Version : v ,
102121 SafeToUpgrade : safeToUpgrade ,
@@ -108,27 +127,35 @@ func (c *RegistrarClient) setZosVersion(v string, safeToUpgrade bool) (err error
108127 return errors .Wrap (err , "failed to encode request body" )
109128 }
110129
111- req , err := http . NewRequest ( "PUT" , url , & body )
130+ resp , err := sendRequest ( body )
112131 if err != nil {
113- return errors . Wrap ( err , "failed to construct http request to the registrar" )
132+ return err
114133 }
134+ defer resp .Body .Close ()
115135
116- authHeader , err := c . signRequest ( time . Now (). Unix ())
117- if err != nil {
118- return errors . Wrap ( err , "failed to sign request" )
119- }
120- req . Header . Set ( "X-Auth" , authHeader )
121- req . Header . Set ( "Content-Type" , "application/json" )
136+ if resp . StatusCode == http . StatusBadRequest {
137+ // fallback to old encoded format
138+ jsonData , err := json . Marshal ( version )
139+ if err != nil {
140+ return errors . Wrap ( err , "failed to marshal zos version" )
141+ }
122142
123- resp , err := c .httpClient .Do (req )
124- if err != nil {
125- return errors .Wrap (err , "failed to send request to get zos version from the registrar" )
126- }
143+ encodedVersion := struct {
144+ Version string `json:"version"`
145+ }{
146+ Version : base64 .StdEncoding .EncodeToString (jsonData ),
147+ }
127148
128- if resp == nil {
129- return errors .New ("no response received" )
149+ jsonData , err = json .Marshal (encodedVersion )
150+ if err != nil {
151+ return errors .Wrap (err , "failed to marshal zos version in hex format" )
152+ }
153+
154+ resp , err = sendRequest (* bytes .NewBuffer (jsonData ))
155+ if err != nil {
156+ return err
157+ }
130158 }
131- defer resp .Body .Close ()
132159
133160 if resp .StatusCode != http .StatusOK {
134161 return parseResponseError (resp .Body )
0 commit comments