def test_floatingip_operation_create(self):
     """
         check for floating_ip operations/create
     """
     # create to end
     fake_client, fake_ctx = self.generate_client_and_context_floating_ip(
         vcloud_plugin_common.SUBSCRIPTION_SERVICE_TYPE
     )
     fake_ctx._target.node.properties = {
         'floatingip': {
             'edge_gateway': 'gateway'
         }
     }
     fake_ctx._target.instance.runtime_properties = {}
     fake_ctx._source.instance.runtime_properties = {
         'vcloud_vapp_name': 'vapp'
     }
     fake_client._vdc_gateway.get_public_ips = mock.MagicMock(return_value=[
         '10.18.1.1'
     ])
     fake_client._vdc_gateway.get_nat_rules = mock.MagicMock(
         return_value=[]
     )
     with mock.patch(
             'vcloud_plugin_common.ctx', fake_ctx
     ):
         with mock.patch(
             'vcloud_network_plugin.floatingip.ctx', fake_ctx
         ):
             floatingip._floatingip_operation(
                 vcloud_network_plugin.CREATE, fake_client, fake_ctx
             )
     runtime_properties = fake_ctx._target.instance.runtime_properties
     self.assertTrue(
         vcloud_network_plugin.PUBLIC_IP in runtime_properties
     )
     self.assertEqual(
         runtime_properties.get(vcloud_network_plugin.PUBLIC_IP),
         '10.18.1.1'
     )
     # with already explicitly defined ip
     fake_client, fake_ctx = self.generate_client_and_context_floating_ip(
         vcloud_plugin_common.SUBSCRIPTION_SERVICE_TYPE
     )
     fake_ctx._target.node.properties = {
         'floatingip': {
             'edge_gateway': 'gateway',
             vcloud_network_plugin.PUBLIC_IP: '10.10.2.3'
         }
     }
     fake_ctx._target.instance.runtime_properties = {}
     fake_ctx._source.instance.runtime_properties = {
         'vcloud_vapp_name': 'vapp'
     }
     fake_client._vdc_gateway.get_public_ips = mock.MagicMock(return_value=[
         '10.18.1.1', '10.10.2.3'
     ])
     fake_client._vdc_gateway.get_nat_rules = mock.MagicMock(
         return_value=[]
     )
     with mock.patch(
             'vcloud_plugin_common.ctx', fake_ctx
     ):
         with mock.patch(
             'vcloud_network_plugin.floatingip.ctx', fake_ctx
         ):
             floatingip._floatingip_operation(
                 vcloud_network_plugin.CREATE, fake_client, fake_ctx
             )
     runtime_properties = fake_ctx._target.instance.runtime_properties
     self.assertTrue(
         vcloud_network_plugin.PUBLIC_IP in runtime_properties
     )
     self.assertEqual(
         runtime_properties.get(vcloud_network_plugin.PUBLIC_IP),
         '10.10.2.3'
     )
 def test_floatingip_operation_create(self):
     """
         check for floating_ip operations/create
     """
     # create to end
     fake_client, fake_ctx = self.generate_client_and_context_floating_ip(
         vcloud_plugin_common.SUBSCRIPTION_SERVICE_TYPE
     )
     fake_ctx._target.node.properties = {
         'floatingip': {
             'edge_gateway': 'gateway'
         }
     }
     fake_ctx._target.instance.runtime_properties = {}
     fake_ctx._source.instance.runtime_properties = {
         'vcloud_vapp_name': 'vapp'
     }
     fake_client._vdc_gateway.get_public_ips = mock.MagicMock(return_value=[
         '10.18.1.1'
     ])
     fake_client._vdc_gateway.get_nat_rules = mock.MagicMock(
         return_value=[]
     )
     with mock.patch(
             'vcloud_plugin_common.ctx', fake_ctx
     ):
         with mock.patch(
             'vcloud_network_plugin.floatingip.ctx', fake_ctx
         ):
             floatingip._floatingip_operation(
                 vcloud_network_plugin.CREATE, fake_client, fake_ctx
             )
     runtime_properties = fake_ctx._target.instance.runtime_properties
     self.assertTrue(
         vcloud_network_plugin.PUBLIC_IP in runtime_properties
     )
     self.assertEqual(
         runtime_properties.get(vcloud_network_plugin.PUBLIC_IP),
         '10.18.1.1'
     )
     # with already explicitly defined ip
     fake_client, fake_ctx = self.generate_client_and_context_floating_ip(
         vcloud_plugin_common.SUBSCRIPTION_SERVICE_TYPE
     )
     fake_ctx._target.node.properties = {
         'floatingip': {
             'edge_gateway': 'gateway',
             vcloud_network_plugin.PUBLIC_IP: '10.10.2.3'
         }
     }
     fake_ctx._target.instance.runtime_properties = {}
     fake_ctx._source.instance.runtime_properties = {
         'vcloud_vapp_name': 'vapp'
     }
     fake_client._vdc_gateway.get_public_ips = mock.MagicMock(return_value=[
         '10.18.1.1', '10.10.2.3'
     ])
     fake_client._vdc_gateway.get_nat_rules = mock.MagicMock(
         return_value=[]
     )
     with mock.patch(
             'vcloud_plugin_common.ctx', fake_ctx
     ):
         with mock.patch(
             'vcloud_network_plugin.floatingip.ctx', fake_ctx
         ):
             floatingip._floatingip_operation(
                 vcloud_network_plugin.CREATE, fake_client, fake_ctx
             )
     runtime_properties = fake_ctx._target.instance.runtime_properties
     self.assertTrue(
         vcloud_network_plugin.PUBLIC_IP in runtime_properties
     )
     self.assertEqual(
         runtime_properties.get(vcloud_network_plugin.PUBLIC_IP),
         '10.10.2.3'
     )
    def test_floatingip_operation_delete(self):
        """
            check for floating_ip operations/delete
        """
        # no public_ip delete
        fake_client, fake_ctx = self.generate_client_and_context_floating_ip()
        fake_ctx._target.node.properties = {
            'floatingip': {
                'edge_gateway': 'gateway'
            }
        }
        with mock.patch(
            'vcloud_plugin_common.ctx', fake_ctx
        ):
            with mock.patch(
                'vcloud_network_plugin.floatingip.ctx', fake_ctx
            ):
                floatingip._floatingip_operation(
                    vcloud_network_plugin.DELETE, fake_client, fake_ctx
                )
        # busy in save with ip in node_properties
        fake_client, fake_ctx = self.generate_client_and_context_floating_ip()
        self.set_services_conf_result(
            fake_client._vdc_gateway, None
        )
        self.set_gateway_busy(fake_client._vdc_gateway)
        self.prepare_retry(fake_ctx)
        fake_ctx._target.node.properties = {
            'floatingip': {
                'edge_gateway': 'gateway',
                vcloud_network_plugin.PUBLIC_IP: '10.10.1.2'
            }
        }
        with mock.patch(
                'vcloud_plugin_common.ctx', fake_ctx
        ):
            with mock.patch(
                'vcloud_network_plugin.floatingip.ctx', fake_ctx
            ):
                self.assertFalse(floatingip._floatingip_operation(
                    vcloud_network_plugin.DELETE, fake_client, fake_ctx
                ))
        # busy in save with ip in runtime_properties
        fake_client, fake_ctx = self.generate_client_and_context_floating_ip()
        self.set_services_conf_result(
            fake_client._vdc_gateway, None
        )
        self.set_gateway_busy(fake_client._vdc_gateway)
        self.prepare_retry(fake_ctx)
        fake_ctx._target.node.properties = {
            'floatingip': {
                'edge_gateway': 'gateway'
            }
        }
        fake_ctx._target.instance.runtime_properties = {
            vcloud_network_plugin.PUBLIC_IP: '10.10.1.2'
        }
        with mock.patch(
                'vcloud_plugin_common.ctx', fake_ctx
        ):
            with mock.patch(
                'vcloud_network_plugin.floatingip.ctx', fake_ctx
            ):
                self.assertFalse(floatingip._floatingip_operation(
                    vcloud_network_plugin.DELETE, fake_client, fake_ctx
                ))
        # unknow operation
        fake_client, fake_ctx = self.generate_client_and_context_floating_ip()
        fake_ctx._target.node.properties = {
            'floatingip': {
                'edge_gateway': 'gateway',
                vcloud_network_plugin.PUBLIC_IP: '10.10.1.2'
            }
        }
        with mock.patch(
                'vcloud_plugin_common.ctx', fake_ctx
        ):
            with mock.patch(
                'vcloud_network_plugin.floatingip.ctx', fake_ctx
            ):
                with self.assertRaises(cfy_exc.NonRecoverableError):
                    floatingip._floatingip_operation(
                        "unknow", fake_client, fake_ctx
                    )
        # delete to end, ondemand
        fake_client, fake_ctx = self.generate_client_and_context_floating_ip()
        fake_ctx._target.node.properties = {
            'floatingip': {
                'edge_gateway': 'gateway'
            }
        }
        fake_ctx._target.instance.runtime_properties = {
            vcloud_network_plugin.PUBLIC_IP: '10.10.1.2'

        }
        fake_ctx._source.instance.runtime_properties = {
            'vcloud_vapp_name': 'vapp',
            'ssh_port': 22,
            'ssh_public_ip': '1.2.3.1'
        }
        fake_client._vdc_gateway.deallocate_public_ip = mock.MagicMock(
            return_value=self.generate_task(
                vcloud_plugin_common.TASK_STATUS_SUCCESS
            )
        )
        with mock.patch(
                'vcloud_plugin_common.ctx', fake_ctx
        ):
            with mock.patch(
                'vcloud_network_plugin.floatingip.ctx', fake_ctx
            ):
                floatingip._floatingip_operation(
                    vcloud_network_plugin.DELETE, fake_client, fake_ctx
                )
        runtime_properties = fake_ctx._target.instance.runtime_properties
        self.assertFalse(
            vcloud_network_plugin.PUBLIC_IP in runtime_properties
        )
        # delete to end, subscription
        fake_client, fake_ctx = self.generate_client_and_context_floating_ip(
            vcloud_plugin_common.SUBSCRIPTION_SERVICE_TYPE
        )
        fake_ctx._target.node.properties = {
            'floatingip': {
                'edge_gateway': 'gateway'
            }
        }
        fake_ctx._target.instance.runtime_properties = {
            vcloud_network_plugin.PUBLIC_IP: '10.10.1.2'
        }
        fake_ctx._source.instance.runtime_properties = {
            'vcloud_vapp_name': 'vapp',
            'ssh_port': 22,
            'ssh_public_ip': '1.2.3.1'
        }
        fake_client._vdc_gateway.deallocate_public_ip = mock.MagicMock(
            return_value=self.generate_task(
                vcloud_plugin_common.TASK_STATUS_SUCCESS
            )
        )
        with mock.patch(
                'vcloud_plugin_common.ctx', fake_ctx
        ):
            with mock.patch(
                'vcloud_network_plugin.floatingip.ctx', fake_ctx
            ):
                floatingip._floatingip_operation(
                    vcloud_network_plugin.DELETE, fake_client, fake_ctx
                )
        runtime_properties = fake_ctx._target.instance.runtime_properties
        self.assertFalse(
            vcloud_network_plugin.PUBLIC_IP in runtime_properties
        )
    def test_floatingip_operation_delete(self):
        """
            check for floating_ip operations/delete
        """
        # no public_ip delete
        fake_client, fake_ctx = self.generate_client_and_context_floating_ip()
        fake_ctx._target.node.properties = {
            'floatingip': {
                'edge_gateway': 'gateway'
            }
        }
        with mock.patch(
            'vcloud_plugin_common.ctx', fake_ctx
        ):
            with mock.patch(
                'vcloud_network_plugin.floatingip.ctx', fake_ctx
            ):
                floatingip._floatingip_operation(
                    vcloud_network_plugin.DELETE, fake_client, fake_ctx
                )
        # busy in save with ip in node_properties
        fake_client, fake_ctx = self.generate_client_and_context_floating_ip()
        self.set_services_conf_result(
            fake_client._vdc_gateway, None
        )
        self.set_gateway_busy(fake_client._vdc_gateway)
        self.prepare_retry(fake_ctx)
        fake_ctx._target.node.properties = {
            'floatingip': {
                'edge_gateway': 'gateway',
                vcloud_network_plugin.PUBLIC_IP: '10.10.1.2'
            }
        }
        with mock.patch(
                'vcloud_plugin_common.ctx', fake_ctx
        ):
            with mock.patch(
                'vcloud_network_plugin.floatingip.ctx', fake_ctx
            ):
                self.assertFalse(floatingip._floatingip_operation(
                    vcloud_network_plugin.DELETE, fake_client, fake_ctx
                ))
        # busy in save with ip in runtime_properties
        fake_client, fake_ctx = self.generate_client_and_context_floating_ip()
        self.set_services_conf_result(
            fake_client._vdc_gateway, None
        )
        self.set_gateway_busy(fake_client._vdc_gateway)
        self.prepare_retry(fake_ctx)
        fake_ctx._target.node.properties = {
            'floatingip': {
                'edge_gateway': 'gateway'
            }
        }
        fake_ctx._target.instance.runtime_properties = {
            vcloud_network_plugin.PUBLIC_IP: '10.10.1.2'
        }
        with mock.patch(
                'vcloud_plugin_common.ctx', fake_ctx
        ):
            with mock.patch(
                'vcloud_network_plugin.floatingip.ctx', fake_ctx
            ):
                self.assertFalse(floatingip._floatingip_operation(
                    vcloud_network_plugin.DELETE, fake_client, fake_ctx
                ))
        # unknow operation
        fake_client, fake_ctx = self.generate_client_and_context_floating_ip()
        fake_ctx._target.node.properties = {
            'floatingip': {
                'edge_gateway': 'gateway',
                vcloud_network_plugin.PUBLIC_IP: '10.10.1.2'
            }
        }
        with mock.patch(
                'vcloud_plugin_common.ctx', fake_ctx
        ):
            with mock.patch(
                'vcloud_network_plugin.floatingip.ctx', fake_ctx
            ):
                with self.assertRaises(cfy_exc.NonRecoverableError):
                    floatingip._floatingip_operation(
                        "unknow", fake_client, fake_ctx
                    )
        # delete to end, ondemand
        fake_client, fake_ctx = self.generate_client_and_context_floating_ip()
        fake_ctx._target.node.properties = {
            'floatingip': {
                'edge_gateway': 'gateway'
            }
        }
        fake_ctx._target.instance.runtime_properties = {
            vcloud_network_plugin.PUBLIC_IP: '10.10.1.2'

        }
        fake_ctx._source.instance.runtime_properties = {
            'vcloud_vapp_name': 'vapp',
            'ssh_port': 22,
            'ssh_public_ip': '1.2.3.1'
        }
        fake_client._vdc_gateway.deallocate_public_ip = mock.MagicMock(
            return_value=self.generate_task(
                vcloud_plugin_common.TASK_STATUS_SUCCESS
            )
        )
        with mock.patch(
                'vcloud_plugin_common.ctx', fake_ctx
        ):
            with mock.patch(
                'vcloud_network_plugin.floatingip.ctx', fake_ctx
            ):
                floatingip._floatingip_operation(
                    vcloud_network_plugin.DELETE, fake_client, fake_ctx
                )
        runtime_properties = fake_ctx._target.instance.runtime_properties
        self.assertFalse(
            vcloud_network_plugin.PUBLIC_IP in runtime_properties
        )
        # delete to end, subscription
        fake_client, fake_ctx = self.generate_client_and_context_floating_ip(
            vcloud_plugin_common.SUBSCRIPTION_SERVICE_TYPE
        )
        fake_ctx._target.node.properties = {
            'floatingip': {
                'edge_gateway': 'gateway'
            }
        }
        fake_ctx._target.instance.runtime_properties = {
            vcloud_network_plugin.PUBLIC_IP: '10.10.1.2'
        }
        fake_ctx._source.instance.runtime_properties = {
            'vcloud_vapp_name': 'vapp',
            'ssh_port': 22,
            'ssh_public_ip': '1.2.3.1'
        }
        fake_client._vdc_gateway.deallocate_public_ip = mock.MagicMock(
            return_value=self.generate_task(
                vcloud_plugin_common.TASK_STATUS_SUCCESS
            )
        )
        with mock.patch(
                'vcloud_plugin_common.ctx', fake_ctx
        ):
            with mock.patch(
                'vcloud_network_plugin.floatingip.ctx', fake_ctx
            ):
                floatingip._floatingip_operation(
                    vcloud_network_plugin.DELETE, fake_client, fake_ctx
                )
        runtime_properties = fake_ctx._target.instance.runtime_properties
        self.assertFalse(
            vcloud_network_plugin.PUBLIC_IP in runtime_properties
        )