CreateProject accepts a project instance and insert it to database
(projectInput *models.ApiInputProject)
| 101 | |
| 102 | // CreateProject accepts a project instance and insert it to database |
| 103 | func CreateProject(projectInput *models.ApiInputProject) (*models.ApiOutputProject, errors.Error) { |
| 104 | // verify input |
| 105 | if err := VerifyStruct(projectInput); err != nil { |
| 106 | return nil, err |
| 107 | } |
| 108 | |
| 109 | // create transaction to update multiple tables |
| 110 | var err errors.Error |
| 111 | tx := db.Begin() |
| 112 | defer func() { |
| 113 | if r := recover(); r != nil || err != nil { |
| 114 | err = tx.Rollback() |
| 115 | if err != nil { |
| 116 | logger.Error(err, "PatchProject: failed to rollback") |
| 117 | } |
| 118 | } |
| 119 | }() |
| 120 | |
| 121 | // create project first |
| 122 | project := &models.Project{} |
| 123 | project.BaseProject = projectInput.BaseProject |
| 124 | err = db.Create(project) |
| 125 | if err != nil { |
| 126 | if db.IsDuplicationError(err) { |
| 127 | return nil, errors.BadInput.New(fmt.Sprintf("A project with name [%s] already exists", project.Name)) |
| 128 | } |
| 129 | return nil, errors.Default.Wrap(err, "error creating DB project") |
| 130 | } |
| 131 | |
| 132 | // check if we need flush the Metrics |
| 133 | if len(projectInput.Metrics) > 0 { |
| 134 | err = refreshProjectMetrics(tx, projectInput) |
| 135 | if err != nil { |
| 136 | return nil, err |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // create blueprint |
| 141 | blueprint := &models.Blueprint{ |
| 142 | Name: project.Name + "-Blueprint", |
| 143 | ProjectName: project.Name, |
| 144 | Mode: "NORMAL", |
| 145 | Enable: true, |
| 146 | CronConfig: "0 0 * * *", |
| 147 | IsManual: false, |
| 148 | SyncPolicy: models.SyncPolicy{ |
| 149 | TimeAfter: func() *time.Time { |
| 150 | t := time.Now().AddDate(0, -6, 0) |
| 151 | t = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()) |
| 152 | return &t |
| 153 | }(), |
| 154 | }, |
| 155 | Connections: nil, |
| 156 | } |
| 157 | if projectInput.Blueprint != nil { |
| 158 | blueprint = projectInput.Blueprint |
| 159 | } |
| 160 | err = tx.Create(blueprint) |
nothing calls this directly
no test coverage detected