4
4
"bytes"
5
5
"encoding/base64"
6
6
"encoding/json"
7
- "io"
8
7
"net/http"
9
8
"net/url"
10
9
"strings"
@@ -97,6 +96,26 @@ func (c *RegistrarClient) setZosVersion(v string, safeToUpgrade bool) (err error
97
96
return errors .Wrap (err , "failed to construct registrar url" )
98
97
}
99
98
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
+
100
119
version := ZosVersion {
101
120
Version : v ,
102
121
SafeToUpgrade : safeToUpgrade ,
@@ -108,27 +127,35 @@ func (c *RegistrarClient) setZosVersion(v string, safeToUpgrade bool) (err error
108
127
return errors .Wrap (err , "failed to encode request body" )
109
128
}
110
129
111
- req , err := http . NewRequest ( "PUT" , url , & body )
130
+ resp , err := sendRequest ( body )
112
131
if err != nil {
113
- return errors . Wrap ( err , "failed to construct http request to the registrar" )
132
+ return err
114
133
}
134
+ defer resp .Body .Close ()
115
135
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
+ }
122
142
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
+ }
127
148
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
+ }
130
158
}
131
- defer resp .Body .Close ()
132
159
133
160
if resp .StatusCode != http .StatusOK {
134
161
return parseResponseError (resp .Body )
0 commit comments