|
| 1 | +"""List IPSEC VPN Tunnel Context Details.""" |
| 2 | +# :license: MIT, see LICENSE for more details. |
| 3 | + |
| 4 | +import click |
| 5 | + |
| 6 | +import SoftLayer |
| 7 | +from SoftLayer.CLI import environment |
| 8 | +from SoftLayer.CLI import formatting |
| 9 | + |
| 10 | + |
| 11 | +@click.command() |
| 12 | +@click.argument('context_id', type=int) |
| 13 | +@click.option('-i', |
| 14 | + '--include', |
| 15 | + default=[], |
| 16 | + multiple=True, |
| 17 | + type=click.Choice(['at', 'is', 'rs', 'sr', 'ss']), |
| 18 | + help='Include additional resources') |
| 19 | +@environment.pass_env |
| 20 | +def cli(env, context_id, include): |
| 21 | + """List IPSEC VPN tunnel context details. |
| 22 | +
|
| 23 | + Additional resources can be joined using multiple instances of the |
| 24 | + include option, for which the following choices are available. |
| 25 | +
|
| 26 | + \b |
| 27 | + at: address translations |
| 28 | + is: internal subnets |
| 29 | + rs: remote subnets |
| 30 | + sr: statically routed subnets |
| 31 | + ss: service subnets |
| 32 | + """ |
| 33 | + mask = _get_tunnel_context_mask(('at' in include), |
| 34 | + ('is' in include), |
| 35 | + ('rs' in include), |
| 36 | + ('sr' in include), |
| 37 | + ('ss' in include)) |
| 38 | + manager = SoftLayer.IPSECManager(env.client) |
| 39 | + context = manager.get_tunnel_context(context_id, mask=mask) |
| 40 | + |
| 41 | + env.out('Context Details:') |
| 42 | + env.fout(_get_context_table(context)) |
| 43 | + |
| 44 | + for relation in include: |
| 45 | + if relation == 'at': |
| 46 | + env.out('Address Translations:') |
| 47 | + env.fout(_get_address_translations_table( |
| 48 | + context.get('addressTranslations', []))) |
| 49 | + elif relation == 'is': |
| 50 | + env.out('Internal Subnets:') |
| 51 | + env.fout(_get_subnets_table(context.get('internalSubnets', []))) |
| 52 | + elif relation == 'rs': |
| 53 | + env.out('Remote Subnets:') |
| 54 | + env.fout(_get_subnets_table(context.get('customerSubnets', []))) |
| 55 | + elif relation == 'sr': |
| 56 | + env.out('Static Subnets:') |
| 57 | + env.fout(_get_subnets_table(context.get('staticRouteSubnets', []))) |
| 58 | + elif relation == 'ss': |
| 59 | + env.out('Service Subnets:') |
| 60 | + env.fout(_get_subnets_table(context.get('serviceSubnets', []))) |
| 61 | + |
| 62 | + |
| 63 | +def _get_address_translations_table(address_translations): |
| 64 | + """Yields a formatted table to print address translations. |
| 65 | +
|
| 66 | + :param List[dict] address_translations: List of address translations. |
| 67 | + :return Table: Formatted for address translation output. |
| 68 | + """ |
| 69 | + table = formatting.Table(['id', |
| 70 | + 'static IP address', |
| 71 | + 'static IP address id', |
| 72 | + 'remote IP address', |
| 73 | + 'remote IP address id', |
| 74 | + 'note']) |
| 75 | + for address_translation in address_translations: |
| 76 | + table.add_row([address_translation.get('id', ''), |
| 77 | + address_translation.get('internalIpAddressRecord', {}) |
| 78 | + .get('ipAddress', ''), |
| 79 | + address_translation.get('internalIpAddressId', ''), |
| 80 | + address_translation.get('customerIpAddressRecord', {}) |
| 81 | + .get('ipAddress', ''), |
| 82 | + address_translation.get('customerIpAddressId', ''), |
| 83 | + address_translation.get('notes', '')]) |
| 84 | + return table |
| 85 | + |
| 86 | + |
| 87 | +def _get_subnets_table(subnets): |
| 88 | + """Yields a formatted table to print subnet details. |
| 89 | +
|
| 90 | + :param List[dict] subnets: List of subnets. |
| 91 | + :return Table: Formatted for subnet output. |
| 92 | + """ |
| 93 | + table = formatting.Table(['id', |
| 94 | + 'network identifier', |
| 95 | + 'cidr', |
| 96 | + 'note']) |
| 97 | + for subnet in subnets: |
| 98 | + table.add_row([subnet.get('id', ''), |
| 99 | + subnet.get('networkIdentifier', ''), |
| 100 | + subnet.get('cidr', ''), |
| 101 | + subnet.get('note', '')]) |
| 102 | + return table |
| 103 | + |
| 104 | + |
| 105 | +def _get_tunnel_context_mask(address_translations=False, |
| 106 | + internal_subnets=False, |
| 107 | + remote_subnets=False, |
| 108 | + static_subnets=False, |
| 109 | + service_subnets=False): |
| 110 | + """Yields a mask object for a tunnel context. |
| 111 | +
|
| 112 | + All exposed properties on the tunnel context service are included in |
| 113 | + the constructed mask. Additional joins may be requested. |
| 114 | +
|
| 115 | + :param bool address_translations: Whether to join the context's address |
| 116 | + translation entries. |
| 117 | + :param bool internal_subnets: Whether to join the context's internal |
| 118 | + subnet associations. |
| 119 | + :param bool remote_subnets: Whether to join the context's remote subnet |
| 120 | + associations. |
| 121 | + :param bool static_subnets: Whether to join the context's statically |
| 122 | + routed subnet associations. |
| 123 | + :param bool service_subnets: Whether to join the SoftLayer service |
| 124 | + network subnets. |
| 125 | + :return string: Encoding for the requested mask object. |
| 126 | + """ |
| 127 | + entries = ['id', |
| 128 | + 'accountId', |
| 129 | + 'advancedConfigurationFlag', |
| 130 | + 'createDate', |
| 131 | + 'customerPeerIpAddress', |
| 132 | + 'modifyDate', |
| 133 | + 'name', |
| 134 | + 'friendlyName', |
| 135 | + 'internalPeerIpAddress', |
| 136 | + 'phaseOneAuthentication', |
| 137 | + 'phaseOneDiffieHellmanGroup', |
| 138 | + 'phaseOneEncryption', |
| 139 | + 'phaseOneKeylife', |
| 140 | + 'phaseTwoAuthentication', |
| 141 | + 'phaseTwoDiffieHellmanGroup', |
| 142 | + 'phaseTwoEncryption', |
| 143 | + 'phaseTwoKeylife', |
| 144 | + 'phaseTwoPerfectForwardSecrecy', |
| 145 | + 'presharedKey'] |
| 146 | + if address_translations: |
| 147 | + entries.append('addressTranslations[internalIpAddressRecord[ipAddress],' |
| 148 | + 'customerIpAddressRecord[ipAddress]]') |
| 149 | + if internal_subnets: |
| 150 | + entries.append('internalSubnets') |
| 151 | + if remote_subnets: |
| 152 | + entries.append('customerSubnets') |
| 153 | + if static_subnets: |
| 154 | + entries.append('staticRouteSubnets') |
| 155 | + if service_subnets: |
| 156 | + entries.append('serviceSubnets') |
| 157 | + return '[mask[{}]]'.format(','.join(entries)) |
| 158 | + |
| 159 | + |
| 160 | +def _get_context_table(context): |
| 161 | + """Yields a formatted table to print context details. |
| 162 | +
|
| 163 | + :param dict context: The tunnel context |
| 164 | + :return Table: Formatted for tunnel context output |
| 165 | + """ |
| 166 | + table = formatting.KeyValueTable(['name', 'value']) |
| 167 | + table.align['name'] = 'r' |
| 168 | + table.align['value'] = 'l' |
| 169 | + |
| 170 | + table.add_row(['id', context.get('id', '')]) |
| 171 | + table.add_row(['name', context.get('name', '')]) |
| 172 | + table.add_row(['friendly name', context.get('friendlyName', '')]) |
| 173 | + table.add_row(['internal peer IP address', |
| 174 | + context.get('internalPeerIpAddress', '')]) |
| 175 | + table.add_row(['remote peer IP address', |
| 176 | + context.get('customerPeerIpAddress', '')]) |
| 177 | + table.add_row(['advanced configuration flag', |
| 178 | + context.get('advancedConfigurationFlag', '')]) |
| 179 | + table.add_row(['preshared key', context.get('presharedKey', '')]) |
| 180 | + table.add_row(['phase 1 authentication', |
| 181 | + context.get('phaseOneAuthentication', '')]) |
| 182 | + table.add_row(['phase 1 diffie hellman group', |
| 183 | + context.get('phaseOneDiffieHellmanGroup', '')]) |
| 184 | + table.add_row(['phase 1 encryption', context.get('phaseOneEncryption', '')]) |
| 185 | + table.add_row(['phase 1 key life', context.get('phaseOneKeylife', '')]) |
| 186 | + table.add_row(['phase 2 authentication', |
| 187 | + context.get('phaseTwoAuthentication', '')]) |
| 188 | + table.add_row(['phase 2 diffie hellman group', |
| 189 | + context.get('phaseTwoDiffieHellmanGroup', '')]) |
| 190 | + table.add_row(['phase 2 encryption', context.get('phaseTwoEncryption', '')]) |
| 191 | + table.add_row(['phase 2 key life', context.get('phaseTwoKeylife', '')]) |
| 192 | + table.add_row(['phase 2 perfect forward secrecy', |
| 193 | + context.get('phaseTwoPerfectForwardSecrecy', '')]) |
| 194 | + table.add_row(['created', context.get('createDate')]) |
| 195 | + table.add_row(['modified', context.get('modifyDate')]) |
| 196 | + return table |
0 commit comments