示例#1
0
 def test_successful_copy_template_with_updated_nested_fields(self, mock_hook):
     mock_hook.return_value.get_instance_template.side_effect = [
         HttpError(resp=httplib2.Response({'status': 404}), content=EMPTY_CONTENT),
         GCE_INSTANCE_TEMPLATE_BODY_GET,
         GCE_INSTANCE_TEMPLATE_BODY_GET_NEW,
     ]
     op = ComputeEngineCopyInstanceTemplateOperator(
         project_id=GCP_PROJECT_ID,
         resource_id=GCE_INSTANCE_TEMPLATE_NAME,
         task_id='id',
         body_patch={
             "name": GCE_INSTANCE_TEMPLATE_NEW_NAME,
             "properties": {"machineType": "n1-standard-2",},
         },
     )
     result = op.execute(None)
     mock_hook.assert_called_once_with(
         api_version='v1', gcp_conn_id='google_cloud_default', impersonation_chain=None,
     )
     body_insert = deepcopy(GCE_INSTANCE_TEMPLATE_BODY_INSERT)
     body_insert["properties"]["machineType"] = "n1-standard-2"
     mock_hook.return_value.insert_instance_template.assert_called_once_with(
         project_id=GCP_PROJECT_ID, body=body_insert, request_id=None
     )
     self.assertEqual(GCE_INSTANCE_TEMPLATE_BODY_GET_NEW, result)
示例#2
0
 def test_copy_with_some_validation_warnings(self, mock_hook):
     mock_hook.return_value.get_instance_template.side_effect = [
         HttpError(resp=httplib2.Response({'status': 404}), content=EMPTY_CONTENT),
         GCE_INSTANCE_TEMPLATE_BODY_GET,
         GCE_INSTANCE_TEMPLATE_BODY_GET_NEW
     ]
     op = ComputeEngineCopyInstanceTemplateOperator(
         project_id=GCP_PROJECT_ID,
         resource_id=GCE_INSTANCE_TEMPLATE_NAME,
         task_id='id',
         body_patch={"name": GCE_INSTANCE_TEMPLATE_NEW_NAME,
                     "some_wrong_field": "test",
                     "properties": {
                         "some_other_wrong_field": "test"
                     }}
     )
     result = op.execute(None)
     mock_hook.assert_called_once_with(api_version='v1',
                                       gcp_conn_id='google_cloud_default')
     body_insert = deepcopy(GCE_INSTANCE_TEMPLATE_BODY_INSERT)
     body_insert["some_wrong_field"] = "test"
     body_insert["properties"]["some_other_wrong_field"] = "test"
     mock_hook.return_value.insert_instance_template.assert_called_once_with(
         project_id=GCP_PROJECT_ID,
         body=body_insert,
         request_id=None,
     )
     self.assertEqual(GCE_INSTANCE_TEMPLATE_BODY_GET_NEW, result)
示例#3
0
    def test_successful_copy_template_with_description_fields(self, mock_hook):
        mock_hook.return_value.get_instance_template.side_effect = [
            HttpError(resp=httplib2.Response({'status': 404}), content=EMPTY_CONTENT),
            GCE_INSTANCE_TEMPLATE_BODY_GET,
            GCE_INSTANCE_TEMPLATE_BODY_GET_NEW
        ]
        op = ComputeEngineCopyInstanceTemplateOperator(
            project_id=GCP_PROJECT_ID,
            resource_id=GCE_INSTANCE_TEMPLATE_NAME,
            request_id=GCE_INSTANCE_TEMPLATE_REQUEST_ID,
            task_id='id',
            body_patch={"name": GCE_INSTANCE_TEMPLATE_NEW_NAME,
                        "description": "New description"}
        )
        result = op.execute(None)
        mock_hook.assert_called_once_with(api_version='v1',
                                          gcp_conn_id='google_cloud_default')

        body_insert = deepcopy(GCE_INSTANCE_TEMPLATE_BODY_INSERT)
        body_insert["description"] = "New description"
        mock_hook.return_value.insert_instance_template.assert_called_once_with(
            project_id=GCP_PROJECT_ID,
            body=body_insert,
            request_id=GCE_INSTANCE_TEMPLATE_REQUEST_ID,
        )
        self.assertEqual(GCE_INSTANCE_TEMPLATE_BODY_GET_NEW, result)
示例#4
0
 def test_successful_copy_template_with_request_id(self, mock_hook):
     mock_hook.return_value.get_instance_template.side_effect = [
         HttpError(resp=httplib2.Response({'status': 404}),
                   content=EMPTY_CONTENT),
         GCE_INSTANCE_TEMPLATE_BODY_GET,
         GCE_INSTANCE_TEMPLATE_BODY_GET_NEW,
     ]
     op = ComputeEngineCopyInstanceTemplateOperator(
         project_id=GCP_PROJECT_ID,
         resource_id=GCE_INSTANCE_TEMPLATE_NAME,
         request_id=GCE_INSTANCE_TEMPLATE_REQUEST_ID,
         task_id='id',
         body_patch={"name": GCE_INSTANCE_TEMPLATE_NEW_NAME},
     )
     result = op.execute(None)
     mock_hook.assert_called_once_with(
         api_version='v1',
         gcp_conn_id='google_cloud_default',
         impersonation_chain=None,
     )
     mock_hook.return_value.insert_instance_template.assert_called_once_with(
         project_id=GCP_PROJECT_ID,
         body=GCE_INSTANCE_TEMPLATE_BODY_INSERT,
         request_id=GCE_INSTANCE_TEMPLATE_REQUEST_ID,
     )
     assert GCE_INSTANCE_TEMPLATE_BODY_GET_NEW == result
示例#5
0
    def test_successful_copy_template_with_bigger_array_fields(self, mock_hook):
        mock_hook.return_value.get_instance_template.side_effect = [
            HttpError(resp=httplib2.Response({'status': 404}), content=EMPTY_CONTENT),
            GCE_INSTANCE_TEMPLATE_BODY_GET,
            GCE_INSTANCE_TEMPLATE_BODY_GET_NEW
        ]
        op = ComputeEngineCopyInstanceTemplateOperator(
            project_id=GCP_PROJECT_ID,
            resource_id=GCE_INSTANCE_TEMPLATE_NAME,
            task_id='id',
            body_patch={
                "name": GCE_INSTANCE_TEMPLATE_NEW_NAME,
                "properties": {
                    "disks": [
                        {
                            "kind": "compute#attachedDisk",
                            "type": "SCRATCH",
                            "licenses": [
                                "Updated String",
                            ]
                        },
                        {
                            "kind": "compute#attachedDisk",
                            "type": "PERSISTENT",
                            "licenses": [
                                "Another String",
                            ]
                        }
                    ],
                }
            }
        )
        result = op.execute(None)
        mock_hook.assert_called_once_with(api_version='v1',
                                          gcp_conn_id='google_cloud_default')

        body_insert = deepcopy(GCE_INSTANCE_TEMPLATE_BODY_INSERT)
        body_insert["properties"]["disks"] = [
            {
                "kind": "compute#attachedDisk",
                "type": "SCRATCH",
                "licenses": [
                    "Updated String",
                ]
            },
            {
                "kind": "compute#attachedDisk",
                "type": "PERSISTENT",
                "licenses": [
                    "Another String",
                ]
            }
        ]
        mock_hook.return_value.insert_instance_template.assert_called_once_with(
            project_id=GCP_PROJECT_ID,
            body=body_insert,
            request_id=None,
        )
        self.assertEqual(GCE_INSTANCE_TEMPLATE_BODY_GET_NEW, result)
示例#6
0
 def test_successful_copy_template_with_smaller_array_fields(self, mock_hook):
     mock_hook.return_value.get_instance_template.side_effect = [
         HttpError(resp=httplib2.Response({'status': 404}), content=EMPTY_CONTENT),
         GCE_INSTANCE_TEMPLATE_BODY_GET,
         GCE_INSTANCE_TEMPLATE_BODY_GET_NEW
     ]
     op = ComputeEngineCopyInstanceTemplateOperator(
         project_id=GCP_PROJECT_ID,
         resource_id=GCE_INSTANCE_TEMPLATE_NAME,
         task_id='id',
         body_patch={
             "name": GCE_INSTANCE_TEMPLATE_NEW_NAME,
             "properties": {
                 "machineType": "n1-standard-1",
                 "networkInterfaces": [
                     {
                         "network": "https://www.googleapis.com/compute/v1/"
                                    "projects/project/global/networks/default",
                         "accessConfigs": [
                             {
                                 "type": "ONE_TO_ONE_NAT",
                                 "natIP": "8.8.8.8"
                             }
                         ]
                     }
                 ]
             }
         }
     )
     result = op.execute(None)
     mock_hook.assert_called_once_with(api_version='v1',
                                       gcp_conn_id='google_cloud_default')
     body_insert = deepcopy(GCE_INSTANCE_TEMPLATE_BODY_INSERT)
     body_insert["properties"]["networkInterfaces"] = [
         {
             "network": "https://www.googleapis.com/compute/v1/"
                        "projects/project/global/networks/default",
             "accessConfigs": [
                 {
                     "type": "ONE_TO_ONE_NAT",
                     "natIP": "8.8.8.8"
                 }
             ]
         }
     ]
     mock_hook.return_value.insert_instance_template.assert_called_once_with(
         project_id=GCP_PROJECT_ID,
         body=body_insert,
         request_id=None
     )
     self.assertEqual(GCE_INSTANCE_TEMPLATE_BODY_GET_NEW, result)
示例#7
0
 def test_idempotent_copy_template_when_already_copied(self, mock_hook):
     mock_hook.return_value.get_instance_template.side_effect = [GCE_INSTANCE_TEMPLATE_BODY_GET_NEW]
     op = ComputeEngineCopyInstanceTemplateOperator(
         project_id=GCP_PROJECT_ID,
         resource_id=GCE_INSTANCE_TEMPLATE_NAME,
         task_id='id',
         body_patch={"name": GCE_INSTANCE_TEMPLATE_NEW_NAME},
     )
     result = op.execute(None)
     mock_hook.assert_called_once_with(
         api_version='v1', gcp_conn_id='google_cloud_default', impersonation_chain=None,
     )
     mock_hook.return_value.insert_instance_template.assert_not_called()
     self.assertEqual(GCE_INSTANCE_TEMPLATE_BODY_GET_NEW, result)
示例#8
0
 def test_missing_name(self, mock_hook):
     mock_hook.return_value.get_instance_template.side_effect = [
         HttpError(resp=httplib2.Response({'status': 404}), content=EMPTY_CONTENT),
         GCE_INSTANCE_TEMPLATE_BODY_GET,
         GCE_INSTANCE_TEMPLATE_BODY_GET_NEW,
     ]
     with self.assertRaises(AirflowException) as cm:
         op = ComputeEngineCopyInstanceTemplateOperator(
             project_id=GCP_PROJECT_ID,
             resource_id=GCE_INSTANCE_TEMPLATE_NAME,
             request_id=GCE_INSTANCE_TEMPLATE_REQUEST_ID,
             task_id='id',
             body_patch={"description": "New description"},
         )
         op.execute(None)
     err = cm.exception
     self.assertIn("should contain at least name for the new operator " "in the 'name' field", str(err))
     mock_hook.assert_not_called()
示例#9
0
    "minReadySec": 1800,
}

# [END howto_operator_compute_igm_update_template_args]


with models.DAG(
    'example_gcp_compute_igm',
    schedule_interval=None,  # Override to match your needs
    start_date=days_ago(1),
    tags=['example'],
) as dag:
    # [START howto_operator_gce_igm_copy_template]
    gce_instance_template_copy = ComputeEngineCopyInstanceTemplateOperator(
        project_id=GCP_PROJECT_ID,
        resource_id=GCE_TEMPLATE_NAME,
        body_patch=GCE_INSTANCE_TEMPLATE_BODY_UPDATE,
        task_id='gcp_compute_igm_copy_template_task',
    )
    # [END howto_operator_gce_igm_copy_template]
    # Added to check for idempotence
    # [START howto_operator_gce_igm_copy_template_no_project_id]
    gce_instance_template_copy2 = ComputeEngineCopyInstanceTemplateOperator(
        resource_id=GCE_TEMPLATE_NAME,
        body_patch=GCE_INSTANCE_TEMPLATE_BODY_UPDATE,
        task_id='gcp_compute_igm_copy_template_task_2',
    )
    # [END howto_operator_gce_igm_copy_template_no_project_id]
    # [START howto_operator_gce_igm_update_template]
    gce_instance_group_manager_update_template = ComputeEngineInstanceGroupUpdateManagerTemplateOperator(
        project_id=GCP_PROJECT_ID,
        resource_id=GCE_INSTANCE_GROUP_MANAGER_NAME,