【问题标题】:SoftLayer API createObjectSoftLayer API 创建对象
【发布时间】:2017-01-13 15:41:22
【问题描述】:

我已经尝试了几天来获取 API 服务 {'Network_Firewall_Update_Request_Rule'].createObject 工作没有成功。我确实让 firewallManager edit_dedicated_fwl_rules 工作,但现在希望服务也能工作。我在整个网络上寻找答案,但找不到答案。

我的问题是传递给防火墙规则的服务 createObject 的参数的语法是什么? 你有例子吗?

正在使用的命令是:

client = SoftLayer.create_client_from_env(username=user, api_key=api)
client['Network_Firewall_Update_Request_Rule'].createObject(id=12345, [{'action': 'permit'}])

是的,我知道我需要更多的规则语句来创建。 这将返回"SyntaxError: non-keyword arg after keyword arg" because of the "id=".

"id=" 放在 API 的末尾: client['Network_Firewall_Update_Request_Rule'].createObject([{'action': 'permit'}], id=12345) then the error is "Either a component ID or an ACL ID must be supplied."

如果我删除“id=”并且只有 client['Network_Firewall_Update_Request_Rule'].createObject(12345, [{'action': 'permit'}])

那么错误是“必须提供组件 ID 或 ACL ID。”

我知道我必须拥有"id=",因为此命令有效:

client['Network_Firewall_Update_Request'].getRules(id=12345)

但是使用 Manager API 命令fw.edit_dedicated_fwl_rules(12345, [{'action': 'permit'}])

没有"id=",因为这会成功创建规则。

感谢您的帮助。

【问题讨论】:

    标签: api rules ibm-cloud-infrastructure createobject


    【解决方案1】:

    查看这篇文章:

    I need to create a softlayer network firewall rule through REST API

    创建规则的 REST 示例如下:

    POST https://[username]:[apikey]@api.softlayer.com/rest/v3/SoftLayer_Network_Firewall_Update_Request/createObjec
    
    Payload:
    
    {
      "parameters": [
        {
          "networkComponentFirewallId": 72605,
          "rules": [
            {
              "action": "permit",
              "destinationIpAddress": "159.8.52.188",
              "destinationIpCidr": 32,
              "destinationPortRangeEnd": 122,
              "destinationPortRangeStart": 12,
              "notes": "This is a test",
              "orderValue": 1,
              "protocol": "tcp",
              "sourceIpAddress": "10.10.10.0",
              "sourceIpCidr": 32,
              "version": 4
            }
          ]
        }
      ]
    }
    

    你需要根据你需要的配置替换所有的值。

    现在你需要为上面的请求获取“networkComponentFirewallId”,可以这样获取:

    GET https://$USERID:$APIKEY@api.softlayer.com/rest/v3/SoftLayer_Virtual_Guest/$VSIID/getFirewallServiceComponent
    

    使用 Python,上面的示例将是这样的:

    client['Network_Firewall_Update_Request_Rule'].createObject(
     {
              "networkComponentFirewallId": 72605,
              "rules": [
                {
                  "action": "permit",
                  "destinationIpAddress": "159.8.52.188",
                  "destinationIpCidr": 32,
                  "destinationPortRangeEnd": 122,
                  "destinationPortRangeStart": 12,
                  "notes": "This is a test",
                  "orderValue": 1,
                  "protocol": "tcp",
                  "sourceIpAddress": "10.10.10.0",
                  "sourceIpCidr": 32,
                  "version": 4
                }
              ]
            }
    )
    

    并获取“networkComponentFirewallId”属性:

    client['Virtual_Guest'].getFirewallServiceComponent(id=VirtualGuest)
    

    请注意,以上示例是编辑附加到 VSI 的防火墙规则。

    为了在 VLAN 中为专用防火墙创建规则,请求如下:

    client['Network_Firewall_Update_Request_Rule'].createObject(
    {
        "firewallContextAccessControlListId": 3092,
        "rules": [{
            "action": "permit",
            "destinationIpAddress": "any",
            "destinationIpCidr": 32,
            "destinationIpSubnetMask": "255.255.255.255",
            "destinationPortRangeEnd": 65535,
            "destinationPortRangeStart": 1,
            "id": 5669281,
            "orderValue": 1,
            "protocol": "tcp",
            "sourceIpAddress": "0.0.0.0",
            "sourceIpCidr": 0,
            "sourceIpSubnetMask": "0.0.0.0",
            "status": "allow_edit",
            "version": 4
        }]
    }
    )
    

    现在如何获取“firewallContextAccessControlListId”的值,你需要使用这个:

    client['SoftLayer_Network_Vlan'].getFirewallInterfaces(id=vlanId, mask="mask[firewallContextAccessControlLists]")
    

    上面的方法会返回外部和内部的接口,目前只有你可以设置outsitde接口的规则

    问候

    【讨论】:

    • 感谢尼尔森的回复。我找到了那个例子,但它没有用。他们使用的格式是“firewallContextAccessControlListId”:2854,“rules”:在传递值时没有使用“id=”。使用此格式返回错误是“必须提供组件 ID 或 ACL ID。”
    • 我会审核并通知您,但代码之前运行良好
    • 我更新了我的答案,只是指出 createObject 方法没有作为参数的 id,在某些情况下,您需要在有效负载中传递该 ID,参数名称将被命名为 id,但在其他情况下,防火墙的 ID 称为 networkComponentFirewallId。此外,了解您正在处理哪种防火墙会更有帮助
    • 另外我在另一个答案中更新了我的代码,两者都工作正常
    • 再次感谢您的详细回复。这个命令的结构现在已经很明显了。当然,我没有使用 VSI 附加防火墙,而是使用 VLAN 专用防火墙。 :) 根据您的描述和关于我使用客户端 ['SoftLayer_Network_Firewall_Update_Request_Rule'].getObject(id=fw_id) 并获取 'networkComponentFirewallId': fw_value 的引用链接。但这一定不是 VLAN 的正确参数 - 专用防火墙作为命令仍然无法获得组件 ID。我确实尝试了参数的许多其他排列但没有成功。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多