user630702
user630702

Reputation: 3127

Ruby - no implicit conversion of Symbol into Integer

I'm trying to create AWS Security Group but I'm getting the error no implicit conversion of Symbol into Integer during ec2.create_security_group(GroupName=sg_name, VpcId=vpc_id)

I'm following this documentation. Any idea how to resolve the error?

begin
 
  ec2 = get_aws_client
  prov = $evm.root['miq_provision_request'] || \
      $evm.root['miq_provision'] || \
      $evm.root['miq_provision_request_template']
  vpc_id = prov.get_option(:dialog_cloud_network).to_s.strip 
  #prov.get_option(:dialog_vm_name).to_s.strip 
  sg_name = 'test_sg_manageiq'
  
  #ec2.create_security_group({vpcid: vpc_id, groupname: sg_name})

  ec2.create_security_group(GroupName=sg_name, VpcId=vpc_id)

rescue => err
  log(:error, "[#{err}]\n#{err.backtrace.join("\n")}")
  exit MIQ_ABORT
end

Error:

[----] I, [2022-02-15T16:17:26.360528 #326:2ad4a042a1e0]  INFO -- automation: Q-task_id([r49_miq_provision_183]) <AEMethod sg_preprovision> Found provider: AWS via default method
[----] E, [2022-02-15T16:17:26.660450 #326:2ad4a042a1e0] ERROR -- automation: Q-task_id([r49_miq_provision_183]) <AEMethod sg_preprovision> [no implicit conversion of Symbol into Integer]
/ManageIQ_Custom/Cloud/VM/Provisioning/StateMachines/Methods/SG_Preprovision:143:in `<main>'
[----] I, [2022-02-15T16:17:26.707133 #326:2ad4959cb960]  INFO -- automation: Q-task_id([r49_miq_provision_183]) <AEMethod [/ManageIQ_Custom/Cloud/VM/Provisioning/StateMachines/Methods/SG_Preprovision]> Ending
[----] E, [2022-02-15T16:17:26.707734 #326:2ad4959cb960] ERROR -- automation: Q-task_id([r49_miq_provision_183]) State=<PreProvision> running  raised exception: <Method exited with rc=MIQ_ABORT>
[----] W, [2022-02-15T16:17:26.707927 #326:2ad4959cb960]  WARN -- automation: Q-task_id([r49_miq_provision_183]) Error in State=[PreProvision]

Upvotes: 0

Views: 70

Answers (1)

mu is too short
mu is too short

Reputation: 434665

An "no implicit conversion of Symbol into Integer" error almost always means that you have an array or string where you're expecting to have a hash. In your case, the suspicious code is:

#ec2.create_security_group({vpcid: vpc_id, groupname: sg_name})
ec2.create_security_group(GroupName=sg_name, VpcId=vpc_id)

The create_security_group call you're using looks more like Python than Ruby to me and is equivalent to:

GroupName = sg_name
VpcId = vpc_id
ec2.create_security_group(GroupName, VpcId)

so you're passing a string (sg_name) to a method that expects a hash of parameters:

#create_security_group(params = {}) ⇒ Types::CreateSecurityGroupResult

Creates a security group.
[...]

Then create_security_group tries to treat sg_name as the params hash it is expecting and that triggers the exception.

I think you want to use the code you commented out and fix the keys to include underscores as per the documentation:

ec2.create_security_group(vpc_id: vpc_id, group_name: sg_name)

Upvotes: 0

Related Questions