@@ -38,6 +38,7 @@ type Interface interface {
3838 Restart (server * v1.Pod ) error
3939 GetConfig (server * v1.Pod ) (acidv1.Patroni , map [string ]string , error )
4040 SetConfig (server * v1.Pod , config map [string ]interface {}) error
41+ RewriteConfig (server * v1.Pod , config map [string ]interface {}) error
4142}
4243
4344// Patroni API client
@@ -113,6 +114,42 @@ func (p *Patroni) httpPostOrPatch(method string, url string, body *bytes.Buffer)
113114 return nil
114115}
115116
117+ func (p * Patroni ) httpPut (method string , url string , body * bytes.Buffer ) (err error ) {
118+ request , err := http .NewRequest (method , url , body )
119+ if err != nil {
120+ return fmt .Errorf ("could not create request: %v" , err )
121+ }
122+
123+ if p .logger != nil {
124+ p .logger .Debugf ("making %s http request: %s" , method , request .URL .String ())
125+ }
126+
127+ resp , err := p .httpClient .Do (request )
128+ if err != nil {
129+ return fmt .Errorf ("could not make request: %v" , err )
130+ }
131+ defer func () {
132+ if err2 := resp .Body .Close (); err2 != nil {
133+ if err != nil {
134+ err = fmt .Errorf ("could not close request: %v, prior error: %v" , err2 , err )
135+ } else {
136+ err = fmt .Errorf ("could not close request: %v" , err2 )
137+ }
138+ return
139+ }
140+ }()
141+
142+ if resp .StatusCode != http .StatusOK {
143+ bodyBytes , err := ioutil .ReadAll (resp .Body )
144+ if err != nil {
145+ return fmt .Errorf ("could not read response: %v" , err )
146+ }
147+
148+ return fmt .Errorf ("patroni returned '%s'" , string (bodyBytes ))
149+ }
150+ return nil
151+ }
152+
116153func (p * Patroni ) httpGet (url string ) (string , error ) {
117154 p .logger .Debugf ("making GET http request: %s" , url )
118155
@@ -178,6 +215,19 @@ func (p *Patroni) SetConfig(server *v1.Pod, config map[string]interface{}) error
178215 return p .httpPostOrPatch (http .MethodPatch , apiURLString + configPath , buf )
179216}
180217
218+ func (p * Patroni ) RewriteConfig (server * v1.Pod , config map [string ]interface {}) error {
219+ buf := & bytes.Buffer {}
220+ err := json .NewEncoder (buf ).Encode (config )
221+ if err != nil {
222+ return fmt .Errorf ("could not encode json: %v" , err )
223+ }
224+ apiURLString , err := apiURL (server )
225+ if err != nil {
226+ return err
227+ }
228+ return p .httpPut (http .MethodPut , apiURLString + configPath , buf )
229+ }
230+
181231// ClusterMembers array of cluster members from Patroni API
182232type ClusterMembers struct {
183233 Members []ClusterMember `json:"members"`
0 commit comments