Ejemplo n.º 1
0
// CreateAndInsertTenant creat the tenant and insert to collection according
// by tenantname and desc.
func (p *TenantService) createAndInsertTenant(tenantName string, desc string) (tenantId string, err error) {
	tenant, erro := p.getTenantByName(tenantName)
	if erro == nil {
		logrus.Infoln("tenant already exist! tenantname: ", tenantName)
		tenantId = tenant.ObjectId.Hex()
		return
	}

	currentTime := dao.GetCurrentTime()
	objectId := bson.NewObjectId()
	newTenant := entity.Tenant{
		ObjectId:    objectId,
		Tenantname:  tenantName,
		Description: desc,
		TimeCreate:  currentTime,
		TimeUpdate:  currentTime,
	}

	err = dao.HandleInsert(p.collectionName, &newTenant)
	if err != nil {
		logrus.Error("create tenant error %v", err)
		return
	}
	tenantId = newTenant.ObjectId.Hex()
	return

}
Ejemplo n.º 2
0
func (p *UserService) createAndInsertUser(userName string, password string, email string, tenanId string, roleId string, company string) (userId string, err error) {
	// var jsondocument interface{}
	currentUser, erro := p.getAllUserByName(userName)
	if erro != nil {
		logrus.Error("get all user by username err is %v", erro)
		return "", erro
	}
	if len(currentUser) != 0 {
		logrus.Infoln("user already exist! username:"******"create user error %v", err)
		return
	}
	userId = user.ObjectId.Hex()

	return
}
Ejemplo n.º 3
0
// CreateAndInsertRole creat and insert the role items according to the given
// rolename and desc.
func (p *RoleService) createAndInsertRole(roleName string, desc string) (roleId string, err error) {
	role := &entity.Role{}
	role, err = p.getRoleByName(roleName)
	if err == nil {
		logrus.Infoln("role already exist! roleName: ", roleName)
		roleId = role.ObjectId.Hex()
		return
	}

	currentTime := dao.GetCurrentTime()
	objectId := bson.NewObjectId()
	role = &entity.Role{
		ObjectId:    objectId,
		Rolename:    roleName,
		Description: desc,
		TimeCreate:  currentTime,
		TimeUpdate:  currentTime,
	}

	err = dao.HandleInsert(p.collectionName, role)
	if err != nil {
		logrus.Warnln("create role error %v", err)
		return
	}
	roleId = role.ObjectId.Hex()
	return

}
Ejemplo n.º 4
0
func (p *ClusterService) CreateUserCluster(cluster entity.Cluster, x_auth_token string) (newCluster *entity.Cluster,
	errorCode string, err error) {

	// generate ObjectId
	cluster.ObjectId = bson.NewObjectId()

	userId := cluster.UserId
	if len(userId) == 0 {
		err = errors.New("user_id not provided")
		errorCode = COMMON_ERROR_INVALIDATE
		logrus.Errorf("create cluster [%v] error is %v", cluster, err)
		return
	}

	user, err := GetUserById(userId, x_auth_token)
	if err != nil {
		logrus.Errorf("get user by id err is %v", err)
		errorCode = CLUSTER_ERROR_CALL_USERMGMT
		return nil, errorCode, err
	}
	cluster.TenantId = user.TenantId
	cluster.Owner = user.Username

	// set created_time and updated_time
	cluster.TimeCreate = dao.GetCurrentTime()
	cluster.TimeUpdate = cluster.TimeCreate
	cluster.Status = CLUSTER_STATUS_DEPLOYING
	// insert bson to mongodb
	err = dao.HandleInsert(p.collectionName, cluster)
	if err != nil {
		errorCode = CLUSTER_ERROR_CALL_MONGODB
		logrus.Errorf("create cluster [%v] to bson error is %v", cluster, err)
		return
	}

	//add records of hosts in db
	for i := 0; i < cluster.Instances; i++ {
		host := entity.Host{}
		host.ClusterId = cluster.ObjectId.Hex()
		host.ClusterName = cluster.Name
		host.Status = HOST_STATUS_DEPLOYING
		host.UserId = cluster.UserId
		host.TimeCreate = dao.GetCurrentTime()
		host.TimeUpdate = host.TimeCreate

		_, _, err := GetHostService().Create(host, x_auth_token)
		if err != nil {
			logrus.Errorf("insert host to db error is [%v]", err)
		}
	}

	if IsDeploymentEnabled() {
		//call deployment
		go CreateCluster(cluster, x_auth_token)
	}

	newCluster = &cluster
	return
}
Ejemplo n.º 5
0
func (p *ProviderService) Create(provider entity.IaaSProvider, token string) (newProvider entity.IaaSProvider, errorCode string, err error) {
	logrus.Infof("create provider [%v]", provider)
	errorCode, err = TokenValidation(token)
	if err != nil {
		logrus.Errorf("token validation failed for provider creation [%v]", err)
		return
	}

	// do authorize first
	if authorized := GetAuthService().Authorize("create_provider", token, "", p.collectionName); !authorized {
		err = errors.New("required opertion is not authorized!")
		errorCode = COMMON_ERROR_UNAUTHORIZED
		logrus.Errorf("create provider [%v] error is %v", provider, err)
		return
	}

	userId := provider.UserId
	if len(userId) == 0 {
		err = errors.New("user_id not provided")
		errorCode = COMMON_ERROR_INVALIDATE
		logrus.Errorf("create provider [%v] error is %v", provider, err)
		return
	}

	user, err := GetUserById(userId, token)
	if err != nil {
		logrus.Errorf("get user by id err is %v", err)
		errorCode = COMMON_ERROR_INTERNAL
		return newProvider, errorCode, err
	}

	provider.ObjectId = bson.NewObjectId()
	provider.TenantId = user.TenantId
	// set created_time and updated_time
	provider.TimeCreate = dao.GetCurrentTime()
	provider.TimeUpdate = provider.TimeCreate

	err = dao.HandleInsert(p.collectionName, provider)
	if err != nil {
		errorCode = PROVIDER_ERROR_CREATE
		logrus.Errorf("create provider [%v] to bson error is %v", provider, err)
		return
	}

	newProvider = provider

	return

}
Ejemplo n.º 6
0
func (p *HostService) Create(host entity.Host, x_auth_token string) (newHost entity.Host,
	errorCode string, err error) {
	logrus.Infof("start to create host [%v]", host)
	// do authorize first
	if authorized := GetAuthService().Authorize("create_host", x_auth_token, "", p.collectionName); !authorized {
		err = errors.New("required opertion is not authorized!")
		errorCode = COMMON_ERROR_UNAUTHORIZED
		logrus.Errorf("create host [%v] error is %v", host, err)
		return
	}

	// generate ObjectId
	host.ObjectId = bson.NewObjectId()

	token, err := GetTokenById(x_auth_token)
	if err != nil {
		errorCode = HOST_ERROR_CREATE
		logrus.Errorf("get token failed when create host [%v], error is %v", host, err)
		return
	}

	// set token_id and user_id from token
	host.TenantId = token.Tenant.Id
	host.UserId = token.User.Id

	// set created_time and updated_time
	host.TimeCreate = dao.GetCurrentTime()
	host.TimeUpdate = host.TimeCreate

	// insert bson to mongodb
	err = dao.HandleInsert(p.collectionName, host)
	if err != nil {
		errorCode = HOST_ERROR_CREATE
		logrus.Errorf("insert host [%v] to db error is %v", host, err)
		return
	}

	newHost = host

	return
}
Ejemplo n.º 7
0
// CheckAndGenerateToken check the token and creat the new token.
func (p *TokenService) checkAndGenerateToken(user string, passwd string, tenant string, needPasswd bool) (result string, err error) {
	//get user by name
	userobjAll, err := GetUserService().getAllUserByName(user)
	if err != nil {
		logrus.Errorf("get user by username err is %v", err)
		return result, err
	}
	if len(userobjAll) == 0 {
		logrus.Errorln("user does not exist! username:"******"user does not exit!")
	}
	userobj := userobjAll[0]

	//get tenant by name
	tenantobj, err := GetTenantService().getTenantByTenantId(tenant)
	if err != nil {
		logrus.Errorln("tenant does not exist! name:", tenant)
		return result, errors.New("tenant does not exist!")
	}

	tenantid := tenantobj.ObjectId.Hex()
	tenantname := tenantobj.Tenantname

	userid := userobj.ObjectId.Hex()
	password := userobj.Password
	role := userobj.RoleId

	if needPasswd {
		encryPassword := HashString(passwd)

		if !strings.EqualFold(encryPassword, password) {
			logrus.Errorln("invalid password!")
			return result, errors.New("invalid password!")
		}
	}

	//get role
	roleobj, err := GetRoleService().getRoleByRoleId(role)
	if err != nil {
		logrus.Errorln("role does not exist! rolename:", role)
		return result, errors.New("role does not exist!")
	}

	roleid := roleobj.ObjectId.Hex()

	time := common.UTIL.Props.GetString("expiration_time", "21600")
	newtime, err := strconv.ParseInt(strings.TrimSpace(time), 10, 64)
	if err != nil {
		logrus.Warnln("invalid expire time configured %v", err)
		newtime = int64(21600)
	}
	expireTime := GenerateExpireTime(newtime)
	currentTime := dao.GetCurrentTime()

	userpart := entity.UserPart{Id: userid, Username: user}
	tenantpart := entity.TenantPart{Id: tenantid, Tenantname: tenantname}
	rolepart := entity.RolePart{Id: roleid, Rolename: roleobj.Rolename}

	objectId := bson.NewObjectId()
	newtoken := entity.Token{
		ObjectId:   objectId,
		Expire:     expireTime,
		User:       userpart,
		Tenant:     tenantpart,
		Role:       rolepart,
		TimeCreate: currentTime,
		TimeUpdate: currentTime,
	}

	err = dao.HandleInsert(p.collectionName, &newtoken)
	result = newtoken.ObjectId.Hex()

	if err != nil {
		logrus.Errorf("save token err is %v", err)
		return result, err
	}

	return
}
Ejemplo n.º 8
0
func (p *ClusterService) CreateMgmtCluster(cluster entity.Cluster, x_auth_token string) (newCluster *entity.Cluster,
	errorCode string, err error) {
	//check if mgmt cluster already exist
	//only 1 allowed in database
	query := bson.M{}
	query["type"] = "mgmt"
	n, _, _, err := p.queryByQuery(query, 0, 0, "", x_auth_token, false)
	if n > 0 {
		return nil, CLUSTER_ERROR_CALL_MONGODB, errors.New("mgmt cluster already exist")
	}

	if cluster.Name == "" {
		cluster.Name = "Management"
	}
	token, err := GetTokenById(x_auth_token)
	if err != nil {
		logrus.Errorf("get token by id error is %v", err)
		errorCode = CLUSTER_ERROR_CALL_USERMGMT
		return nil, errorCode, err
	}
	if cluster.UserId == "" {
		cluster.UserId = token.User.Id
	}
	if cluster.Owner == "" {
		cluster.Owner = token.User.Username
	}
	if cluster.Details == "" {
		cluster.Details = "Cluster to manage other clusters"
	}
	cluster.TimeCreate = dao.GetCurrentTime()
	cluster.TimeUpdate = cluster.TimeCreate

	cluster.Status = CLUSTER_STATUS_DEPLOYING

	// insert bson to mongodb
	err = dao.HandleInsert(p.collectionName, cluster)
	if err != nil {
		errorCode = CLUSTER_ERROR_CALL_MONGODB
		logrus.Errorf("create cluster [%v] to bson error is %v", cluster, err)
		return
	}

	for i := 0; i < cluster.Instances; i++ {
		host := entity.Host{}
		host.ClusterId = cluster.ObjectId.Hex()
		host.ClusterName = cluster.Name
		host.Status = HOST_STATUS_DEPLOYING
		host.UserId = cluster.UserId
		host.TimeCreate = dao.GetCurrentTime()
		host.TimeUpdate = host.TimeCreate

		_, _, err := GetHostService().Create(host, x_auth_token)
		if err != nil {
			logrus.Errorf("insert host to db error is [%v]", err)
		}
	}

	newCluster = &cluster

	return
}