|
11 | 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
12 | 12 | # License for the specific language governing permissions and limitations
|
13 | 13 | # under the License.
|
| 14 | +import collections |
| 15 | +from collections import abc |
14 | 16 |
|
15 | 17 | from oslo_utils import timeutils
|
16 | 18 | from ovsdbapp.backend.ovs_idl import command
|
@@ -894,3 +896,35 @@ def run_idl(self, txn):
|
894 | 896 | virtual_parents)
|
895 | 897 |
|
896 | 898 | setattr(lsp, 'options', options)
|
| 899 | + |
| 900 | + |
| 901 | +class DbSetCommand(command.BaseCommand): |
| 902 | + def __init__(self, api, table, record, *col_values, if_exists=False, |
| 903 | + **columns): |
| 904 | + super().__init__(api) |
| 905 | + self.table = table |
| 906 | + self.record = record |
| 907 | + self.col_values = col_values or columns.items() |
| 908 | + self.if_exists = if_exists |
| 909 | + |
| 910 | + def run_idl(self, txn): |
| 911 | + try: |
| 912 | + record = self.api.lookup(self.table, self.record) |
| 913 | + except idlutils.RowNotFound: |
| 914 | + if self.if_exists: |
| 915 | + return |
| 916 | + raise |
| 917 | + |
| 918 | + for col, val in self.col_values: |
| 919 | + if isinstance(val, abc.Mapping): |
| 920 | + if isinstance(val, collections.OrderedDict): |
| 921 | + val = dict(val) |
| 922 | + existing = getattr(record, col, {}) |
| 923 | + existing.update(val) |
| 924 | + val = existing |
| 925 | + # Since we are updating certain keys and leaving existing keys |
| 926 | + # but rewriting the whole external_ids column, we must verify() |
| 927 | + record.verify(col) |
| 928 | + # For non-map columns, we unconditionally overwrite the values that |
| 929 | + # exist, so prior state doesn't matter and we don't need verify() |
| 930 | + self.set_column(record, col, val) |
0 commit comments