|
| 1 | +package coderd |
| 2 | + |
| 3 | +import ( |
| 4 | + "database/sql" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/go-chi/chi/v5" |
| 11 | + "github.com/go-chi/render" |
| 12 | + "github.com/google/uuid" |
| 13 | + |
| 14 | + "github.com/coder/coder/database" |
| 15 | + "github.com/coder/coder/httpapi" |
| 16 | +) |
| 17 | + |
| 18 | +type ParameterScope string |
| 19 | + |
| 20 | +const ( |
| 21 | + ParameterOrganization ParameterScope = "organization" |
| 22 | + ParameterProject ParameterScope = "project" |
| 23 | + ParameterUser ParameterScope = "user" |
| 24 | + ParameterWorkspace ParameterScope = "workspace" |
| 25 | +) |
| 26 | + |
| 27 | +// Parameter represents a set value for the scope. |
| 28 | +type Parameter struct { |
| 29 | + ID uuid.UUID `db:"id" json:"id"` |
| 30 | + CreatedAt time.Time `db:"created_at" json:"created_at"` |
| 31 | + UpdatedAt time.Time `db:"updated_at" json:"updated_at"` |
| 32 | + Scope ParameterScope `db:"scope" json:"scope"` |
| 33 | + ScopeID string `db:"scope_id" json:"scope_id"` |
| 34 | + Name string `db:"name" json:"name"` |
| 35 | + SourceScheme database.ParameterSourceScheme `db:"source_scheme" json:"source_scheme"` |
| 36 | + DestinationScheme database.ParameterDestinationScheme `db:"destination_scheme" json:"destination_scheme"` |
| 37 | +} |
| 38 | + |
| 39 | +// CreateParameterRequest is used to create a new parameter value for a scope. |
| 40 | +type CreateParameterRequest struct { |
| 41 | + Name string `json:"name" validate:"required"` |
| 42 | + SourceValue string `json:"source_value" validate:"required"` |
| 43 | + SourceScheme database.ParameterSourceScheme `json:"source_scheme" validate:"oneof=data,required"` |
| 44 | + DestinationScheme database.ParameterDestinationScheme `json:"destination_scheme" validate:"oneof=environment_variable provisioner_variable,required"` |
| 45 | +} |
| 46 | + |
| 47 | +func (api *api) postParameter(rw http.ResponseWriter, r *http.Request) { |
| 48 | + var createRequest CreateParameterRequest |
| 49 | + if !httpapi.Read(rw, r, &createRequest) { |
| 50 | + return |
| 51 | + } |
| 52 | + scope, id, valid := readScopeAndID(rw, r) |
| 53 | + if !valid { |
| 54 | + return |
| 55 | + } |
| 56 | + parameterValue, err := api.Database.GetParameterValueByScopeAndName(r.Context(), database.GetParameterValueByScopeAndNameParams{ |
| 57 | + Scope: scope, |
| 58 | + ScopeID: id, |
| 59 | + Name: createRequest.Name, |
| 60 | + }) |
| 61 | + if err == nil { |
| 62 | + httpapi.Write(rw, http.StatusConflict, httpapi.Response{ |
| 63 | + Message: fmt.Sprintf("a parameter already exists in scope %q with name %q", scope, createRequest.Name), |
| 64 | + }) |
| 65 | + return |
| 66 | + } |
| 67 | + if !errors.Is(err, sql.ErrNoRows) { |
| 68 | + httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{ |
| 69 | + Message: fmt.Sprintf("get parameter value: %s", err), |
| 70 | + }) |
| 71 | + return |
| 72 | + } |
| 73 | + parameterValue, err = api.Database.InsertParameterValue(r.Context(), database.InsertParameterValueParams{ |
| 74 | + ID: uuid.New(), |
| 75 | + Name: createRequest.Name, |
| 76 | + CreatedAt: database.Now(), |
| 77 | + UpdatedAt: database.Now(), |
| 78 | + Scope: scope, |
| 79 | + ScopeID: id, |
| 80 | + SourceScheme: createRequest.SourceScheme, |
| 81 | + SourceValue: createRequest.SourceValue, |
| 82 | + DestinationScheme: createRequest.DestinationScheme, |
| 83 | + }) |
| 84 | + if err != nil { |
| 85 | + httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{ |
| 86 | + Message: fmt.Sprintf("insert parameter value: %s", err), |
| 87 | + }) |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + render.Status(r, http.StatusCreated) |
| 92 | + render.JSON(rw, r, convertParameterValue(parameterValue)) |
| 93 | +} |
| 94 | + |
| 95 | +func (api *api) parameters(rw http.ResponseWriter, r *http.Request) { |
| 96 | + scope, id, valid := readScopeAndID(rw, r) |
| 97 | + if !valid { |
| 98 | + return |
| 99 | + } |
| 100 | + parameterValues, err := api.Database.GetParameterValuesByScope(r.Context(), database.GetParameterValuesByScopeParams{ |
| 101 | + Scope: scope, |
| 102 | + ScopeID: id, |
| 103 | + }) |
| 104 | + if errors.Is(err, sql.ErrNoRows) { |
| 105 | + err = nil |
| 106 | + } |
| 107 | + if err != nil { |
| 108 | + httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{ |
| 109 | + Message: fmt.Sprintf("get parameter values by scope: %s", err), |
| 110 | + }) |
| 111 | + return |
| 112 | + } |
| 113 | + apiParameterValues := make([]Parameter, 0, len(parameterValues)) |
| 114 | + for _, parameterValue := range parameterValues { |
| 115 | + apiParameterValues = append(apiParameterValues, convertParameterValue(parameterValue)) |
| 116 | + } |
| 117 | + |
| 118 | + render.Status(r, http.StatusOK) |
| 119 | + render.JSON(rw, r, apiParameterValues) |
| 120 | +} |
| 121 | + |
| 122 | +func (api *api) deleteParameter(rw http.ResponseWriter, r *http.Request) { |
| 123 | + scope, id, valid := readScopeAndID(rw, r) |
| 124 | + if !valid { |
| 125 | + return |
| 126 | + } |
| 127 | + name := chi.URLParam(r, "name") |
| 128 | + parameterValue, err := api.Database.GetParameterValueByScopeAndName(r.Context(), database.GetParameterValueByScopeAndNameParams{ |
| 129 | + Scope: scope, |
| 130 | + ScopeID: id, |
| 131 | + Name: name, |
| 132 | + }) |
| 133 | + if errors.Is(err, sql.ErrNoRows) { |
| 134 | + httpapi.Write(rw, http.StatusNotFound, httpapi.Response{ |
| 135 | + Message: fmt.Sprintf("parameter doesn't exist in the provided scope with name %q", name), |
| 136 | + }) |
| 137 | + return |
| 138 | + } |
| 139 | + if err != nil { |
| 140 | + httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{ |
| 141 | + Message: fmt.Sprintf("get parameter value: %s", err), |
| 142 | + }) |
| 143 | + return |
| 144 | + } |
| 145 | + err = api.Database.DeleteParameterValueByID(r.Context(), parameterValue.ID) |
| 146 | + if err != nil { |
| 147 | + httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{ |
| 148 | + Message: fmt.Sprintf("delete parameter: %s", err), |
| 149 | + }) |
| 150 | + return |
| 151 | + } |
| 152 | + httpapi.Write(rw, http.StatusOK, httpapi.Response{ |
| 153 | + Message: "parameter deleted", |
| 154 | + }) |
| 155 | +} |
| 156 | + |
| 157 | +func convertParameterValue(parameterValue database.ParameterValue) Parameter { |
| 158 | + return Parameter{ |
| 159 | + ID: parameterValue.ID, |
| 160 | + CreatedAt: parameterValue.CreatedAt, |
| 161 | + UpdatedAt: parameterValue.UpdatedAt, |
| 162 | + Scope: ParameterScope(parameterValue.Scope), |
| 163 | + ScopeID: parameterValue.ScopeID, |
| 164 | + Name: parameterValue.Name, |
| 165 | + SourceScheme: parameterValue.SourceScheme, |
| 166 | + DestinationScheme: parameterValue.DestinationScheme, |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +func readScopeAndID(rw http.ResponseWriter, r *http.Request) (scope database.ParameterScope, id string, valid bool) { |
| 171 | + switch chi.URLParam(r, "scope") { |
| 172 | + case string(ParameterOrganization): |
| 173 | + scope = database.ParameterScopeOrganization |
| 174 | + case string(ParameterProject): |
| 175 | + scope = database.ParameterScopeProject |
| 176 | + case string(ParameterUser): |
| 177 | + scope = database.ParameterScopeUser |
| 178 | + case string(ParameterWorkspace): |
| 179 | + scope = database.ParameterScopeWorkspace |
| 180 | + default: |
| 181 | + httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{ |
| 182 | + Message: fmt.Sprintf("invalid scope %q", scope), |
| 183 | + }) |
| 184 | + return |
| 185 | + } |
| 186 | + |
| 187 | + id = chi.URLParam(r, "id") |
| 188 | + valid = true |
| 189 | + return |
| 190 | +} |
0 commit comments