本文整理汇总了Python中airflow.contrib.hooks.gcp_spanner_hook.CloudSpannerHook.delete_instance方法的典型用法代码示例。如果您正苦于以下问题:Python CloudSpannerHook.delete_instance方法的具体用法?Python CloudSpannerHook.delete_instance怎么用?Python CloudSpannerHook.delete_instance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类airflow.contrib.hooks.gcp_spanner_hook.CloudSpannerHook
的用法示例。
在下文中一共展示了CloudSpannerHook.delete_instance方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: CloudSpannerInstanceDeleteOperator
# 需要导入模块: from airflow.contrib.hooks.gcp_spanner_hook import CloudSpannerHook [as 别名]
# 或者: from airflow.contrib.hooks.gcp_spanner_hook.CloudSpannerHook import delete_instance [as 别名]
class CloudSpannerInstanceDeleteOperator(BaseOperator):
"""
Deletes a Cloud Spanner instance. If an instance does not exist,
no action is taken and the operator succeeds.
.. seealso::
For more information on how to use this operator, take a look at the guide:
:ref:`howto/operator:CloudSpannerInstanceDeleteOperator`
:param instance_id: The Cloud Spanner instance ID.
:type instance_id: str
:param project_id: Optional, the ID of the project that owns the Cloud Spanner
Database. If set to None or missing, the default project_id from the GCP connection is used.
:type project_id: str
:param gcp_conn_id: The connection ID used to connect to Google Cloud Platform.
:type gcp_conn_id: str
"""
# [START gcp_spanner_delete_template_fields]
template_fields = ('project_id', 'instance_id', 'gcp_conn_id')
# [END gcp_spanner_delete_template_fields]
@apply_defaults
def __init__(self,
instance_id,
project_id=None,
gcp_conn_id='google_cloud_default',
*args, **kwargs):
self.instance_id = instance_id
self.project_id = project_id
self.gcp_conn_id = gcp_conn_id
self._validate_inputs()
self._hook = CloudSpannerHook(gcp_conn_id=gcp_conn_id)
super(CloudSpannerInstanceDeleteOperator, self).__init__(*args, **kwargs)
def _validate_inputs(self):
if self.project_id == '':
raise AirflowException("The required parameter 'project_id' is empty")
if not self.instance_id:
raise AirflowException("The required parameter 'instance_id' "
"is empty or None")
def execute(self, context):
if self._hook.get_instance(project_id=self.project_id, instance_id=self.instance_id):
return self._hook.delete_instance(project_id=self.project_id,
instance_id=self.instance_id)
else:
self.log.info("Instance '%s' does not exist in project '%s'. "
"Aborting delete.", self.instance_id, self.project_id)
return True
示例2: CloudSpannerInstanceDeleteOperator
# 需要导入模块: from airflow.contrib.hooks.gcp_spanner_hook import CloudSpannerHook [as 别名]
# 或者: from airflow.contrib.hooks.gcp_spanner_hook.CloudSpannerHook import delete_instance [as 别名]
class CloudSpannerInstanceDeleteOperator(BaseOperator):
"""
Deletes a Cloud Spanner instance. If an instance does not exist,
no action is taken and the operator succeeds.
:param project_id: The ID of the project that owns the Cloud Spanner Database.
:type project_id: str
:param instance_id: The Cloud Spanner instance ID.
:type instance_id: str
:param gcp_conn_id: The connection ID used to connect to Google Cloud Platform.
:type gcp_conn_id: str
"""
# [START gcp_spanner_delete_template_fields]
template_fields = ('project_id', 'instance_id', 'gcp_conn_id')
# [END gcp_spanner_delete_template_fields]
@apply_defaults
def __init__(self,
project_id,
instance_id,
gcp_conn_id='google_cloud_default',
*args, **kwargs):
self.instance_id = instance_id
self.project_id = project_id
self.gcp_conn_id = gcp_conn_id
self._validate_inputs()
self._hook = CloudSpannerHook(gcp_conn_id=gcp_conn_id)
super(CloudSpannerInstanceDeleteOperator, self).__init__(*args, **kwargs)
def _validate_inputs(self):
if not self.project_id:
raise AirflowException("The required parameter 'project_id' is empty")
if not self.instance_id:
raise AirflowException("The required parameter 'instance_id' is empty")
def execute(self, context):
if self._hook.get_instance(self.project_id, self.instance_id):
return self._hook.delete_instance(self.project_id,
self.instance_id)
else:
self.log.info("Instance '%s' does not exist in project '%s'. "
"Aborting delete.", self.instance_id, self.project_id)
return True
示例3: TestGcpSpannerHookNoDefaultProjectID
# 需要导入模块: from airflow.contrib.hooks.gcp_spanner_hook import CloudSpannerHook [as 别名]
# 或者: from airflow.contrib.hooks.gcp_spanner_hook.CloudSpannerHook import delete_instance [as 别名]
#.........这里部分代码省略.........
with self.assertRaises(AirflowException) as cm:
self.spanner_hook_no_default_project_id.update_instance(
instance_id=SPANNER_INSTANCE,
configuration_name=SPANNER_CONFIGURATION,
node_count=2,
display_name=SPANNER_DATABASE)
get_client.assert_not_called()
instance_method.assert_not_called()
update_method.assert_not_called()
err = cm.exception
self.assertIn("The project id must be passed", str(err))
@mock.patch('airflow.contrib.hooks.gcp_spanner_hook.CloudSpannerHook._get_client')
def test_update_instance_overridden_project_id(self, get_client):
instance_method = get_client.return_value.instance
instance_exists_method = instance_method.return_value.exists
instance_exists_method.return_value = True
update_method = instance_method.return_value.update
update_method.return_value = False
res = self.spanner_hook_no_default_project_id.update_instance(
project_id=GCP_PROJECT_ID_HOOK_UNIT_TEST,
instance_id=SPANNER_INSTANCE,
configuration_name=SPANNER_CONFIGURATION,
node_count=2,
display_name=SPANNER_DATABASE)
get_client.assert_called_once_with(project_id='example-project')
instance_method.assert_called_once_with(
instance_id='instance', configuration_name='configuration', display_name='database-name',
node_count=2)
update_method.assert_called_with()
self.assertIsNone(res)
@mock.patch('airflow.contrib.hooks.gcp_spanner_hook.CloudSpannerHook._get_client')
def test_delete_instance_missing_project_id(self, get_client):
instance_method = get_client.return_value.instance
instance_exists_method = instance_method.return_value.exists
instance_exists_method.return_value = True
delete_method = instance_method.return_value.delete
delete_method.return_value = False
with self.assertRaises(AirflowException) as cm:
self.spanner_hook_no_default_project_id.delete_instance(
instance_id=SPANNER_INSTANCE)
get_client.assert_not_called()
instance_method.assert_not_called()
delete_method.assert_not_called()
err = cm.exception
self.assertIn("The project id must be passed", str(err))
@mock.patch('airflow.contrib.hooks.gcp_spanner_hook.CloudSpannerHook._get_client')
def test_delete_instance_overridden_project_id(self, get_client):
instance_method = get_client.return_value.instance
instance_exists_method = instance_method.return_value.exists
instance_exists_method.return_value = True
delete_method = instance_method.return_value.delete
delete_method.return_value = False
res = self.spanner_hook_no_default_project_id.delete_instance(
project_id=GCP_PROJECT_ID_HOOK_UNIT_TEST,
instance_id=SPANNER_INSTANCE)
get_client.assert_called_once_with(project_id='example-project')
instance_method.assert_called_once_with(
'instance')
delete_method.assert_called_with()
self.assertIsNone(res)
@mock.patch('airflow.contrib.hooks.gcp_spanner_hook.CloudSpannerHook._get_client')
def test_get_database_missing_project_id(self, get_client):