Ejemplo n.º 1
0
func init() {
	// Load from database
	replicationControllerNotifierSerializableSlice, err := notification.GetStorage().LoadAllReplicationControllerNotifierSerializable()
	if err != nil {
		log.Error(err)
	} else {
		for _, replicationControllerNotifierSerializable := range replicationControllerNotifierSerializableSlice {
			replicationControllerNotifier, err := notification.ConvertFromSerializable(replicationControllerNotifierSerializable)
			if err != nil {
				log.Error(err)
			} else {
				AddReplicationControllerNotifier(&replicationControllerNotifier)
			}
		}
	}
}
Ejemplo 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)
}