(ctx context.Context, orgID, name, location, description string, provider CASBackendProvider, creds any, defaultB bool, fallbackB bool, maxBytes *int64)
| 421 | } |
| 422 | |
| 423 | func (uc *CASBackendUseCase) Create(ctx context.Context, orgID, name, location, description string, provider CASBackendProvider, creds any, defaultB bool, fallbackB bool, maxBytes *int64) (*CASBackend, error) { |
| 424 | ctx, span := otelx.Start(ctx, casBackendTracer, "CASBackendUseCase.Create") |
| 425 | defer span.End() |
| 426 | |
| 427 | if orgID == "" || name == "" { |
| 428 | return nil, NewErrValidationStr("organization and name are required") |
| 429 | } |
| 430 | |
| 431 | // Backend cannot be both default and fallback |
| 432 | if defaultB && fallbackB { |
| 433 | return nil, NewErrValidationStr("a backend cannot be both default and fallback") |
| 434 | } |
| 435 | |
| 436 | orgUUID, err := uuid.Parse(orgID) |
| 437 | if err != nil { |
| 438 | return nil, NewErrInvalidUUID(err) |
| 439 | } |
| 440 | |
| 441 | // validate format of the name and the project |
| 442 | if err := ValidateIsDNS1123(name); err != nil { |
| 443 | return nil, NewErrValidation(err) |
| 444 | } |
| 445 | |
| 446 | // Determine max bytes: use provided value or default |
| 447 | finalMaxBytes := uc.MaxBytesDefault |
| 448 | if maxBytes != nil { |
| 449 | if *maxBytes < MinCASBackendMaxBytes { |
| 450 | return nil, NewErrValidationStr(fmt.Sprintf("max_bytes must be at least %s", bytefmt.ByteSize(uint64(MinCASBackendMaxBytes)))) |
| 451 | } |
| 452 | finalMaxBytes = *maxBytes |
| 453 | } |
| 454 | |
| 455 | secretName, err := uc.credsRW.SaveCredentials(ctx, orgID, creds) |
| 456 | if err != nil { |
| 457 | return nil, fmt.Errorf("storing the credentials: %w", err) |
| 458 | } |
| 459 | |
| 460 | backend, err := uc.repo.Create(ctx, &CASBackendCreateOpts{ |
| 461 | MaxBytes: finalMaxBytes, |
| 462 | Name: name, |
| 463 | CASBackendOpts: &CASBackendOpts{ |
| 464 | Location: location, SecretName: secretName, Provider: provider, Default: ToPtr(defaultB), Fallback: ToPtr(fallbackB), |
| 465 | Description: &description, |
| 466 | OrgID: orgUUID, |
| 467 | }, |
| 468 | }) |
| 469 | |
| 470 | if err != nil { |
| 471 | if IsErrAlreadyExists(err) { |
| 472 | return nil, NewErrAlreadyExistsStr("name already taken") |
| 473 | } |
| 474 | return nil, fmt.Errorf("failed to create CAS backend: %w", err) |
| 475 | } |
| 476 | |
| 477 | // Record CAS backend creation in audit log |
| 478 | if uc.auditorUC != nil { |
| 479 | uc.auditorUC.Dispatch(ctx, &events.CASBackendCreated{ |
| 480 | CASBackendBase: &events.CASBackendBase{ |
nothing calls this directly
no test coverage detected