-
Notifications
You must be signed in to change notification settings - Fork 582
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add parameter querying to the API (#143)
- Loading branch information
Showing
7 changed files
with
225 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,7 @@ | |
"nolint", | ||
"nosec", | ||
"oneof", | ||
"parameterscopeid", | ||
"protobuf", | ||
"provisionerd", | ||
"provisionersdk", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package coderd | ||
|
||
import ( | ||
"database/sql" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/go-chi/render" | ||
"github.com/google/uuid" | ||
|
||
"github.com/coder/coder/database" | ||
"github.com/coder/coder/httpapi" | ||
) | ||
|
||
// CreateParameterValueRequest is used to create a new parameter value for a scope. | ||
type CreateParameterValueRequest struct { | ||
Name string `json:"name"` | ||
SourceValue string `json:"source_value"` | ||
SourceScheme database.ParameterSourceScheme `json:"source_scheme"` | ||
DestinationScheme database.ParameterDestinationScheme `json:"destination_scheme"` | ||
DestinationValue string `json:"destination_value"` | ||
} | ||
|
||
// ParameterValue represents a set value for the scope. | ||
type ParameterValue struct { | ||
ID uuid.UUID `json:"id"` | ||
Name string `json:"name"` | ||
CreatedAt time.Time `json:"created_at"` | ||
UpdatedAt time.Time `json:"updated_at"` | ||
Scope database.ParameterScope `json:"scope"` | ||
ScopeID string `json:"scope_id"` | ||
SourceScheme database.ParameterSourceScheme `json:"source_scheme"` | ||
DestinationScheme database.ParameterDestinationScheme `json:"destination_scheme"` | ||
DestinationValue string `json:"destination_value"` | ||
} | ||
|
||
// Abstracts creating parameters into a single request/response format. | ||
// Callers are in charge of validating the requester has permissions to | ||
// perform the creation. | ||
func postParameterValueForScope(rw http.ResponseWriter, r *http.Request, db database.Store, scope database.ParameterScope, scopeID string) { | ||
var createRequest CreateParameterValueRequest | ||
if !httpapi.Read(rw, r, &createRequest) { | ||
return | ||
} | ||
parameterValue, err := db.InsertParameterValue(r.Context(), database.InsertParameterValueParams{ | ||
ID: uuid.New(), | ||
Name: createRequest.Name, | ||
CreatedAt: database.Now(), | ||
UpdatedAt: database.Now(), | ||
Scope: scope, | ||
ScopeID: scopeID, | ||
SourceScheme: createRequest.SourceScheme, | ||
SourceValue: createRequest.SourceValue, | ||
DestinationScheme: createRequest.DestinationScheme, | ||
DestinationValue: createRequest.DestinationValue, | ||
}) | ||
if err != nil { | ||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{ | ||
Message: fmt.Sprintf("insert parameter value: %s", err), | ||
}) | ||
return | ||
} | ||
|
||
render.Status(r, http.StatusCreated) | ||
render.JSON(rw, r, parameterValue) | ||
} | ||
|
||
// Abstracts returning parameters for a scope into a standardized | ||
// request/response format. Callers are responsible for checking | ||
// requester permissions. | ||
func parametersForScope(rw http.ResponseWriter, r *http.Request, db database.Store, req database.GetParameterValuesByScopeParams) { | ||
parameterValues, err := db.GetParameterValuesByScope(r.Context(), req) | ||
if errors.Is(err, sql.ErrNoRows) { | ||
err = nil | ||
parameterValues = []database.ParameterValue{} | ||
} | ||
if err != nil { | ||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{ | ||
Message: fmt.Sprintf("get parameter values: %s", err), | ||
}) | ||
return | ||
} | ||
|
||
apiParameterValues := make([]ParameterValue, 0, len(parameterValues)) | ||
for _, parameterValue := range parameterValues { | ||
apiParameterValues = append(apiParameterValues, convertParameterValue(parameterValue)) | ||
} | ||
|
||
render.Status(r, http.StatusOK) | ||
render.JSON(rw, r, apiParameterValues) | ||
} | ||
|
||
func convertParameterValue(parameterValue database.ParameterValue) ParameterValue { | ||
return ParameterValue{ | ||
ID: parameterValue.ID, | ||
Name: parameterValue.Name, | ||
CreatedAt: parameterValue.CreatedAt, | ||
UpdatedAt: parameterValue.UpdatedAt, | ||
Scope: parameterValue.Scope, | ||
ScopeID: parameterValue.ScopeID, | ||
SourceScheme: parameterValue.SourceScheme, | ||
DestinationScheme: parameterValue.DestinationScheme, | ||
DestinationValue: parameterValue.DestinationValue, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters