コード例 #1
0
    def create_route_table(self, resource_group_name, route_table_name, region,
                           route_table):
        """Create Route Table.

        :param str resource_group_name:
        :param str route_table_name:
        :param str region:
        :param route_table:
        :return:
        """
        self._logger.info(f"Creating Route Table: {route_table_name}")
        route_table = RouteTable(
            location=region,
            routes=[
                Route(
                    name=route.name,
                    next_hop_ip_address=route.next_hop_address,
                    next_hop_type=route.next_hop_type,
                    address_prefix=route.address_prefix,
                ) for route in route_table.routes
            ],
        )

        return self._azure_client.create_route_table(
            resource_group_name=resource_group_name,
            route_table_name=route_table_name,
            route_table=route_table,
        )
コード例 #2
0
def _ensure_route_table(cli_ctx, resource_group_name, ase_name, location,
                        subnet_id, force):
    subnet_id_parts = parse_resource_id(subnet_id)
    vnet_resource_group = subnet_id_parts['resource_group']
    vnet_name = subnet_id_parts['name']
    subnet_name = subnet_id_parts['resource_name']
    ase_route_table_name = ase_name + '-Route-Table'
    ase_route_name = ase_name + '-route'
    network_client = _get_network_client_factory(cli_ctx)

    subnet_obj = network_client.subnets.get(vnet_resource_group, vnet_name,
                                            subnet_name)
    if subnet_obj.route_table is None or force:
        rt_list = network_client.route_tables.list(resource_group_name)
        rt_found = False
        for rt in list(rt_list):
            if rt.name.lower() == ase_route_table_name.lower():
                rt_found = True
                break

        if not rt_found:
            logger.info('Ensure Route Table...')
            ase_route_table = RouteTable(location=location)
            poller = network_client.route_tables.begin_create_or_update(
                resource_group_name, ase_route_table_name, ase_route_table)
            LongRunningOperation(cli_ctx)(poller)

            logger.info('Ensure Internet Route...')
            internet_route = Route(address_prefix='0.0.0.0/0',
                                   next_hop_type='Internet')
            poller = network_client.routes.begin_create_or_update(
                resource_group_name, ase_route_table_name, ase_route_name,
                internet_route)
            LongRunningOperation(cli_ctx)(poller)

        rt = network_client.route_tables.get(resource_group_name,
                                             ase_route_table_name)
        if not subnet_obj.route_table or subnet_obj.route_table.id != rt.id:
            logger.info('Associate Route Table with Subnet...')
            subnet_obj.route_table = rt
            poller = network_client.subnets.begin_create_or_update(
                vnet_resource_group,
                vnet_name,
                subnet_name,
                subnet_parameters=subnet_obj)
            LongRunningOperation(cli_ctx)(poller)
    else:
        route_table_id_parts = parse_resource_id(subnet_obj.route_table.id)
        rt_name = route_table_id_parts['name']
        if rt_name.lower() != ase_route_table_name.lower():
            err_msg = 'Route table already exists.'
            rec_msg = 'Use --ignore-route-table to use existing route table ' \
                      'or --force-route-table to replace existing route table'
            validation_error = ValidationError(err_msg)
            validation_error.set_recommendation(rec_msg)
            raise validation_error