|
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
|
@@ -922,3 +924,35 @@ def run_idl(self, txn):
|
922 | 924 | virtual_parents)
|
923 | 925 |
|
924 | 926 | setattr(lsp, 'options', options)
|
| 927 | + |
| 928 | + |
| 929 | +class DbSetCommand(command.BaseCommand): |
| 930 | + def __init__(self, api, table, record, *col_values, if_exists=False, |
| 931 | + **columns): |
| 932 | + super().__init__(api) |
| 933 | + self.table = table |
| 934 | + self.record = record |
| 935 | + self.col_values = col_values or columns.items() |
| 936 | + self.if_exists = if_exists |
| 937 | + |
| 938 | + def run_idl(self, txn): |
| 939 | + try: |
| 940 | + record = self.api.lookup(self.table, self.record) |
| 941 | + except idlutils.RowNotFound: |
| 942 | + if self.if_exists: |
| 943 | + return |
| 944 | + raise |
| 945 | + |
| 946 | + for col, val in self.col_values: |
| 947 | + if isinstance(val, abc.Mapping): |
| 948 | + if isinstance(val, collections.OrderedDict): |
| 949 | + val = dict(val) |
| 950 | + existing = getattr(record, col, {}) |
| 951 | + existing.update(val) |
| 952 | + val = existing |
| 953 | + # Since we are updating certain keys and leaving existing keys |
| 954 | + # but rewriting the whole external_ids column, we must verify() |
| 955 | + record.verify(col) |
| 956 | + # For non-map columns, we unconditionally overwrite the values that |
| 957 | + # exist, so prior state doesn't matter and we don't need verify() |
| 958 | + self.set_column(record, col, val) |
0 commit comments