From 9329b6283af2ef3bf177d1616ca94f526f2dfd98 Mon Sep 17 00:00:00 2001 From: maplelabs Date: Fri, 30 Jan 2015 00:46:29 +0530 Subject: [PATCH] code to clean lbass, metering and fwaas resources Change-Id: If8b41fc072fc04544cf855d197e4c82700152a13 Added required exception Change-Id: If8b41fc072fc04544cf855d197e4c82700152a13 --- ospurge/ospurge.py | 115 +++++++++++++++ ospurge/tests/client_fixtures.py | 243 +++++++++++++++++++++++++++++++ ospurge/tests/test_ospurge.py | 171 +++++++++++++++++++++- 3 files changed, 527 insertions(+), 2 deletions(-) diff --git a/ospurge/ospurge.py b/ospurge/ospurge.py index ab476f6..9b691e9 100755 --- a/ospurge/ospurge.py +++ b/ospurge/ospurge.py @@ -81,6 +81,14 @@ RESOURCES_CLASSES = ['CinderSnapshots', 'CinderBackups', 'NovaServers', 'NeutronFloatingIps', + 'NeutronFireWall', + 'NeutronFireWallPolicy', + 'NeutronFireWallRule', + 'NeutronLbMembers', + 'NeutronLbVip', + 'NeutronLbHealthMonitor', + 'NeutronLbPool', + 'NeutronMeteringLabel', 'NeutronInterfaces', 'NeutronRouters', 'NeutronPorts', @@ -423,6 +431,110 @@ class NeutronFloatingIps(NeutronResources): floating_ip['floating_ip_address'], floating_ip['id']) +class NeutronLbMembers(NeutronResources): + + def list(self): + return filter(self._owned_resource, self.client.list_members()['members']) + + def delete(self, member): + super(NeutronLbMembers, self).delete(member) + self.client.delete_member(member['id']) + + def resource_str(self, member): + return "lb-member {} (id {})".format(member['address'], member['id']) + + +class NeutronLbPool(NeutronResources): + + def list(self): + return filter(self._owned_resource, self.client.list_pools()['pools']) + + def delete(self, pool): + super(NeutronLbPool, self).delete(pool) + self.client.delete_pool(pool['id']) + + def resource_str(self, pool): + return "lb-pool {} (id {})".format(pool['name'], pool['id']) + + +class NeutronLbVip(NeutronResources): + + def list(self): + return filter(self._owned_resource, self.client.list_vips()['vips']) + + def delete(self, vip): + super(NeutronLbVip, self).delete(vip) + self.client.delete_vip(vip['id']) + + def resource_str(self, vip): + return "lb-vip {} (id {})".format(vip['name'], vip['id']) + + +class NeutronLbHealthMonitor(NeutronResources): + + def list(self): + return filter(self._owned_resource, self.client.list_health_monitors()['health_monitors']) + + def delete(self, health_monitor): + super(NeutronLbHealthMonitor, self).delete(health_monitor) + self.client.delete_health_monitor(health_monitor['id']) + + def resource_str(self, health_monitor): + return "lb-health_monotor type {} (id {})".format(health_monitor['type'], health_monitor['id']) + + +class NeutronMeteringLabel(NeutronResources): + + def list(self): + return filter(self._owned_resource, self.client.list_metering_labels()['metering_labels']) + + def delete(self, metering_label): + super(NeutronMeteringLabel, self).delete(metering_label) + self.client.delete_metering_label(metering_label['id']) + + def resource_str(self, metering_label): + return "meter-label {} (id {})".format(metering_label['name'], metering_label['id']) + + +class NeutronFireWallPolicy(NeutronResources): + + def list(self): + return filter(self._owned_resource, self.client.list_firewall_policies()['firewall_policies']) + + def delete(self, firewall_policy): + super(NeutronFireWallPolicy, self).delete(firewall_policy) + self.client.delete_firewall_policy(firewall_policy['id']) + + def resource_str(self, firewall_policy): + return "Firewall policy {} (id {})".format(firewall_policy['name'], firewall_policy['id']) + + +class NeutronFireWallRule(NeutronResources): + + def list(self): + return filter(self._owned_resource, self.client.list_firewall_rules()['firewall_rules']) + + def delete(self, firewall_rule): + super(NeutronFireWallRule, self).delete(firewall_rule) + self.client.delete_firewall_rule(firewall_rule['id']) + + def resource_str(self, firewall_rule): + return "Firewall rule {} (id {})".format(firewall_rule['name'], firewall_rule['id']) + + +class NeutronFireWall(NeutronResources): + + def list(self): + return filter(self._owned_resource, self.client.list_firewalls()['firewalls']) + + def delete(self, firewall): + super(NeutronFireWall, self).delete(firewall) + self.client.delete_firewall(firewall['id']) + + def resource_str(self, firewall): + return "Firewall {} (id {})".format(firewall['name'], firewall['id']) + + class NovaServers(Resources): def __init__(self, session): @@ -599,6 +711,9 @@ def perform_on_project(admin_name, password, project, auth_url, logging.warning( "Unable to connect to {} endpoint : {}".format(rc, e.message)) error = InvalidEndpoint(rc) + except (neutronclient.common.exceptions.NeutronClientException): + # If service is not configured, ignoring it + pass if error: raise error diff --git a/ospurge/tests/client_fixtures.py b/ospurge/tests/client_fixtures.py index 85f48a7..f9ecc22 100644 --- a/ospurge/tests/client_fixtures.py +++ b/ospurge/tests/client_fixtures.py @@ -56,6 +56,18 @@ IMAGES_IDS = ["37717f53-3707-49b9-9dd0-fd063e6b9fc5", "4e150966-cbe7-4fd7-a964-4 ALARMS_IDS = ["ca950223-e982-4552-9dec-5dc5d3ea4172"] UNBOUND_PORT_ID = "abcdb45e-45fe-4e04-8704-bf6f58760000" +PRIVATE_PORT_IDS = ["p7815f5b-a228-47bb-a5e5-f139c4f476ft", "p78o5f5t-a228-47bb-a5e2-f139c4f476ft"] +FIREWALL_RULE_IDS = ["firebcc3-d831-411d-a073-ddc828a7a9id", + "fi7815f5b-a328-47cb-a5e5-f139c4e476f7"] + +FIREWALL_POLICY_IDS = ["firebcc3-d831-422d-a073-ccc818a7a9id", "poa119a8-d25b-45a7-8d1b-88e127885630"] +FIREWALL_IDS = ["firewal1-d831-422d-a073-ckc818a7a9ab", "firewa1l-d831-422d-a073-ckc818a7a9ab"] +METERING_LABEL_IDS = ["mbcdb45e-45fe-4e04-8704-bf6f58760011", "meteb45e-45fe-4e04-8704-bf6f58760000"] +LBAAS_MEMBER_IDS = ["37717f53-3707-49b9-9dd0-fd063e6lbass", "la650123-e982-4552-9dec-5dc5d3ea4172"] +LBAAS_VIP_IDS = ["616fb98f-36ca-475e-917e-1563e5a8cd10", "102fbcc3-d831-411d-a333-ddc828a7a9ed"] +LBAAS_HEALTHMONITOR_IDS = ["he717f53-3707-49b9-9dd0-fd063e6lbass"] +LBAAS_POOL_IDS = ["lb815f5b-a228-17bb-a5e5-f139c3e476f6", "dlb15f5b-a228-47bb-a5e5-f139c4e47po6"] + # Simulating JSON sent from the Server PROJECT_SCOPED_TOKEN = { @@ -610,6 +622,237 @@ FLOATING_IPS_LIST = { ] } +LBAAS_HEALTHMONITOR_LIST = { + "health_monitors": + [ + { + "admin_state_up": True, + "tenant_id": PROJECT_ID, + "delay": 5, + "expected_codes": "200", + "max_retries": 5, + "http_method": "GET", + "timeout": 2, + "pools": [], + "url_path": "/", + "type": "HTTP", + "id": LBAAS_HEALTHMONITOR_IDS[0] + } + ] +} + +LBAAS_VIP_LIST = { + "vips": + [ + { + "status": "ACTIVE", + "protocol": "HTTP", + "description": "", + "address": "10.0.0.125", + "protocol_port": 80, + "port_id": PRIVATE_PORT_IDS[0], + "id": LBAAS_VIP_IDS[0], + "status_description": "", + "name": "test-http-vip", + "admin_state_up": True, + "tenant_id": PROJECT_ID, + "subnet_id": "b892434a-59f7-4404-a05d-9562977e1678", + "connection_limit": -1, + "pool_id": LBAAS_POOL_IDS[0], + "session_persistence": None + }, + { + "status": "ACTIVE", + "protocol": "HTTP", + "description": "", + "address": "10.0.0.126", + "protocol_port": 80, + "port_id": PRIVATE_PORT_IDS[1], + "id": LBAAS_VIP_IDS[1], + "status_description": "", + "name": "test-http-vip", + "admin_state_up": True, + "tenant_id": PROJECT_ID, + "subnet_id": "b892434a-49f7-4404-a05d-9562977e1678", + "connection_limit": -1, + "pool_id": LBAAS_POOL_IDS[1], + "session_persistence": None + } + ] +} + +LBAAS_POOL_LIST = { + "pools": + [ + { + "status": "ACTIVE", + "lb_method": "ROUND_ROBIN", + "protocol": "HTTP", + "description": "", + "health_monitors": [], + "subnet_id": "b892434a-59f7-4404-a05d-9562977e1678", + "tenant_id": PROJECT_ID, + "admin_state_up": True, + "name": "Test-Pools", + "health_monitors_status": [], + "members": [], + "provider": "haproxy", + "status_description": None, + "id": LBAAS_POOL_IDS[0] + }, + { + "status": "ACTIVE", + "lb_method": "ROUND_ROBIN", + "protocol": "HTTP", + "description": "", + "health_monitors": [], + "subnet_id": "b892434a-49f7-4404-a05d-9562977e1678", + "tenant_id": PROJECT_ID, + "admin_state_up": True, + "name": "Test-Pools", + "health_monitors_status": [], + "members": [], + "provider": "haproxy", + "status_description": None, + "id": LBAAS_POOL_IDS[1] + } + ] +} + +LBAAS_MEMBER_LIST = { + "members": + [ + { + "id": LBAAS_MEMBER_IDS[0], + "address": "10.0.0.122", + "protocol_port": 80, + "tenant_id": PROJECT_ID, + "admin_state_up": True, + "weight": 1, + "status": "ACTIVE", + "status_description": "member test1", + "pool_id": LBAAS_POOL_IDS[0] + }, + { + "id": LBAAS_MEMBER_IDS[1], + "address": "10.0.0.123", + "protocol_port": 80, + "tenant_id": PROJECT_ID, + "admin_state_up": True, + "weight": 1, + "status": "ACTIVE", + "status_description": "member test1", + "pool_id": LBAAS_POOL_IDS[1] + } + ] +} + +FIREWALL_LIST = { + "firewalls": + [ + { + "status": "ACTIVE", + "name": "fwass-test-1", + "admin_state_up": True, + "tenant_id": PROJECT_ID, + "firewall_policy_id": FIREWALL_POLICY_IDS[0], + "id": FIREWALL_IDS[0], + "description": "" + }, + { + "status": "ACTIVE", + "name": "fwass-test-2", + "admin_state_up": True, + "tenant_id": PROJECT_ID, + "firewall_policy_id": FIREWALL_POLICY_IDS[1], + "id": FIREWALL_IDS[1], + "description": "" + } + ] +} + +METERING_LABEL_LIST = { + "metering_labels": + [ + { + "tenant_id": PROJECT_ID, + "description": "Meter label test1", + "name": "Meterlabel1", + "id": METERING_LABEL_IDS[0] + }, + { + "tenant_id": PROJECT_ID, + "description": "Meter label test2", + "name": "Meterlabel2", + "id": METERING_LABEL_IDS[1] + } + ] +} + +FIREWALL_POLICY_LIST = { + "firewall_policies": + [ + { + "name": "TestFireWallPolicy1", + "firewall_rules": [FIREWALL_RULE_IDS[0]], + "tenant_id": PROJECT_ID, + "audited": False, + "shared": False, + "id": FIREWALL_POLICY_IDS[0], + "description": "Testing firewall policy 1" + }, + { + "name": "TestFireWallPolicy2", + "firewall_rules": [FIREWALL_RULE_IDS[1]], + "tenant_id": PROJECT_ID, + "audited": False, + "shared": False, + "id": FIREWALL_POLICY_IDS[1], + "description": "Testing firewall policy 2" + } + ] +} + +FIREWALL_RULE_LIST = { + "firewall_rules": + [ + { + "protocol": "tcp", + "description": "Firewall rule 1", + "source_port": None, + "source_ip_address": None, + "destination_ip_address": None, + "firewall_policy_id": None, + "position": None, + "destination_port": "80", + "id": FIREWALL_RULE_IDS[0], + "name": "", + "tenant_id": PROJECT_ID, + "enabled": True, + "action": "allow", + "ip_version": 4, + "shared": False + }, + { + "protocol": "tcp", + "description": "Firewall rule 1", + "source_port": None, + "source_ip_address": None, + "destination_ip_address": None, + "firewall_policy_id": None, + "position": None, + "destination_port": "80", + "id": FIREWALL_RULE_IDS[1], + "name": "", + "tenant_id": PROJECT_ID, + "enabled": True, + "action": "allow", + "ip_version": 4, + "shared": False + } + ] +} + SERVERS_LIST = { "servers": [ diff --git a/ospurge/tests/test_ospurge.py b/ospurge/tests/test_ospurge.py index eba7dd8..bfceafd 100644 --- a/ospurge/tests/test_ospurge.py +++ b/ospurge/tests/test_ospurge.py @@ -374,8 +374,7 @@ class TestNeutronFloatingIps(TestNeutronBase): def stub_delete(self): ip_id = client_fixtures.FLOATING_IPS_IDS[0] - self.stub_url('DELETE', parts=['v2.0', 'floatingips', - "{}.json".format(ip_id)], json={}) + self.stub_url('DELETE', parts=['v2.0', 'floatingips', "{}.json".format(ip_id)], json={}) def setUp(self): super(TestNeutronFloatingIps, self).setUp() @@ -388,6 +387,174 @@ class TestNeutronFloatingIps(TestNeutronBase): self._test_delete() +class TestNeutronFireWallRule(TestNeutronBase): + IDS = client_fixtures.FIREWALL_RULE_IDS + + def stub_list(self): + self.stub_url('GET', parts=['v2.0', 'fw/firewall_rules.json'], json=client_fixtures.FIREWALL_RULE_LIST) + + def stub_delete(self): + firewall_rule_id = client_fixtures.FIREWALL_RULE_IDS[0] + self.stub_url('DELETE', parts=['v2.0', 'fw/firewall_rules', "{}.json".format(firewall_rule_id)], json={}) + + def setUp(self): + super(TestNeutronFireWallRule, self).setUp() + self.resources = ospurge.NeutronFireWallRule(self.session) + + def test_list(self): + self._test_list() + + def test_delete(self): + self._test_delete() + + +class TestNeutronFireWallPolicy(TestNeutronBase): + IDS = client_fixtures.FIREWALL_POLICY_IDS + + def stub_list(self): + self.stub_url('GET', parts=['v2.0', 'fw/firewall_policies.json'], json=client_fixtures.FIREWALL_POLICY_LIST) + + def stub_delete(self): + firewall_policy_id = client_fixtures.FIREWALL_POLICY_IDS[0] + self.stub_url('DELETE', parts=['v2.0', 'fw/firewall_policies', "{}.json".format(firewall_policy_id)], json={}) + + def setUp(self): + super(TestNeutronFireWallPolicy, self).setUp() + self.resources = ospurge.NeutronFireWallPolicy(self.session) + + def test_list(self): + self._test_list() + + def test_delete(self): + self._test_delete() + + +class TestNeutronFireWall(TestNeutronBase): + IDS = client_fixtures.FIREWALL_IDS + + def stub_list(self): + self.stub_url('GET', parts=['v2.0', 'fw/firewalls.json'], json=client_fixtures.FIREWALL_LIST) + + def stub_delete(self): + firewall_id = client_fixtures.FIREWALL_IDS[0] + self.stub_url('DELETE', parts=['v2.0', 'fw/firewalls', "{}.json".format(firewall_id)], json={}) + + def setUp(self): + super(TestNeutronFireWall, self).setUp() + self.resources = ospurge.NeutronFireWall(self.session) + + def test_list(self): + self._test_list() + + def test_delete(self): + self._test_delete() + + +class TestNeutronMeteringLabel(TestNeutronBase): + IDS = client_fixtures.METERING_LABEL_IDS + + def stub_list(self): + self.stub_url('GET', parts=['v2.0', 'metering/metering-labels.json'], json=client_fixtures.METERING_LABEL_LIST) + + def stub_delete(self): + firewall_id = client_fixtures.METERING_LABEL_IDS[0] + self.stub_url('DELETE', parts=['v2.0', 'metering/metering-labels', "{}.json".format(firewall_id)], json={}) + + def setUp(self): + super(TestNeutronMeteringLabel, self).setUp() + self.resources = ospurge.NeutronMeteringLabel(self.session) + + def test_list(self): + self._test_list() + + def test_delete(self): + self._test_delete() + + +class TestNeutronLbMembers(TestNeutronBase): + IDS = client_fixtures.LBAAS_MEMBER_IDS + + def stub_list(self): + self.stub_url('GET', parts=['v2.0', 'lb/members.json'], json=client_fixtures.LBAAS_MEMBER_LIST) + + def stub_delete(self): + lb_member_id = client_fixtures.LBAAS_MEMBER_IDS[0] + self.stub_url('DELETE', parts=['v2.0', 'lb/members', "{}.json".format(lb_member_id)], json={}) + + def setUp(self): + super(TestNeutronLbMembers, self).setUp() + self.resources = ospurge.NeutronLbMembers(self.session) + + def test_list(self): + self._test_list() + + def test_delete(self): + self._test_delete() + + +class TestNeutronLbVip(TestNeutronBase): + IDS = client_fixtures.LBAAS_VIP_IDS + + def stub_list(self): + self.stub_url('GET', parts=['v2.0', 'lb/vips.json'], json=client_fixtures.LBAAS_VIP_LIST) + + def stub_delete(self): + lb_vip_id = client_fixtures.LBAAS_VIP_IDS[0] + self.stub_url('DELETE', parts=['v2.0', 'lb/vips', "{}.json".format(lb_vip_id)], json={}) + + def setUp(self): + super(TestNeutronLbVip, self).setUp() + self.resources = ospurge.NeutronLbVip(self.session) + + def test_list(self): + self._test_list() + + def test_delete(self): + self._test_delete() + + +class TestNeutronLbHealthMonitor(TestNeutronBase): + IDS = client_fixtures.LBAAS_HEALTHMONITOR_IDS + + def stub_list(self): + self.stub_url('GET', parts=['v2.0', 'lb/health_monitors.json'], json=client_fixtures.LBAAS_HEALTHMONITOR_LIST) + + def stub_delete(self): + lb_healthmonitor_id = client_fixtures.LBAAS_HEALTHMONITOR_IDS[0] + self.stub_url('DELETE', parts=['v2.0', 'lb/health_monitors', "{}.json".format(lb_healthmonitor_id)], json={}) + + def setUp(self): + super(TestNeutronLbHealthMonitor, self).setUp() + self.resources = ospurge.NeutronLbHealthMonitor(self.session) + + def test_list(self): + self._test_list() + + def test_delete(self): + self._test_delete() + + +class TestNeutronLbPool(TestNeutronBase): + IDS = client_fixtures.LBAAS_POOL_IDS + + def stub_list(self): + self.stub_url('GET', parts=['v2.0', 'lb/pools.json'], json=client_fixtures.LBAAS_POOL_LIST) + + def stub_delete(self): + lb_pool_id = client_fixtures.LBAAS_POOL_IDS[0] + self.stub_url('DELETE', parts=['v2.0', 'lb/pools', "{}.json".format(lb_pool_id)], json={}) + + def setUp(self): + super(TestNeutronLbPool, self).setUp() + self.resources = ospurge.NeutronLbPool(self.session) + + def test_list(self): + self._test_list() + + def test_delete(self): + self._test_delete() + + class TestNovaServers(TestResourcesBase): TEST_URL = client_fixtures.COMPUTE_PUBLIC_ENDPOINT IDS = client_fixtures.SERVERS_IDS