| 1 |
# Copyright 2016 FUJITSU LIMITED |
| 2 |
# All Rights Reserved |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 |
# not use this file except in compliance with the License. You may obtain |
| 6 |
# a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
# License for the specific language governing permissions and limitations |
| 14 |
# under the License. |
| 15 |
# |
| 16 |
|
| 17 |
import collections |
| 18 |
from unittest import mock |
| 19 |
|
| 20 |
from openstack.network.v2 import firewall_group as fw_group |
| 21 |
from openstack.network.v2 import firewall_policy as fw_policy |
| 22 |
from openstack.network.v2 import firewall_rule as fw_rule |
| 23 |
from oslo_utils import uuidutils |
| 24 |
|
| 25 |
|
| 26 |
class FakeFWaaS(object): |
| 27 |
|
| 28 |
def create(self, attrs={}): |
| 29 |
"""Create a fake fwaas resources |
| 30 |
|
| 31 |
:param Dictionary attrs: |
| 32 |
A dictionary with all attributes |
| 33 |
:return: |
| 34 |
A OrderedDict faking the fwaas resource |
| 35 |
""" |
| 36 |
self.ordered.update(attrs) |
| 37 |
if 'FirewallGroup' == self.__class__.__name__: |
| 38 |
return fw_group.FirewallGroup(**self.ordered) |
| 39 |
if 'FirewallPolicy' == self.__class__.__name__: |
| 40 |
return fw_policy.FirewallPolicy(**self.ordered) |
| 41 |
if 'FirewallRule' == self.__class__.__name__: |
| 42 |
fw_r = fw_rule.FirewallRule(**self.ordered) |
| 43 |
protocol = fw_r['protocol'].upper() if fw_r['protocol'] else 'ANY' |
| 44 |
src_ip = str(fw_r['source_ip_address']).lower() |
| 45 |
src_port = '(' + str(fw_r['source_port']).lower() + ')' |
| 46 |
dst_ip = str(fw_r['destination_ip_address']).lower() |
| 47 |
dst_port = '(' + str(fw_r['destination_port']).lower() + ')' |
| 48 |
src = 'source(port): ' + src_ip + src_port |
| 49 |
dst = 'dest(port): ' + dst_ip + dst_port |
| 50 |
action = fw_r['action'] if fw_r.get('action') else 'no-action' |
| 51 |
fw_r['summary'] = ',\n '.join([protocol, src, dst, action]) |
| 52 |
return fw_r |
| 53 |
|
| 54 |
def bulk_create(self, attrs=None, count=2): |
| 55 |
"""Create multiple fake fwaas resources |
| 56 |
|
| 57 |
:param Dictionary attrs: |
| 58 |
A dictionary with all attributes |
| 59 |
:param int count: |
| 60 |
The number of fwaas resources to fake |
| 61 |
:return: |
| 62 |
A list of dictionaries faking the fwaas resources |
| 63 |
""" |
| 64 |
return [self.create(attrs=attrs) for i in range(0, count)] |
| 65 |
|
| 66 |
def get(self, attrs=None, count=2): |
| 67 |
"""Create multiple fake fwaas resources |
| 68 |
|
| 69 |
:param Dictionary attrs: |
| 70 |
A dictionary with all attributes |
| 71 |
:param int count: |
| 72 |
The number of fwaas resources to fake |
| 73 |
:return: |
| 74 |
A list of dictionaries faking the fwaas resource |
| 75 |
""" |
| 76 |
if attrs is None: |
| 77 |
self.attrs = self.bulk_create(count=count) |
| 78 |
return mock.Mock(side_effect=attrs) |
| 79 |
|
| 80 |
|
| 81 |
class FirewallGroup(FakeFWaaS): |
| 82 |
"""Fake one or more firewall group""" |
| 83 |
|
| 84 |
def __init__(self): |
| 85 |
super(FirewallGroup, self).__init__() |
| 86 |
self.ordered = collections.OrderedDict(( |
| 87 |
('id', 'firewall-group-id-' + |
| 88 |
uuidutils.generate_uuid(dashed=False)), |
| 89 |
('name', 'my-group-' + |
| 90 |
uuidutils.generate_uuid(dashed=False)), |
| 91 |
('ingress_firewall_policy_id', None), |
| 92 |
('egress_firewall_policy_id', None), |
| 93 |
('description', 'my-desc-' + |
| 94 |
uuidutils.generate_uuid(dashed=False)), |
| 95 |
('status', 'INACTIVE'), |
| 96 |
('ports', []), |
| 97 |
('admin_state_up', True), |
| 98 |
('shared', False), |
| 99 |
('tenant_id', 'tenant-id-' + |
| 100 |
uuidutils.generate_uuid(dashed=False)), |
| 101 |
)) |
| 102 |
|
| 103 |
|
| 104 |
class FirewallPolicy(FakeFWaaS): |
| 105 |
"""Fake one or more firewall policy""" |
| 106 |
|
| 107 |
def __init__(self): |
| 108 |
super(FirewallPolicy, self).__init__() |
| 109 |
self.ordered = collections.OrderedDict(( |
| 110 |
('id', 'firewall-policy-' + |
| 111 |
uuidutils.generate_uuid(dashed=False)), |
| 112 |
('name', 'my-policy-' + |
| 113 |
uuidutils.generate_uuid(dashed=False)), |
| 114 |
('firewall_rules', []), |
| 115 |
('description', 'my-desc-' + |
| 116 |
uuidutils.generate_uuid(dashed=False)), |
| 117 |
('audited', True), |
| 118 |
('shared', False), |
| 119 |
('tenant_id', 'tenant-id-' + |
| 120 |
uuidutils.generate_uuid(dashed=False)), |
| 121 |
)) |
| 122 |
|
| 123 |
|
| 124 |
class FirewallRule(FakeFWaaS): |
| 125 |
"""Fake one or more firewall rule""" |
| 126 |
|
| 127 |
def __init__(self): |
| 128 |
super(FirewallRule, self).__init__() |
| 129 |
self.ordered = collections.OrderedDict(( |
| 130 |
('id', 'firewall-rule-id-' + |
| 131 |
uuidutils.generate_uuid(dashed=False)), |
| 132 |
('name', 'my-rule-' + |
| 133 |
uuidutils.generate_uuid(dashed=False)), |
| 134 |
('enabled', False), |
| 135 |
('description', 'my-desc-' + |
| 136 |
uuidutils.generate_uuid(dashed=False)), |
| 137 |
('ip_version', 4), |
| 138 |
('action', 'deny'), |
| 139 |
('protocol', None), |
| 140 |
('source_ip_address', '192.168.1.0/24'), |
| 141 |
('source_port', '1:11111'), |
| 142 |
('destination_ip_address', '192.168.2.2'), |
| 143 |
('destination_port', '2:22222'), |
| 144 |
('shared', False), |
| 145 |
('tenant_id', 'tenant-id-' + |
| 146 |
uuidutils.generate_uuid(dashed=False)), |
| 147 |
('source_firewall_group_id', 'firewall-group-id-' + |
| 148 |
uuidutils.generate_uuid(dashed=False)), |
| 149 |
('destination_firewall_group_id', 'firewall-group-id-' + |
| 150 |
uuidutils.generate_uuid(dashed=False)), |
| 151 |
)) |