|
| 1 | +import abc |
| 2 | +import enum |
| 3 | +from abc import abstractmethod |
| 4 | +from .. import issues, operation, settings as settings_impl, _apis |
| 5 | + |
| 6 | + |
| 7 | +class IDynamicConfigClient(abc.ABC): |
| 8 | + @abstractmethod |
| 9 | + def __init__(self, driver): |
| 10 | + pass |
| 11 | + |
| 12 | + @abstractmethod |
| 13 | + def replace_config(self, config, dry_run, allow_unknown_fields, settings): |
| 14 | + pass |
| 15 | + |
| 16 | + @abstractmethod |
| 17 | + def set_config(self, config, dry_run, allow_unknown_fields, settings): |
| 18 | + pass |
| 19 | + |
| 20 | + @abstractmethod |
| 21 | + def get_config(self, settings): |
| 22 | + pass |
| 23 | + |
| 24 | + @abstractmethod |
| 25 | + def get_node_labels(self, node_id, settings): |
| 26 | + pass |
| 27 | + |
| 28 | + |
| 29 | +class DynamicConfig(object): |
| 30 | + __slots__ = ("version", "cluster", "config") |
| 31 | + |
| 32 | + def __init__(self, version, cluster, config, *args, **kwargs): |
| 33 | + self.version = version |
| 34 | + self.cluster = cluster |
| 35 | + self.config = config |
| 36 | + |
| 37 | + |
| 38 | +class NodeLabels(object): |
| 39 | + __slots__ = ("labels") |
| 40 | + |
| 41 | + def __init__(self, labels, *args, **kwargs): |
| 42 | + self.labels = labels |
| 43 | + |
| 44 | + |
| 45 | +def _replace_config_request_factory(config, dry_run, allow_unknown_fields): |
| 46 | + request = _apis.ydb_dynamic_config.ReplaceConfigRequest() |
| 47 | + request.config = config |
| 48 | + request.dry_run = dry_run |
| 49 | + request.allow_unknown_fields = allow_unknown_fields |
| 50 | + return request |
| 51 | + |
| 52 | + |
| 53 | +def _set_config_request_factory(config, dry_run, allow_unknown_fields): |
| 54 | + request = _apis.ydb_dynamic_config.SetConfigRequest() |
| 55 | + request.config = config |
| 56 | + request.dry_run = dry_run |
| 57 | + request.allow_unknown_fields = allow_unknown_fields |
| 58 | + return request |
| 59 | + |
| 60 | + |
| 61 | +def _get_config_request_factory(): |
| 62 | + request = _apis.ydb_dynamic_config.GetConfigRequest() |
| 63 | + return request |
| 64 | + |
| 65 | + |
| 66 | +def _get_node_labels_request_factory(node_id): |
| 67 | + request = _apis.ydb_dynamic_config.GetNodeLabelsRequest() |
| 68 | + request.node_id = node_id |
| 69 | + return request |
| 70 | + |
| 71 | + |
| 72 | +def _wrap_dynamic_config(config_pb, dynamic_config_cls=None, *args, **kwargs): |
| 73 | + dynamic_config_cls = DynamicConfig if dynamic_config_cls is None else dynamic_config_cls |
| 74 | + return dynamic_config_cls( |
| 75 | + config_pb.identity.version, |
| 76 | + config_pb.identity.cluster, |
| 77 | + config_pb.config, |
| 78 | + *args, |
| 79 | + **kwargs |
| 80 | + ) |
| 81 | + |
| 82 | + |
| 83 | +def _wrap_get_config_response(rpc_state, response): |
| 84 | + issues._process_response(response.operation) |
| 85 | + message = _apis.ydb_dynamic_config.GetConfigResult() |
| 86 | + response.operation.result.Unpack(message) |
| 87 | + return _wrap_dynamic_config(message) |
| 88 | + |
| 89 | + |
| 90 | +def _wrap_node_labels(labels_pb, node_labels_cls=None, *args, **kwargs): |
| 91 | + node_labels_cls = NodeLabels if node_labels_cls is None else node_labels_cls |
| 92 | + return node_labels_cls( |
| 93 | + dict([(entry.label, entry.value) for entry in labels_pb.labels]), |
| 94 | + *args, |
| 95 | + **kwargs |
| 96 | + ) |
| 97 | + |
| 98 | + |
| 99 | +def _wrap_get_node_labels_response(rpc_state, response): |
| 100 | + issues._process_response(response.operation) |
| 101 | + message = _apis.ydb_dynamic_config.GetNodeLabelsResult() |
| 102 | + response.operation.result.Unpack(message) |
| 103 | + return _wrap_node_labels(message) |
| 104 | + |
| 105 | + |
| 106 | +class BaseDynamicConfigClient(IDynamicConfigClient): |
| 107 | + __slots__ = ("_driver",) |
| 108 | + |
| 109 | + def __init__(self, driver): |
| 110 | + self._driver = driver |
| 111 | + |
| 112 | + def replace_config(self, config, dry_run, allow_unknown_fields, settings=None): |
| 113 | + return self._driver( |
| 114 | + _replace_config_request_factory(config, dry_run, allow_unknown_fields), |
| 115 | + _apis.DynamicConfigService.Stub, |
| 116 | + _apis.DynamicConfigService.ReplaceConfig, |
| 117 | + operation.Operation, |
| 118 | + settings, |
| 119 | + ) |
| 120 | + |
| 121 | + def set_config(self, config, dry_run, allow_unknown_fields, settings=None): |
| 122 | + return self._driver( |
| 123 | + _set_config_request_factory(config, dry_run, allow_unknown_fields), |
| 124 | + _apis.DynamicConfigService.Stub, |
| 125 | + _apis.DynamicConfigService.SetConfig, |
| 126 | + operation.Operation, |
| 127 | + settings, |
| 128 | + ) |
| 129 | + |
| 130 | + def get_config(self, settings=None): |
| 131 | + return self._driver( |
| 132 | + _get_config_request_factory(), |
| 133 | + _apis.DynamicConfigService.Stub, |
| 134 | + _apis.DynamicConfigService.GetConfig, |
| 135 | + _wrap_get_config_response, |
| 136 | + settings, |
| 137 | + ) |
| 138 | + |
| 139 | + def get_node_labels(self, node_id, settings=None): |
| 140 | + return self._driver( |
| 141 | + _get_node_labels_request_factory(node_id), |
| 142 | + _apis.DynamicConfigService.Stub, |
| 143 | + _apis.DynamicConfigService.GetNodeLabels, |
| 144 | + _wrap_get_node_labels_response, |
| 145 | + settings, |
| 146 | + ) |
| 147 | + |
| 148 | + |
| 149 | +class DynamicConfigClient(BaseDynamicConfigClient): |
| 150 | + def async_replace_config(self, config, dry_run, allow_unknown_fields, settings=None): |
| 151 | + return self._driver.future( |
| 152 | + _replace_config_request_factory(config, dry_run, allow_unknown_fields), |
| 153 | + _apis.DynamicConfigService.Stub, |
| 154 | + _apis.DynamicConfigService.ReplaceConfig, |
| 155 | + operation.Operation, |
| 156 | + settings, |
| 157 | + ) |
| 158 | + |
| 159 | + def async_set_config(self, config, dry_run, allow_unknown_fields, settings=None): |
| 160 | + return self._driver.future( |
| 161 | + _set_config_request_factory(config, dry_run, allow_unknown_fields), |
| 162 | + _apis.DynamicConfigService.Stub, |
| 163 | + _apis.DynamicConfigService.SetConfig, |
| 164 | + operation.Operation, |
| 165 | + settings, |
| 166 | + ) |
| 167 | + |
| 168 | + def async_get_config(self, settings=None): |
| 169 | + return self._driver.future( |
| 170 | + _get_config_request_factory(), |
| 171 | + _apis.DynamicConfigService.Stub, |
| 172 | + _apis.DynamicConfigService.GetConfig, |
| 173 | + _wrap_get_config_response, |
| 174 | + settings, |
| 175 | + ) |
| 176 | + |
| 177 | + def async_get_node_labels(self, node_id, settings=None): |
| 178 | + return self._driver.future( |
| 179 | + _get_node_labels_request_factory(node_id), |
| 180 | + _apis.DynamicConfigService.Stub, |
| 181 | + _apis.DynamicConfigService.GetNodeLabels, |
| 182 | + _wrap_get_node_labels_response, |
| 183 | + settings, |
| 184 | + ) |
0 commit comments