CreateProject creates a project.
(ctx context.Context, create *ProjectMessage, creator *UserMessage)
| 200 | |
| 201 | // CreateProject creates a project. |
| 202 | func (s *Store) CreateProject(ctx context.Context, create *ProjectMessage, creator *UserMessage) (*ProjectMessage, error) { |
| 203 | if creator == nil { |
| 204 | return nil, errors.Errorf("creator cannot be nil") |
| 205 | } |
| 206 | if create.Setting == nil { |
| 207 | create.Setting = &storepb.Project{} |
| 208 | } |
| 209 | payload, err := protojson.Marshal(create.Setting) |
| 210 | if err != nil { |
| 211 | return nil, err |
| 212 | } |
| 213 | |
| 214 | tx, err := s.GetDB().BeginTx(ctx, nil) |
| 215 | if err != nil { |
| 216 | return nil, err |
| 217 | } |
| 218 | defer tx.Rollback() |
| 219 | |
| 220 | project := &ProjectMessage{ |
| 221 | ResourceID: create.ResourceID, |
| 222 | Workspace: create.Workspace, |
| 223 | Title: create.Title, |
| 224 | Setting: create.Setting, |
| 225 | } |
| 226 | q := qb.Q().Space("INSERT INTO project (resource_id, workspace, name, setting)") |
| 227 | q.Space("VALUES (?, ?, ?, ?)", create.ResourceID, create.Workspace, create.Title, payload) |
| 228 | sql, args, err := q.ToSQL() |
| 229 | if err != nil { |
| 230 | return nil, err |
| 231 | } |
| 232 | if _, err := tx.ExecContext(ctx, sql, args...); err != nil { |
| 233 | return nil, err |
| 234 | } |
| 235 | |
| 236 | iamPolicy := &storepb.IamPolicy{ |
| 237 | Bindings: []*storepb.Binding{ |
| 238 | { |
| 239 | Role: common.FormatRole(ProjectOwnerRole), |
| 240 | Members: []string{ |
| 241 | common.FormatUserEmail(creator.Email), |
| 242 | }, |
| 243 | Condition: &expr.Expr{}, |
| 244 | }, |
| 245 | }, |
| 246 | } |
| 247 | policyPayload, err := protojson.Marshal(iamPolicy) |
| 248 | if err != nil { |
| 249 | return nil, err |
| 250 | } |
| 251 | policyMessage, err := upsertPolicyImpl(ctx, tx, &PolicyMessage{ |
| 252 | Workspace: create.Workspace, |
| 253 | ResourceType: storepb.Policy_PROJECT, |
| 254 | Resource: common.FormatProject(project.ResourceID), |
| 255 | Payload: string(policyPayload), |
| 256 | Type: storepb.Policy_IAM, |
| 257 | InheritFromParent: false, |
| 258 | // Enforce cannot be false while creating a policy. |
| 259 | Enforce: true, |
nothing calls this directly
no test coverage detected