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 }
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 }