|
| 1 | +// Copyright 2025 Cloudbase Solutions SRL |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +// not use this file except in compliance with the License. You may obtain |
| 5 | +// 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, WITHOUT |
| 11 | +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +// License for the specific language governing permissions and limitations |
| 13 | +// under the License. |
| 14 | +package controllers |
| 15 | + |
| 16 | +import ( |
| 17 | + "encoding/json" |
| 18 | + "log/slog" |
| 19 | + "math" |
| 20 | + "net/http" |
| 21 | + "strconv" |
| 22 | + |
| 23 | + "github.com/gorilla/mux" |
| 24 | + |
| 25 | + gErrors "github.com/cloudbase/garm-provider-common/errors" |
| 26 | + "github.com/cloudbase/garm/params" |
| 27 | +) |
| 28 | + |
| 29 | +// swagger:route GET /gitea/credentials credentials ListGiteaCredentials |
| 30 | +// |
| 31 | +// List all credentials. |
| 32 | +// |
| 33 | +// Responses: |
| 34 | +// 200: Credentials |
| 35 | +// 400: APIErrorResponse |
| 36 | +func (a *APIController) ListGiteaCredentials(w http.ResponseWriter, r *http.Request) { |
| 37 | + ctx := r.Context() |
| 38 | + creds, err := a.r.ListGiteaCredentials(ctx) |
| 39 | + if err != nil { |
| 40 | + handleError(ctx, w, err) |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + w.Header().Set("Content-Type", "application/json") |
| 45 | + if err := json.NewEncoder(w).Encode(creds); err != nil { |
| 46 | + slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to encode response") |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// swagger:route POST /gitea/credentials credentials CreateGiteaCredentials |
| 51 | +// |
| 52 | +// Create a Gitea credential. |
| 53 | +// |
| 54 | +// Parameters: |
| 55 | +// + name: Body |
| 56 | +// description: Parameters used when creating a Gitea credential. |
| 57 | +// type: CreateGiteaCredentialsParams |
| 58 | +// in: body |
| 59 | +// required: true |
| 60 | +// |
| 61 | +// Responses: |
| 62 | +// 200: ForgeCredentials |
| 63 | +// 400: APIErrorResponse |
| 64 | +func (a *APIController) CreateGiteaCredential(w http.ResponseWriter, r *http.Request) { |
| 65 | + ctx := r.Context() |
| 66 | + |
| 67 | + var params params.CreateGiteaCredentialsParams |
| 68 | + if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil { |
| 69 | + slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to decode request") |
| 70 | + handleError(ctx, w, gErrors.ErrBadRequest) |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + cred, err := a.r.CreateGiteaCredentials(ctx, params) |
| 75 | + if err != nil { |
| 76 | + slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to create Gitea credential") |
| 77 | + handleError(ctx, w, err) |
| 78 | + return |
| 79 | + } |
| 80 | + w.Header().Set("Content-Type", "application/json") |
| 81 | + if err := json.NewEncoder(w).Encode(cred); err != nil { |
| 82 | + slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to encode response") |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// swagger:route GET /gitea/credentials/{id} credentials GetGiteaCredentials |
| 87 | +// |
| 88 | +// Get a Gitea credential. |
| 89 | +// |
| 90 | +// Parameters: |
| 91 | +// + name: id |
| 92 | +// description: ID of the Gitea credential. |
| 93 | +// type: integer |
| 94 | +// in: path |
| 95 | +// required: true |
| 96 | +// |
| 97 | +// Responses: |
| 98 | +// 200: ForgeCredentials |
| 99 | +// 400: APIErrorResponse |
| 100 | +func (a *APIController) GetGiteaCredential(w http.ResponseWriter, r *http.Request) { |
| 101 | + ctx := r.Context() |
| 102 | + vars := mux.Vars(r) |
| 103 | + idParam, ok := vars["id"] |
| 104 | + if !ok { |
| 105 | + slog.ErrorContext(ctx, "missing id in request") |
| 106 | + handleError(ctx, w, gErrors.ErrBadRequest) |
| 107 | + return |
| 108 | + } |
| 109 | + |
| 110 | + id, err := strconv.ParseUint(idParam, 10, 64) |
| 111 | + if err != nil { |
| 112 | + slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to parse id") |
| 113 | + handleError(ctx, w, gErrors.ErrBadRequest) |
| 114 | + return |
| 115 | + } |
| 116 | + |
| 117 | + if id > math.MaxUint { |
| 118 | + slog.With(slog.Any("error", err)).ErrorContext(ctx, "id is too large") |
| 119 | + handleError(ctx, w, gErrors.ErrBadRequest) |
| 120 | + return |
| 121 | + } |
| 122 | + |
| 123 | + cred, err := a.r.GetGiteaCredentials(ctx, uint(id)) |
| 124 | + if err != nil { |
| 125 | + slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to get Gitea credential") |
| 126 | + handleError(ctx, w, err) |
| 127 | + return |
| 128 | + } |
| 129 | + |
| 130 | + w.Header().Set("Content-Type", "application/json") |
| 131 | + if err := json.NewEncoder(w).Encode(cred); err != nil { |
| 132 | + slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to encode response") |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +// swagger:route DELETE /gitea/credentials/{id} credentials DeleteGiteaCredentials |
| 137 | +// |
| 138 | +// Delete a Gitea credential. |
| 139 | +// |
| 140 | +// Parameters: |
| 141 | +// + name: id |
| 142 | +// description: ID of the Gitea credential. |
| 143 | +// type: integer |
| 144 | +// in: path |
| 145 | +// required: true |
| 146 | +// |
| 147 | +// Responses: |
| 148 | +// default: APIErrorResponse |
| 149 | +func (a *APIController) DeleteGiteaCredential(w http.ResponseWriter, r *http.Request) { |
| 150 | + ctx := r.Context() |
| 151 | + vars := mux.Vars(r) |
| 152 | + idParam, ok := vars["id"] |
| 153 | + if !ok { |
| 154 | + slog.ErrorContext(ctx, "missing id in request") |
| 155 | + handleError(ctx, w, gErrors.ErrBadRequest) |
| 156 | + return |
| 157 | + } |
| 158 | + |
| 159 | + id, err := strconv.ParseUint(idParam, 10, 64) |
| 160 | + if err != nil { |
| 161 | + slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to parse id") |
| 162 | + handleError(ctx, w, gErrors.ErrBadRequest) |
| 163 | + return |
| 164 | + } |
| 165 | + |
| 166 | + if id > math.MaxUint { |
| 167 | + slog.With(slog.Any("error", err)).ErrorContext(ctx, "id is too large") |
| 168 | + handleError(ctx, w, gErrors.ErrBadRequest) |
| 169 | + return |
| 170 | + } |
| 171 | + |
| 172 | + if err := a.r.DeleteGiteaCredentials(ctx, uint(id)); err != nil { |
| 173 | + slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to delete Gitea credential") |
| 174 | + handleError(ctx, w, err) |
| 175 | + return |
| 176 | + } |
| 177 | + |
| 178 | + w.WriteHeader(http.StatusNoContent) |
| 179 | +} |
| 180 | + |
| 181 | +// swagger:route PUT /gitea/credentials/{id} credentials UpdateGiteaCredentials |
| 182 | +// |
| 183 | +// Update a Gitea credential. |
| 184 | +// |
| 185 | +// Parameters: |
| 186 | +// + name: id |
| 187 | +// description: ID of the Gitea credential. |
| 188 | +// type: integer |
| 189 | +// in: path |
| 190 | +// required: true |
| 191 | +// + name: Body |
| 192 | +// description: Parameters used when updating a Gitea credential. |
| 193 | +// type: UpdateGiteaCredentialsParams |
| 194 | +// in: body |
| 195 | +// required: true |
| 196 | +// |
| 197 | +// Responses: |
| 198 | +// 200: ForgeCredentials |
| 199 | +// 400: APIErrorResponse |
| 200 | +func (a *APIController) UpdateGiteaCredential(w http.ResponseWriter, r *http.Request) { |
| 201 | + ctx := r.Context() |
| 202 | + vars := mux.Vars(r) |
| 203 | + idParam, ok := vars["id"] |
| 204 | + if !ok { |
| 205 | + slog.ErrorContext(ctx, "missing id in request") |
| 206 | + handleError(ctx, w, gErrors.ErrBadRequest) |
| 207 | + return |
| 208 | + } |
| 209 | + |
| 210 | + id, err := strconv.ParseUint(idParam, 10, 64) |
| 211 | + if err != nil { |
| 212 | + slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to parse id") |
| 213 | + handleError(ctx, w, gErrors.ErrBadRequest) |
| 214 | + return |
| 215 | + } |
| 216 | + |
| 217 | + if id > math.MaxUint { |
| 218 | + slog.With(slog.Any("error", err)).ErrorContext(ctx, "id is too large") |
| 219 | + handleError(ctx, w, gErrors.ErrBadRequest) |
| 220 | + return |
| 221 | + } |
| 222 | + |
| 223 | + var params params.UpdateGiteaCredentialsParams |
| 224 | + if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil { |
| 225 | + slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to decode request") |
| 226 | + handleError(ctx, w, gErrors.ErrBadRequest) |
| 227 | + return |
| 228 | + } |
| 229 | + |
| 230 | + cred, err := a.r.UpdateGiteaCredentials(ctx, uint(id), params) |
| 231 | + if err != nil { |
| 232 | + slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to update Gitea credential") |
| 233 | + handleError(ctx, w, err) |
| 234 | + return |
| 235 | + } |
| 236 | + |
| 237 | + w.Header().Set("Content-Type", "application/json") |
| 238 | + if err := json.NewEncoder(w).Encode(cred); err != nil { |
| 239 | + slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to encode response") |
| 240 | + } |
| 241 | +} |
0 commit comments