MCPcopy Create free account
hub / github.com/chainloop-dev/chainloop / Create

Method Create

app/controlplane/pkg/data/casbackend.go:130–193  ·  view source on GitHub ↗

Create creates a new CAS backend in the given organization If it's set as default, it will unset the previous default backend

(ctx context.Context, opts *biz.CASBackendCreateOpts)

Source from the content-addressed store, hash-verified

128// Create creates a new CAS backend in the given organization
129// If it's set as default, it will unset the previous default backend
130func (r *CASBackendRepo) Create(ctx context.Context, opts *biz.CASBackendCreateOpts) (*biz.CASBackend, error) {
131 ctx, span := otelx.Start(ctx, casBackendRepoTracer, "CASBackendRepo.Create")
132 defer span.End()
133
134 var (
135 backend *ent.CASBackend
136 err error
137 )
138 if err := WithTx(ctx, r.data.DB, func(tx *ent.Tx) error {
139 // 0 - Prevent setting a backend as both default and fallback
140 if opts.Default != nil && *opts.Default && opts.Fallback != nil && *opts.Fallback {
141 return fmt.Errorf("a backend cannot be both default and fallback")
142 }
143
144 // 1 - unset default backend for all the other backends in the org
145 if opts.Default != nil && *opts.Default {
146 if err := tx.CASBackend.Update().
147 Where(casbackend.HasOrganizationWith(organization.ID(opts.OrgID))).
148 Where(casbackend.Default(true)).
149 SetDefault(false).
150 Exec(ctx); err != nil {
151 return fmt.Errorf("failed to clear previous default backend: %w", err)
152 }
153 }
154
155 // 2 - unset fallback backend for all the other backends in the org
156 if opts.Fallback != nil && *opts.Fallback {
157 if err := tx.CASBackend.Update().
158 Where(casbackend.HasOrganizationWith(organization.ID(opts.OrgID))).
159 Where(casbackend.Fallback(true)).
160 SetFallback(false).
161 Exec(ctx); err != nil {
162 return fmt.Errorf("failed to clear previous fallback backend: %w", err)
163 }
164 }
165
166 // 3 - create the new backend and set it as default/fallback if needed
167 backend, err = tx.CASBackend.Create().
168 SetName(opts.Name).
169 SetOrganizationID(opts.OrgID).
170 SetLocation(opts.Location).
171 SetNillableDescription(opts.Description).
172 SetNillableFallback(opts.Fallback).
173 SetProvider(opts.Provider).
174 SetNillableDefault(opts.Default).
175 SetNillableManaged(opts.Managed).
176 SetSecretName(opts.SecretName).
177 SetMaxBlobSizeBytes(opts.MaxBytes).
178 Save(ctx)
179 if err != nil {
180 if ent.IsConstraintError(err) {
181 return biz.NewErrAlreadyExists(err)
182 }
183
184 return fmt.Errorf("failed to create backend: %w", err)
185 }
186 return nil
187 }); err != nil {

Callers

nothing calls this directly

Implementers 3

CASBackendRepoapp/controlplane/pkg/biz/mocks/CASBack
CASBackendRepo_Expecterapp/controlplane/pkg/biz/mocks/CASBack
CASBackendRepoapp/controlplane/pkg/data/casbackend.g

Calls 15

FindByIDMethod · 0.95
StartFunction · 0.92
HasOrganizationWithFunction · 0.92
IDFunction · 0.92
DefaultFunction · 0.92
FallbackFunction · 0.92
IsConstraintErrorFunction · 0.92
NewErrAlreadyExistsFunction · 0.92
WithTxFunction · 0.85
ExecMethod · 0.65
UpdateMethod · 0.65
SaveMethod · 0.65

Tested by

no test coverage detected