AddObjectReference to an existing object. If the class contains a network ref, it has a side-effect on the schema: The schema will be updated to include this particular network ref class.
(ctx context.Context, principal *models.Principal, input *AddReferenceInput, repl *additional.ReplicationProperties, tenant string, )
| 35 | // ref, it has a side-effect on the schema: The schema will be updated to |
| 36 | // include this particular network ref class. |
| 37 | func (m *Manager) AddObjectReference(ctx context.Context, principal *models.Principal, |
| 38 | input *AddReferenceInput, repl *additional.ReplicationProperties, tenant string, |
| 39 | ) *Error { |
| 40 | m.metrics.AddReferenceInc() |
| 41 | defer m.metrics.AddReferenceDec() |
| 42 | |
| 43 | ctx = classcache.ContextWithClassCache(ctx) |
| 44 | if input.Class != "" { |
| 45 | class, _, err := m.resolveNS(principal, input.Class) |
| 46 | if err != nil { |
| 47 | return &Error{err.Error(), StatusUnprocessableEntity, err} |
| 48 | } |
| 49 | input.Class = class |
| 50 | } |
| 51 | |
| 52 | if err := m.authorizer.Authorize(ctx, principal, authorization.UPDATE, authorization.ShardsData(input.Class, tenant)...); err != nil { |
| 53 | return &Error{err.Error(), StatusForbidden, err} |
| 54 | } |
| 55 | |
| 56 | deprecatedEndpoint := input.Class == "" |
| 57 | if deprecatedEndpoint { |
| 58 | // NS-enabled: refuse the legacy scan-all-collections fallback. The |
| 59 | // REST layer rejects the deprecated route with 410 before this point; |
| 60 | // this is defensive for direct callers. |
| 61 | if m.config.Config.Namespaces.Enabled { |
| 62 | err := fmt.Errorf("adding a reference without a class is not supported; use /objects/{className}/{id}/references/{propertyName}") |
| 63 | return &Error{err.Error(), StatusGone, err} |
| 64 | } |
| 65 | // Non-NS clusters: legacy backward-compatibility scan. |
| 66 | if err := m.authorizer.Authorize(ctx, principal, authorization.READ, authorization.Collections()...); err != nil { |
| 67 | return &Error{err.Error(), StatusForbidden, err} |
| 68 | } |
| 69 | objectRes, err := m.getObjectFromRepo(ctx, "", input.ID, |
| 70 | additional.Properties{}, nil, tenant) |
| 71 | if err != nil { |
| 72 | errnf := ErrNotFound{} // treated as StatusBadRequest for backward comp |
| 73 | if errors.As(err, &errnf) { |
| 74 | return &Error{"source object deprecated", StatusBadRequest, err} |
| 75 | } else if errors.As(err, &ErrMultiTenancy{}) { |
| 76 | return &Error{"source object deprecated", StatusUnprocessableEntity, err} |
| 77 | } |
| 78 | return &Error{"source object deprecated", StatusInternalServerError, err} |
| 79 | } |
| 80 | input.Class = objectRes.Object().Class |
| 81 | } |
| 82 | |
| 83 | if err := validateReferenceName(input.Class, input.Property); err != nil { |
| 84 | return &Error{err.Error(), StatusBadRequest, err} |
| 85 | } |
| 86 | |
| 87 | class, schemaVersion, fetchedClass, typedErr := m.getAuthorizedFromClass(ctx, principal, input.Class) |
| 88 | if typedErr != nil { |
| 89 | return typedErr |
| 90 | } |
| 91 | |
| 92 | validator := validation.New(m.vectorRepo.Exists, m.config, repl, |
| 93 | principal, m.config.Config.Namespaces.Enabled) |
| 94 | targetRef, err := input.validate(validator, class) |
nothing calls this directly
no test coverage detected