UpdateResourceInTransaction updates resource in db in transaction
(
context middleware.Context,
resourceSchema *schema.Schema, resourceID string,
dataMap map[string]interface{}, tenantIDs []string)
| 790 | |
| 791 | // UpdateResourceInTransaction updates resource in db in transaction |
| 792 | func UpdateResourceInTransaction( |
| 793 | context middleware.Context, |
| 794 | resourceSchema *schema.Schema, resourceID string, |
| 795 | dataMap map[string]interface{}, tenantIDs []string) error { |
| 796 | defer measureRequestTime(time.Now(), "update.in_tx", resourceSchema.ID) |
| 797 | |
| 798 | manager := schema.GetManager() |
| 799 | mainTransaction := context["transaction"].(transaction.Transaction) |
| 800 | environmentManager := extension.GetManager() |
| 801 | environment, ok := environmentManager.GetEnvironment(resourceSchema.ID) |
| 802 | if !ok { |
| 803 | return fmt.Errorf("No environment for schema") |
| 804 | } |
| 805 | filter := transaction.IDFilter(resourceID) |
| 806 | if tenantIDs != nil { |
| 807 | filter["tenant_id"] = tenantIDs |
| 808 | } |
| 809 | |
| 810 | var resource *schema.Resource |
| 811 | var err error |
| 812 | switch resourceSchema.GetLockingPolicy("update") { |
| 813 | case schema.NoLocking: |
| 814 | resource, err = mainTransaction.Fetch(resourceSchema, filter, nil) |
| 815 | case schema.LockRelatedResources: |
| 816 | resource, err = mainTransaction.LockFetch(resourceSchema, filter, schema.LockRelatedResources, nil) |
| 817 | case schema.SkipRelatedResources: |
| 818 | resource, err = mainTransaction.LockFetch(resourceSchema, filter, schema.SkipRelatedResources, nil) |
| 819 | } |
| 820 | |
| 821 | if err != nil { |
| 822 | return ResourceError{err, err.Error(), WrongQuery} |
| 823 | } |
| 824 | |
| 825 | if err := validate(context, &dataMap, resourceSchema.ValidateOnUpdate); err != nil { |
| 826 | return err |
| 827 | } |
| 828 | policy := context["policy"].(*schema.Policy) |
| 829 | // apply property filter |
| 830 | currCond := policy.GetCurrentResourceCondition() |
| 831 | err = currCond.ApplyPropertyConditionFilter(schema.ActionUpdate, resource.Data(), dataMap) |
| 832 | if err != nil { |
| 833 | return ResourceError{err, "", Unauthorized} |
| 834 | } |
| 835 | |
| 836 | err = resource.Update(dataMap) |
| 837 | if err != nil { |
| 838 | return ResourceError{err, err.Error(), WrongData} |
| 839 | } |
| 840 | context["resource"] = resource.Data() |
| 841 | |
| 842 | if err := extension.HandleEvent(context, environment, "pre_update_in_transaction", resourceSchema.ID); err != nil { |
| 843 | return err |
| 844 | } |
| 845 | |
| 846 | dataMap, ok = context["resource"].(map[string]interface{}) |
| 847 | if !ok { |
| 848 | return fmt.Errorf("Resource not JSON") |
| 849 | } |
no test coverage detected