|
| 1 | +// Copyright 2022 HAProxy Technologies |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package handlers |
| 16 | + |
| 17 | +import ( |
| 18 | + "github.com/go-openapi/runtime/middleware" |
| 19 | + client_native "github.com/haproxytech/client-native/v4" |
| 20 | + "github.com/haproxytech/client-native/v4/models" |
| 21 | + |
| 22 | + "github.com/haproxytech/dataplaneapi/haproxy" |
| 23 | + "github.com/haproxytech/dataplaneapi/misc" |
| 24 | + "github.com/haproxytech/dataplaneapi/operations/fcgi_app" |
| 25 | +) |
| 26 | + |
| 27 | +type CreateFCGIAppHandlerImpl struct { |
| 28 | + Client client_native.HAProxyClient |
| 29 | + ReloadAgent *haproxy.ReloadAgent |
| 30 | +} |
| 31 | + |
| 32 | +func (c CreateFCGIAppHandlerImpl) Handle(params fcgi_app.CreateFCGIAppParams, _ interface{}) middleware.Responder { |
| 33 | + var t string |
| 34 | + var v int64 |
| 35 | + |
| 36 | + if params.TransactionID != nil { |
| 37 | + t = *params.TransactionID |
| 38 | + } |
| 39 | + if params.Version != nil { |
| 40 | + v = *params.Version |
| 41 | + } |
| 42 | + |
| 43 | + if t != "" && *params.ForceReload { |
| 44 | + code := misc.ErrHTTPBadRequest |
| 45 | + |
| 46 | + e := &models.Error{ |
| 47 | + Message: misc.StringP("Both force_reload and transaction specified, specify only one"), |
| 48 | + Code: &code, |
| 49 | + } |
| 50 | + |
| 51 | + return fcgi_app.NewCreateFCGIAppDefault(int(*e.Code)).WithPayload(e) |
| 52 | + } |
| 53 | + |
| 54 | + configuration, err := c.Client.Configuration() |
| 55 | + if err != nil { |
| 56 | + e := misc.HandleError(err) |
| 57 | + return fcgi_app.NewCreateFCGIAppDefault(int(*e.Code)).WithPayload(e) |
| 58 | + } |
| 59 | + |
| 60 | + if err = configuration.CreateFCGIApplication(params.Data, t, v); err != nil { |
| 61 | + e := misc.HandleError(err) |
| 62 | + return fcgi_app.NewCreateFCGIAppDefault(int(*e.Code)).WithPayload(e) |
| 63 | + } |
| 64 | + |
| 65 | + if params.TransactionID == nil { |
| 66 | + if *params.ForceReload { |
| 67 | + if err = c.ReloadAgent.ForceReload(); err != nil { |
| 68 | + e := misc.HandleError(err) |
| 69 | + |
| 70 | + return fcgi_app.NewCreateFCGIAppDefault(int(*e.Code)).WithPayload(e) |
| 71 | + } |
| 72 | + |
| 73 | + return fcgi_app.NewCreateFCGIAppCreated().WithPayload(params.Data) |
| 74 | + } |
| 75 | + |
| 76 | + return fcgi_app.NewCreateFCGIAppAccepted().WithReloadID(c.ReloadAgent.Reload()).WithPayload(params.Data) |
| 77 | + } |
| 78 | + return fcgi_app.NewCreateFCGIAppAccepted().WithPayload(params.Data) |
| 79 | +} |
| 80 | + |
| 81 | +type DeleteFCGIAppHandlerImpl struct { |
| 82 | + ReloadAgent *haproxy.ReloadAgent |
| 83 | + Client client_native.HAProxyClient |
| 84 | +} |
| 85 | + |
| 86 | +func (d DeleteFCGIAppHandlerImpl) Handle(params fcgi_app.DeleteFCGIAppParams, _ interface{}) middleware.Responder { |
| 87 | + var t string |
| 88 | + var v int64 |
| 89 | + |
| 90 | + if params.TransactionID != nil { |
| 91 | + t = *params.TransactionID |
| 92 | + } |
| 93 | + if params.Version != nil { |
| 94 | + v = *params.Version |
| 95 | + } |
| 96 | + |
| 97 | + if t != "" && *params.ForceReload { |
| 98 | + code := misc.ErrHTTPBadRequest |
| 99 | + |
| 100 | + e := &models.Error{ |
| 101 | + Message: misc.StringP("Both force_reload and transaction specified, specify only one"), |
| 102 | + Code: &code, |
| 103 | + } |
| 104 | + |
| 105 | + return fcgi_app.NewDeleteFCGIAppDefault(int(*e.Code)).WithPayload(e) |
| 106 | + } |
| 107 | + |
| 108 | + configuration, err := d.Client.Configuration() |
| 109 | + if err != nil { |
| 110 | + e := misc.HandleError(err) |
| 111 | + return fcgi_app.NewDeleteFCGIAppDefault(int(*e.Code)).WithPayload(e) |
| 112 | + } |
| 113 | + |
| 114 | + if err = configuration.DeleteFCGIApplication(params.Name, t, v); err != nil { |
| 115 | + e := misc.HandleError(err) |
| 116 | + return fcgi_app.NewDeleteFCGIAppDefault(int(*e.Code)).WithPayload(e) |
| 117 | + } |
| 118 | + |
| 119 | + if params.TransactionID == nil { |
| 120 | + if *params.ForceReload { |
| 121 | + if err = d.ReloadAgent.ForceReload(); err != nil { |
| 122 | + e := misc.HandleError(err) |
| 123 | + |
| 124 | + return fcgi_app.NewDeleteFCGIAppDefault(int(*e.Code)).WithPayload(e) |
| 125 | + } |
| 126 | + |
| 127 | + return fcgi_app.NewDeleteFCGIAppNoContent() |
| 128 | + } |
| 129 | + |
| 130 | + return fcgi_app.NewDeleteFCGIAppAccepted().WithReloadID(d.ReloadAgent.Reload()) |
| 131 | + } |
| 132 | + |
| 133 | + return fcgi_app.NewDeleteFCGIAppAccepted() |
| 134 | +} |
| 135 | + |
| 136 | +type GetFCGIAppHandlerImpl struct { |
| 137 | + Client client_native.HAProxyClient |
| 138 | +} |
| 139 | + |
| 140 | +func (g GetFCGIAppHandlerImpl) Handle(params fcgi_app.GetFCGIAppParams, _ interface{}) middleware.Responder { |
| 141 | + var t string |
| 142 | + |
| 143 | + if params.TransactionID != nil { |
| 144 | + t = *params.TransactionID |
| 145 | + } |
| 146 | + |
| 147 | + configuration, err := g.Client.Configuration() |
| 148 | + if err != nil { |
| 149 | + e := misc.HandleError(err) |
| 150 | + |
| 151 | + return fcgi_app.NewGetFCGIAppDefault(int(*e.Code)).WithPayload(e) |
| 152 | + } |
| 153 | + |
| 154 | + v, r, err := configuration.GetFCGIApplication(params.Name, t) |
| 155 | + if err != nil { |
| 156 | + e := misc.HandleError(err) |
| 157 | + |
| 158 | + return fcgi_app.NewGetFCGIAppDefault(int(*e.Code)).WithPayload(e) |
| 159 | + } |
| 160 | + |
| 161 | + return fcgi_app.NewGetFCGIAppOK().WithPayload(&fcgi_app.GetFCGIAppOKBody{Version: v, Data: r}) |
| 162 | +} |
| 163 | + |
| 164 | +type GetFCGIAppsHandlerImpl struct { |
| 165 | + Client client_native.HAProxyClient |
| 166 | +} |
| 167 | + |
| 168 | +func (g GetFCGIAppsHandlerImpl) Handle(params fcgi_app.GetFCGIAppsParams, _ interface{}) middleware.Responder { |
| 169 | + var t string |
| 170 | + |
| 171 | + if params.TransactionID != nil { |
| 172 | + t = *params.TransactionID |
| 173 | + } |
| 174 | + |
| 175 | + configuration, err := g.Client.Configuration() |
| 176 | + if err != nil { |
| 177 | + e := misc.HandleError(err) |
| 178 | + |
| 179 | + return fcgi_app.NewGetFCGIAppsDefault(int(*e.Code)).WithPayload(e) |
| 180 | + } |
| 181 | + |
| 182 | + v, r, err := configuration.GetFCGIApplications(t) |
| 183 | + if err != nil { |
| 184 | + e := misc.HandleError(err) |
| 185 | + |
| 186 | + return fcgi_app.NewGetFCGIAppsDefault(int(*e.Code)).WithPayload(e) |
| 187 | + } |
| 188 | + |
| 189 | + return fcgi_app.NewGetFCGIAppsOK().WithPayload(&fcgi_app.GetFCGIAppsOKBody{Version: v, Data: r}) |
| 190 | +} |
| 191 | + |
| 192 | +type ReplaceFCGIAppHandlerImpl struct { |
| 193 | + Client client_native.HAProxyClient |
| 194 | + ReloadAgent *haproxy.ReloadAgent |
| 195 | +} |
| 196 | + |
| 197 | +func (r ReplaceFCGIAppHandlerImpl) Handle(params fcgi_app.ReplaceFCGIAppParams, _ interface{}) middleware.Responder { |
| 198 | + var t string |
| 199 | + var v int64 |
| 200 | + |
| 201 | + if params.TransactionID != nil { |
| 202 | + t = *params.TransactionID |
| 203 | + } |
| 204 | + |
| 205 | + if params.Version != nil { |
| 206 | + v = *params.Version |
| 207 | + } |
| 208 | + |
| 209 | + if t != "" && *params.ForceReload { |
| 210 | + code := misc.ErrHTTPBadRequest |
| 211 | + |
| 212 | + e := &models.Error{ |
| 213 | + Message: misc.StringP("Both force_reload and transaction specified, specify only one"), |
| 214 | + Code: &code, |
| 215 | + } |
| 216 | + |
| 217 | + return fcgi_app.NewReplaceFCGIAppDefault(int(*e.Code)).WithPayload(e) |
| 218 | + } |
| 219 | + |
| 220 | + configuration, err := r.Client.Configuration() |
| 221 | + if err != nil { |
| 222 | + e := misc.HandleError(err) |
| 223 | + |
| 224 | + return fcgi_app.NewReplaceFCGIAppDefault(int(*e.Code)).WithPayload(e) |
| 225 | + } |
| 226 | + |
| 227 | + params.Data.Name = params.Name |
| 228 | + |
| 229 | + if err = configuration.EditFCGIApplication(params.Name, params.Data, t, v); err != nil { |
| 230 | + e := misc.HandleError(err) |
| 231 | + |
| 232 | + return fcgi_app.NewReplaceFCGIAppDefault(int(*e.Code)).WithPayload(e) |
| 233 | + } |
| 234 | + |
| 235 | + if params.TransactionID == nil { |
| 236 | + if *params.ForceReload { |
| 237 | + if err = r.ReloadAgent.ForceReload(); err != nil { |
| 238 | + e := misc.HandleError(err) |
| 239 | + |
| 240 | + return fcgi_app.NewReplaceFCGIAppDefault(int(*e.Code)).WithPayload(e) |
| 241 | + } |
| 242 | + |
| 243 | + return fcgi_app.NewReplaceFCGIAppOK().WithPayload(params.Data) |
| 244 | + } |
| 245 | + |
| 246 | + return fcgi_app.NewReplaceFCGIAppAccepted().WithReloadID(r.ReloadAgent.Reload()).WithPayload(params.Data) |
| 247 | + } |
| 248 | + |
| 249 | + return fcgi_app.NewReplaceFCGIAppAccepted().WithPayload(params.Data) |
| 250 | +} |
0 commit comments