2021-07-25 17:40:54 -04:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-02-02 17:15:17 -08:00
|
|
|
|
2021-06-06 07:57:24 -04:00
|
|
|
package event
|
|
|
|
|
|
|
|
|
|
import (
|
2021-06-22 16:23:27 -05:00
|
|
|
"fmt"
|
|
|
|
|
|
2021-07-14 17:31:58 -07:00
|
|
|
"github.com/hashicorp/go-secure-stdlib/base62"
|
2021-06-06 07:57:24 -04:00
|
|
|
)
|
|
|
|
|
|
2021-07-28 06:31:16 -04:00
|
|
|
const IdPrefix = "e"
|
2021-07-25 17:40:54 -04:00
|
|
|
|
|
|
|
|
// NewId is a bit of a modified NewId has been done to stop a circular
|
2021-06-23 12:26:09 -05:00
|
|
|
// dependency with the errors package that is caused by importing
|
|
|
|
|
// boundary/internal/db
|
2021-07-25 17:40:54 -04:00
|
|
|
func NewId(prefix string) (string, error) {
|
2021-06-06 07:57:24 -04:00
|
|
|
const op = "event.newId"
|
2021-06-23 12:26:09 -05:00
|
|
|
if prefix == "" {
|
|
|
|
|
return "", fmt.Errorf("%s: missing prefix: %w", op, ErrInvalidParameter)
|
|
|
|
|
}
|
|
|
|
|
var publicId string
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
|
|
publicId, err = base62.Random(10)
|
2021-06-06 07:57:24 -04:00
|
|
|
if err != nil {
|
2021-06-23 12:26:09 -05:00
|
|
|
return "", fmt.Errorf("%s: unable to generate id %v: %w", op, err, ErrIo)
|
2021-06-06 07:57:24 -04:00
|
|
|
}
|
2021-06-23 12:26:09 -05:00
|
|
|
return fmt.Sprintf("%s_%s", prefix, publicId), nil
|
2021-06-06 07:57:24 -04:00
|
|
|
}
|