Exemplo n.º 1
0
func sliceOrEmptyVal(yamlMap generic.Map, key string, errs *ManifestErrors) *[]string {
	if !yamlMap.Has(key) {
		return new([]string)
	}

	var (
		stringSlice []string
		err         error
	)

	errMsg := fmt.Sprintf("Expected %s to be a list of strings.", key)

	switch input := yamlMap.Get(key).(type) {
	case []interface{}:
		for _, value := range input {
			stringValue, ok := value.(string)
			if !ok {
				err = errors.New(errMsg)
				break
			}
			stringSlice = append(stringSlice, stringValue)
		}
	default:
		err = errors.New(errMsg)
	}

	if err != nil {
		*errs = append(*errs, err)
		return nil
	}

	return &stringSlice
}
Exemplo n.º 2
0
func envVarOrEmptyMap(yamlMap generic.Map, errs *ManifestErrors) *map[string]string {
	key := "env"
	switch envVars := yamlMap.Get(key).(type) {
	case nil:
		aMap := make(map[string]string, 0)
		return &aMap
	case map[string]interface{}:
		yamlMap.Set(key, generic.NewMap(yamlMap.Get(key)))
		return envVarOrEmptyMap(yamlMap, errs)
	case generic.Map:
		merrs := validateEnvVars(envVars)
		if merrs != nil {
			*errs = append(*errs, merrs)
			return nil
		}

		result := make(map[string]string, envVars.Count())
		generic.Each(envVars, func(key, value interface{}) {
			result[key.(string)] = value.(string)
		})
		return &result
	default:
		*errs = append(*errs, errors.New(fmt.Sprintf("Expected %s to be a set of key => value.", key)))
		return nil
	}
}
Exemplo n.º 3
0
func setStringSlice(appMap generic.Map, key string, val interface{}, errs *ManifestErrors) {
	var (
		stringSlice []string
		err         error
	)

	errMsg := fmt.Sprintf("Expected %s to be a list of strings.", key)

	switch input := val.(type) {
	case []interface{}:
		for _, value := range input {
			stringValue, ok := value.(string)
			if !ok {
				err = errors.New(errMsg)
				break
			}
			stringSlice = append(stringSlice, stringValue)
		}
	default:
		err = errors.New(errMsg)
	}

	if err != nil {
		*errs = append(*errs, err)
		return
	}

	appMap.Set(key, stringSlice)
	return
}
Exemplo n.º 4
0
func setStringVal(appMap generic.Map, key string, val interface{}, errs *ManifestErrors) {
	stringVal, ok := val.(string)
	if !ok {
		*errs = append(*errs, errors.New(fmt.Sprintf("%s must be a string value", key)))
		return
	}
	appMap.Set(key, stringVal)
}
Exemplo n.º 5
0
Arquivo: manifest.go Projeto: nsnt/cli
func setBytesVal(appMap, yamlMap generic.Map, key string, errs *ManifestErrors) {
	value, err := formatters.ToMegabytes(yamlMap.Get(key).(string))
	if err != nil {
		*errs = append(*errs, errors.New(fmt.Sprintf("Unexpected value for %s :\n%s", key, err.Error())))
		return
	}
	appMap.Set(key, value)
}
Exemplo n.º 6
0
Arquivo: manifest.go Projeto: nsnt/cli
func setStringOrNullVal(appMap, yamlMap generic.Map, key string, errs *ManifestErrors) {
	switch val := yamlMap.Get(key).(type) {
	case string:
		appMap.Set(key, val)
	case nil:
		appMap.Set(key, "")
	default:
		*errs = append(*errs, errors.New(fmt.Sprintf("%s must be a string or null value", key)))
	}
}
Exemplo n.º 7
0
func formatDescription(metadata generic.Map, keys []string) string {
	parts := []string{}
	for _, key := range keys {
		value := metadata.Get(key)
		if value != nil {
			parts = append(parts, fmt.Sprintf("%s: %s", key, formatDescriptionPart(value)))
		}
	}
	return strings.Join(parts, ", ")
}
Exemplo n.º 8
0
Arquivo: manifest.go Projeto: nsnt/cli
func checkForNulls(appParams generic.Map) (errs ManifestErrors) {
	for key, _ := range manifestKeys {
		if key == "command" {
			continue
		}
		if appParams.IsNil(key) {
			errs = append(errs, errors.New(fmt.Sprintf("%s should not be null", key)))
		}
	}

	return
}
Exemplo n.º 9
0
Arquivo: manifest.go Projeto: julz/cli
func stringVal(yamlMap generic.Map, key string, errs *[]error) *string {
	val := yamlMap.Get(key)
	if val == nil {
		return nil
	}
	result, ok := val.(string)
	if !ok {
		*errs = append(*errs, errors.NewWithFmt("%s must be a string value", key))
		return nil
	}
	return &result
}
Exemplo n.º 10
0
func bytesVal(yamlMap generic.Map, key string, errs *ManifestErrors) *uint64 {
	yamlVal := yamlMap.Get(key)
	if yamlVal == nil {
		return nil
	}
	value, err := formatters.ToMegabytes(yamlVal.(string))
	if err != nil {
		*errs = append(*errs, errors.New(fmt.Sprintf("Unexpected value for %s :\n%s", key, err.Error())))
		return nil
	}
	return &value
}
Exemplo n.º 11
0
func stringVal(yamlMap generic.Map, key string, errs *ManifestErrors) *string {
	val := yamlMap.Get(key)
	if val == nil {
		return nil
	}
	result, ok := val.(string)
	if !ok {
		*errs = append(*errs, errors.New(fmt.Sprintf("%s must be a string value", key)))
		return nil
	}
	return &result
}
Exemplo n.º 12
0
Arquivo: manifest.go Projeto: nsnt/cli
func setBoolVal(appMap, yamlMap generic.Map, key string, errs *ManifestErrors) {
	switch val := yamlMap.Get(key).(type) {
	case bool:
		appMap.Set(key, val)
	case string:
		boolVal := val == "true"
		appMap.Set(key, boolVal)
	default:
		*errs = append(*errs, errors.New(fmt.Sprintf("Expected %s to be a boolean.", key)))
	}

	return
}
Exemplo n.º 13
0
func setEnvVar(appMap generic.Map, env interface{}, errs *ManifestErrors) {
	if !generic.IsMappable(env) {
		*errs = append(*errs, errors.New("Expected env vars to be a set of key => value."))
		return
	}

	merrs := validateEnvVars(env)
	if merrs != nil {
		*errs = append(*errs, merrs)
		return
	}

	appMap.Set("env", generic.NewMap(env))
}
Exemplo n.º 14
0
func stringOrNullVal(yamlMap generic.Map, key string, errs *ManifestErrors) *string {
	if !yamlMap.Has(key) {
		return nil
	}
	switch val := yamlMap.Get(key).(type) {
	case string:
		return &val
	case nil:
		empty := ""
		return &empty
	default:
		*errs = append(*errs, errors.New(fmt.Sprintf("%s must be a string or null value", key)))
		return nil
	}
}
Exemplo n.º 15
0
Arquivo: manifest.go Projeto: nsnt/cli
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
}
Exemplo n.º 16
0
func intVal(yamlMap generic.Map, key string, errs *ManifestErrors) *int {
	var (
		intVal int
		err    error
	)

	switch val := yamlMap.Get(key).(type) {
	case string:
		intVal, err = strconv.Atoi(val)
	case int:
		intVal = val
	case int64:
		intVal = int(val)
	case nil:
		return nil
	default:
		err = errors.New(fmt.Sprintf("Expected %s to be a number, but it was a %T.", key, val))
	}

	if err != nil {
		*errs = append(*errs, err)
		return nil
	}

	return &intVal
}
Exemplo n.º 17
0
func stringValOrDefault(yamlMap generic.Map, key string, errs *[]error) *string {
	if !yamlMap.Has(key) {
		return nil
	}
	empty := ""
	switch val := yamlMap.Get(key).(type) {
	case string:
		if val == "default" {
			return &empty
		} else {
			return &val
		}
	case nil:
		return &empty
	default:
		*errs = append(*errs, errors.NewWithFmt("%s must be a string or null value", key))
		return nil
	}
}
Exemplo n.º 18
0
func servicesOrEmptyVal(yamlMap generic.Map, key string, errs *ManifestErrors) (*[]string, *[]map[string]string) {
	if !yamlMap.Has(key) {
		return new([]string), new([]map[string]string)
	}

	var (
		stringSlice []string
		mapSlice    []map[string]string
		err         error
	)

	errMsg := fmt.Sprintf("Expected %s to be a list of strings.", key)

	switch input := yamlMap.Get(key).(type) {
	case []map[interface{}]interface{}:
		for _, mapValue := range input {
			stringMap := map[string]string{}
			for key, value := range mapValue {
				stringMap[key.(string)] = value.(string)
			}
			mapSlice = append(mapSlice, stringMap)
		}
	case []interface{}:
		for _, value := range input {
			stringValue, ok := value.(string)
			if !ok {
				err = errors.New(errMsg)
				break
			}
			stringSlice = append(stringSlice, stringValue)
		}
	default:
		err = errors.New(errMsg)
	}

	if err != nil {
		*errs = append(*errs, err)
		return nil, nil
	}

	return &stringSlice, &mapSlice
}
Exemplo n.º 19
0
func mapToAppSet(basePath string, data generic.Map) (appSet []models.AppParams, errs ManifestErrors) {
	if data.Has("applications") {
		appMaps, ok := data.Get("applications").([]interface{})
		if !ok {
			errs = append(errs, errors.New("Expected applications to be a list"))
			return
		}

		// we delete applications so that we may merge top level app params into each app
		data.Delete("applications")

		for _, appData := range appMaps {
			if !generic.IsMappable(appData) {
				errs = append(errs, errors.New("Expected application to be a dictionary"))
				continue
			}

			appMap := generic.DeepMerge(data, generic.NewMap(appData))

			appParams, appErrs := mapToAppParams(basePath, appMap)
			if !appErrs.Empty() {
				errs = append(errs, appErrs)
				continue
			}

			appSet = append(appSet, appParams)
		}
	}

	return
}
Exemplo n.º 20
0
func (m Manifest) getAppMaps(data generic.Map) (apps []generic.Map, errs []error) {
	globalProperties := data.Except([]interface{}{"applications"})

	if data.Has("applications") {
		appMaps, ok := data.Get("applications").([]interface{})
		if !ok {
			errs = append(errs, errors.New("Expected applications to be a list"))
			return
		}

		for _, appData := range appMaps {
			if !generic.IsMappable(appData) {
				errs = append(errs, errors.New(fmt.Sprintf("Expected application to be a list of key/value pairs\nError occurred in manifest near:\n'%s'", appData)))
				continue
			}

			appMap := generic.DeepMerge(globalProperties, generic.NewMap(appData))
			apps = append(apps, appMap)
		}
	} else {
		apps = append(apps, globalProperties)
	}

	return
}
Exemplo n.º 21
0
Arquivo: manifest.go Projeto: nsnt/cli
func setIntVal(appMap, yamlMap generic.Map, key string, errs *ManifestErrors) {
	var (
		intVal int
		err    error
	)

	switch val := yamlMap.Get(key).(type) {
	case string:
		intVal, err = strconv.Atoi(val)
	case int:
		intVal = val
	default:
		err = errors.New(fmt.Sprintf("Expected %s to be a number.", key))
	}

	if err != nil {
		*errs = append(*errs, err)
		return
	}

	appMap.Set(key, intVal)
}
Exemplo n.º 22
0
func boolVal(yamlMap generic.Map, key string, errs *ManifestErrors) bool {
	switch val := yamlMap.Get(key).(type) {
	case nil:
		return false
	case bool:
		return val
	case string:
		return val == "true"
	default:
		*errs = append(*errs, errors.New(fmt.Sprintf("Expected %s to be a boolean.", key)))
		return false
	}
}
Exemplo n.º 23
0
Arquivo: manifest.go Projeto: nsnt/cli
func setTimeoutVal(appMap, yamlMap generic.Map, key string, errs *ManifestErrors) {
	var (
		intVal int
		err    error
	)

	switch val := yamlMap.Get(key).(type) {
	case string:
		intVal, err = strconv.Atoi(val)
	case int:
		intVal = val
	default:
		err = errors.New("Expected health_check_timeout to be a number.")
	}

	if err != nil {
		*errs = append(*errs, err)
		return
	}

	appMap.Set("health_check_timeout", intVal)
}
Exemplo n.º 24
0
func envVarOrEmptyMap(yamlMap generic.Map, errs *ManifestErrors) *map[string]string {
	key := "env"
	switch envVars := yamlMap.Get(key).(type) {
	case nil:
		aMap := make(map[string]string, 0)
		return &aMap
	case map[string]interface{}:
		yamlMap.Set(key, generic.NewMap(yamlMap.Get(key)))
		return envVarOrEmptyMap(yamlMap, errs)
	case map[interface{}]interface{}:
		yamlMap.Set(key, generic.NewMap(yamlMap.Get(key)))
		return envVarOrEmptyMap(yamlMap, errs)
	case generic.Map:
		merrs := validateEnvVars(envVars)
		if merrs != nil {
			*errs = append(*errs, merrs)
			return nil
		}

		result := make(map[string]string, envVars.Count())
		generic.Each(envVars, func(key, value interface{}) {

			switch value.(type) {
			case string:
				result[key.(string)] = value.(string)
			case int64, int, int32:
				result[key.(string)] = fmt.Sprintf("%d", value)
			case float32, float64:
				result[key.(string)] = fmt.Sprintf("%f", value)
			default:
				*errs = append(*errs, errors.New(fmt.Sprintf("Expected environment variable %s to have a string value, but it was a %T.", key, value)))
			}

		})
		return &result
	default:
		*errs = append(*errs, errors.New(fmt.Sprintf("Expected %s to be a set of key => value, but it was a %T.", key, envVars)))
		return nil
	}
}
Exemplo n.º 25
0
Arquivo: manifest.go Projeto: nsnt/cli
func setEnvVarOrEmptyMap(appMap, yamlMap generic.Map, key string, errs *ManifestErrors) {
	if !yamlMap.Has(key) {
		appMap.Set(key, generic.NewMap())
		return
	}

	envVars := yamlMap.Get(key)

	if !generic.IsMappable(envVars) {
		*errs = append(*errs, errors.New(fmt.Sprintf("Expected %s to be a set of key => value.", key)))
		return
	}

	merrs := validateEnvVars(envVars)
	if merrs != nil {
		*errs = append(*errs, merrs)
		return
	}

	appMap.Set(key, generic.NewMap(envVars))
}
Exemplo n.º 26
0
		Expect(routeRepo.BoundRouteGuid).To(Equal("my-new-app-route-guid"))

		testassert.SliceContains(ui.Outputs, testassert.Lines{
			{"Creating app", "my-new-app", "my-org", "my-space"},
			{"OK"},
			{"Creating", "my-new-app.shared.cf-app.com"},
			{"OK"},
			{"Binding", "my-new-app.shared.cf-app.com"},
			{"OK"},
			{"Uploading my-new-app"},
			{"OK"},
		})
	})

	Describe("randomized hostnames", func() {
		var manifestApp generic.Map

		BeforeEach(func() {
			appRepo.ReadNotFound = true

			manifest := singleAppManifest()
			manifestApp = manifest.Data.Get("applications").([]interface{})[0].(generic.Map)
			manifestApp.Delete("host")
			manifestRepo.ReadManifestReturns.Manifest = manifest
		})

		It("provides a random hostname when the --random-route flag is passed", func() {
			callPush("--random-route", "my-new-app")
			Expect(routeRepo.FindByHostAndDomainHost).To(Equal("my-new-app-laughing-cow"))
		})
Exemplo n.º 27
0
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
}