func (cmd *SetEnv) Run(c *cli.Context) { varName := c.Args()[1] varValue := c.Args()[2] app := cmd.appReq.GetApplication() cmd.ui.Say("Setting env variable '%s' to '%s' for app %s in org %s / space %s as %s...", terminal.EntityNameColor(varName), terminal.EntityNameColor(varValue), terminal.EntityNameColor(app.Name), terminal.EntityNameColor(cmd.config.OrganizationFields.Name), terminal.EntityNameColor(cmd.config.SpaceFields.Name), terminal.EntityNameColor(cmd.config.Username()), ) appParams := app.ToParams() envParams := appParams.Get("env").(generic.Map) envParams.Set(varName, varValue) updateParams := cf.NewEmptyAppParams() updateParams.Set("env", envParams) _, apiResponse := cmd.appRepo.Update(app.Guid, updateParams) if apiResponse.IsNotSuccessful() { cmd.ui.Failed(apiResponse.Message) return } cmd.ui.Ok() cmd.ui.Say("TIP: Use '%s' to ensure your env variable changes take effect", terminal.CommandColor(cf.Name()+" push")) }
func (cmd *Scale) Run(c *cli.Context) { currentApp := cmd.appReq.GetApplication() cmd.ui.Say("Scaling app %s in org %s / space %s as %s...", terminal.EntityNameColor(currentApp.Name), terminal.EntityNameColor(cmd.config.OrganizationFields.Name), terminal.EntityNameColor(cmd.config.SpaceFields.Name), terminal.EntityNameColor(cmd.config.Username()), ) params := cf.NewEmptyAppParams() if c.String("m") != "" { memory, err := formatters.ToMegabytes(c.String("m")) if err != nil { cmd.ui.Say("Invalid value for memory") cmd.ui.FailWithUsage(c, "scale") return } params.Set("memory", memory) } if c.Int("i") != -1 { params.Set("instances", c.Int("i")) } _, apiResponse := cmd.appRepo.Update(currentApp.Guid, params) if apiResponse.IsNotSuccessful() { cmd.ui.Failed(apiResponse.Message) return } cmd.ui.Ok() cmd.ui.Say("") }
func (cmd *Stop) ApplicationStop(app cf.Application) (updatedApp cf.Application, err error) { if app.State == "stopped" { updatedApp = app cmd.ui.Say(terminal.WarningColor("App " + app.Name + " is already stopped")) return } cmd.ui.Say("Stopping app %s in org %s / space %s as %s...", terminal.EntityNameColor(app.Name), terminal.EntityNameColor(cmd.config.OrganizationFields.Name), terminal.EntityNameColor(cmd.config.SpaceFields.Name), terminal.EntityNameColor(cmd.config.Username()), ) params := cf.NewEmptyAppParams() params.Set("state", "STOPPED") updatedApp, apiResponse := cmd.appRepo.Update(app.Guid, params) if apiResponse.IsNotSuccessful() { err = errors.New(apiResponse.Message) cmd.ui.Failed(apiResponse.Message) return } cmd.ui.Ok() return }
func defaultAppParams() (params cf.AppParams) { params = cf.NewEmptyAppParams() params.Set("name", "my-cool-app") params.Set("buildpack", "buildpack-url") params.Set("space_guid", "some-space-guid") params.Set("stack_guid", "some-stack-guid") params.Set("command", "some-command") params.Set("memory", 2048) params.Set("instances", 3) return }
func TestCreateRejectsInproperNames(t *testing.T) { baseRequest := testnet.TestRequest{ Method: "POST", Path: "/v2/apps", Response: testnet.TestResponse{Status: http.StatusCreated, Body: "{}"}, } requests := []testnet.TestRequest{ baseRequest, baseRequest, } ts, _, repo := createAppRepo(t, requests) defer ts.Close() params := cf.NewEmptyAppParams() params.Set("name", "name with space") params.Set("space_guid", "some-space-guid") createdApp, apiResponse := repo.Create(params) assert.Equal(t, createdApp, cf.Application{}) assert.Contains(t, apiResponse.Message, "App name is invalid") params = cf.NewEmptyAppParams() params.Set("name", "name-with-inv@lid-chars!") params.Set("space_guid", "some-space-guid") _, apiResponse = repo.Create(params) assert.True(t, apiResponse.IsNotSuccessful()) params = cf.NewEmptyAppParams() params.Set("name", "Valid-Name") params.Set("space_guid", "some-space-guid") _, apiResponse = repo.Create(params) assert.True(t, apiResponse.IsSuccessful()) params = cf.NewEmptyAppParams() params.Set("name", "name_with_numbers_2") params.Set("space_guid", "some-space-guid") _, apiResponse = repo.Create(params) assert.True(t, apiResponse.IsSuccessful()) }
func mapToAppParams(yamlMap generic.Map) (appParams cf.AppParams, errs ManifestErrors) { appParams = cf.NewEmptyAppParams() errs = checkForNulls(yamlMap) if !errs.Empty() { return } for key, handler := range manifestKeys { if yamlMap.Has(key) { handler(appParams, yamlMap, key, &errs) } } return }
func mapToAppParams(yamlMap generic.Map) (appParams cf.AppParams, errs ManifestErrors) { appParams = cf.NewEmptyAppParams() errs = checkForNulls(yamlMap) if !errs.Empty() { return } for _, key := range []string{"buildpack", "command", "disk_quota", "domain", "host", "name", "path", "stack", "no-route"} { if yamlMap.Has(key) { setStringVal(appParams, key, yamlMap.Get(key), &errs) } } if yamlMap.Has("memory") { memory, err := formatters.ToMegabytes(yamlMap.Get("memory").(string)) if err != nil { errs = append(errs, errors.New(fmt.Sprintf("Unexpected value for app memory:\n%s", err.Error()))) return } appParams.Set("memory", memory) } if yamlMap.Has("timeout") { setIntVal(appParams, "health_check_timeout", yamlMap.Get("timeout"), &errs) } if yamlMap.Has("instances") { setIntVal(appParams, "instances", yamlMap.Get("instances"), &errs) } if yamlMap.Has("services") { setStringSlice(appParams, "services", yamlMap.Get("services"), &errs) } else { appParams.Set("services", []string{}) } if yamlMap.Has("env") { setEnvVar(appParams, yamlMap.Get("env"), &errs) } else { appParams.Set("env", generic.NewMap()) } return }
func TestUpdateApplicationSetCommandToNull(t *testing.T) { request := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "PUT", Path: "/v2/apps/my-app-guid", Matcher: testnet.RequestBodyMatcher(`{"command":""}`), Response: testnet.TestResponse{Status: http.StatusOK, Body: updateApplicationResponse}, }) ts, handler, repo := createAppRepo(t, []testnet.TestRequest{request}) defer ts.Close() app := cf.NewEmptyAppParams() app.Set("command", "") _, apiResponse := repo.Update("my-app-guid", app) assert.True(t, handler.AllRequestsCalled()) assert.False(t, apiResponse.IsNotSuccessful()) }
func (cmd *Start) ApplicationStart(app cf.Application) (updatedApp cf.Application, err error) { if app.State == "started" { cmd.ui.Say(terminal.WarningColor("App " + app.Name + " is already started")) return } stopLoggingChan := make(chan bool, 1) defer close(stopLoggingChan) loggingStartedChan := make(chan bool) defer close(loggingStartedChan) go cmd.tailStagingLogs(app, loggingStartedChan, stopLoggingChan) <-loggingStartedChan cmd.ui.Say("Starting app %s in org %s / space %s as %s...", terminal.EntityNameColor(app.Name), terminal.EntityNameColor(cmd.config.OrganizationFields.Name), terminal.EntityNameColor(cmd.config.SpaceFields.Name), terminal.EntityNameColor(cmd.config.Username()), ) params := cf.NewEmptyAppParams() params.Set("state", "STARTED") updatedApp, apiResponse := cmd.appRepo.Update(app.Guid, params) if apiResponse.IsNotSuccessful() { cmd.ui.Failed(apiResponse.Message) return } cmd.ui.Ok() cmd.waitForInstancesToStage(updatedApp) stopLoggingChan <- true cmd.ui.Say("") cmd.waitForOneRunningInstance(app.Guid) cmd.ui.Say(terminal.HeaderColor("\nApp started\n")) cmd.appDisplayer.ShowApp(app) return }
func TestCreateApplicationWithoutBuildpackStackOrCommand(t *testing.T) { request := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "POST", Path: "/v2/apps", Matcher: testnet.RequestBodyMatcher(`{"name":"my-cool-app","instances":1,"memory":128,"space_guid":"some-space-guid"}`), Response: testnet.TestResponse{Status: http.StatusCreated, Body: createApplicationResponse}, }) ts, handler, repo := createAppRepo(t, []testnet.TestRequest{request}) defer ts.Close() params := cf.NewEmptyAppParams() params.Set("name", "my-cool-app") params.Set("space_guid", "some-space-guid") params.Set("memory", 128) params.Set("instances", 1) _, apiResponse := repo.Create(params) assert.True(t, handler.AllRequestsCalled()) assert.False(t, apiResponse.IsNotSuccessful()) }
func TestSetEnv(t *testing.T) { request := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "PUT", Path: "/v2/apps/app1-guid", Matcher: testnet.RequestBodyMatcher(`{"environment_json":{"DATABASE_URL":"mysql://example.com/my-db"}}`), Response: testnet.TestResponse{Status: http.StatusCreated}, }) ts, handler, repo := createAppRepo(t, []testnet.TestRequest{request}) defer ts.Close() envParams := generic.NewMap() envParams.Set("DATABASE_URL", "mysql://example.com/my-db") params := cf.NewEmptyAppParams() params.Set("env", envParams) _, apiResponse := repo.Update("app1-guid", params) assert.True(t, handler.AllRequestsCalled()) assert.False(t, apiResponse.IsNotSuccessful()) }
func (cmd *RenameApp) Run(c *cli.Context) { app := cmd.appReq.GetApplication() new_name := c.Args()[1] cmd.ui.Say("Renaming app %s to %s in org %s / space %s as %s...", terminal.EntityNameColor(app.Name), terminal.EntityNameColor(new_name), terminal.EntityNameColor(cmd.config.OrganizationFields.Name), terminal.EntityNameColor(cmd.config.SpaceFields.Name), terminal.EntityNameColor(cmd.config.Username()), ) params := cf.NewEmptyAppParams() params.Set("name", new_name) _, apiResponse := cmd.appRepo.Update(app.Guid, params) if apiResponse.IsNotSuccessful() { cmd.ui.Failed(apiResponse.Message) return } cmd.ui.Ok() }