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 = GceInstanceTemplateCopyOperator(
         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')
     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)
 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 = GceInstanceTemplateCopyOperator(
         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')
     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)
 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 = GceInstanceTemplateCopyOperator(
         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)
 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 = GceInstanceTemplateCopyOperator(
         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)
Exemplo n.º 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 = GceInstanceTemplateCopyOperator(
            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)
    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 = GceInstanceTemplateCopyOperator(
            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)
Exemplo n.º 7
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 = GceInstanceTemplateCopyOperator(
         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)
 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 = GceInstanceTemplateCopyOperator(
         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)
 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 = GceInstanceTemplateCopyOperator(
         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')
     mock_hook.return_value.insert_instance_template.assert_not_called()
     self.assertEqual(GCE_INSTANCE_TEMPLATE_BODY_GET_NEW, result)
 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 = GceInstanceTemplateCopyOperator(
         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')
     mock_hook.return_value.insert_instance_template.assert_not_called()
     self.assertEqual(GCE_INSTANCE_TEMPLATE_BODY_GET_NEW, result)
 def test_successful_copy_template_missing_project_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 = GceInstanceTemplateCopyOperator(
         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')
     mock_hook.return_value.insert_instance_template.assert_called_once_with(
         project_id=None,
         body=GCE_INSTANCE_TEMPLATE_BODY_INSERT,
         request_id=None)
     self.assertEqual(GCE_INSTANCE_TEMPLATE_BODY_GET_NEW, result)
 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 = GceInstanceTemplateCopyOperator(
             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()
 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 = GceInstanceTemplateCopyOperator(
             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()
 def test_successful_copy_template_missing_project_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 = GceInstanceTemplateCopyOperator(
         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')
     mock_hook.return_value.insert_instance_template.assert_called_once_with(
         project_id=None,
         body=GCE_INSTANCE_TEMPLATE_BODY_INSERT,
         request_id=None
     )
     self.assertEqual(GCE_INSTANCE_TEMPLATE_BODY_GET_NEW, result)
Exemplo n.º 15
0
    "minimalAction": "RESTART",
    "maxSurge": {
        "fixed": 1
    },
    "minReadySec": 1800
}

# [END howto_operator_compute_igm_update_template_args]

with models.DAG('example_gcp_compute_igm',
                default_args=default_args,
                schedule_interval=datetime.timedelta(days=1)) as dag:
    # [START howto_operator_gce_igm_copy_template]
    gce_instance_template_copy = GceInstanceTemplateCopyOperator(
        project_id=PROJECT_ID,
        resource_id=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
    gce_instance_template_copy2 = GceInstanceTemplateCopyOperator(
        project_id=PROJECT_ID,
        resource_id=TEMPLATE_NAME,
        body_patch=GCE_INSTANCE_TEMPLATE_BODY_UPDATE,
        task_id='gcp_compute_igm_copy_template_task_2')
    # [START howto_operator_gce_igm_update_template]
    gce_instance_group_manager_update_template = \
        GceInstanceGroupManagerUpdateTemplateOperator(
            project_id=PROJECT_ID,
            resource_id=INSTANCE_GROUP_MANAGER_NAME,
            zone=ZONE,