(ctx context.Context, opts *biz.CASBackendUpdateOpts)
| 193 | } |
| 194 | |
| 195 | func (r *CASBackendRepo) Update(ctx context.Context, opts *biz.CASBackendUpdateOpts) (*biz.CASBackend, error) { |
| 196 | ctx, span := otelx.Start(ctx, casBackendRepoTracer, "CASBackendRepo.Update") |
| 197 | defer span.End() |
| 198 | |
| 199 | var ( |
| 200 | backend *ent.CASBackend |
| 201 | err error |
| 202 | ) |
| 203 | if err = WithTx(ctx, r.data.DB, func(tx *ent.Tx) error { |
| 204 | // 1 - unset default backend for all the other backends in the org |
| 205 | if opts.Default != nil && *opts.Default { |
| 206 | if err := tx.CASBackend.Update(). |
| 207 | Where(casbackend.HasOrganizationWith(organization.ID(opts.OrgID))). |
| 208 | Where(casbackend.Default(true)). |
| 209 | SetDefault(false). |
| 210 | Exec(ctx); err != nil { |
| 211 | return fmt.Errorf("failed to clear previous default backend: %w", err) |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | // 2 - unset fallback backend for all the other backends in the org |
| 216 | if opts.Fallback != nil && *opts.Fallback { |
| 217 | if err := tx.CASBackend.Update(). |
| 218 | Where(casbackend.HasOrganizationWith(organization.ID(opts.OrgID))). |
| 219 | Where(casbackend.Fallback(true)). |
| 220 | SetFallback(false). |
| 221 | Exec(ctx); err != nil { |
| 222 | return fmt.Errorf("failed to clear previous fallback backend: %w", err) |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | // 3 - Chain the list of updates |
| 227 | // TODO: allow setting values as empty, currently it's not possible. |
| 228 | // We do it in other models by providing pointers to string + setNillableX methods |
| 229 | updateChain := tx.CASBackend.UpdateOneID(opts.ID). |
| 230 | SetNillableDescription(opts.Description). |
| 231 | SetUpdatedAt(time.Now()) |
| 232 | |
| 233 | // Make default and fallback mutually exclusive |
| 234 | if opts.Default != nil { |
| 235 | updateChain = updateChain.SetDefault(*opts.Default) |
| 236 | if *opts.Default { |
| 237 | updateChain = updateChain.SetFallback(false) |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | if opts.Fallback != nil { |
| 242 | updateChain = updateChain.SetFallback(*opts.Fallback) |
| 243 | if *opts.Fallback { |
| 244 | updateChain = updateChain.SetDefault(false) |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | // If secretName is provided we set it |
| 249 | if opts.SecretName != "" { |
| 250 | updateChain = updateChain.SetSecretName(opts.SecretName) |
| 251 | } |
| 252 |
nothing calls this directly
no test coverage detected