| 1 |
# Copyright 2017 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 |
import uuid |
| 20 |
|
| 21 |
from openstack.network.v2 import vpn_endpoint_group as vpn_epg |
| 22 |
from openstack.network.v2 import vpn_ike_policy as vpn_ikep |
| 23 |
from openstack.network.v2 import vpn_ipsec_policy as vpn_ipsecp |
| 24 |
from openstack.network.v2 import vpn_ipsec_site_connection as vpn_sitec |
| 25 |
from openstack.network.v2 import vpn_service |
| 26 |
|
| 27 |
|
| 28 |
class FakeVPNaaS(object): |
| 29 |
|
| 30 |
def create(self, attrs={}): |
| 31 |
"""Create a fake vpnaas resources |
| 32 |
|
| 33 |
:param Dictionary attrs: |
| 34 |
A dictionary with all attributes |
| 35 |
:return: |
| 36 |
A OrderedDict faking the vpnaas resource |
| 37 |
""" |
| 38 |
self.ordered.update(attrs) |
| 39 |
if 'IKEPolicy' == self.__class__.__name__: |
| 40 |
return vpn_ikep.VpnIkePolicy(**self.ordered) |
| 41 |
if 'IPSecPolicy' == self.__class__.__name__: |
| 42 |
return vpn_ipsecp.VpnIpsecPolicy(**self.ordered) |
| 43 |
if 'VPNService' == self.__class__.__name__: |
| 44 |
return vpn_service.VpnService(**self.ordered) |
| 45 |
if 'EndpointGroup' == self.__class__.__name__: |
| 46 |
return vpn_epg.VpnEndpointGroup(**self.ordered) |
| 47 |
|
| 48 |
def bulk_create(self, attrs=None, count=2): |
| 49 |
"""Create multiple fake vpnaas resources |
| 50 |
|
| 51 |
:param Dictionary attrs: |
| 52 |
A dictionary with all attributes |
| 53 |
:param int count: |
| 54 |
The number of vpnaas resources to fake |
| 55 |
:return: |
| 56 |
A list of dictionaries faking the vpnaas resources |
| 57 |
""" |
| 58 |
return [self.create(attrs=attrs) for i in range(0, count)] |
| 59 |
|
| 60 |
def get(self, attrs=None, count=2): |
| 61 |
"""Get multiple fake vpnaas resources |
| 62 |
|
| 63 |
:param Dictionary attrs: |
| 64 |
A dictionary with all attributes |
| 65 |
:param int count: |
| 66 |
The number of vpnaas resources to fake |
| 67 |
:return: |
| 68 |
A list of dictionaries faking the vpnaas resource |
| 69 |
""" |
| 70 |
if attrs is None: |
| 71 |
self.attrs = self.bulk_create(count=count) |
| 72 |
return mock.Mock(side_effect=attrs) |
| 73 |
|
| 74 |
|
| 75 |
class IKEPolicy(FakeVPNaaS): |
| 76 |
"""Fake one or more IKE policies""" |
| 77 |
|
| 78 |
def __init__(self): |
| 79 |
super(IKEPolicy, self).__init__() |
| 80 |
self.ordered = collections.OrderedDict(( |
| 81 |
('id', 'ikepolicy-id-' + uuid.uuid4().hex), |
| 82 |
('name', 'my-ikepolicy-' + uuid.uuid4().hex), |
| 83 |
('auth_algorithm', 'sha1'), |
| 84 |
('encryption_algorithm', 'aes-128'), |
| 85 |
('ike_version', 'v1'), |
| 86 |
('pfs', 'group5'), |
| 87 |
('description', 'my-desc-' + uuid.uuid4().hex), |
| 88 |
('phase1_negotiation_mode', 'main'), |
| 89 |
('project_id', 'project-id-' + uuid.uuid4().hex), |
| 90 |
('lifetime', {'units': 'seconds', 'value': 3600}), |
| 91 |
)) |
| 92 |
|
| 93 |
|
| 94 |
class IPSecPolicy(FakeVPNaaS): |
| 95 |
"""Fake one or more IPsec policies""" |
| 96 |
|
| 97 |
def __init__(self): |
| 98 |
super(IPSecPolicy, self).__init__() |
| 99 |
self.ordered = collections.OrderedDict(( |
| 100 |
('id', 'ikepolicy-id-' + uuid.uuid4().hex), |
| 101 |
('name', 'my-ikepolicy-' + uuid.uuid4().hex), |
| 102 |
('auth_algorithm', 'sha1'), |
| 103 |
('encapsulation_mode', 'tunnel'), |
| 104 |
('transform_protocol', 'esp'), |
| 105 |
('encryption_algorithm', 'aes-128'), |
| 106 |
('pfs', 'group5'), |
| 107 |
('description', 'my-desc-' + uuid.uuid4().hex), |
| 108 |
('project_id', 'project-id-' + uuid.uuid4().hex), |
| 109 |
('lifetime', {'units': 'seconds', 'value': 3600}), |
| 110 |
)) |
| 111 |
|
| 112 |
|
| 113 |
class VPNService(FakeVPNaaS): |
| 114 |
"""Fake one or more VPN services""" |
| 115 |
|
| 116 |
def __init__(self): |
| 117 |
super(VPNService, self).__init__() |
| 118 |
self.ordered = collections.OrderedDict(( |
| 119 |
('id', 'vpnservice-id-' + uuid.uuid4().hex), |
| 120 |
('name', 'my-vpnservice-' + uuid.uuid4().hex), |
| 121 |
('router_id', 'router-id-' + uuid.uuid4().hex), |
| 122 |
('subnet_id', 'subnet-id-' + uuid.uuid4().hex), |
| 123 |
('flavor_id', 'flavor-id-' + uuid.uuid4().hex), |
| 124 |
('admin_state_up', True), |
| 125 |
('status', 'ACTIVE'), |
| 126 |
('description', 'my-desc-' + uuid.uuid4().hex), |
| 127 |
('project_id', 'project-id-' + uuid.uuid4().hex), |
| 128 |
('external_v4_ip', '192.0.2.42'), |
| 129 |
('external_v6_ip', '2001:0db8:207a:4a3a:053b:6fab:7df9:1afd'), |
| 130 |
)) |
| 131 |
|
| 132 |
|
| 133 |
class EndpointGroup(FakeVPNaaS): |
| 134 |
"""Fake one or more Endpoint Groups""" |
| 135 |
|
| 136 |
def __init__(self): |
| 137 |
super(EndpointGroup, self).__init__() |
| 138 |
self.ordered = collections.OrderedDict(( |
| 139 |
('id', 'ep-group-id-' + uuid.uuid4().hex), |
| 140 |
('name', 'my-ep-group-' + uuid.uuid4().hex), |
| 141 |
('type', 'cidr'), |
| 142 |
('endpoints', ['10.0.0.0/24', '20.0.0.0/24']), |
| 143 |
('description', 'my-desc-' + uuid.uuid4().hex), |
| 144 |
('project_id', 'project-id-' + uuid.uuid4().hex), |
| 145 |
)) |
| 146 |
|
| 147 |
|
| 148 |
class IPsecSiteConnection(object): |
| 149 |
"""Fake one or more IPsec site connections""" |
| 150 |
@staticmethod |
| 151 |
def create_conn(attrs=None): |
| 152 |
"""Create a fake IPsec conn. |
| 153 |
|
| 154 |
:param Dictionary attrs: |
| 155 |
A dictionary with all attributes |
| 156 |
:return: |
| 157 |
A Dictionary with id, name, peer_address, auth_mode, status, |
| 158 |
project_id, peer_cidrs, vpnservice_id, ipsecpolicy_id, |
| 159 |
ikepolicy_id, mtu, initiator, admin_state_up, description, |
| 160 |
psk, route_mode, local_id, peer_id, local_ep_group_id, |
| 161 |
peer_ep_group_id |
| 162 |
""" |
| 163 |
attrs = attrs or {} |
| 164 |
|
| 165 |
# Set default attributes. |
| 166 |
conn_attrs = { |
| 167 |
'id': 'ipsec-site-conn-id-' + uuid.uuid4().hex, |
| 168 |
'name': 'my-ipsec-site-conn-' + uuid.uuid4().hex, |
| 169 |
'peer_address': '192.168.2.10', |
| 170 |
'auth_mode': '', |
| 171 |
'status': '', |
| 172 |
'project_id': 'project-id-' + uuid.uuid4().hex, |
| 173 |
'peer_cidrs': [], |
| 174 |
'vpnservice_id': 'vpnservice-id-' + uuid.uuid4().hex, |
| 175 |
'ipsecpolicy_id': 'ipsecpolicy-id-' + uuid.uuid4().hex, |
| 176 |
'ikepolicy_id': 'ikepolicy-id-' + uuid.uuid4().hex, |
| 177 |
'mtu': 1500, |
| 178 |
'initiator': 'bi-directional', |
| 179 |
'admin_state_up': True, |
| 180 |
'description': 'my-vpn-connection', |
| 181 |
'psk': 'abcd', |
| 182 |
'route_mode': '', |
| 183 |
'local_id': '', |
| 184 |
'peer_id': '192.168.2.10', |
| 185 |
'local_ep_group_id': 'local-ep-group-id-' + uuid.uuid4().hex, |
| 186 |
'peer_ep_group_id': 'peer-ep-group-id-' + uuid.uuid4().hex, |
| 187 |
} |
| 188 |
|
| 189 |
# Overwrite default attributes. |
| 190 |
conn_attrs.update(attrs) |
| 191 |
return vpn_sitec.VpnIPSecSiteConnection(**conn_attrs) |