Home > Software engineering >  Ruby - Unable to append data to existing hash
Ruby - Unable to append data to existing hash

Time:07-18

I'm getting error no implicit conversion of String into Integer (TypeError) when I'm trying to push new_rule to existing hash.

Any idea what the issue is here?

require 'json'
network_sg_existing_rules  = network_sg_properties["securityRules"]
puts network_sg_existing_rules  

[{"name"=>"Port_8080", "id"=>"/subscriptions/44a91bb8-d388-467e-89e6-123456/resourceGroups/cloud-shell-storage-centralindia/providers/Microsoft.Network/networkSecurityGroups/sg_testing_temp_1/securityRules/Port_8080", "etag"=>"W/\"0d87997d-2a2a-4725-9be5-123456\"", "type"=>"Microsoft.Network/networkSecurityGroups/securityRules", "properties"=>{"provisioningState"=>"Succeeded", "protocol"=>"*", "sourcePortRange"=>"*", "destinationPortRange"=>"8080", "sourceAddressPrefix"=>"*", "destinationAddressPrefix"=>"*", "access"=>"Allow", "priority"=>100, "direction"=>"Inbound", "sourcePortRanges"=>[], "destinationPortRanges"=>[], "sourceAddressPrefixes"=>[], "destinationAddressPrefixes"=>[]}}]

# Append new rule.

new_rule = {
    :name => "rule_4",
    :properties => {
      :protocol          => "TCP",
      :sourceAddressPrefix => "*",
      :destinationAddressPrefix => "*",
      :access => "Allow",
      :destinationPortRange => "22",
      :sourcePortRange => "*",
      :priority => "301",
      :direction => "Inbound"
    }
  }

  
network_sg_new_rules = network_sg_existing_rules["securityRules"].push(new_rule)    

puts network_sg_new_rules

CodePudding user response:

network_sg_properties is a hash which has a key called securityRules. you are trying to add it in value of securityRules.

Instead of

network_sg_new_rules = network_sg_existing_rules["securityRules"].push(new_rule)    

try

network_sg_properties = network_sg_properties["securityRules"].push(new_rule)

Hence value of network_sg_properties[securityRules] will be

[
    [0] {
              "name" => "Port_8080",
                "id" => "/subscriptions/44a91bb8-d388-467e-89e6-123456/resourceGroups/cloud-shell-storage-centralindia/providers/Microsoft.Network/networkSecurityGroups/sg_testing_temp_1/securityRules/Port_8080",
              "etag" => "W/\"0d87997d-2a2a-4725-9be5-123456\"",
              "type" => "Microsoft.Network/networkSecurityGroups/securityRules",
        "properties" => {
                     "provisioningState" => "Succeeded",
                              "protocol" => "*",
                       "sourcePortRange" => "*",
                  "destinationPortRange" => "8080",
                   "sourceAddressPrefix" => "*",
              "destinationAddressPrefix" => "*",
                                "access" => "Allow",
                              "priority" => 100,
                             "direction" => "Inbound",
                      "sourcePortRanges" => [],
                 "destinationPortRanges" => [],
                 "sourceAddressPrefixes" => [],
            "destinationAddressPrefixes" => []
        }
    },
    [1] {
              :name => "rule_4",
        :properties => {
                            :protocol => "TCP",
                 :sourceAddressPrefix => "*",
            :destinationAddressPrefix => "*",
                              :access => "Allow",
                :destinationPortRange => "22",
                     :sourcePortRange => "*",
                            :priority => "301",
                           :direction => "Inbound"
        }
    }
  • Related