Skip to content

Commit

Permalink
feat(api,ui): first iteration on worker model view (#6400)
Browse files Browse the repository at this point in the history
  • Loading branch information
sguiheux authored Jan 2, 2023
1 parent 182d73c commit b29d61c
Show file tree
Hide file tree
Showing 29 changed files with 829 additions and 90 deletions.
3 changes: 3 additions & 0 deletions engine/api/api_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,8 @@ func (api *API) InitRouter() {
r.Handle("/v2/hatchery/heartbeat", nil, r.POSTv2(api.postHatcheryHeartbeatHandler))
r.Handle("/v2/hatchery/{hatcheryIdentifier}", nil, r.GETv2(api.getHatcheryHandler), r.DELETEv2(api.deleteHatcheryHandler))

r.Handle("/v2/jsonschema/{type}", nil, r.GETv2(api.getJsonSchemaHandler))

r.Handle("/v2/organization", nil, r.POSTv2(api.postOrganizationHandler), r.GETv2(api.getOrganizationsHandler))
r.Handle("/v2/organization/{organizationIdentifier}", nil, r.GETv2(api.getOrganizationHandler), r.DELETEv2(api.deleteOrganizationHandler))

Expand All @@ -462,6 +464,7 @@ func (api *API) InitRouter() {
r.Handle("/v2/project/{projectKey}/vcs/{vcsIdentifier}/repository/{repositoryIdentifier}/analysis", nil, r.GETv2(api.getProjectRepositoryAnalysesHandler))
r.Handle("/v2/project/{projectKey}/vcs/{vcsIdentifier}/repository/{repositoryIdentifier}/analysis/{analysisID}", nil, r.GETv2(api.getProjectRepositoryAnalysisHandler))
r.Handle("/v2/project/{projectKey}/vcs/{vcsIdentifier}/repository/{repositoryIdentifier}/entities", nil, r.GETv2(api.getEntitiesHandler))
r.Handle("/v2/project/{projectKey}/vcs/{vcsIdentifier}/repository/{repositoryIdentifier}/entities/{entityType}/{entityName}", nil, r.GETv2(api.getEntityHandler))
r.Handle("/v2/project/{projectKey}/vcs/{vcsIdentifier}/repository/{repositoryIdentifier}/hook/regen", nil, r.POSTv2(api.postRepositoryHookRegenKeyHandler))
r.Handle("/v2/project/{projectKey}/vcs/{vcsIdentifier}/repository/{repositoryIdentifier}/workermodel", nil, r.GETv2(api.getWorkerModelsV2Handler))
r.Handle("/v2/project/{projectKey}/vcs/{vcsIdentifier}/repository/{repositoryIdentifier}/workermodel/template", nil, r.GETv2(api.getWorkerModelTemplatesHandler))
Expand Down
58 changes: 58 additions & 0 deletions engine/api/v2_entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,61 @@ func (api *API) getEntitiesHandler() ([]service.RbacChecker, service.Handler) {
return service.WriteJSON(w, result, http.StatusOK)
}
}

func (api *API) getEntityHandler() ([]service.RbacChecker, service.Handler) {
return service.RBAC(api.projectRead),
func(ctx context.Context, w http.ResponseWriter, req *http.Request) error {
vars := mux.Vars(req)
pKey := vars["projectKey"]
vcsIdentifier, err := url.PathUnescape(vars["vcsIdentifier"])
if err != nil {
return sdk.NewError(sdk.ErrWrongRequest, err)
}
repositoryIdentifier, err := url.PathUnescape(vars["repositoryIdentifier"])
if err != nil {
return sdk.WithStack(err)
}
entityType := vars["entityType"]
entityName := vars["entityName"]

branch := QueryString(req, "branch")

vcsProject, err := api.getVCSByIdentifier(ctx, pKey, vcsIdentifier)
if err != nil {
return err
}

repo, err := api.getRepositoryByIdentifier(ctx, vcsProject.ID, repositoryIdentifier)
if err != nil {
return err
}

if branch == "" {
tx, err := api.mustDB().Begin()
if err != nil {
return err
}
vcsClient, err := repositoriesmanager.AuthorizedClient(ctx, tx, api.Cache, pKey, vcsProject.Name)
if err != nil {
_ = tx.Rollback()
return err
}
defaultBranch, err := vcsClient.Branch(ctx, repo.Name, sdk.VCSBranchFilters{Default: true})
if err != nil {
_ = tx.Rollback()
return err
}
if err := tx.Commit(); err != nil {
_ = tx.Rollback()
return err
}
branch = defaultBranch.DisplayID
}

entity, err := entity.LoadByBranchTypeName(ctx, api.mustDB(), repo.ID, branch, entityType, entityName)
if err != nil {
return err
}
return service.WriteJSON(w, entity, http.StatusOK)
}
}
90 changes: 90 additions & 0 deletions engine/api/v2_entities_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package api

import (
"context"
"encoding/json"
"fmt"
"github.com/ovh/cds/engine/api/entity"
"github.com/ovh/cds/engine/api/project"
"github.com/ovh/cds/engine/api/repository"
"github.com/ovh/cds/engine/api/test"
"github.com/ovh/cds/engine/api/test/assets"
"github.com/ovh/cds/engine/api/vcs"
"github.com/ovh/cds/sdk"
"github.com/stretchr/testify/require"
"net/http/httptest"
"testing"
"time"
)

func TestGetEntitiesHandler(t *testing.T) {
api, db, _ := newTestAPI(t)

p := assets.InsertTestProject(t, db, api.Cache, sdk.RandomString(10), sdk.RandomString(10))
u, pass := assets.InsertAdminUser(t, db)

vcsProject := &sdk.VCSProject{
Name: "the-name",
Type: "github",
Auth: sdk.VCSAuthProject{Username: "the-username", Token: "the-token"},
Description: "the-username",
ProjectID: p.ID,
}

err := vcs.Insert(context.TODO(), db, vcsProject)
require.NoError(t, err)
require.NotEmpty(t, vcsProject.ID)

repo := sdk.ProjectRepository{
Name: "myrepo",
Created: time.Now(),
VCSProjectID: vcsProject.ID,
CreatedBy: "me",
CloneURL: "myurl",
}
require.NoError(t, repository.Insert(context.TODO(), db, &repo))

myscret := "my-registry-password"
encodedSecret, err := project.EncryptWithBuiltinKey(context.TODO(), db, p.ID, "mysecret", myscret)
require.NoError(t, err)

e := sdk.Entity{
Name: "model1",
Commit: "123456",
Branch: "master",
Type: sdk.EntityTypeWorkerModel,
ProjectRepositoryID: repo.ID,
ProjectKey: p.Key,
Data: fmt.Sprintf(`name: model1
type: docker
spec:
image: monimage
cmd: curl {{.API}}/download/worker/linux/$(uname -m) -o worker && chmod +x worker && exec ./worker
password: %s
shell: sh -c
envs:
MYVAR: toto`, encodedSecret),
}
require.NoError(t, entity.Insert(context.TODO(), db, &e))

vars := map[string]string{
"projectKey": p.Key,
"vcsIdentifier": vcsProject.ID,
"repositoryIdentifier": repo.Name,
"entityType": sdk.EntityTypeWorkerModel,
"entityName": "model1",
}
uri := api.Router.GetRouteV2("GET", api.getEntityHandler, vars)
test.NotEmpty(t, uri)
req := assets.NewAuthentifiedRequest(t, u, pass, "GET", uri+"?branch=master", nil)

w := httptest.NewRecorder()
api.Router.Mux.ServeHTTP(w, req)
require.Equal(t, 200, w.Code)

body := w.Body.Bytes()
var wm sdk.Entity
require.NoError(t, json.Unmarshal(body, &wm))

t.Logf("%+v", wm)
}
27 changes: 27 additions & 0 deletions engine/api/v2_jsonschema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package api

import (
"context"
"net/http"

"github.com/gorilla/mux"
"github.com/invopop/jsonschema"

"github.com/ovh/cds/engine/service"
"github.com/ovh/cds/sdk"
)

func (api *API) getJsonSchemaHandler() ([]service.RbacChecker, service.Handler) {
return nil,
func(ctx context.Context, w http.ResponseWriter, req *http.Request) error {
vars := mux.Vars(req)
t := vars["type"]

var schema *jsonschema.Schema
switch t {
case sdk.EntityTypeWorkerModel:
schema = sdk.GetWorkerModelJsonSchema()
}
return service.WriteJSON(w, schema, http.StatusOK)
}
}
4 changes: 4 additions & 0 deletions sdk/doc/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ func CleanURLParameter(u string) string {
u = "hatchery-identifier"
case "workerModelName":
u = "worker-model-name"
case "entityName":
u = "entity-name"
case "entityType":
u = "entity-type"
}
return u
}
Expand Down
9 changes: 7 additions & 2 deletions ui/angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
"glob": "*",
"input": "node_modules/@ant-design/icons-angular/src/inline-svg/",
"output": "/assets/"
},
{
"glob": "**/*",
"input": "node_modules/monaco-editor/min/vs",
"output": "/assets/vs/"
}
],
"styles": [
Expand Down Expand Up @@ -71,7 +76,7 @@
"node_modules/js-yaml/dist/js-yaml.js",
"resources/cds-hint.js",
"node_modules/ansi_up/ansi_up.js",
"node_modules/prismjs/prism.js",
"node_modules/prismjs/prism.js"
],
"allowedCommonJsDependencies": [
"raven-js",
Expand Down Expand Up @@ -166,7 +171,7 @@
"node_modules/codemirror/addon/edit/matchbrackets.js",
"node_modules/codemirror/addon/display/autorefresh.js",
"resources/cds-hint.js",
"node_modules/ansi_up/ansi_up.js",
"node_modules/ansi_up/ansi_up.js"
],
"styles": [
"node_modules/dragula/dist/dragula.css",
Expand Down
Loading

0 comments on commit b29d61c

Please sign in to comment.