-
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.
refactor: Allow provisioner jobs to be disconnected from projects (#194)
* Nest jobs under an organization * Rename project parameter to parameter schema * Update references when computing project parameters * Add files endpoint * Allow one-off project import jobs * Allow variables to be injected that are not defined by the schema * Update API to use jobs first * Fix CLI tests * Fix linting * Fix hex length for files table * Reduce memory allocation for windows
- Loading branch information
Showing
37 changed files
with
1,373 additions
and
988 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package coderd | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/go-chi/render" | ||
|
||
"github.com/coder/coder/database" | ||
"github.com/coder/coder/httpapi" | ||
"github.com/coder/coder/httpmw" | ||
) | ||
|
||
type UploadFileResponse struct { | ||
Hash string `json:"hash"` | ||
} | ||
|
||
func (api *api) postFiles(rw http.ResponseWriter, r *http.Request) { | ||
apiKey := httpmw.APIKey(r) | ||
contentType := r.Header.Get("Content-Type") | ||
|
||
switch contentType { | ||
case "application/x-tar": | ||
default: | ||
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{ | ||
Message: fmt.Sprintf("unsupported content type: %s", contentType), | ||
}) | ||
return | ||
} | ||
|
||
r.Body = http.MaxBytesReader(rw, r.Body, 10*(10<<20)) | ||
data, err := io.ReadAll(r.Body) | ||
if err != nil { | ||
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{ | ||
Message: fmt.Sprintf("read file: %s", err), | ||
}) | ||
return | ||
} | ||
hashBytes := sha256.Sum256(data) | ||
file, err := api.Database.InsertFile(r.Context(), database.InsertFileParams{ | ||
Hash: hex.EncodeToString(hashBytes[:]), | ||
CreatedBy: apiKey.UserID, | ||
CreatedAt: database.Now(), | ||
Mimetype: contentType, | ||
Data: data, | ||
}) | ||
if err != nil { | ||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{ | ||
Message: fmt.Sprintf("insert file: %s", err), | ||
}) | ||
return | ||
} | ||
render.Status(r, http.StatusCreated) | ||
render.JSON(rw, r, UploadFileResponse{ | ||
Hash: file.Hash, | ||
}) | ||
} |
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,30 @@ | ||
package coderd_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/coder/coder/coderd/coderdtest" | ||
"github.com/coder/coder/codersdk" | ||
) | ||
|
||
func TestPostFiles(t *testing.T) { | ||
t.Parallel() | ||
t.Run("BadContentType", func(t *testing.T) { | ||
t.Parallel() | ||
client := coderdtest.New(t) | ||
_ = coderdtest.CreateInitialUser(t, client) | ||
_, err := client.UploadFile(context.Background(), "bad", []byte{'a'}) | ||
require.Error(t, err) | ||
}) | ||
|
||
t.Run("Insert", func(t *testing.T) { | ||
t.Parallel() | ||
client := coderdtest.New(t) | ||
_ = coderdtest.CreateInitialUser(t, client) | ||
_, err := client.UploadFile(context.Background(), codersdk.ContentTypeTar, make([]byte, 1024)) | ||
require.NoError(t, err) | ||
}) | ||
} |
Oops, something went wrong.