Skip to content

Commit

Permalink
fix(worker): be able to get external service configuration (#5166)
Browse files Browse the repository at this point in the history
  • Loading branch information
sguiheux authored May 11, 2020
1 parent 72da5f9 commit 7f83084
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 40 deletions.
1 change: 1 addition & 0 deletions engine/api/authentication/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func NewConsumerWorker(ctx context.Context, db gorp.SqlExecutor, name string, ha
sdk.AuthConsumerScopeProject,
sdk.AuthConsumerScopeRun,
sdk.AuthConsumerScopeRunExecution,
sdk.AuthConsumerScopeService,
),
IssuedAt: time.Now(),
}
Expand Down
17 changes: 12 additions & 5 deletions engine/api/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@ func (api *API) getExternalServiceHandler() service.Handler {

for _, s := range api.Config.Services {
if s.Type == typeService {
return service.WriteJSON(w, s, http.StatusOK)
extService := sdk.ExternalService{
HealthPath: s.HealthPath,
HealthPort: s.HealthPort,
Path: s.Path,
HealthURL: s.HealthURL,
Port: s.Port,
URL: s.URL,
}
return service.WriteJSON(w, extService, http.StatusOK)
}
}

return sdk.WrapError(sdk.ErrNotFound, "service %s not found", typeService)
}
}
Expand Down Expand Up @@ -60,9 +67,9 @@ func (api *API) postServiceRegisterHandler() service.Handler {
}
if srv != nil && !(srv.Type == data.Type) {
return sdk.WrapError(sdk.ErrForbidden, "cannot register service %s of type %s for consumer %s while existing service type is different", data.Name, data.Type, consumer.ID)
}
// Update or create the service
}

// Update or create the service
if srv != nil {
srv.Update(data)
if err := services.Update(ctx, tx, srv); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion engine/worker/internal/handler_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func Test_cachePushPullHandler(t *testing.T) {

// Prepare mock client for cds workers
ctrl := gomock.NewController(t)
m := mock_cdsclient.NewMockInterface(ctrl)
m := mock_cdsclient.NewMockWorkerInterface(ctrl)
wkPush.client = m
wkPull.client = m

Expand Down
26 changes: 26 additions & 0 deletions engine/worker/internal/handler_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package internal

import (
"context"
"fmt"
"github.com/gorilla/mux"
"net/http"

"github.com/ovh/cds/sdk/log"
)

func serviceHandler(ctx context.Context, wk *CurrentWorker) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
serviceType := vars["type"]

log.Debug("Getting service configuration...")
serviceConfig, err := wk.Client().ServiceConfigurationGet(ctx, serviceType)
if err != nil {
log.Warning(ctx, "unable to get data: %v", err)
writeError(w, r, fmt.Errorf("unable to get service configuration"))
}
writeJSON(w, serviceConfig, http.StatusOK)
return
}
}
1 change: 1 addition & 0 deletions engine/worker/internal/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (w *CurrentWorker) Serve(c context.Context) error {
r.HandleFunc("/download", LogMiddleware(downloadHandler(c, w)))
r.HandleFunc("/exit", LogMiddleware(exitHandler(c, w)))
r.HandleFunc("/key/{key}/install", LogMiddleware(keyInstallHandler(c, w)))
r.HandleFunc("/services/{type}", LogMiddleware(serviceHandler(c, w)))
r.HandleFunc("/tag", LogMiddleware(tagHandler(c, w)))
r.HandleFunc("/tmpl", LogMiddleware(tmplHandler(c, w)))
r.HandleFunc("/upload", LogMiddleware(uploadHandler(c, w)))
Expand Down
9 changes: 9 additions & 0 deletions sdk/cdsclient/client_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,12 @@ func (c *client) ServiceRegister(ctx context.Context, s sdk.Service) (*sdk.Servi
}
return &s, nil
}

func (c *client) ServiceConfigurationGet(ctx context.Context, t string) (*sdk.ExternalService, error) {
var serviceConf sdk.ExternalService
_, err := c.GetJSON(ctx, fmt.Sprintf("/services/%s", t), &serviceConf)
if err != nil {
return nil, err
}
return &serviceConf, nil
}
1 change: 1 addition & 0 deletions sdk/cdsclient/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ type WorkerInterface interface {
ProjectIntegrationGet(projectKey string, integrationName string, clearPassword bool) (sdk.ProjectIntegration, error)
QueueClient
Requirements() ([]sdk.Requirement, error)
ServiceConfigurationGet(context.Context, string) (*sdk.ExternalService, error)
WorkerClient
WorkflowRunArtifacts(projectKey string, name string, number int64) ([]sdk.WorkflowNodeRunArtifact, error)
WorkflowCachePush(projectKey, integrationName, ref string, tarContent io.Reader, size int) error
Expand Down
53 changes: 19 additions & 34 deletions sdk/cdsclient/mock_cdsclient/interface_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7f83084

Please sign in to comment.