Skip to content
This repository has been archived by the owner on Dec 1, 2022. It is now read-only.

Refactored daemon-scheduler environment and deployment structure. #185

Merged
merged 2 commits into from Mar 13, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 15 additions & 14 deletions daemon-scheduler/pkg/api/v1/api.go
Expand Up @@ -21,8 +21,9 @@ import (

"github.com/aws/aws-sdk-go/service/ecs"
"github.com/blox/blox/daemon-scheduler/pkg/deployment"
"github.com/blox/blox/daemon-scheduler/pkg/environment"
environmenttypes "github.com/blox/blox/daemon-scheduler/pkg/environment/types"
"github.com/blox/blox/daemon-scheduler/pkg/facade"
"github.com/blox/blox/daemon-scheduler/pkg/types"
"github.com/blox/blox/daemon-scheduler/pkg/validate"
"github.com/blox/blox/daemon-scheduler/swagger/v1/generated/models"
log "github.com/cihub/seelog"
Expand All @@ -47,17 +48,17 @@ var (
)

type API struct {
environment deployment.Environment
deployment deployment.Deployment
ecs facade.ECS
environmentService environment.EnvironmentService
deployment deployment.Deployment
ecs facade.ECS
}

// NewAPI initializes the API struct
func NewAPI(e deployment.Environment, d deployment.Deployment, ecs facade.ECS) API {
func NewAPI(e environment.EnvironmentService, d deployment.Deployment, ecs facade.ECS) API {
return API{
environment: e,
deployment: d,
ecs: ecs,
environmentService: e,
deployment: d,
ecs: ecs,
}
}

Expand Down Expand Up @@ -91,7 +92,7 @@ func (api API) CreateEnvironment(w http.ResponseWriter, r *http.Request) {
return
}

env, err := api.environment.CreateEnvironment(r.Context(), *createEnvReq.Name, *ecsTaskDefinition.TaskDefinitionArn, *ecsCluster.ClusterArn)
env, err := api.environmentService.CreateEnvironment(r.Context(), *createEnvReq.Name, *ecsTaskDefinition.TaskDefinitionArn, *ecsCluster.ClusterArn)
if err != nil {
handleBackendError(w, err)
return
Expand All @@ -110,7 +111,7 @@ func (api API) GetEnvironment(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
name := vars[envNameKey]

env, err := api.environment.GetEnvironment(r.Context(), name)
env, err := api.environmentService.GetEnvironment(r.Context(), name)
if err != nil {
writeInternalServerError(w, err)
return
Expand Down Expand Up @@ -143,7 +144,7 @@ func (api API) ListEnvironments(w http.ResponseWriter, r *http.Request) {
return
}

var envs []types.Environment
var envs []environmenttypes.Environment
var err error

cluster := query.Get(clusterFilter)
Expand All @@ -152,9 +153,9 @@ func (api API) ListEnvironments(w http.ResponseWriter, r *http.Request) {
writeBadRequestError(w, invalidClusterError)
return
}
envs, err = api.environment.FilterEnvironments(r.Context(), clusterFilter, cluster)
envs, err = api.environmentService.FilterEnvironments(r.Context(), clusterFilter, cluster)
} else {
envs, err = api.environment.ListEnvironments(r.Context())
envs, err = api.environmentService.ListEnvironments(r.Context())
}

if err != nil {
Expand Down Expand Up @@ -183,7 +184,7 @@ func (api API) DeleteEnvironment(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
name := vars[envNameKey]

err := api.environment.DeleteEnvironment(r.Context(), name)
err := api.environmentService.DeleteEnvironment(r.Context(), name)
if err != nil {
handleBackendError(w, err)
return
Expand Down
67 changes: 34 additions & 33 deletions daemon-scheduler/pkg/api/v1/api_test.go
Expand Up @@ -22,6 +22,7 @@ import (
"testing"

"github.com/aws/aws-sdk-go/aws"
environmenttypes "github.com/blox/blox/daemon-scheduler/pkg/environment/types"
"github.com/blox/blox/daemon-scheduler/pkg/mocks"
"github.com/blox/blox/daemon-scheduler/pkg/types"
"github.com/blox/blox/daemon-scheduler/swagger/v1/generated/models"
Expand All @@ -41,10 +42,10 @@ const (

type APITestSuite struct {
suite.Suite
environment *mocks.MockEnvironment
deployment *mocks.MockDeployment
ecs *mocks.MockECS
api API
environmentService *mocks.MockEnvironmentService
deployment *mocks.MockDeployment
ecs *mocks.MockECS
api API

// We need a router because some of the apis use mux.Vars() which uses the URL
// parameters parsed and stored in a global map in the global context by the router.
Expand All @@ -53,10 +54,10 @@ type APITestSuite struct {

func (suite *APITestSuite) SetupTest() {
mockCtrl := gomock.NewController(suite.T())
suite.environment = mocks.NewMockEnvironment(mockCtrl)
suite.environmentService = mocks.NewMockEnvironmentService(mockCtrl)
suite.deployment = mocks.NewMockDeployment(mockCtrl)
suite.ecs = mocks.NewMockECS(mockCtrl)
suite.api = NewAPI(suite.environment, suite.deployment, suite.ecs)
suite.api = NewAPI(suite.environmentService, suite.deployment, suite.ecs)
suite.router = suite.getRouter()
}

Expand All @@ -81,7 +82,7 @@ func (suite *APITestSuite) TestPing() {
func (suite *APITestSuite) TestGetEnvironmentReturnsError() {
name := "testEnv"
err := errors.New("Error from GetEnvironment")
suite.environment.EXPECT().GetEnvironment(gomock.Any(), name).Return(nil, err)
suite.environmentService.EXPECT().GetEnvironment(gomock.Any(), name).Return(nil, err)
request := suite.generateGetEnvironmentRequest(name)

responseRecorder := httptest.NewRecorder()
Expand All @@ -92,7 +93,7 @@ func (suite *APITestSuite) TestGetEnvironmentReturnsError() {

func (suite *APITestSuite) TestGetEnvironmentMissingReturnsError() {
name := "testEnv"
suite.environment.EXPECT().GetEnvironment(gomock.Any(), name).Return(nil, nil)
suite.environmentService.EXPECT().GetEnvironment(gomock.Any(), name).Return(nil, nil)
request := suite.generateGetEnvironmentRequest(name)

responseRecorder := httptest.NewRecorder()
Expand All @@ -104,7 +105,7 @@ func (suite *APITestSuite) TestGetEnvironmentMissingReturnsError() {
func (suite *APITestSuite) TestGetEnvironment() {
name := "testEnv"
environment := suite.createEnvironmentObject(name, taskDefinitionARN, clusterARN1)
suite.environment.EXPECT().GetEnvironment(gomock.Any(), name).Return(environment, nil)
suite.environmentService.EXPECT().GetEnvironment(gomock.Any(), name).Return(environment, nil)
request := suite.generateGetEnvironmentRequest(name)

responseRecorder := httptest.NewRecorder()
Expand All @@ -122,9 +123,9 @@ func (suite *APITestSuite) TestGetEnvironment() {
func (suite *APITestSuite) TestListEnvironments() {
e1 := suite.createEnvironmentObject("e1", taskDefinitionARN, clusterARN1)
e2 := suite.createEnvironmentObject("e2", taskDefinitionARN, clusterARN2)
environments := []types.Environment{*e1, *e2}
suite.environment.EXPECT().ListEnvironments(gomock.Any()).Return(environments, nil)
suite.environment.EXPECT().FilterEnvironments(gomock.Any(), gomock.Any(), gomock.Any()).Times(0)
environments := []environmenttypes.Environment{*e1, *e2}
suite.environmentService.EXPECT().ListEnvironments(gomock.Any()).Return(environments, nil)
suite.environmentService.EXPECT().FilterEnvironments(gomock.Any(), gomock.Any(), gomock.Any()).Times(0)

request := suite.generateListEnvironmentsRequest()
responseRecorder := httptest.NewRecorder()
Expand All @@ -145,8 +146,8 @@ func (suite *APITestSuite) TestListEnvironments() {

func (suite *APITestSuite) TestListEnvironmentsServerError() {
err := errors.New("Error when calling ListEnvironments")
suite.environment.EXPECT().ListEnvironments(gomock.Any()).Return(nil, err)
suite.environment.EXPECT().FilterEnvironments(gomock.Any(), gomock.Any(), gomock.Any()).Times(0)
suite.environmentService.EXPECT().ListEnvironments(gomock.Any()).Return(nil, err)
suite.environmentService.EXPECT().FilterEnvironments(gomock.Any(), gomock.Any(), gomock.Any()).Times(0)

request := suite.generateListEnvironmentsRequest()
responseRecorder := httptest.NewRecorder()
Expand All @@ -156,8 +157,8 @@ func (suite *APITestSuite) TestListEnvironmentsServerError() {
}

func (suite *APITestSuite) TestListEnvironmentsUnsupportedFilter() {
suite.environment.EXPECT().ListEnvironments(gomock.Any()).Times(0)
suite.environment.EXPECT().FilterEnvironments(gomock.Any(), gomock.Any(), gomock.Any()).Times(0)
suite.environmentService.EXPECT().ListEnvironments(gomock.Any()).Times(0)
suite.environmentService.EXPECT().FilterEnvironments(gomock.Any(), gomock.Any(), gomock.Any()).Times(0)

url := "/v1/environments?unsupportedFilter=val"
request, err := http.NewRequest("GET", url, nil)
Expand All @@ -172,9 +173,9 @@ func (suite *APITestSuite) TestListEnvironmentsUnsupportedFilter() {
func (suite *APITestSuite) TestListEnvironmentsWithClusterARNFilter() {
e1 := suite.createEnvironmentObject("e1", taskDefinitionARN, clusterARN1)
e2 := suite.createEnvironmentObject("e2", taskDefinitionARN, clusterARN1)
environments := []types.Environment{*e1, *e2}
suite.environment.EXPECT().FilterEnvironments(gomock.Any(), clusterFilter, clusterARN1).Return(environments, nil)
suite.environment.EXPECT().ListEnvironments(gomock.Any()).Times(0)
environments := []environmenttypes.Environment{*e1, *e2}
suite.environmentService.EXPECT().FilterEnvironments(gomock.Any(), clusterFilter, clusterARN1).Return(environments, nil)
suite.environmentService.EXPECT().ListEnvironments(gomock.Any()).Times(0)

request := suite.generateFilterEnvironmentsRequest(clusterARN1)

Expand All @@ -196,8 +197,8 @@ func (suite *APITestSuite) TestListEnvironmentsWithClusterARNFilter() {

func (suite *APITestSuite) TestListEnvironmentsWithClusterARNFilterServerError() {
err := errors.New("Error when calling ListEnvironments with cluster ARN filter")
suite.environment.EXPECT().FilterEnvironments(gomock.Any(), clusterFilter, clusterARN1).Return(nil, err)
suite.environment.EXPECT().ListEnvironments(gomock.Any()).Times(0)
suite.environmentService.EXPECT().FilterEnvironments(gomock.Any(), clusterFilter, clusterARN1).Return(nil, err)
suite.environmentService.EXPECT().ListEnvironments(gomock.Any()).Times(0)
request := suite.generateFilterEnvironmentsRequest(clusterARN1)

responseRecorder := httptest.NewRecorder()
Expand All @@ -209,9 +210,9 @@ func (suite *APITestSuite) TestListEnvironmentsWithClusterARNFilterServerError()
func (suite *APITestSuite) TestListEnvironmentsWithClusterNameFilter() {
e1 := suite.createEnvironmentObject("e1", taskDefinitionARN, clusterARN1)
e2 := suite.createEnvironmentObject("e2", taskDefinitionARN, clusterARN1)
environments := []types.Environment{*e1, *e2}
suite.environment.EXPECT().FilterEnvironments(gomock.Any(), clusterFilter, clusterName1).Return(environments, nil)
suite.environment.EXPECT().ListEnvironments(gomock.Any()).Times(0)
environments := []environmenttypes.Environment{*e1, *e2}
suite.environmentService.EXPECT().FilterEnvironments(gomock.Any(), clusterFilter, clusterName1).Return(environments, nil)
suite.environmentService.EXPECT().ListEnvironments(gomock.Any()).Times(0)

request := suite.generateFilterEnvironmentsRequest(clusterName1)

Expand All @@ -233,8 +234,8 @@ func (suite *APITestSuite) TestListEnvironmentsWithClusterNameFilter() {

func (suite *APITestSuite) TestListEnvironmentsWithClusterNameFilterServerError() {
err := errors.New("Error when calling ListEnvironments with cluster name filter")
suite.environment.EXPECT().FilterEnvironments(gomock.Any(), clusterFilter, clusterName1).Return(nil, err)
suite.environment.EXPECT().ListEnvironments(gomock.Any()).Times(0)
suite.environmentService.EXPECT().FilterEnvironments(gomock.Any(), clusterFilter, clusterName1).Return(nil, err)
suite.environmentService.EXPECT().ListEnvironments(gomock.Any()).Times(0)
request := suite.generateFilterEnvironmentsRequest(clusterName1)

responseRecorder := httptest.NewRecorder()
Expand All @@ -244,8 +245,8 @@ func (suite *APITestSuite) TestListEnvironmentsWithClusterNameFilterServerError(
}

func (suite *APITestSuite) TestListEnvironmentsWithInvalidClusterFilter() {
suite.environment.EXPECT().FilterEnvironments(gomock.Any(), gomock.Any(), gomock.Any()).Times(0)
suite.environment.EXPECT().ListEnvironments(gomock.Any()).Times(0)
suite.environmentService.EXPECT().FilterEnvironments(gomock.Any(), gomock.Any(), gomock.Any()).Times(0)
suite.environmentService.EXPECT().ListEnvironments(gomock.Any()).Times(0)

url := "/v1/environments?cluster=cl/cl"
request, err := http.NewRequest("GET", url, nil)
Expand All @@ -259,7 +260,7 @@ func (suite *APITestSuite) TestListEnvironmentsWithInvalidClusterFilter() {

func (suite *APITestSuite) TestDeleteEnvironment() {
name := "testEnv"
suite.environment.EXPECT().DeleteEnvironment(gomock.Any(), name).Return(nil)
suite.environmentService.EXPECT().DeleteEnvironment(gomock.Any(), name).Return(nil)

request := suite.generateDeleteEnvironmentRequest(name)

Expand All @@ -272,7 +273,7 @@ func (suite *APITestSuite) TestDeleteEnvironment() {
func (suite *APITestSuite) TestDeleteEnvironmentMissingEnvironment() {
name := "testEnv"
notfounderr := types.NewNotFoundError(errors.New("Environment is missing"))
suite.environment.EXPECT().DeleteEnvironment(gomock.Any(), name).Return(notfounderr)
suite.environmentService.EXPECT().DeleteEnvironment(gomock.Any(), name).Return(notfounderr)

request := suite.generateDeleteEnvironmentRequest(name)

Expand All @@ -282,7 +283,7 @@ func (suite *APITestSuite) TestDeleteEnvironmentMissingEnvironment() {
assert.Equal(suite.T(), http.StatusNotFound, responseRecorder.Code)
}

func (suite *APITestSuite) assertSame(environment *types.Environment, environmentModel *models.Environment) {
func (suite *APITestSuite) assertSame(environment *environmenttypes.Environment, environmentModel *models.Environment) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't it be environmentTypes? camelCase?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes for variables. No for packages. environmenttypes is a package, not a variable. You'll see we reference package names in all lowercase everywhere.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh I see

assert.Equal(suite.T(), environment.Name, aws.StringValue(environmentModel.Name))
assert.Equal(suite.T(), environment.Cluster, environmentModel.InstanceGroup.Cluster)
assert.Equal(suite.T(), environment.DesiredTaskDefinition, environmentModel.TaskDefinition)
Expand Down Expand Up @@ -317,8 +318,8 @@ func (suite *APITestSuite) generateFilterEnvironmentsRequest(cluster string) *ht
return request
}

func (suite *APITestSuite) createEnvironmentObject(name string, td string, cluster string) *types.Environment {
environment, err := types.NewEnvironment(name, td, cluster)
func (suite *APITestSuite) createEnvironmentObject(name string, td string, cluster string) *environmenttypes.Environment {
environment, err := environmenttypes.NewEnvironment(name, td, cluster)
assert.Nil(suite.T(), err, "Unexpected error generating an environment object")
return environment
}
Expand Down
5 changes: 3 additions & 2 deletions daemon-scheduler/pkg/api/v1/transforms.go
Expand Up @@ -15,13 +15,14 @@ package v1

import (
"github.com/aws/aws-sdk-go/aws"
environmenttypes "github.com/blox/blox/daemon-scheduler/pkg/environment/types"
"github.com/blox/blox/daemon-scheduler/pkg/types"
"github.com/blox/blox/daemon-scheduler/swagger/v1/generated/models"
)

func toEnvironmentModel(envType types.Environment) models.Environment {
func toEnvironmentModel(envType environmenttypes.Environment) models.Environment {
health := models.HealthStatusHealthy
if envType.Health == types.EnvironmentUnhealthy {
if envType.Health == environmenttypes.EnvironmentUnhealthy {
health = models.HealthStatusUnhealthy
}
return models.Environment{
Expand Down