From 630d7e3b70363e5a4eadce109753595e3f103b28 Mon Sep 17 00:00:00 2001 From: Silvestre Zabala Date: Thu, 26 Sep 2024 15:28:09 +0200 Subject: [PATCH] chore(apiserver): Clean up routes # Issue It was possible to get information for a currently unbound app from the API server # Fix Check if the app is currently bound to the service on all routes. --- src/acceptance/api/api_test.go | 29 +- src/acceptance/helpers/helpers.go | 5 + .../api/publicapiserver/public_api_server.go | 8 +- ...tegration_golangapi_eventgenerator_test.go | 416 +++++++-------- ...ntegration_golangapi_scalingengine_test.go | 476 +++++++++--------- src/autoscaler/routes/routes.go | 5 - 6 files changed, 493 insertions(+), 446 deletions(-) diff --git a/src/acceptance/api/api_test.go b/src/acceptance/api/api_test.go index 4a484ea1b8..17fb5d0bd9 100644 --- a/src/acceptance/api/api_test.go +++ b/src/acceptance/api/api_test.go @@ -79,7 +79,7 @@ var _ = Describe("AutoScaler Public API", func() { Expect(appGUID).NotTo(BeEmpty()) }) - Context("when no policy defined", func() { + When("no scaling policy is set", func() { BeforeEach(func() { _, status := deletePolicy() @@ -114,7 +114,7 @@ var _ = Describe("AutoScaler Public API", func() { }) - Context("When policy is defined", func() { + When("a scaling policy is set", func() { memThreshold := int64(10) var policy string @@ -161,7 +161,7 @@ var _ = Describe("AutoScaler Public API", func() { }) - Context("for an unrelated user", func() { + When("an unrelated user tries to access the API", func() { BeforeEach(func() { workflowhelpers.AsUser(setup.AdminUserContext(), cfg.DefaultTimeoutDuration(), func() { // Make "other user" a space auditor in the space along with a space developer in the other space @@ -179,7 +179,7 @@ var _ = Describe("AutoScaler Public API", func() { }) }) - Context("When scale out is triggered ", func() { + When("a scale out is triggered ", func() { BeforeEach(func() { totalTime := time.Duration(cfg.AggregateInterval*2)*time.Second + 3*time.Minute WaitForNInstancesRunning(appGUID, 2, totalTime) @@ -199,6 +199,27 @@ var _ = Describe("AutoScaler Public API", func() { } }) }) + + When("trying to get info for an app not bound to the service", func() { + BeforeEach(func() { + UnbindServiceFromApp(cfg, appName, instanceName) + }) + + It("should not be possible to get information from the API", func() { + By("getting the policy") + _, status := getPolicy() + Expect(status).To(Equal(http.StatusForbidden)) + + By("getting the history") + _, status = get(historyURL) + Expect(status).To(Equal(http.StatusForbidden)) + + By("getting the aggregated metrics") + _, status = get(aggregatedMetricURL) + Expect(status).To(Equal(http.StatusForbidden)) + }) + }) + }) }) diff --git a/src/acceptance/helpers/helpers.go b/src/acceptance/helpers/helpers.go index f2a1ac5c2c..5af7ec3c68 100644 --- a/src/acceptance/helpers/helpers.go +++ b/src/acceptance/helpers/helpers.go @@ -501,6 +501,11 @@ func BindServiceToAppWithPolicy(cfg *config.Config, appName string, instanceName return err } +func UnbindServiceFromApp(cfg *config.Config, appName string, instanceName string) { + unbindService := cf.Cf("unbind-service", appName, instanceName).Wait(cfg.DefaultTimeoutDuration()) + Expect(unbindService).To(Exit(0), fmt.Sprintf("Failed to unbind service %s from app %s \n CLI Output:\n %s %s", instanceName, appName, unbindService.Buffer().Contents(), unbindService.Err.Contents())) +} + func CreateService(cfg *config.Config) string { instanceName := generator.PrefixedRandomName(cfg.Prefix, cfg.InstancePrefix) FailOnError(CreateServiceWithPlan(cfg, cfg.ServicePlan, instanceName)) diff --git a/src/autoscaler/api/publicapiserver/public_api_server.go b/src/autoscaler/api/publicapiserver/public_api_server.go index 80f5159431..40891dc747 100644 --- a/src/autoscaler/api/publicapiserver/public_api_server.go +++ b/src/autoscaler/api/publicapiserver/public_api_server.go @@ -52,6 +52,7 @@ func NewPublicApiServer(logger lager.Logger, conf *config.Config, policydb db.Po rp.Use(rateLimiterMiddleware.CheckRateLimit) rp.Use(mw.HasClientToken) rp.Use(mw.Oauth) + rp.Use(mw.CheckServiceBinding) rp.Use(httpStatusCollectMiddleware.Collect) rp.Get(routes.PublicApiScalingHistoryRouteName).Handler(scalingHistoryHandler) @@ -68,13 +69,6 @@ func NewPublicApiServer(logger lager.Logger, conf *config.Config, policydb db.Po rpolicy.Get(routes.PublicApiAttachPolicyRouteName).Handler(VarsFunc(pah.AttachScalingPolicy)) rpolicy.Get(routes.PublicApiDetachPolicyRouteName).Handler(VarsFunc(pah.DetachScalingPolicy)) - rcredential := routes.ApiCredentialRoutes() - rcredential.Use(rateLimiterMiddleware.CheckRateLimit) - - rcredential.Use(httpStatusCollectMiddleware.Collect) - rcredential.Use(mw.HasClientToken) - rcredential.Use(mw.Oauth) - return helpers.NewHTTPServer(logger, conf.PublicApiServer, r) } diff --git a/src/autoscaler/integration/integration_golangapi_eventgenerator_test.go b/src/autoscaler/integration/integration_golangapi_eventgenerator_test.go index 8c3af3a3ba..4a9db50d28 100644 --- a/src/autoscaler/integration/integration_golangapi_eventgenerator_test.go +++ b/src/autoscaler/integration/integration_golangapi_eventgenerator_test.go @@ -1,6 +1,7 @@ package integration_test import ( + "encoding/base64" "fmt" "net/http" @@ -18,6 +19,10 @@ var _ = Describe("Integration_GolangApi_EventGenerator", func() { metric *models.AppMetric metricType = "memoryused" initInstanceCount = 2 + serviceInstanceId string + bindingId string + orgId string + spaceId string ) BeforeEach(func() { @@ -37,7 +42,12 @@ var _ = Describe("Integration_GolangApi_EventGenerator", func() { fmt.Sprintf("https://127.0.0.1:%d", components.Ports[EventGenerator]), "https://127.0.0.1:8888", tmpDir) + brokerAuth = base64.StdEncoding.EncodeToString([]byte("broker_username:broker_password")) startGolangApiServer() + serviceInstanceId = getRandomIdRef("serviceInstId") + orgId = getRandomIdRef("orgId") + spaceId = getRandomIdRef("spaceId") + bindingId = getRandomIdRef("bindingId") appId = getRandomIdRef("appId") pathVariables = []string{appId, metricType} @@ -102,228 +112,234 @@ var _ = Describe("Integration_GolangApi_EventGenerator", func() { }) }) - Context("EventGenerator is down", func() { - JustBeforeEach(func() { - stopEventGenerator() + When("the app is bound to the service instance", func() { + BeforeEach(func() { + provisionAndBind(serviceInstanceId, orgId, spaceId, bindingId, appId, components.Ports[GolangServiceBroker], httpClientForPublicApi) }) - It("should error with status code 500", func() { - checkPublicAPIResponseContentWithParameters(getAppAggregatedMetrics, components.Ports[GolangAPIServer], pathVariables, parameters, http.StatusInternalServerError, map[string]interface{}{ - "code": "Internal Server Error", - "message": "Error retrieving metrics history from eventgenerator", + Context("EventGenerator is down", func() { + JustBeforeEach(func() { + stopEventGenerator() + }) + + It("should error with status code 500", func() { + checkPublicAPIResponseContentWithParameters(getAppAggregatedMetrics, components.Ports[GolangAPIServer], pathVariables, parameters, http.StatusInternalServerError, map[string]interface{}{ + "code": "Internal Server Error", + "message": "Error retrieving metrics history from eventgenerator", + }) }) }) - }) - Context("Get aggregated metrics", func() { - BeforeEach(func() { - metric = &models.AppMetric{ - AppId: appId, - MetricType: models.MetricNameMemoryUsed, - Unit: models.UnitMegaBytes, - Value: "123456", - } + Context("Get aggregated metrics", func() { + BeforeEach(func() { + metric = &models.AppMetric{ + AppId: appId, + MetricType: models.MetricNameMemoryUsed, + Unit: models.UnitMegaBytes, + Value: "123456", + } - metric.Timestamp = 666666 - insertAppMetric(metric) + metric.Timestamp = 666666 + insertAppMetric(metric) - metric.Timestamp = 555555 - insertAppMetric(metric) + metric.Timestamp = 555555 + insertAppMetric(metric) - metric.Timestamp = 555556 - insertAppMetric(metric) + metric.Timestamp = 555556 + insertAppMetric(metric) - metric.Timestamp = 333333 - insertAppMetric(metric) + metric.Timestamp = 333333 + insertAppMetric(metric) - metric.Timestamp = 444444 - insertAppMetric(metric) + metric.Timestamp = 444444 + insertAppMetric(metric) - //add some other metric-type - metric.MetricType = models.MetricNameThroughput - metric.Unit = models.UnitNum - metric.Timestamp = 444444 - insertAppMetric(metric) - //add some other appId - metric.AppId = getRandomIdRef("metric.appId") - metric.MetricType = models.MetricNameMemoryUsed - metric.Unit = models.UnitMegaBytes - metric.Timestamp = 444444 - insertAppMetric(metric) - }) - It("should get the metrics ", func() { - By("get the 1st page") - parameters = map[string]string{"start-time": "111111", "end-time": "999999", "order-direction": "asc", "page": "1", "results-per-page": "2"} - result := AppAggregatedMetricResult{ - TotalResults: 5, - TotalPages: 3, - Page: 1, - NextUrl: getAppAggregatedMetricUrl(appId, metricType, parameters, 2), - Resources: []models.AppMetric{ - { - AppId: appId, - MetricType: models.MetricNameMemoryUsed, - Unit: models.UnitMegaBytes, - Value: "123456", - Timestamp: 333333, - }, - { - AppId: appId, - MetricType: models.MetricNameMemoryUsed, - Unit: models.UnitMegaBytes, - Value: "123456", - Timestamp: 444444, + //add some other metric-type + metric.MetricType = models.MetricNameThroughput + metric.Unit = models.UnitNum + metric.Timestamp = 444444 + insertAppMetric(metric) + //add some other appId + metric.AppId = getRandomIdRef("metric.appId") + metric.MetricType = models.MetricNameMemoryUsed + metric.Unit = models.UnitMegaBytes + metric.Timestamp = 444444 + insertAppMetric(metric) + }) + It("should get the metrics ", func() { + By("get the 1st page") + parameters = map[string]string{"start-time": "111111", "end-time": "999999", "order-direction": "asc", "page": "1", "results-per-page": "2"} + result := AppAggregatedMetricResult{ + TotalResults: 5, + TotalPages: 3, + Page: 1, + NextUrl: getAppAggregatedMetricUrl(appId, metricType, parameters, 2), + Resources: []models.AppMetric{ + { + AppId: appId, + MetricType: models.MetricNameMemoryUsed, + Unit: models.UnitMegaBytes, + Value: "123456", + Timestamp: 333333, + }, + { + AppId: appId, + MetricType: models.MetricNameMemoryUsed, + Unit: models.UnitMegaBytes, + Value: "123456", + Timestamp: 444444, + }, }, - }, - } - checkAggregatedMetricResult(components.Ports[GolangAPIServer], pathVariables, parameters, result) + } + checkAggregatedMetricResult(components.Ports[GolangAPIServer], pathVariables, parameters, result) - By("get the 2nd page") - parameters = map[string]string{"start-time": "111111", "end-time": "999999", "order-direction": "asc", "page": "2", "results-per-page": "2"} - result = AppAggregatedMetricResult{ - TotalResults: 5, - TotalPages: 3, - Page: 2, - PrevUrl: getAppAggregatedMetricUrl(appId, metricType, parameters, 1), - NextUrl: getAppAggregatedMetricUrl(appId, metricType, parameters, 3), - Resources: []models.AppMetric{ - { - AppId: appId, - MetricType: models.MetricNameMemoryUsed, - Unit: models.UnitMegaBytes, - Value: "123456", - Timestamp: 555555, - }, - { - AppId: appId, - MetricType: models.MetricNameMemoryUsed, - Unit: models.UnitMegaBytes, - Value: "123456", - Timestamp: 555556, + By("get the 2nd page") + parameters = map[string]string{"start-time": "111111", "end-time": "999999", "order-direction": "asc", "page": "2", "results-per-page": "2"} + result = AppAggregatedMetricResult{ + TotalResults: 5, + TotalPages: 3, + Page: 2, + PrevUrl: getAppAggregatedMetricUrl(appId, metricType, parameters, 1), + NextUrl: getAppAggregatedMetricUrl(appId, metricType, parameters, 3), + Resources: []models.AppMetric{ + { + AppId: appId, + MetricType: models.MetricNameMemoryUsed, + Unit: models.UnitMegaBytes, + Value: "123456", + Timestamp: 555555, + }, + { + AppId: appId, + MetricType: models.MetricNameMemoryUsed, + Unit: models.UnitMegaBytes, + Value: "123456", + Timestamp: 555556, + }, }, - }, - } - checkAggregatedMetricResult(components.Ports[GolangAPIServer], pathVariables, parameters, result) + } + checkAggregatedMetricResult(components.Ports[GolangAPIServer], pathVariables, parameters, result) - By("get the 3rd page") - parameters = map[string]string{"start-time": "111111", "end-time": "999999", "order-direction": "asc", "page": "3", "results-per-page": "2"} - result = AppAggregatedMetricResult{ - TotalResults: 5, - TotalPages: 3, - Page: 3, - PrevUrl: getAppAggregatedMetricUrl(appId, metricType, parameters, 2), - Resources: []models.AppMetric{ - { - AppId: appId, - MetricType: models.MetricNameMemoryUsed, - Unit: models.UnitMegaBytes, - Value: "123456", - Timestamp: 666666, + By("get the 3rd page") + parameters = map[string]string{"start-time": "111111", "end-time": "999999", "order-direction": "asc", "page": "3", "results-per-page": "2"} + result = AppAggregatedMetricResult{ + TotalResults: 5, + TotalPages: 3, + Page: 3, + PrevUrl: getAppAggregatedMetricUrl(appId, metricType, parameters, 2), + Resources: []models.AppMetric{ + { + AppId: appId, + MetricType: models.MetricNameMemoryUsed, + Unit: models.UnitMegaBytes, + Value: "123456", + Timestamp: 666666, + }, }, - }, - } - checkAggregatedMetricResult(components.Ports[GolangAPIServer], pathVariables, parameters, result) + } + checkAggregatedMetricResult(components.Ports[GolangAPIServer], pathVariables, parameters, result) - By("the 4th page should be empty") - parameters = map[string]string{"start-time": "111111", "end-time": "999999", "order-direction": "asc", "page": "4", "results-per-page": "2"} - result = AppAggregatedMetricResult{ - TotalResults: 5, - TotalPages: 3, - Page: 4, - PrevUrl: getAppAggregatedMetricUrl(appId, metricType, parameters, 3), - Resources: []models.AppMetric{}, - } - checkAggregatedMetricResult(components.Ports[GolangAPIServer], pathVariables, parameters, result) - }) - It("should get the metrics in specified time scope", func() { - By("get the results from 555555") - parameters = map[string]string{"start-time": "555555", "order-direction": "asc", "page": "1", "results-per-page": "10"} - result := AppAggregatedMetricResult{ - TotalResults: 3, - TotalPages: 1, - Page: 1, - Resources: []models.AppMetric{ - { - AppId: appId, - MetricType: models.MetricNameMemoryUsed, - Unit: models.UnitMegaBytes, - Value: "123456", - Timestamp: 555555, - }, - { - AppId: appId, - MetricType: models.MetricNameMemoryUsed, - Unit: models.UnitMegaBytes, - Value: "123456", - Timestamp: 555556, - }, - { - AppId: appId, - MetricType: models.MetricNameMemoryUsed, - Unit: models.UnitMegaBytes, - Value: "123456", - Timestamp: 666666, + By("the 4th page should be empty") + parameters = map[string]string{"start-time": "111111", "end-time": "999999", "order-direction": "asc", "page": "4", "results-per-page": "2"} + result = AppAggregatedMetricResult{ + TotalResults: 5, + TotalPages: 3, + Page: 4, + PrevUrl: getAppAggregatedMetricUrl(appId, metricType, parameters, 3), + Resources: []models.AppMetric{}, + } + checkAggregatedMetricResult(components.Ports[GolangAPIServer], pathVariables, parameters, result) + }) + It("should get the metrics in specified time scope", func() { + By("get the results from 555555") + parameters = map[string]string{"start-time": "555555", "order-direction": "asc", "page": "1", "results-per-page": "10"} + result := AppAggregatedMetricResult{ + TotalResults: 3, + TotalPages: 1, + Page: 1, + Resources: []models.AppMetric{ + { + AppId: appId, + MetricType: models.MetricNameMemoryUsed, + Unit: models.UnitMegaBytes, + Value: "123456", + Timestamp: 555555, + }, + { + AppId: appId, + MetricType: models.MetricNameMemoryUsed, + Unit: models.UnitMegaBytes, + Value: "123456", + Timestamp: 555556, + }, + { + AppId: appId, + MetricType: models.MetricNameMemoryUsed, + Unit: models.UnitMegaBytes, + Value: "123456", + Timestamp: 666666, + }, }, - }, - } - checkAggregatedMetricResult(components.Ports[GolangAPIServer], pathVariables, parameters, result) + } + checkAggregatedMetricResult(components.Ports[GolangAPIServer], pathVariables, parameters, result) - By("get the results to 444444") - parameters = map[string]string{"end-time": "444444", "order-direction": "asc", "page": "1", "results-per-page": "10"} - result = AppAggregatedMetricResult{ - TotalResults: 2, - TotalPages: 1, - Page: 1, - Resources: []models.AppMetric{ - { - AppId: appId, - MetricType: models.MetricNameMemoryUsed, - Unit: models.UnitMegaBytes, - Value: "123456", - Timestamp: 333333, - }, - { - AppId: appId, - MetricType: models.MetricNameMemoryUsed, - Unit: models.UnitMegaBytes, - Value: "123456", - Timestamp: 444444, + By("get the results to 444444") + parameters = map[string]string{"end-time": "444444", "order-direction": "asc", "page": "1", "results-per-page": "10"} + result = AppAggregatedMetricResult{ + TotalResults: 2, + TotalPages: 1, + Page: 1, + Resources: []models.AppMetric{ + { + AppId: appId, + MetricType: models.MetricNameMemoryUsed, + Unit: models.UnitMegaBytes, + Value: "123456", + Timestamp: 333333, + }, + { + AppId: appId, + MetricType: models.MetricNameMemoryUsed, + Unit: models.UnitMegaBytes, + Value: "123456", + Timestamp: 444444, + }, }, - }, - } - checkAggregatedMetricResult(components.Ports[GolangAPIServer], pathVariables, parameters, result) + } + checkAggregatedMetricResult(components.Ports[GolangAPIServer], pathVariables, parameters, result) - By("get the results from 444444 to 555556") - parameters = map[string]string{"start-time": "444444", "end-time": "555556", "order-direction": "asc", "page": "1", "results-per-page": "10"} - result = AppAggregatedMetricResult{ - TotalResults: 3, - TotalPages: 1, - Page: 1, - Resources: []models.AppMetric{ - { - AppId: appId, - MetricType: models.MetricNameMemoryUsed, - Unit: models.UnitMegaBytes, - Value: "123456", - Timestamp: 444444, - }, - { - AppId: appId, - MetricType: models.MetricNameMemoryUsed, - Unit: models.UnitMegaBytes, - Value: "123456", - Timestamp: 555555, + By("get the results from 444444 to 555556") + parameters = map[string]string{"start-time": "444444", "end-time": "555556", "order-direction": "asc", "page": "1", "results-per-page": "10"} + result = AppAggregatedMetricResult{ + TotalResults: 3, + TotalPages: 1, + Page: 1, + Resources: []models.AppMetric{ + { + AppId: appId, + MetricType: models.MetricNameMemoryUsed, + Unit: models.UnitMegaBytes, + Value: "123456", + Timestamp: 444444, + }, + { + AppId: appId, + MetricType: models.MetricNameMemoryUsed, + Unit: models.UnitMegaBytes, + Value: "123456", + Timestamp: 555555, + }, + { + AppId: appId, + MetricType: models.MetricNameMemoryUsed, + Unit: models.UnitMegaBytes, + Value: "123456", + Timestamp: 555556, + }, }, - { - AppId: appId, - MetricType: models.MetricNameMemoryUsed, - Unit: models.UnitMegaBytes, - Value: "123456", - Timestamp: 555556, - }, - }, - } - checkAggregatedMetricResult(components.Ports[GolangAPIServer], pathVariables, parameters, result) + } + checkAggregatedMetricResult(components.Ports[GolangAPIServer], pathVariables, parameters, result) + }) }) }) }) diff --git a/src/autoscaler/integration/integration_golangapi_scalingengine_test.go b/src/autoscaler/integration/integration_golangapi_scalingengine_test.go index 4887057abd..fd089bdb1d 100644 --- a/src/autoscaler/integration/integration_golangapi_scalingengine_test.go +++ b/src/autoscaler/integration/integration_golangapi_scalingengine_test.go @@ -1,6 +1,7 @@ package integration_test import ( + "encoding/base64" "fmt" "net/http" @@ -17,6 +18,10 @@ var _ = Describe("Integration_GolangApi_ScalingEngine", func() { appId string pathVariables []string parameters map[string]string + serviceInstanceId string + bindingId string + orgId string + spaceId string ) BeforeEach(func() { @@ -36,7 +41,12 @@ var _ = Describe("Integration_GolangApi_ScalingEngine", func() { fmt.Sprintf("https://127.0.0.1:%d", components.Ports[EventGenerator]), "https://127.0.0.1:8888", tmpDir) + brokerAuth = base64.StdEncoding.EncodeToString([]byte("broker_username:broker_password")) startGolangApiServer() + serviceInstanceId = getRandomIdRef("serviceInstId") + orgId = getRandomIdRef("orgId") + spaceId = getRandomIdRef("spaceId") + bindingId = getRandomIdRef("bindingId") appId = uuid.NewString() pathVariables = []string{appId} @@ -107,259 +117,265 @@ var _ = Describe("Integration_GolangApi_ScalingEngine", func() { }) }) - Context("ScalingEngine is down", func() { - JustBeforeEach(func() { - stopScalingEngine() - parameters = map[string]string{"start-time": "1111", "end-time": "9999", "order-direction": "desc", "page": "1", "results-per-page": "5"} + When("the app is bound to the service instance", func() { + BeforeEach(func() { + provisionAndBind(serviceInstanceId, orgId, spaceId, bindingId, appId, components.Ports[GolangServiceBroker], httpClientForPublicApi) }) - It("should error with status code 500", func() { - checkPublicAPIResponseContentWithParameters(getScalingHistories, components.Ports[GolangAPIServer], pathVariables, parameters, http.StatusInternalServerError, map[string]interface{}{ - "message": "Error retrieving scaling history from scaling engine", - "code": "Internal Server Error", + Context("ScalingEngine is down", func() { + JustBeforeEach(func() { + stopScalingEngine() + parameters = map[string]string{"start-time": "1111", "end-time": "9999", "order-direction": "desc", "page": "1", "results-per-page": "5"} }) - }) + It("should error with status code 500", func() { + checkPublicAPIResponseContentWithParameters(getScalingHistories, components.Ports[GolangAPIServer], pathVariables, parameters, http.StatusInternalServerError, map[string]interface{}{ + "message": "Error retrieving scaling history from scaling engine", + "code": "Internal Server Error", + }) - }) + }) - Context("Get scaling histories", func() { - BeforeEach(func() { - history1 := createScalingHistoryError(appId, 666666) - insertScalingHistory(&history1) + }) - history2 := createScalingHistory(appId, 222222) - insertScalingHistory(&history2) + Context("Get scaling histories", func() { + BeforeEach(func() { + history1 := createScalingHistoryError(appId, 666666) + insertScalingHistory(&history1) - history3 := createScalingHistory(appId, 555555) - insertScalingHistory(&history3) + history2 := createScalingHistory(appId, 222222) + insertScalingHistory(&history2) - history4 := createScalingHistory(appId, 333333) - insertScalingHistory(&history4) + history3 := createScalingHistory(appId, 555555) + insertScalingHistory(&history3) - history5 := createScalingHistory(appId, 444444) - insertScalingHistory(&history5) + history4 := createScalingHistory(appId, 333333) + insertScalingHistory(&history4) - //add some other app id - history6 := createScalingHistory(getRandomIdRef("history6"), 444444) - insertScalingHistory(&history6) - }) - It("should get the scaling histories ", func() { - By("get the 1st page") - parameters = map[string]string{"start-time": "111111", "end-time": "999999", "order-direction": "desc", "page": "1", "results-per-page": "2"} - result := ScalingHistoryResult{ - TotalResults: 5, - TotalPages: 3, - Page: 1, - NextUrl: getScalingHistoriesUrl(appId, parameters, 2), - Resources: []models.AppScalingHistory{ - { - AppId: appId, - Timestamp: 666666, - ScalingType: models.ScalingTypeDynamic, - Status: models.ScalingStatusFailed, - OldInstances: -1, - NewInstances: -1, - Reason: "a reason", - Message: "a message", - Error: "an error", - }, - { - AppId: appId, - Timestamp: 555555, - ScalingType: models.ScalingTypeDynamic, - Status: models.ScalingStatusSucceeded, - OldInstances: 2, - NewInstances: 4, - Reason: "a reason", - Message: "a message", - Error: "", - }, - }, - } - checkScalingHistoryResult(components.Ports[GolangAPIServer], pathVariables, parameters, result) + history5 := createScalingHistory(appId, 444444) + insertScalingHistory(&history5) - By("get the 2nd page") - parameters = map[string]string{"start-time": "111111", "end-time": "999999", "order-direction": "desc", "page": "2", "results-per-page": "2"} - result = ScalingHistoryResult{ - TotalResults: 5, - TotalPages: 3, - Page: 2, - PrevUrl: getScalingHistoriesUrl(appId, parameters, 1), - NextUrl: getScalingHistoriesUrl(appId, parameters, 3), - Resources: []models.AppScalingHistory{ - { - AppId: appId, - Timestamp: 444444, - ScalingType: models.ScalingTypeDynamic, - Status: models.ScalingStatusSucceeded, - OldInstances: 2, - NewInstances: 4, - Reason: "a reason", - Message: "a message", - Error: "", - }, - { - AppId: appId, - Timestamp: 333333, - ScalingType: models.ScalingTypeDynamic, - Status: models.ScalingStatusSucceeded, - OldInstances: 2, - NewInstances: 4, - Reason: "a reason", - Message: "a message", - Error: "", + //add some other app id + history6 := createScalingHistory(getRandomIdRef("history6"), 444444) + insertScalingHistory(&history6) + }) + It("should get the scaling histories ", func() { + By("get the 1st page") + parameters = map[string]string{"start-time": "111111", "end-time": "999999", "order-direction": "desc", "page": "1", "results-per-page": "2"} + result := ScalingHistoryResult{ + TotalResults: 5, + TotalPages: 3, + Page: 1, + NextUrl: getScalingHistoriesUrl(appId, parameters, 2), + Resources: []models.AppScalingHistory{ + { + AppId: appId, + Timestamp: 666666, + ScalingType: models.ScalingTypeDynamic, + Status: models.ScalingStatusFailed, + OldInstances: -1, + NewInstances: -1, + Reason: "a reason", + Message: "a message", + Error: "an error", + }, + { + AppId: appId, + Timestamp: 555555, + ScalingType: models.ScalingTypeDynamic, + Status: models.ScalingStatusSucceeded, + OldInstances: 2, + NewInstances: 4, + Reason: "a reason", + Message: "a message", + Error: "", + }, }, - }, - } - checkScalingHistoryResult(components.Ports[GolangAPIServer], pathVariables, parameters, result) + } + checkScalingHistoryResult(components.Ports[GolangAPIServer], pathVariables, parameters, result) - By("get the 3rd page") - parameters = map[string]string{"start-time": "111111", "end-time": "999999", "order-direction": "desc", "page": "3", "results-per-page": "2"} - result = ScalingHistoryResult{ - TotalResults: 5, - TotalPages: 3, - Page: 3, - PrevUrl: getScalingHistoriesUrl(appId, parameters, 2), - Resources: []models.AppScalingHistory{ - { - AppId: appId, - Timestamp: 222222, - ScalingType: models.ScalingTypeDynamic, - Status: models.ScalingStatusSucceeded, - OldInstances: 2, - NewInstances: 4, - Reason: "a reason", - Message: "a message", - Error: "", + By("get the 2nd page") + parameters = map[string]string{"start-time": "111111", "end-time": "999999", "order-direction": "desc", "page": "2", "results-per-page": "2"} + result = ScalingHistoryResult{ + TotalResults: 5, + TotalPages: 3, + Page: 2, + PrevUrl: getScalingHistoriesUrl(appId, parameters, 1), + NextUrl: getScalingHistoriesUrl(appId, parameters, 3), + Resources: []models.AppScalingHistory{ + { + AppId: appId, + Timestamp: 444444, + ScalingType: models.ScalingTypeDynamic, + Status: models.ScalingStatusSucceeded, + OldInstances: 2, + NewInstances: 4, + Reason: "a reason", + Message: "a message", + Error: "", + }, + { + AppId: appId, + Timestamp: 333333, + ScalingType: models.ScalingTypeDynamic, + Status: models.ScalingStatusSucceeded, + OldInstances: 2, + NewInstances: 4, + Reason: "a reason", + Message: "a message", + Error: "", + }, }, - }, - } - checkScalingHistoryResult(components.Ports[GolangAPIServer], pathVariables, parameters, result) + } + checkScalingHistoryResult(components.Ports[GolangAPIServer], pathVariables, parameters, result) - By("the 4th page should be empty") - parameters = map[string]string{"start-time": "111111", "end-time": "999999", "order-direction": "desc", "page": "4", "results-per-page": "2"} - result = ScalingHistoryResult{ - TotalResults: 5, - TotalPages: 3, - Page: 4, - PrevUrl: getScalingHistoriesUrl(appId, parameters, 3), - Resources: []models.AppScalingHistory{}, - } - checkScalingHistoryResult(components.Ports[GolangAPIServer], pathVariables, parameters, result) - }) - It("should get the scaling histories in specified time scope", func() { - By("get the results from 555555") - parameters = map[string]string{"start-time": "555555", "order-direction": "desc", "page": "1", "results-per-page": "10"} - result := ScalingHistoryResult{ - TotalResults: 2, - TotalPages: 1, - Page: 1, - Resources: []models.AppScalingHistory{ - { - AppId: appId, - Timestamp: 666666, - ScalingType: models.ScalingTypeDynamic, - Status: models.ScalingStatusFailed, - OldInstances: -1, - NewInstances: -1, - Reason: "a reason", - Message: "a message", - Error: "an error", - }, - { - AppId: appId, - Timestamp: 555555, - ScalingType: models.ScalingTypeDynamic, - Status: models.ScalingStatusSucceeded, - OldInstances: 2, - NewInstances: 4, - Reason: "a reason", - Message: "a message", - Error: "", + By("get the 3rd page") + parameters = map[string]string{"start-time": "111111", "end-time": "999999", "order-direction": "desc", "page": "3", "results-per-page": "2"} + result = ScalingHistoryResult{ + TotalResults: 5, + TotalPages: 3, + Page: 3, + PrevUrl: getScalingHistoriesUrl(appId, parameters, 2), + Resources: []models.AppScalingHistory{ + { + AppId: appId, + Timestamp: 222222, + ScalingType: models.ScalingTypeDynamic, + Status: models.ScalingStatusSucceeded, + OldInstances: 2, + NewInstances: 4, + Reason: "a reason", + Message: "a message", + Error: "", + }, }, - }, - } - checkScalingHistoryResult(components.Ports[GolangAPIServer], pathVariables, parameters, result) + } + checkScalingHistoryResult(components.Ports[GolangAPIServer], pathVariables, parameters, result) - By("get the results to 333333") - parameters = map[string]string{"end-time": "333333", "order-direction": "desc", "page": "1", "results-per-page": "10"} - result = ScalingHistoryResult{ - TotalResults: 2, - TotalPages: 1, - Page: 1, - Resources: []models.AppScalingHistory{ - { - AppId: appId, - Timestamp: 333333, - ScalingType: models.ScalingTypeDynamic, - Status: models.ScalingStatusSucceeded, - OldInstances: 2, - NewInstances: 4, - Reason: "a reason", - Message: "a message", - Error: "", - }, - { - AppId: appId, - Timestamp: 222222, - ScalingType: models.ScalingTypeDynamic, - Status: models.ScalingStatusSucceeded, - OldInstances: 2, - NewInstances: 4, - Reason: "a reason", - Message: "a message", - Error: "", + By("the 4th page should be empty") + parameters = map[string]string{"start-time": "111111", "end-time": "999999", "order-direction": "desc", "page": "4", "results-per-page": "2"} + result = ScalingHistoryResult{ + TotalResults: 5, + TotalPages: 3, + Page: 4, + PrevUrl: getScalingHistoriesUrl(appId, parameters, 3), + Resources: []models.AppScalingHistory{}, + } + checkScalingHistoryResult(components.Ports[GolangAPIServer], pathVariables, parameters, result) + }) + It("should get the scaling histories in specified time scope", func() { + By("get the results from 555555") + parameters = map[string]string{"start-time": "555555", "order-direction": "desc", "page": "1", "results-per-page": "10"} + result := ScalingHistoryResult{ + TotalResults: 2, + TotalPages: 1, + Page: 1, + Resources: []models.AppScalingHistory{ + { + AppId: appId, + Timestamp: 666666, + ScalingType: models.ScalingTypeDynamic, + Status: models.ScalingStatusFailed, + OldInstances: -1, + NewInstances: -1, + Reason: "a reason", + Message: "a message", + Error: "an error", + }, + { + AppId: appId, + Timestamp: 555555, + ScalingType: models.ScalingTypeDynamic, + Status: models.ScalingStatusSucceeded, + OldInstances: 2, + NewInstances: 4, + Reason: "a reason", + Message: "a message", + Error: "", + }, }, - }, - } - checkScalingHistoryResult(components.Ports[GolangAPIServer], pathVariables, parameters, result) + } + checkScalingHistoryResult(components.Ports[GolangAPIServer], pathVariables, parameters, result) - By("get the results from 333333 to 555555") - parameters = map[string]string{"start-time": "333333", "end-time": "555555", "order-direction": "asc", "page": "1", "results-per-page": "10"} - result = ScalingHistoryResult{ - TotalResults: 3, - TotalPages: 1, - Page: 1, - Resources: []models.AppScalingHistory{ - { - AppId: appId, - Timestamp: 333333, - ScalingType: models.ScalingTypeDynamic, - Status: models.ScalingStatusSucceeded, - OldInstances: 2, - NewInstances: 4, - Reason: "a reason", - Message: "a message", - Error: "", - }, - { - AppId: appId, - Timestamp: 444444, - ScalingType: models.ScalingTypeDynamic, - Status: models.ScalingStatusSucceeded, - OldInstances: 2, - NewInstances: 4, - Reason: "a reason", - Message: "a message", - Error: "", + By("get the results to 333333") + parameters = map[string]string{"end-time": "333333", "order-direction": "desc", "page": "1", "results-per-page": "10"} + result = ScalingHistoryResult{ + TotalResults: 2, + TotalPages: 1, + Page: 1, + Resources: []models.AppScalingHistory{ + { + AppId: appId, + Timestamp: 333333, + ScalingType: models.ScalingTypeDynamic, + Status: models.ScalingStatusSucceeded, + OldInstances: 2, + NewInstances: 4, + Reason: "a reason", + Message: "a message", + Error: "", + }, + { + AppId: appId, + Timestamp: 222222, + ScalingType: models.ScalingTypeDynamic, + Status: models.ScalingStatusSucceeded, + OldInstances: 2, + NewInstances: 4, + Reason: "a reason", + Message: "a message", + Error: "", + }, }, - { - AppId: appId, - Timestamp: 555555, - ScalingType: models.ScalingTypeDynamic, - Status: models.ScalingStatusSucceeded, - OldInstances: 2, - NewInstances: 4, - Reason: "a reason", - Message: "a message", - Error: "", + } + checkScalingHistoryResult(components.Ports[GolangAPIServer], pathVariables, parameters, result) + + By("get the results from 333333 to 555555") + parameters = map[string]string{"start-time": "333333", "end-time": "555555", "order-direction": "asc", "page": "1", "results-per-page": "10"} + result = ScalingHistoryResult{ + TotalResults: 3, + TotalPages: 1, + Page: 1, + Resources: []models.AppScalingHistory{ + { + AppId: appId, + Timestamp: 333333, + ScalingType: models.ScalingTypeDynamic, + Status: models.ScalingStatusSucceeded, + OldInstances: 2, + NewInstances: 4, + Reason: "a reason", + Message: "a message", + Error: "", + }, + { + AppId: appId, + Timestamp: 444444, + ScalingType: models.ScalingTypeDynamic, + Status: models.ScalingStatusSucceeded, + OldInstances: 2, + NewInstances: 4, + Reason: "a reason", + Message: "a message", + Error: "", + }, + { + AppId: appId, + Timestamp: 555555, + ScalingType: models.ScalingTypeDynamic, + Status: models.ScalingStatusSucceeded, + OldInstances: 2, + NewInstances: 4, + Reason: "a reason", + Message: "a message", + Error: "", + }, }, - }, - } - checkScalingHistoryResult(components.Ports[GolangAPIServer], pathVariables, parameters, result) - }) + } + checkScalingHistoryResult(components.Ports[GolangAPIServer], pathVariables, parameters, result) + }) + }) }) }) }) diff --git a/src/autoscaler/routes/routes.go b/src/autoscaler/routes/routes.go index d845239d11..44dcf307ab 100644 --- a/src/autoscaler/routes/routes.go +++ b/src/autoscaler/routes/routes.go @@ -68,7 +68,6 @@ type AutoScalerRoute struct { apiOpenRoutes *mux.Router apiRoutes *mux.Router apiPolicyRoutes *mux.Router - apiCredentialRoutes *mux.Router } var autoScalerRouteInstance = newRouters() @@ -83,7 +82,6 @@ func newRouters() *AutoScalerRoute { apiOpenRoutes: mux.NewRouter(), apiRoutes: mux.NewRouter(), apiPolicyRoutes: mux.NewRouter(), - apiCredentialRoutes: mux.NewRouter(), } instance.metricsCollectorRoutes.Path(MetricHistoriesPath).Methods(http.MethodGet).Name(GetMetricHistoriesRouteName) @@ -146,6 +144,3 @@ func ApiRoutes() *mux.Router { func ApiPolicyRoutes() *mux.Router { return autoScalerRouteInstance.apiPolicyRoutes } -func ApiCredentialRoutes() *mux.Router { - return autoScalerRouteInstance.apiCredentialRoutes -}