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