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 }