Exemplo n.º 1
0
func postLaunchStatelessApplication(request *restful.Request, response *restful.Response) {
	namespace := request.PathParameter("namespace")
	name := request.PathParameter("statelessapplication")

	kubeApiServerEndPoint, kubeApiServerToken, err := configuration.GetAvailablekubeApiServerEndPoint()
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Get kube apiserver endpoint and token failure"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["namespace"] = namespace
		jsonMap["name"] = name
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(404, string(errorMessageByteSlice))
		return
	}

	environmentSlice := make([]interface{}, 0)
	err = request.ReadEntity(&environmentSlice)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Read body failure"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["kubeApiServerEndPoint"] = kubeApiServerEndPoint
		jsonMap["namespace"] = namespace
		jsonMap["name"] = name
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(400, string(errorMessageByteSlice))
		return
	}

	exist, err := monitor.ExistReplicationController(kubeApiServerEndPoint, kubeApiServerToken, namespace, name)
	if exist {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "The replication controller to use already exists"
		jsonMap["kubeApiServerEndPoint"] = kubeApiServerEndPoint
		jsonMap["namespace"] = namespace
		jsonMap["name"] = name
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(409, string(errorMessageByteSlice))
		return
	}

	err = application.LaunchStatelessApplication(kubeApiServerEndPoint, kubeApiServerToken, namespace, name, environmentSlice)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Launch stateless application failure"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["kubeApiServerEndPoint"] = kubeApiServerEndPoint
		jsonMap["namespace"] = namespace
		jsonMap["name"] = name
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(422, string(errorMessageByteSlice))
		return
	}
}
Exemplo n.º 2
0
func putReplicationControllerNotifier(request *restful.Request, response *restful.Response) {
	replicationControllerNotifierSerializable := new(notification.ReplicationControllerNotifierSerializable)
	err := request.ReadEntity(&replicationControllerNotifierSerializable)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Read body failure"
		jsonMap["ErrorMessage"] = err.Error()
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(400, string(errorMessageByteSlice))
		return
	}

	kubeApiServerEndPoint, kubeApiServerToken, err := configuration.GetAvailablekubeApiServerEndPoint()
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Get kube apiserver endpoint and token failure"
		jsonMap["ErrorMessage"] = err.Error()
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(404, string(errorMessageByteSlice))
		return
	}

	replicationControllerNotifierSerializable.KubeApiServerEndPoint = kubeApiServerEndPoint
	replicationControllerNotifierSerializable.KubeApiServerToken = kubeApiServerToken

	switch replicationControllerNotifierSerializable.Kind {
	case "application":
		_, err := deploy.GetStorage().LoadDeployInformation(replicationControllerNotifierSerializable.Namespace, replicationControllerNotifierSerializable.Name)
		if err != nil {
			jsonMap := make(map[string]interface{})
			jsonMap["Error"] = "Check whether the application exists or not failure"
			jsonMap["ErrorMessage"] = err.Error()
			jsonMap["replicationControllerNotifierSerializable"] = replicationControllerNotifierSerializable
			errorMessageByteSlice, _ := json.Marshal(jsonMap)
			log.Error(jsonMap)
			response.WriteErrorString(422, string(errorMessageByteSlice))
			return
		}
	case "selector":
		nameSlice, err := monitor.GetReplicationControllerNameFromSelector(replicationControllerNotifierSerializable.KubeApiServerEndPoint, replicationControllerNotifierSerializable.KubeApiServerToken, replicationControllerNotifierSerializable.Namespace, replicationControllerNotifierSerializable.Name)
		if err != nil {
			for _, name := range nameSlice {
				exist, err := monitor.ExistReplicationController(replicationControllerNotifierSerializable.KubeApiServerEndPoint, replicationControllerNotifierSerializable.KubeApiServerToken, replicationControllerNotifierSerializable.Namespace, name)
				if err != nil {
					jsonMap := make(map[string]interface{})
					jsonMap["Error"] = "Check whether the replication controller exists or not failure"
					jsonMap["ErrorMessage"] = err.Error()
					jsonMap["replicationControllerNotifierSerializable"] = replicationControllerNotifierSerializable
					errorMessageByteSlice, _ := json.Marshal(jsonMap)
					log.Error(jsonMap)
					response.WriteErrorString(422, string(errorMessageByteSlice))
					return
				}
				if exist == false {
					jsonMap := make(map[string]interface{})
					jsonMap["Error"] = "The replication controller to notify doesn't exist"
					jsonMap["ErrorMessage"] = err.Error()
					jsonMap["replicationControllerNotifierSerializable"] = replicationControllerNotifierSerializable
					errorMessageByteSlice, _ := json.Marshal(jsonMap)
					log.Error(jsonMap)
					response.WriteErrorString(404, string(errorMessageByteSlice))
					return
				}
			}
		}
	case "replicationController":
		exist, err := monitor.ExistReplicationController(replicationControllerNotifierSerializable.KubeApiServerEndPoint, replicationControllerNotifierSerializable.KubeApiServerToken, replicationControllerNotifierSerializable.Namespace, replicationControllerNotifierSerializable.Name)
		if err != nil {
			jsonMap := make(map[string]interface{})
			jsonMap["Error"] = "Check whether the replication controller exists or not failure"
			jsonMap["ErrorMessage"] = err.Error()
			jsonMap["replicationControllerNotifierSerializable"] = replicationControllerNotifierSerializable
			errorMessageByteSlice, _ := json.Marshal(jsonMap)
			log.Error(jsonMap)
			response.WriteErrorString(422, string(errorMessageByteSlice))
			return
		}
		if exist == false {
			jsonMap := make(map[string]interface{})
			jsonMap["Error"] = "The replication controller to notify doesn't exist"
			jsonMap["ErrorMessage"] = err.Error()
			jsonMap["replicationControllerNotifierSerializable"] = replicationControllerNotifierSerializable
			errorMessageByteSlice, _ := json.Marshal(jsonMap)
			log.Error(jsonMap)
			response.WriteErrorString(400, string(errorMessageByteSlice))
			return
		}
	default:
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "No such kind"
		jsonMap["replicationControllerNotifierSerializable"] = replicationControllerNotifierSerializable
		jsonMap["kind"] = replicationControllerNotifierSerializable.Kind
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(400, string(errorMessageByteSlice))
		return
	}

	replicationControllerNotifier, err := notification.ConvertFromSerializable(*replicationControllerNotifierSerializable)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Convert replication controller notifier failure"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["replicationControllerNotifierSerializable"] = replicationControllerNotifierSerializable
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(400, string(errorMessageByteSlice))
		return
	}

	err = notification.GetStorage().SaveReplicationControllerNotifierSerializable(replicationControllerNotifierSerializable)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Save replication controller notifier failure"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["replicationControllerNotifierSerializable"] = replicationControllerNotifierSerializable
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(422, string(errorMessageByteSlice))
		return
	}

	execute.AddReplicationControllerNotifier(&replicationControllerNotifier)
}