func (p *ClusterService) queryByQuery(query bson.M, skip int, limit int, sort string,
	x_auth_token string, skipAuth bool) (total int, clusters []entity.Cluster,
	errorCode string, err error) {
	authQuery := bson.M{}
	if !skipAuth {
		// get auth query from auth service first
		authQuery, err = GetAuthService().BuildQueryByAuth("list_cluster", x_auth_token)
		if err != nil {
			logrus.Errorf("get auth query by token [%v] error is %v", x_auth_token, err)
			errorCode = COMMON_ERROR_INTERNAL
			return
		}
	}

	selector := generateQueryWithAuth(query, authQuery)
	clusters = []entity.Cluster{}
	queryStruct := dao.QueryStruct{
		CollectionName: p.collectionName,
		Selector:       selector,
		Skip:           skip,
		Limit:          limit,
		Sort:           sort,
	}
	total, err = dao.HandleQueryAll(&clusters, queryStruct)
	if err != nil {
		logrus.Errorf("query clusters by query [%v] error is %v", query, err)
		errorCode = CLUSTER_ERROR_QUERY
		return
	}
	return
}
Example #2
0
func (p *HostService) queryByQuery(query bson.M, skip int, limit int,
	x_auth_token string, skipAuth bool) (total int, hosts []entity.Host,
	errorCode string, err error) {
	authQuery := bson.M{}
	if !skipAuth {
		// get auth query from auth first
		authQuery, err = GetAuthService().BuildQueryByAuth("list_host", x_auth_token)
		if err != nil {
			logrus.Errorf("get auth query by token [%v] error is %v", x_auth_token, err)
			errorCode = COMMON_ERROR_INTERNAL
			return
		}
	}

	selector := generateQueryWithAuth(query, authQuery)
	hosts = []entity.Host{}
	// fix : "...." sort by time_create
	queryStruct := dao.QueryStruct{p.collectionName, selector, skip, limit, "time_create"}
	total, err = dao.HandleQueryAll(&hosts, queryStruct)
	if err != nil {
		logrus.Errorf("query hosts by query [%v] error is %v", query, err)
		errorCode = HOST_ERROR_QUERY

	}
	return
}
Example #3
0
func (p *UserService) getAllUserByName(username string) (user []entity.User, err error) {
	query := strings.Join([]string{"{\"username\": \"", username, "\"}"}, "")

	selector := make(bson.M)
	err = json.Unmarshal([]byte(query), &selector)
	if err != nil {
		return
	}
	selector, err = mejson.Unmarshal(selector)
	if err != nil {
		return
	}

	user = []entity.User{}
	queryStruct := dao.QueryStruct{
		CollectionName: p.userCollectionName,
		Selector:       selector,
		Skip:           0,
		Limit:          0,
		Sort:           ""}

	_, err = dao.HandleQueryAll(&user, queryStruct)

	return
}
Example #4
0
func (p *RoleService) RoleList(token string) (ret []entity.Role, count int, errorCode string, err error) {
	code, err := GetTokenService().TokenValidate(token)
	if err != nil {
		return nil, 0, code, err
	}

	query, err := GetAuthService().BuildQueryByAuth("list_roles", token)
	if err != nil {
		logrus.Error("auth failed during query all role: %v", err)
		return nil, 0, ROLE_ERROR_GET, err
	}

	// ret = interface{}
	ret = []entity.Role{}
	queryStruct := dao.QueryStruct{
		CollectionName: p.collectionName,
		Selector:       query,
		Skip:           0,
		Limit:          0,
		Sort:           ""}

	count, err = dao.HandleQueryAll(&ret, queryStruct)

	return
}
func (p *TenantService) TenantList(token string, limit int, skip int, sort string) (ret []entity.Tenant, count int, errorCode string, err error) {
	code, err := GetTokenService().TokenValidate(token)
	if err != nil {
		return nil, 0, code, err
	}

	query, err := GetAuthService().BuildQueryByAuth("list_tenants", token)
	if err != nil {
		logrus.Error("auth failed during query all tenant: %v", err)
		return nil, 0, TENANT_ERROR_GET, err
	}

	ret = []entity.Tenant{}
	queryStruct := dao.QueryStruct{
		CollectionName: p.collectionName,
		Selector:       query,
		Skip:           skip,
		Limit:          limit,
		Sort:           sort}
	count, err = dao.HandleQueryAll(&ret, queryStruct)

	return
}
Example #6
0
func (p *UserService) UserList(token string, limit int, skip int, sort string) (ret []entity.User, count int, errorCode string, err error) {
	code, err := GetTokenService().TokenValidate(token)
	if err != nil {
		return nil, 0, code, err
	}

	query, err := GetAuthService().BuildQueryByAuth("list_users", token)
	if err != nil {
		logrus.Error("auth failed during query all user: %v", err)
		return nil, 0, USER_ERROR_GET, err
	}

	result := []entity.User{}
	queryStruct := dao.QueryStruct{
		CollectionName: p.userCollectionName,
		Selector:       query,
		Skip:           skip,
		Limit:          limit,
		Sort:           sort}
	count, err = dao.HandleQueryAll(&result, queryStruct)

	return result, count, "", err
}
//delete someone's clusters
func (p *ClusterService) DeleteByUserId(userId string, token string) (errorCode string, err error) {
	logrus.Infof("start to delete Cluster with userid [%v]", userId)

	if len(userId) == 0 {
		errorCode := COMMON_ERROR_INVALIDATE
		err = errors.New("Invalid parameter userid")
		return errorCode, err
	}

	selector, err := GetAuthService().BuildQueryByAuth("delete_clusters", token)
	if err != nil {
		logrus.Errorf("get auth query by token [%v] error is %v", token, err)
		errorCode = COMMON_ERROR_INTERNAL
		return errorCode, err
	}

	selector["user_id"] = userId

	clusters := []entity.Cluster{}
	_, err = dao.HandleQueryAll(&clusters, dao.QueryStruct{p.collectionName, selector, 0, 0, ""})
	if err != nil {
		logrus.Errorf("get all cluster by userId error %v", err)
		errorCode = CLUSTER_ERROR_QUERY
		return
	}

	for _, cluster := range clusters {
		_, err := p.DeleteById(cluster.ObjectId.Hex(), token)
		if err != nil {
			logrus.Errorf("delete cluster by id error %v", err)
			continue
		}
	}

	return
}