You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在GCP Deployment Manager Python模板中发送HTTP Patch请求配置Cloud Router

解决GCP Deployment Manager配置HA VPN Cloud Router的两个问题

一、修复linkedVpnTunnel格式错误问题

你遇到的400错误是因为linkedVpnTunnel字段需要传入VPN隧道的完整资源API URL,而非简单的资源名称。在Deployment Manager模板中,无需手动拼接URL,直接使用资源引用语法即可自动获取正确的URL:

修改方法

将模板中interfaces里的linkedVpnTunnel值替换为$(ref.{VPN隧道资源名}.selfLink),其中{VPN隧道资源名}是你定义的VPN隧道资源的name字段值。

结合你的代码,把:

"linkedVpnTunnel": "vpn_tunnel",

改成:

"linkedVpnTunnel": "$(ref.{vpn_tunnel}0.selfLink)",

(因为你的dependsOn里用了f"{vpn_tunnel}0",说明VPN隧道资源名是vpn_tunnel变量加后缀0)

修改后的完整Cloud Router资源配置示例:

resources.extend([
    {
        # Cloud Router resource for HA VPN.
        'name': 'cloud_router',
        'type': 'gcp-types/compute-v1:routers',
        'properties': {
            'name': cloud_router,
            'project': project_id,
            'network': network,
            'region': context.properties['region'],
            'interfaces': [{
                "name": f"{cloud_router}-bgp-int-0",
                "linkedVpnTunnel": "$(ref.{vpn_tunnel}0.selfLink)",
                "ipRange": f"{context.properties['bgp_ip_0']}{context.properties['subnet_mask_0']}"
            }],
        },
        'metadata': {
            'dependsOn': [
                f"{vpn_tunnel}0",
                f"{vpn_tunnel}1"
            ]
        }
    }
])

这样Deployment Manager会自动解析出VPN隧道的完整API URL,避免格式错误,同时确保BGP对等体正确关联到VPN隧道。

二、在Python模板中实现HTTP Patch请求

如果需要对已创建的Cloud Router做部分字段更新(而非全量替换),可以通过Deployment Manager的自定义HTTP资源类型发送PATCH请求:

示例代码

先创建基础Cloud Router资源,再添加一个PATCH请求资源来更新interfaces:

resources.extend([
    # 创建基础Cloud Router(不含interfaces)
    {
        'name': 'cloud_router',
        'type': 'gcp-types/compute-v1:routers',
        'properties': {
            'name': cloud_router,
            'project': project_id,
            'network': network,
            'region': context.properties['region'],
        },
        'metadata': {
            'dependsOn': [f"{vpn_tunnel}0", f"{vpn_tunnel}1"]
        }
    },
    # PATCH请求更新Router的interfaces
    {
        'name': 'update-cloud-router-interfaces',
        'type': 'http',
        'properties': {
            'url': f"https://compute.googleapis.com/compute/v1/projects/{project_id}/regions/{context.properties['region']}/routers/{cloud_router}",
            'method': 'PATCH',
            'headers': {
                'Content-Type': 'application/json'
            },
            'body': {
                'interfaces': [
                    {
                        "name": f"{cloud_router}-bgp-int-0",
                        "linkedVpnTunnel": "$(ref.{vpn_tunnel}0.selfLink)",
                        "ipRange": f"{context.properties['bgp_ip_0']}{context.properties['subnet_mask_0']}"
                    },
                    {
                        "name": f"{cloud_router}-bgp-int-1",
                        "linkedVpnTunnel": "$(ref.{vpn_tunnel}1.selfLink)",
                        "ipRange": f"{context.properties['bgp_ip_1']}{context.properties['subnet_mask_1']}"
                    }
                ]
            }
        },
        'metadata': {
            'dependsOn': ['cloud_router', f"{vpn_tunnel}0", f"{vpn_tunnel}1"]
        }
    }
])

注意事项

  • 确保Deployment Manager服务账号拥有compute.routers.update权限;
  • 使用$(ref)语法引用资源selfLink,避免手动拼接URL出错;
  • PATCH请求的body只需包含要更新的字段(如interfaces),无需传入完整的Router配置。

内容的提问来源于stack exchange,提问作者SirHoppington

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 10:50:25