|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "log/slog" |
| 6 | + "net/http" |
| 7 | + |
| 8 | + gErrors "github.com/cloudbase/garm-provider-common/errors" |
| 9 | + "github.com/cloudbase/garm/params" |
| 10 | + "github.com/gorilla/mux" |
| 11 | +) |
| 12 | + |
| 13 | +// swagger:route POST /github/endpoints endpoints CreateGithubEndpoint |
| 14 | +// |
| 15 | +// Create a GitHub Endpoint. |
| 16 | +// |
| 17 | +// Parameters: |
| 18 | +// + name: Body |
| 19 | +// description: Parameters used when creating a GitHub endpoint. |
| 20 | +// type: CreateGithubEndpointParams |
| 21 | +// in: body |
| 22 | +// required: true |
| 23 | +// |
| 24 | +// Responses: |
| 25 | +// 200: GithubEndpoint |
| 26 | +// default: APIErrorResponse |
| 27 | +func (a *APIController) CreateGithubEndpoint(w http.ResponseWriter, r *http.Request) { |
| 28 | + ctx := r.Context() |
| 29 | + |
| 30 | + var params params.CreateGithubEndpointParams |
| 31 | + if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil { |
| 32 | + slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to decode request") |
| 33 | + handleError(ctx, w, gErrors.ErrBadRequest) |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + endpoint, err := a.r.CreateGithubEndpoint(ctx, params) |
| 38 | + if err != nil { |
| 39 | + slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to create GitHub endpoint") |
| 40 | + handleError(ctx, w, err) |
| 41 | + return |
| 42 | + } |
| 43 | + w.Header().Set("Content-Type", "application/json") |
| 44 | + if err := json.NewEncoder(w).Encode(endpoint); err != nil { |
| 45 | + slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to encode response") |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// swagger:route GET /github/endpoints endpoints ListGithubEndpoints |
| 50 | +// |
| 51 | +// List all GitHub Endpoints. |
| 52 | +// |
| 53 | +// Responses: |
| 54 | +// 200: GithubEndpoints |
| 55 | +// default: APIErrorResponse |
| 56 | +func (a *APIController) ListGithubEndpoints(w http.ResponseWriter, r *http.Request) { |
| 57 | + ctx := r.Context() |
| 58 | + |
| 59 | + endpoints, err := a.r.ListGithubEndpoints(ctx) |
| 60 | + if err != nil { |
| 61 | + slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to list GitHub endpoints") |
| 62 | + handleError(ctx, w, err) |
| 63 | + return |
| 64 | + } |
| 65 | + w.Header().Set("Content-Type", "application/json") |
| 66 | + if err := json.NewEncoder(w).Encode(endpoints); err != nil { |
| 67 | + slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to encode response") |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// swagger:route GET /github/endpoints/{name} endpoints GetGithubEndpoint |
| 72 | +// |
| 73 | +// Get a GitHub Endpoint. |
| 74 | +// |
| 75 | +// Parameters: |
| 76 | +// + name: name |
| 77 | +// description: The name of the GitHub endpoint. |
| 78 | +// type: string |
| 79 | +// in: path |
| 80 | +// required: true |
| 81 | +// |
| 82 | +// Responses: |
| 83 | +// 200: GithubEndpoint |
| 84 | +// default: APIErrorResponse |
| 85 | +func (a *APIController) GetGithubEndpoint(w http.ResponseWriter, r *http.Request) { |
| 86 | + ctx := r.Context() |
| 87 | + |
| 88 | + vars := mux.Vars(r) |
| 89 | + name, ok := vars["name"] |
| 90 | + if !ok { |
| 91 | + slog.ErrorContext(ctx, "missing name in request") |
| 92 | + handleError(ctx, w, gErrors.ErrBadRequest) |
| 93 | + return |
| 94 | + } |
| 95 | + endpoint, err := a.r.GetGithubEndpoint(ctx, name) |
| 96 | + if err != nil { |
| 97 | + slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to get GitHub endpoint") |
| 98 | + handleError(ctx, w, err) |
| 99 | + return |
| 100 | + } |
| 101 | + w.Header().Set("Content-Type", "application/json") |
| 102 | + if err := json.NewEncoder(w).Encode(endpoint); err != nil { |
| 103 | + slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to encode response") |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +// swagger:route DELETE /github/endpoints/{name} endpoints DeleteGithubEndpoint |
| 108 | +// |
| 109 | +// Delete a GitHub Endpoint. |
| 110 | +// |
| 111 | +// Parameters: |
| 112 | +// + name: name |
| 113 | +// description: The name of the GitHub endpoint. |
| 114 | +// type: string |
| 115 | +// in: path |
| 116 | +// required: true |
| 117 | +// |
| 118 | +// Responses: |
| 119 | +// default: APIErrorResponse |
| 120 | +func (a *APIController) DeleteGithubEndpoint(w http.ResponseWriter, r *http.Request) { |
| 121 | + ctx := r.Context() |
| 122 | + |
| 123 | + vars := mux.Vars(r) |
| 124 | + name, ok := vars["name"] |
| 125 | + if !ok { |
| 126 | + slog.ErrorContext(ctx, "missing name in request") |
| 127 | + handleError(ctx, w, gErrors.ErrBadRequest) |
| 128 | + return |
| 129 | + } |
| 130 | + if err := a.r.DeleteGithubEndpoint(ctx, name); err != nil { |
| 131 | + slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to delete GitHub endpoint") |
| 132 | + handleError(ctx, w, err) |
| 133 | + return |
| 134 | + } |
| 135 | + w.WriteHeader(http.StatusNoContent) |
| 136 | +} |
| 137 | + |
| 138 | +// swagger:route PUT /github/endpoints/{name} endpoints UpdateGithubEndpoint |
| 139 | +// |
| 140 | +// Update a GitHub Endpoint. |
| 141 | +// |
| 142 | +// Parameters: |
| 143 | +// + name: name |
| 144 | +// description: The name of the GitHub endpoint. |
| 145 | +// type: string |
| 146 | +// in: path |
| 147 | +// required: true |
| 148 | +// + name: Body |
| 149 | +// description: Parameters used when updating a GitHub endpoint. |
| 150 | +// type: UpdateGithubEndpointParams |
| 151 | +// in: body |
| 152 | +// required: true |
| 153 | +// |
| 154 | +// Responses: |
| 155 | +// 200: GithubEndpoint |
| 156 | +// default: APIErrorResponse |
| 157 | +func (a *APIController) UpdateGithubEndpoint(w http.ResponseWriter, r *http.Request) { |
| 158 | + ctx := r.Context() |
| 159 | + |
| 160 | + vars := mux.Vars(r) |
| 161 | + name, ok := vars["name"] |
| 162 | + if !ok { |
| 163 | + slog.ErrorContext(ctx, "missing name in request") |
| 164 | + handleError(ctx, w, gErrors.ErrBadRequest) |
| 165 | + return |
| 166 | + } |
| 167 | + |
| 168 | + var params params.UpdateGithubEndpointParams |
| 169 | + if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil { |
| 170 | + slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to decode request") |
| 171 | + handleError(ctx, w, gErrors.ErrBadRequest) |
| 172 | + return |
| 173 | + } |
| 174 | + |
| 175 | + endpoint, err := a.r.UpdateGithubEndpoint(ctx, name, params) |
| 176 | + if err != nil { |
| 177 | + slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to update GitHub endpoint") |
| 178 | + handleError(ctx, w, err) |
| 179 | + return |
| 180 | + } |
| 181 | + w.Header().Set("Content-Type", "application/json") |
| 182 | + if err := json.NewEncoder(w).Encode(endpoint); err != nil { |
| 183 | + slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to encode response") |
| 184 | + } |
| 185 | +} |
0 commit comments