// POST /api/orgs func CreateOrg(c *middleware.Context, cmd m.CreateOrgCommand) Response { if !c.IsSignedIn || (!setting.AllowUserOrgCreate && !c.IsGrafanaAdmin) { return ApiError(403, "Access denied", nil) } cmd.UserId = c.UserId if err := bus.Dispatch(&cmd); err != nil { if err == m.ErrOrgNameTaken { return ApiError(400, "Organization name taken", err) } return ApiError(500, "Failed to create organization", err) } metrics.M_Api_Org_Create.Inc(1) // We need to add the data source defined in config for this org to data_source table if err := sqlstore.AddDatasourceForOrg(cmd.Result.Id); err != nil { return ApiError(500, "Failed to add data source for organization", err) } return Json(200, &util.DynMap{ "orgId": cmd.Result.Id, "message": "Organization created", }) }
func CreateOrg(cmd *m.CreateOrgCommand) error { return inTransaction2(func(sess *session) error { if isNameTaken, err := isOrgNameTaken(cmd.Name, 0, sess); err != nil { return err } else if isNameTaken { return m.ErrOrgNameTaken } org := m.Org{ Name: cmd.Name, Created: time.Now(), Updated: time.Now(), } if _, err := sess.Insert(&org); err != nil { return err } systems := m.AddSystemsCommand{SystemsName: cmd.SystemsName, OrgId: org.Id} err := AddSystem(&systems) user := m.OrgUser{ OrgId: org.Id, UserId: cmd.UserId, Role: m.ROLE_ADMIN, Created: time.Now(), Updated: time.Now(), } _, err = sess.Insert(&user) cmd.Result = org sess.publishAfterCommit(&events.OrgCreated{ Timestamp: org.Created, Id: org.Id, Name: org.Name, }) return err }) }