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-armrestgem的transform_create_options方法会调用Rails哈希的deep_transform_keys方法处理参数,但当前传入的参数(或其内部某个嵌套字段)是数组,而数组没有这个方法。- 正常情况下,
network_sg_properties应该是符合Azure ARM API规范的顶级哈希结构,仅部分嵌套字段(如security_rules)允许使用数组。如果误将整个参数设为数组,或把需要哈希的嵌套字段写成数组,就会触发该错误。
解决方案
- 校验参数结构:确保
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" } } ] } } - 排查参数生成逻辑:如果
network_sg_properties是通过其他代码生成的,检查是否存在误将哈希转为数组、或遗漏外层哈希包装的情况。 - 打印参数验证:在调用
update方法前,添加代码确认参数类型和内容:puts "参数类型: #{network_sg_properties.class}" # 应输出 Hash puts "参数内容: #{network_sg_properties.inspect}"
内容的提问来源于stack exchange,提问作者user630702
相关产品推荐
相关产品推荐

