Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #27952 - Values lost on Host and profiles edit page #43

Merged
merged 8 commits into from Oct 31, 2019

Conversation

apuntamb
Copy link
Member

@apuntamb apuntamb commented Sep 27, 2019

As first iteration round, it fixes the existing properties to be displayed upon edit action for the form.
This PR is dependent on #42 for compute profile edit page.
Meanwhile @vijay8451 , if you can review this at your end and let me know if there are still issues. :)

@apuntamb apuntamb force-pushed the 27952_values_lost branch 2 times, most recently from 28acd05 to a0d6615 Compare September 27, 2019 10:23
@vijay8451
Copy link

@apuntamb as checked the edit following are still not showing. Are they expected so far as it is or values should be there ?

VM Tab => Azure Region, Resource Group, 
Interface Tab => Azure Subnet, Public IP

@apuntamb
Copy link
Member Author

@apuntamb as checked the edit following are still not showing. Are they expected so far as it is or values should be there ?

VM Tab => Azure Region, Resource Group, 
Interface Tab => Azure Subnet, Public IP

VM Tab => Azure Region, Resource Group should be shown now. Although, I agree the Interface Tab => Azure Subnet, Public IP is not showing. I'll look into it and update the PR. But, Region and RG should work.

@vijay8451
Copy link

@apuntamb Region and RG are not working ..I am only able to see VM Size

rg

premium_os_disk: opts[:premium_os_disk],
)
vm = AzureRMCompute.new(azure_vm: raw_vm ,sdk: sdk)
vm.resource_group = opts[:resource_group]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works well in case of Compute Profile. However, it fails for normal host creation since there it is expecting [:azure_vm][:resource_group]. Playing around with this, I could understand that keeping the above hierarchy for rg would fail for host edit and keeping just [:resource_group] would fail for profile. Can we do something here where it can handle both the scenarios?
If still it errors out, how about removing the Resource Group field from profiles page?
@ShimShtein thoughts?

Copy link
Member

@kgaikwad kgaikwad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @apuntamb. Added few inline comments.

@ShimShtein,
I think it would be good to handle resource_group inside custom class i.e AzureRMCompute.
But I would like to know your suggestions on the same.

premium_os_disk: opts[:premium_os_disk],
)
vm = AzureRMCompute.new(azure_vm: raw_vm ,sdk: sdk)
vm.resource_group = opts[:resource_group]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resource_group is required while vm creation so instead of assigning resource_group here, would it be possible to pass it as an argument while initializing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kgaikwad do you mean while initializing the above custom/wrapper class, something like:
AzureRMCompute.new(azure_vm: raw_vm ,sdk: sdk, resource_group: opts[:resource_group]) ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It still fails for create_vm. In _base partial, it still expects f.object.azure_vm.resource_group for host creation (create_vm) while it expects f.object.resource_group for compute profiles (new_vm).

Copy link
Member

@kgaikwad kgaikwad Oct 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why it is expecting f.object.azure_vm.resource_group for host creation in _base partial not f.object.resource_group?
By any chance, would it be possible to override resource_group's get method & handle it accordingly?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To achieve this you have to tweak behaviour inside get method for resource_group in your custom class i.e. either @azure_vm.resource_group or resource_group whichever having value.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I had to override resource_group's getter and handle the scenarios. Updating the changes.

app/models/foreman_azure_rm/azure_rm.rb Show resolved Hide resolved
sshkey.key_data
end

def premium_os_disk
Copy link
Member

@kgaikwad kgaikwad Oct 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same like platform, can't you set value for premium_os_disk using @azure_vm.storage_profile.os_disk.managed_disk?

Would it be possible to fetch storage_account_type value from managed_disk?

@kgaikwad
Copy link
Member

kgaikwad commented Oct 3, 2019

@apuntamb,
From next time, could you please push new changes into separate commits?
It would be easy to review new version of suggested changes.
If you want, you can squash it at the end.

@apuntamb
Copy link
Member Author

apuntamb commented Oct 3, 2019

@apuntamb,
From next time, could you please push new changes into separate commits?
It would be easy to review new version of suggested changes.
If you want, you can squash it at the end.

Sure thing :)

@apuntamb
Copy link
Member Author

apuntamb commented Oct 3, 2019

@apuntamb Region and RG are not working ..I am only able to see VM Size

rg

@vijay8451 Can you please try now. I have updated the PR. This should work for both Host edit page and Compute Profiles edit page. However, Network Interfaces tab values might not still work.

app/models/foreman_azure_rm/azure_rm_compute.rb Outdated Show resolved Hide resolved
@@ -22,6 +33,39 @@ def vm_size
@azure_vm.hardware_profile.vm_size
end

def platform
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we either separate azure-specific fields like platform to a concern, or at least put a comment that this property is a convenience for us, and not part of foreman's interface?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. I'll add comment here.

@@ -155,7 +180,9 @@ def vm_sizes(region)
end

def vm_instance_defaults
ActiveSupport::HashWithIndifferentAccess.new
super.merge(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deep_merge suits better - if there are some properties defined on interfaces in super, we still want them. Regular merge will replace the whole interfaces member.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

@vijay8451
Copy link

@apuntamb Region and RG are not working ..I am only able to see VM Size
rg

@vijay8451 Can you please try now. I have updated the PR. This should work for both Host edit page and Compute Profiles edit page. However, Network Interfaces tab values might not still work.

@apuntamb I'll start once code refactor part complete :)

{ :include_blank => _('Please first select an image and an Azure region')},
<%= selectable_f f, :network, [],#options_for_select(compute_resource.subnets("eastus")),
{ :include_blank => _('Please first select an image and an Azure region'),
:selected => nil#f.object.network
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This f.object.network is constructed after the vm is created. So, doesn't work for #new_vm method. This f comes from the new_interface method which returns nothing but the azure's (sdk's) NetworkInterface.new. How should we display the value for this on edit page or Profile?

@apuntamb
Copy link
Member Author

As per the commit
fb07d60
, there is a structural change for AzureRM compute resource. The Compute resource will now have region as an attribute and hence, region will be available throughout, on the compute resource object under consideration.
This is a much consistent approach if compared with all other compute resources that foreman supports.
Also, now we are at the stage where the values on edit host or compute profiles page for size, resource_group or subnets are also available.
@vijay8451 I would like you to now start testing since we have the code ready.
Also, @kgaikwad will be helping us with the review here, including some duplicate code refactoring (if present).

@apuntamb apuntamb force-pushed the 27952_values_lost branch 2 times, most recently from 72e07b8 to cbd94f8 Compare October 18, 2019 10:28
@vijay8451
Copy link

vijay8451 commented Oct 18, 2019

@apuntamb test results

Works:
Test Connection
Create Compute Resource with Region attribute set
Create Image with User data ENABLED
Create Image with User data DISABLED
Go to VM tab directly (no dependency of OS tab for VM tab)
Create VM with password (Finish Template)
Create VM with sshkey (Finish Template)
Create VM with password (User data Template)
Create VM with sshkey (User data Template)
Create VM with password with custom script (Finish Template)
Create VM with sshkey with custom script (Finish Template)
Create VM with password with custom script (User data Template)
Create VM with sshkey with custom script (User data Template)
Edit Host Page opens
Values on Edit host page present
Network Interfaces tab values on edit host page present
Power On works
Power Off works
Destroy host/vm present in azure portal
Destroy host/vm NOT present in azure portal
Create Compute Profile for Azure Compute Resource
Edit Compute Profile for Azure Compute Resource
Values present on Edit Compute profile page
List all VMs in Virtual Machines tab on Compute Resource page
Diassociate VM

Not works:
Compute Resourse:
In CR VM list should show region wise
Region should not be hardcoded

Create Image:
Provided password get blank after second time edit
Create image does not have formate validation for provided URN

Compute Profile:
Selected Image and password get blank after save

Not works are not a blocker for this PR but requires to be fix and can handle separately.
hence ACK from QE.

@apuntamb
Copy link
Member Author

@vijay8451 Thanks for the thorough testing!
In the latest commit, the VMs list will be shown region wise under ComputeResource page.
Also, now AssociateVM should work smoothly too. Kindly try de-associating followed by associating the vm. Also, we can keep the region hardcoded for now, as it doesn't depend on the Test Connection operation.
For the Compute profile, I cannot reproduce the issue of image getting blank. Password shall show ******* after save but, Image value will be set on edit page.
For Image validation, I have for now put the help text for users.

@vijay8451
Copy link

@vijay8451 Thanks for the thorough testing!
In the latest commit, the VMs list will be shown region wise under ComputeResource page.

Able to see now .

Also, now AssociateVM should work smoothly too. Kindly try de-associating followed by associating the vm.

Works fine when click on Associate VMs under CR , However a feature still requires to add where a specific VM can Associate too.(But that more looks a enhancement and can handle under different PR )

Also, we can keep the region hardcoded for now, as it doesn't depend on the Test Connection operation.

Okay, If i understood correctly , so we have a plan to move from hardcoded to automate way in near future ?

For the Compute profile, I cannot reproduce the issue of image getting blank. Password shall show ******* after save but, Image value will be set on edit page.

I tired to latest dev setup and found :
a. Selected Image, password, 'Custom Script Command', 'Comma seperated file URIs' get blank after save.
b. Unable to click on "Add Interface button" under 'Network interfaces'.
c. Using same compute profile Host Create fails with following trace back:

05:52:10 rails.1   | 2019-10-22T05:52:10 [D|app|1b85a0b6] Backtrace for 'Failed to create a compute CR_Test1 (Azure Resource Manager) instance elva-giblin.example.com: undefined method `start_with?' for nil:NilClass
05:52:10 rails.1   |  |  ' error (NoMethodError): undefined method `start_with?' for nil:NilClass
05:52:10 rails.1   |  | /home/vagrant/foreman_azure_rm/app/models/concerns/foreman_azure_rm/vm_extensions/managed_vm.rb:135:in `block in initialize_vm'
05:52:10 rails.1   |  | /home/vagrant/foreman_azure_rm/app/models/concerns/foreman_azure_rm/vm_extensions/managed_vm.rb:126:in `tap'
05:52:10 rails.1   |  | /home/vagrant/foreman_azure_rm/app/models/concerns/foreman_azure_rm/vm_extensions/managed_vm.rb:126:in `initialize_vm'

For Image validation, I have for now put the help text for users.

Works for me now .

@apuntamb
Copy link
Member Author

apuntamb commented Oct 22, 2019

@vijay8451 I have pushed a commit that fixes the associate vm and network interface problem for compute profile.
Although, I would like @ShimShtein and @kgaikwad to review the PR code-wise so, that we do not keep on retesting functionalities before and after refactoring. How does that sound?

@vijay8451
Copy link

@vijay8451 I have pushed a commit that fixes the associate vm and network interface problem for compute profile.
Although, I would like @ShimShtein and @kgaikwad to review the PR code-wise so, that we do not keep on retesting functionalities before and after refactoring. How does that sound?

@apuntamb sounds good to me .

app/models/foreman_azure_rm/azure_rm_compute.rb Outdated Show resolved Hide resolved
@@ -4,13 +4,14 @@ class Engine < ::Rails::Engine

#autoloading all files inside lib dir
config.autoload_paths += Dir["#{config.root}/lib"]
config.autoload_paths += Dir["#{config.root}/lib/foreman_azure_rm"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need this directive, If it is present class ::Foo could be resolved to file lib/foreman_azure_rm/foo.rb. I don't think this is what we want.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed.

attrs[:privip_alloc] = (attrs[:name] == 'false') ? false : true
pip_alloc = case attrs[:pubip_alloc]
private_ip = (attrs[:private_ip] == 'false') ? false : true
pip_alloc = case attrs[:public_ip]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we call it pub_ip? pip can be both public and private.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

sdk.vnets.select { |vnet| vnet.location == stripped_region }
end
def virtual_networks
sdk.vnets.select { |vnet| vnet.location == region }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the region is constant, I think it's worth memoizing the result:

Suggested change
sdk.vnets.select { |vnet| vnet.location == region }
@virtual_networks ||= sdk.vnets.select { |vnet| vnet.location == region }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

def new_vm(attr = {})
AzureRMCompute.new(sdk: sdk)
def new_vm(args = {})
if args.empty? || args[:image_id].nil?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding if..else, could you please add return statement with if condition. Something like:
return AzureRMCompute.new(sdk: sdk) if args.empty? || args[:image_id].nil?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

ifaces << new_interface(iface_attrs)
end
end
vm = AzureRMCompute.new(azure_vm: raw_vm ,sdk: sdk, resource_group: opts[:resource_group], nics: ifaces)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, no need of assignment. In Ruby, methods always return the evaluated result of the last line of the expression unless an explicit return comes before it.

Suggested change
vm = AzureRMCompute.new(azure_vm: raw_vm ,sdk: sdk, resource_group: opts[:resource_group], nics: ifaces)
AzureRMCompute.new(azure_vm: raw_vm ,sdk: sdk, resource_group: opts[:resource_group], nics: ifaces)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

@@ -35,7 +35,7 @@ def define_managed_storage_profile(vm_name, vhd_path, publisher, offer, sku, ver
os_disk.managed_disk = managed_disk_params
storage_profile.os_disk = os_disk

# Currently eliminating data disk creation since capability does not exist.
# ToDo disk creation for volume capability
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# ToDo disk creation for volume capability
# TODO - disk creation for volume capability

Could you please create an upstream issue under Compute resources - Azure category for better tracking instead of having it here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will create a tracker.

vm_params = initialize_vm(vm_hash)
vm_params.network_profile = define_network_profile(vm_hash[:network_interface_card_ids])

actual_vm = sdk.create_or_update_vm(vm_hash[:resource_group], vm_hash[:name], vm_params)
logger.debug "Virtual Machine #{vm_hash[:name]} Created Successfully."
Copy link
Member

@kgaikwad kgaikwad Oct 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this logger statement inside azure_rm.rb after this line.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

logger.debug "Virtual Machine #{vm_hash[:name]} Created Successfully."
response
actual_vm
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of lines(194..196), simply return -

sdk.create_or_update_vm(vm_hash[:resource_group], vm_hash[:name], vm_params)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

return AzureRMCompute.new(sdk: sdk) if args.empty? || args[:image_id].nil?
opts = vm_instance_defaults.merge(args.to_h).deep_symbolize_keys
# convert rails nested_attributes into a plain hash
[:interfaces].each do |collection|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need this generic code, or can we simplify it to:

nested_args = opts.delete(:interfaces_attributes)
opts[collection] = nested_attributes_for(:interfaces, nested_args) if nested_args

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

Copy link
Member

@ShimShtein ShimShtein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK from me.
@vijay8451, can you please give it a run? If you're OK with it, we can merge it

@vijay8451
Copy link

vijay8451 commented Oct 23, 2019

@apuntamb

Below are not working :

Compute Profile:

  1. Selected Image, password, 'Custom Script Command', 'Comma seperated file URIs' get blank after save
  2. Unable to see Network interfaces

Below comes during profile edit:

CP

VM Power on/off/Edit/Delete action always end with below screen:

edit

@apuntamb
Copy link
Member Author

apuntamb commented Oct 23, 2019

@vijay8451 Following is my Compute Profiles Edit page. I can see Network Interfaces tab, Password and Image both are set after save.
About Custom Script and URIs, I can't see. Need to fix this.

cp_Interfaces_tab

@apuntamb
Copy link
Member Author

@vijay8451 Ohkk I see that network interfaces for profiles is not loading.
@ShimShtein This is caused because nics if passed as empty [], it doesn't load the network interfaces partial for compute profiles and hence, the nics were earlier initialized with
nics: [OpenStruct.new(:network => "", :public_ip => "", :private_ip => false)].

Copy link
Member

@kgaikwad kgaikwad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@apuntamb, added few inline comments not major.

nic_conf.private_ipallocation_method = priv_ip_alloc
nic_conf.subnet = subnets(args[:azure_vm][:location]).select{ |subnet| subnet.id == attrs[:network] }.first
nic_conf.subnet = subnets.select{ |subnet| subnet.id == attrs[:network] }.first
nic_conf.public_ipaddress = pip.present? ? pip : nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
nic_conf.public_ipaddress = pip.present? ? pip : nil
nic_conf.public_ipaddress = pip

pip will be nil if pub_ip_alloc not present, right? so to add condition here doesn't make any sense.

when 'Static'
NetworkModels::IPAllocationMethod::Static
when 'Dynamic'
NetworkModels::IPAllocationMethod::Dynamic
when 'None'
nil
end
priv_ip_alloc = if attrs[:priv_ip_alloc]
priv_ip_alloc = if private_ip
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this priv_ip_alloc assignment is not dependent on pub_ip_alloc, could you please move this block above before pub_ip_alloc assignment to make it better readable?

Currently it looks like -
private_ip value then pub_ip_alloc assignment -> priv_ip_alloc assignment -> again a condition based on pub_ip_alloc.

@@ -120,7 +119,7 @@ def create_nics(args = {})
nics
end

def create_managed_virtual_machine(vm_hash)
def initialize_vm(vm_hash)
custom_data = vm_hash[:custom_data]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see that custom_data used only once in this method, do we really need to declare this custom_data?
why not to use vm_hash[:custom_data] directly wherever required.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should be a part of this PR. I would like to address functionality issues here, and refactor the code later on, while we are not under delivery pressure.

@@ -120,7 +119,7 @@ def create_nics(args = {})
nics
end

def create_managed_virtual_machine(vm_hash)
def initialize_vm(vm_hash)
custom_data = vm_hash[:custom_data]
msg = "Creating Virtual Machine #{vm_hash[:name]} in Resource Group #{vm_hash[:resource_group]}."
logger.debug msg
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At line-128,

if vm_hash[:availability_set_id] 
    sub_resource = MsRestAzure::SubResource.new
    sub_resource.id = vm_hash[:availability_set_id]
    vm.availability_set = sub_resource
end

looks much better, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, it's cosmetics in the original code, let's leave it to after delivery.

@@ -183,30 +182,34 @@ def create_managed_virtual_machine(vm_hash)
vm.hardware_profile = ComputeModels::HardwareProfile.new.tap do |hw_profile|
hw_profile.vm_size = vm_hash[:vm_size]
end
vm.network_profile = define_network_profile(vm_hash[:network_interface_card_ids])
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of hard-coding ssh_key path at two places, could you initialize a variable with given path.
Then use that variable at these places.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is only one place with hard-coded path right now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line number 156 & 163.

if args[:azure_vm][:script_command].present? || args[:azure_vm][:script_uris].present?
def create_vm_extension(region, args = {})
if args[:script_command].present? || args[:script_uris].present?
args[:script_uris] = args[:script_uris].to_s unless args[:script_uris].present?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
args[:script_uris] = args[:script_uris].to_s unless args[:script_uris].present?
args[:script_uris] ||= args[:script_uris].to_s

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fail if the value is nil (it would be nil.to_s)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, current statement will also fail, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ShimShtein It will not fail as nil.to_s will give us "".

public_ip_address || private_ip_address
end

def public_ip_address
interfaces.each do |nic|
nic.ip_configurations.each do |configuration|
next unless configuration.primary
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not to return nil after this when configuration.public_ipaddress is not present.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm returning nil here if the public ip is not present: line 86

Copy link
Member

@kgaikwad kgaikwad Oct 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that you have returned nil in case public_ipaddress is not present.
What I meant to say was that instead of having if.. else loop why not to mention something like -

return nil if configuration.public_ipaddress.blank?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

def vm_nics(vm)
ifaces = []
vm.network_profile.network_interfaces.each do |nic|
nic_rg = nic.id.split('/')[4]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
nic_rg = nic.id.split('/')[4]
nic_rg = ((splits_nic_id = nic.id.split('/'))[4]
nic_name = splits_nic_id[-1]

stripped_region = region.gsub(/\s+/, '').downcase
vnets = virtual_networks(stripped_region)
def subnets
vnets = virtual_networks
subnets = []
vnets.each do |vnet|
subnets.concat(sdk.subnets(vnet.resource_group, vnet.name))
end
subnets
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently method available_subnets internally calls to subnets then why not to it alias_method of subnets?

alias_method :available_subnets, :subnets

rescue RuntimeError => e
Foreman::Logging.exception('Unhandled Azure RM error', e)
destroy_vm vm.id if vm
raise e
end

def destroy_vm(uuid)
#vm.azure_vm because that's the azure object and vm is the wrapper
vm = find_vm_by_uuid(uuid)
vm_name = vm.name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not to use vm.name directly as used only once? Similar case for nic_ids.

Copy link
Member

@ShimShtein ShimShtein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kgaikwad, I would prefer doing those cosmetic changes in a different PR. I think this PR is already very big, to add more code that is not contributing to functionality.

@apuntamb, I have found how to force nic template to appear properly in compute profile.

All in all it looks good.

# causes compute profiles issue
# NetworkModels::NetworkInterface.new
def new_interface(attrs = {})
args = { :network => "", :public_ip => "", :private_ip => false }.merge(attrs.to_h)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
args = { :network => "", :public_ip => "", :private_ip => false }.merge(attrs.to_h)
args = { :network => "", :public_ip => "", :private_ip => false, 'persisted?' => false }.merge(attrs.to_h)

@apuntamb, this will make fields_for generate new interface template in compute profiles.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ShimShtein This worked like a charm.

@@ -120,7 +119,7 @@ def create_nics(args = {})
nics
end

def create_managed_virtual_machine(vm_hash)
def initialize_vm(vm_hash)
custom_data = vm_hash[:custom_data]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should be a part of this PR. I would like to address functionality issues here, and refactor the code later on, while we are not under delivery pressure.

@@ -120,7 +119,7 @@ def create_nics(args = {})
nics
end

def create_managed_virtual_machine(vm_hash)
def initialize_vm(vm_hash)
custom_data = vm_hash[:custom_data]
msg = "Creating Virtual Machine #{vm_hash[:name]} in Resource Group #{vm_hash[:resource_group]}."
logger.debug msg
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, it's cosmetics in the original code, let's leave it to after delivery.

@@ -183,30 +182,34 @@ def create_managed_virtual_machine(vm_hash)
vm.hardware_profile = ComputeModels::HardwareProfile.new.tap do |hw_profile|
hw_profile.vm_size = vm_hash[:vm_size]
end
vm.network_profile = define_network_profile(vm_hash[:network_interface_card_ids])
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is only one place with hard-coded path right now.

if args[:azure_vm][:script_command].present? || args[:azure_vm][:script_uris].present?
def create_vm_extension(region, args = {})
if args[:script_command].present? || args[:script_uris].present?
args[:script_uris] = args[:script_uris].to_s unless args[:script_uris].present?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fail if the value is nil (it would be nil.to_s)

@kgaikwad
Copy link
Member

@ShimShtein,
+1 surely! I don't mind having those changes in a different pull-request.

@vijay8451
Copy link

vijay8451 commented Oct 31, 2019

@apuntamb @ShimShtein @kgaikwad
Not further blocker seen ..Below can handle under separate PR .

In CP, 'Selected Image', 'Custom Script Command', 'Comma seperated file URIs' get blank after save

LGTM 👍 ... ACK from QE

@ShimShtein ShimShtein merged commit d40391d into theforeman:master Oct 31, 2019
@ShimShtein
Copy link
Member

Merged. 🎉
Awesome work, @apuntamb and @vijay8451!

Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants