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

Azure网络安全组规则更新报错:undefined method `deep_transform_keys' for Array

问题:更新Azure网络安全组时触发undefined method 'deep_transform_keys' for Array错误

问题场景

尝试通过Ruby的azure-armrest gem更新Azure网络安全组(NSG)规则,执行代码如下:

network_sg = Azure::Armrest::Network::NetworkSecurityGroupService.new(conf)  
network_sg.update('sg_testing_temp_1', 'cloud-shell-storage', network_sg_properties)

完整报错栈

[----] E, [2022-07-17T16:36:12.489747 #32747:2ad3b3e0be04] ERROR -- automation: Q-task_id([r988_miq_provision_2589]) <AEMethod azure_sg_provision> The following error occurred during method evaluation:
[----] E, [2022-07-17T16:36:12.501593 #32747:2ad3b3e0be04] ERROR -- automation: Q-task_id([r988_miq_provision_2589]) <AEMethod azure_sg_provision>   NoMethodError: undefined method `deep_transform_keys' for #<Array:0x0000562e6b8e9898>
[----] E, [2022-07-17T16:36:12.503179 #32747:2ad3b3e0be04] ERROR -- automation: Q-task_id([r988_miq_provision_2589]) <AEMethod azure_sg_provision>   /opt/manageiq/manageiq-gemset/gems/azure-armrest-0.14.0/lib/azure/armrest/resource_group_based_service.rb:199:in `transform_create_options'
[----] E, [2022-07-17T16:36:12.507115 #32747:2ad3b3e0601c] ERROR -- automation: Method STDERR: from /ManageIQ_Custom/Cloud/VM/Provisioning/StateMachines/Methods/Azure_SG_Provision:268:in `<main>'
[----] I, [2022-07-17T16:36:12.534925 #32747:2ad3a008b968]  INFO -- automation: Q-task_id([r988_miq_provision_2589]) <AEMethod [/ManageIQ_Custom/Cloud/VM/Provisioning/StateMachines/Methods/Azure_SG_Provision]> Ending

错误原因

错误根源是传入network_sg.update方法的network_sg_properties参数格式不符合要求:

  • azure-armrest gem的transform_create_options方法会调用Rails哈希的deep_transform_keys方法处理参数,但当前传入的参数(或其内部某个嵌套字段)是数组,而数组没有这个方法。
  • 正常情况下,network_sg_properties应该是符合Azure ARM API规范的顶级哈希结构,仅部分嵌套字段(如security_rules)允许使用数组。如果误将整个参数设为数组,或把需要哈希的嵌套字段写成数组,就会触发该错误。

解决方案

  1. 校验参数结构:确保network_sg_properties是顶级哈希,所有需要对象的嵌套字段(如单条安全规则)均为哈希类型。正确的参数结构示例:
    network_sg_properties = {
      properties: {
        security_rules: [
          {
            name: "allow-ssh",
            properties: {
              protocol: "Tcp",
              source_port_range: "*",
              destination_port_range: "22",
              source_address_prefix: "*",
              destination_address_prefix: "*",
              access: "Allow",
              priority: 100,
              direction: "Inbound"
            }
          }
        ]
      }
    }
    
  2. 排查参数生成逻辑:如果network_sg_properties是通过其他代码生成的,检查是否存在误将哈希转为数组、或遗漏外层哈希包装的情况。
  3. 打印参数验证:在调用update方法前,添加代码确认参数类型和内容:
    puts "参数类型: #{network_sg_properties.class}" # 应输出 Hash
    puts "参数内容: #{network_sg_properties.inspect}"
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 23:54:24