| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent b9b9191 commit 7699d2e
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,15 +13,15 @@ | |||
| 13 | 13 | # limitations under the License. | |
| 14 | 14 | """Class that handles API calls to Spanner that deal with table metadata.""" | |
| 15 | 15 | ||
| 16 | - from typing import Iterable, Optional | ||
| 16 | + from typing import Any, Dict, Iterable, Optional, Union | ||
| 17 | 17 | import warnings | |
| 18 | 18 | ||
| 19 | - from spanner_orm import api | ||
| 20 | - from spanner_orm import error | ||
| 21 | - | ||
| 19 | + from google.api_core import client_options as api_client_options | ||
| 22 | 20 | from google.auth import credentials as auth_credentials | |
| 23 | 21 | from google.cloud.spanner_v1 import database as spanner_database | |
| 24 | 22 | from google.cloud.spanner_v1 import pool as spanner_pool | |
| 23 | + from spanner_orm import api | ||
| 24 | + from spanner_orm import error | ||
| 25 | 25 | ||
| 26 | 26 | ||
| 27 | 27 | class SpannerAdminApi(api.SpannerReadApi, api.SpannerWriteApi): | |
@@ -58,7 +58,11 @@ def connect(instance: str, | |||
| 58 | 58 | project: Optional[str] = None, | |
| 59 | 59 | credentials: Optional[auth_credentials.Credentials] = None, | |
| 60 | 60 | pool: Optional[spanner_pool.AbstractSessionPool] = None, | |
| 61 | - create_ddl: Optional[Iterable[str]] = None) -> SpannerAdminApi: | ||
| 61 | + create_ddl: Optional[Iterable[str]] = None, | ||
| 62 | + *, | ||
| 63 | + client_options: Union[api_client_options.ClientOptions, | ||
| 64 | + Dict[Any, Any], None] = None, | ||
| 65 | + disable_builtin_metrics: Optional[bool] = None) -> SpannerAdminApi: | ||
| 62 | 66 | """Connects the global Spanner admin API to a Spanner database. | |
| 63 | 67 | ||
| 64 | 68 | Deprecated in favor of from_connection(). | |
@@ -72,7 +76,9 @@ def connect(instance: str, | |||
| 72 | 76 | project=project, | |
| 73 | 77 | credentials=credentials, | |
| 74 | 78 | pool=pool, | |
| 75 | - create_ddl=create_ddl) | ||
| 79 | + create_ddl=create_ddl, | ||
| 80 | + client_options=client_options, | ||
| 81 | + disable_builtin_metrics=disable_builtin_metrics) | ||
| 76 | 82 | return from_connection(connection) | |
| 77 | 83 | ||
| 78 | 84 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -117,6 +117,7 @@ def __init__( | |||
| 117 | 117 | *, | |
| 118 | 118 | client_options: Union[api_client_options.ClientOptions, Dict[Any, Any], | |
| 119 | 119 | None] = None, | |
| 120 | + disable_builtin_metrics: Optional[bool] = None, | ||
| 120 | 121 | ): | |
| 121 | 122 | """Connects to the specified Spanner database.""" | |
| 122 | 123 | self._instance = instance | |
@@ -126,15 +127,20 @@ def __init__( | |||
| 126 | 127 | self._pool = pool | |
| 127 | 128 | self._create_ddl = create_ddl | |
| 128 | 129 | self._client_options = client_options | |
| 130 | + self._disable_builtin_metrics = disable_builtin_metrics | ||
| 129 | 131 | self.connect() | |
| 130 | 132 | ||
| 131 | 133 | def connect(self): | |
| 132 | 134 | """Establish a new connection to the specified Spanner database.""" | |
| 133 | - client = spanner.Client( | ||
| 134 | - project=self._project, | ||
| 135 | - credentials=self._credentials, | ||
| 136 | - client_options=self._client_options, | ||
| 137 | - ) | ||
| 135 | + client_kwargs = { | ||
| 136 | + 'project': self._project, | ||
| 137 | + 'credentials': self._credentials, | ||
| 138 | + 'client_options': self._client_options, | ||
| 139 | + } | ||
| 140 | + if self._disable_builtin_metrics is not None: | ||
| 141 | + client_kwargs['disable_builtin_metrics'] = self._disable_builtin_metrics | ||
| 142 | + | ||
| 143 | + client = spanner.Client(**client_kwargs) | ||
| 138 | 144 | instance = client.instance(self._instance) | |
| 139 | 145 | self.database = instance.database( | |
| 140 | 146 | self._database, pool=self._pool, ddl_statements=self._create_ddl or ()) | |
@@ -164,7 +170,11 @@ def connect( | |||
| 164 | 170 | database: str, | |
| 165 | 171 | project: Optional[str] = None, | |
| 166 | 172 | credentials: Optional[auth_credentials.Credentials] = None, | |
| 167 | - pool: Optional[spanner_pool.AbstractSessionPool] = None) -> SpannerApi: | ||
| 173 | + pool: Optional[spanner_pool.AbstractSessionPool] = None, | ||
| 174 | + *, | ||
| 175 | + client_options: Union[api_client_options.ClientOptions, Dict[Any, Any], | ||
| 176 | + None] = None, | ||
| 177 | + disable_builtin_metrics: Optional[bool] = None) -> SpannerApi: | ||
| 168 | 178 | """Connects to the Spanner database and sets the global spanner_api. | |
| 169 | 179 | ||
| 170 | 180 | Deprecated in favor of from_connection(). | |
@@ -174,7 +184,13 @@ def connect( | |||
| 174 | 184 | 'Please use ' | |
| 175 | 185 | 'spanner_orm.from_connection(spanner_orm.SpannerConnection(...))')) | |
| 176 | 186 | connection = SpannerConnection( | |
| 177 | - instance, database, project=project, credentials=credentials, pool=pool) | ||
| 187 | + instance, | ||
| 188 | + database, | ||
| 189 | + project=project, | ||
| 190 | + credentials=credentials, | ||
| 191 | + pool=pool, | ||
| 192 | + client_options=client_options, | ||
| 193 | + disable_builtin_metrics=disable_builtin_metrics) | ||
| 178 | 194 | return from_connection(connection) | |
| 179 | 195 | ||
| 180 | 196 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -73,6 +73,39 @@ def test_connection_args(self, client): | |||
| 73 | 73 | client.mock_calls, | |
| 74 | 74 | ) | |
| 75 | 75 | ||
| 76 | + @mock.patch.object(spanner, 'Client', autospec=True, spec_set=True) | ||
| 77 | + def test_connection_args_with_disable_builtin_metrics(self, client): | ||
| 78 | + client.return_value.instance.return_value.database.return_value = ( | ||
| 79 | + 'fake-database') | ||
| 80 | + connection = api.SpannerConnection( | ||
| 81 | + instance='some-instance', | ||
| 82 | + database='some-database', | ||
| 83 | + project='some-project', | ||
| 84 | + credentials='fake-credentials', | ||
| 85 | + pool='fake-pool', | ||
| 86 | + create_ddl=('fake-ddl',), | ||
| 87 | + client_options=dict(fake='options'), | ||
| 88 | + disable_builtin_metrics=True, | ||
| 89 | + ) | ||
| 90 | + self.assertEqual('fake-database', connection.database) | ||
| 91 | + self.assertSequenceEqual( | ||
| 92 | + ( | ||
| 93 | + mock.call( | ||
| 94 | + project='some-project', | ||
| 95 | + credentials='fake-credentials', | ||
| 96 | + client_options=dict(fake='options'), | ||
| 97 | + disable_builtin_metrics=True, | ||
| 98 | + ), | ||
| 99 | + mock.call().instance('some-instance'), | ||
| 100 | + mock.call().instance().database( | ||
| 101 | + 'some-database', | ||
| 102 | + pool='fake-pool', | ||
| 103 | + ddl_statements=('fake-ddl',), | ||
| 104 | + ), | ||
| 105 | + ), | ||
| 106 | + client.mock_calls, | ||
| 107 | + ) | ||
| 108 | + | ||
| 76 | 109 | @mock.patch('google.cloud.spanner.Client') | |
| 77 | 110 | def test_api_connection(self, client): | |
| 78 | 111 | connection = self.mock_connection(client) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments