Skip to content
This repository has been archived by the owner on Mar 11, 2021. It is now read-only.

validation added for create env #22

Merged
merged 7 commits into from
Jan 14, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
296 changes: 248 additions & 48 deletions Gopkg.lock

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@ ignored = [

[[constraint]]
name = "github.com/fabric8-services/fabric8-common"
revision = "b10e057d860d730661b2440dc1326c7d82606acc"
revision = "5bb2b51fb241b42824f86cb79c50574ab0c0c57f"

[[constraint]]
name = "github.com/fabric8-services/fabric8-cluster-client"
branch = "master"
13 changes: 12 additions & 1 deletion configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
errs "github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"
)

func (c *Registry) String() string {
Expand All @@ -29,6 +29,7 @@ const (
varLogLevel = "log.level"
varLogJSON = "log.json"
varAuthURL = "auth.url"
varClusterURL = "cluster.url"
varAuthKeysPath = "auth.keys.path"
varHTTPAddress = "http.address"
varMetricsHTTPAddress = "metrics.http.address"
Expand Down Expand Up @@ -145,6 +146,16 @@ func (c *Registry) GetAuthServiceURL() string {
return ""
}

func (c *Registry) GetClusterServiceURL() string {
if c.v.IsSet(varClusterURL) {
return c.v.GetString(varClusterURL)
}
if c.DeveloperModeEnabled() {
return "https://cluster.prod-preview.openshift.io"
}
return ""
}

func (c *Registry) GetDevModePrivateKey() []byte {
if c.DeveloperModeEnabled() {
return []byte(commonconfig.DevModeRsaPrivateKey)
Expand Down
41 changes: 33 additions & 8 deletions controller/environment.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package controller

import (
"context"
"fmt"

clusterclient "github.com/fabric8-services/fabric8-cluster-client/service"
"github.com/fabric8-services/fabric8-common/auth"
"github.com/fabric8-services/fabric8-common/errors"
"github.com/fabric8-services/fabric8-common/httpsupport"
Expand All @@ -18,15 +22,17 @@ const (

type EnvironmentController struct {
*goa.Controller
db application.DB
authService auth.AuthService
db application.DB
authService auth.AuthService
clusterService clusterclient.Service
}

func NewEnvironmentController(service *goa.Service, db application.DB, authService auth.AuthService) *EnvironmentController {
func NewEnvironmentController(service *goa.Service, db application.DB, authService auth.AuthService, clusterService clusterclient.Service) *EnvironmentController {
return &EnvironmentController{
Controller: service.NewController("EnvironmentController"),
db: db,
authService: authService,
Controller: service.NewController("EnvironmentController"),
db: db,
authService: authService,
clusterService: clusterService,
}
}

Expand Down Expand Up @@ -65,6 +71,11 @@ func (c *EnvironmentController) Create(ctx *app.CreateEnvironmentContext) error
return app.JSONErrorResponse(ctx, err)
}

err = c.checkClustersUser(ctx, *reqEnv.Attributes.ClusterURL)
if err != nil {
return app.JSONErrorResponse(ctx, err)
}

var env *environment.Environment
err = application.Transactional(c.db, func(appl application.Application) error {
newEnv := environment.Environment{
Expand All @@ -78,8 +89,8 @@ func (c *EnvironmentController) Create(ctx *app.CreateEnvironmentContext) error
env, err = appl.Environments().Create(ctx, &newEnv)
if err != nil {
log.Error(ctx, map[string]interface{}{"err": err},
"failed to create environment: %s", newEnv.Name)
return errs.Wrapf(err, "failed to create environment: %s", newEnv.Name)
"failed to create environment: %s", *newEnv.Name)
return errs.Wrapf(err, "failed to create environment: %s", *newEnv.Name)
}
return nil
})
Expand Down Expand Up @@ -132,3 +143,17 @@ func (c *EnvironmentController) Show(ctx *app.ShowEnvironmentContext) error {
}
return ctx.OK(res)
}

func (c *EnvironmentController) checkClustersUser(ctx context.Context, clusterURL string) error {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add a test for this func?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this link test is added to test this function only. If you observe blackbox_tests, it is a way to test all different combination. This new test is added such that only checkClustersUser function is tested rest are assumed to be working. Do you still see need to add test, which will have less pre-setup but everything else same as from part of blackbox test ??

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, fair enough, if all cases in the this function are covered but your blackbox tests

clusters, err := c.clusterService.UserClusters(ctx)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, if can be simplified in the future, once fabric8-services/fabric8-cluster#59 is done

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't use /api/clusters with URL as we need clusters who linked to users. For ex, we call /api/clusters with URL and we get response with cluster_details. It just mean that the cluster is a valid cluster and available with cluster_service but it can't guarantee that the cluster is linked to user also.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nurali-techie ok, my bad indeed! In that case, would you need a new endpoint such as /api/user/cluster?url=... to get a single response (or 404 if the user is not linked to the cluster with the given URL)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kool, so we don't have such a endpoint today.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, but if you need it, please open an issue and I'll take care of it

if err != nil {
return err
}
clusterURL = httpsupport.RemoveTrailingSlashFromURL(clusterURL)
for _, cluster := range clusters.Data {
if httpsupport.RemoveTrailingSlashFromURL(cluster.APIURL) == clusterURL {
return nil
}
}
return errors.NewInternalErrorFromString(fmt.Sprintf("cluster with URL '%s' not linked with user account", clusterURL))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you return an InternalServerError here? Could you return something more explicit, such as Forbidden if the cluster is not linked to the user?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done a15f2d7

}
24 changes: 23 additions & 1 deletion controller/environment_blackbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

clusterclient "github.com/fabric8-services/fabric8-cluster-client/cluster"
testauth "github.com/fabric8-services/fabric8-common/test/auth"
testsuite "github.com/fabric8-services/fabric8-common/test/suite"
"github.com/fabric8-services/fabric8-env/app"
Expand All @@ -34,6 +35,19 @@ func (s *testAuthService) RequireScope(ctx context.Context, resourceID, required
return nil
}

type testClusterService struct{}

func (s *testClusterService) UserClusters(ctx context.Context) (*clusterclient.ClusterList, error) {
return &clusterclient.ClusterList{
Data: []*clusterclient.ClusterData{
{
Name: "cluster1",
APIURL: "cluster1.com",
},
},
}, nil
}

func TestEnvironmentController(t *testing.T) {
config, err := configuration.New("")
require.NoError(t, err)
Expand All @@ -48,7 +62,7 @@ func (s *EnvironmentControllerSuite) SetupSuite() {
svc := testauth.UnsecuredService("enviroment-test")
s.svc = svc
s.ctx = s.svc.Context
s.ctrl = controller.NewEnvironmentController(s.svc, s.db, &testAuthService{})
s.ctrl = controller.NewEnvironmentController(s.svc, s.db, &testAuthService{}, &testClusterService{})
}

func (s *EnvironmentControllerSuite) TestCreate() {
Expand All @@ -65,6 +79,14 @@ func (s *EnvironmentControllerSuite) TestCreate() {
require.NotNil(t, env)
assert.Equal(t, env.Data.ID, newEnv.Data.ID)
})

s.T().Run("cluster_not_linked", func(t *testing.T) {
spaceID := uuid.NewV4()
payload := newCreateEnvironmentPayload("osio-stage", "stage", "cluster2.com")

_, err := test.CreateEnvironmentInternalServerError(t, s.ctx, s.svc, s.ctrl, spaceID, payload)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see question above about the InternalServerError returned when the cluster is not linked to the user.

assert.NotNil(t, err)
})
}

func (s *EnvironmentControllerSuite) TestList() {
Expand Down
2 changes: 1 addition & 1 deletion controller/environment_space_scope_blackbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (s *EnvironmentSpaceScopeSuite) SetupSuite() {
require.NoError(s.T(), err)

s.svc = testauth.UnsecuredService("enviroment-test")
s.ctrl = controller.NewEnvironmentController(s.svc, s.db, authService)
s.ctrl = controller.NewEnvironmentController(s.svc, s.db, authService, &testClusterService{})
s.spaceID = uuid.NewV4()

s.ctx1, _, err = testauth.EmbedUserTokenInContext(context.Background(), testUser1)
Expand Down
11 changes: 10 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"runtime"
"time"

clusterclient "github.com/fabric8-services/fabric8-cluster-client/service"
"github.com/fabric8-services/fabric8-common/auth"
"github.com/fabric8-services/fabric8-common/closeable"
"github.com/fabric8-services/fabric8-common/convert/ptr"
Expand Down Expand Up @@ -102,17 +103,25 @@ func main() {
service.Use(metric.Recorder("fabric8_env"))
// ---

// Used services
authService, err := auth.NewAuthService(config.GetAuthServiceURL())
if err != nil {
log.Panic(nil, map[string]interface{}{"url": config.GetAuthServiceURL(), "err": err},
"could not create Auth client")
}

clusterService, err := clusterclient.NewClusterService(config.GetClusterServiceURL())
if err != nil {
log.Panic(nil, map[string]interface{}{"url": config.GetClusterServiceURL(), "err": err},
"could not create Cluster client")
}

appDB := gormapp.NewGormDB(db)
// ---

// Mount controllers
app.MountStatusController(service, controller.NewStatusController(service, controller.NewGormDBChecker(db)))
app.MountEnvironmentController(service, controller.NewEnvironmentController(service, appDB, authService))
app.MountEnvironmentController(service, controller.NewEnvironmentController(service, appDB, authService, clusterService))
// ---

log.Logger().Infoln("Git Commit SHA: ", app.Commit)
Expand Down
5 changes: 5 additions & 0 deletions openshift/f8env.app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ objects:
configMapKeyRef:
name: f8env
key: auth.url
- name: F8_CLUSTER_URL
valueFrom:
configMapKeyRef:
name: f8env
key: cluster.url
imagePullPolicy: Always
name: f8env
ports:
Expand Down
1 change: 1 addition & 0 deletions openshift/f8env.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ objects:
postgres.connection.maxopen: "90"
environment: prod-preview
auth.url: https://auth.prod-preview.openshift.io
cluster.url: https://cluster.prod-preview.openshift.io