theforeman / foreman_azure_rm

Adds Azure Resource Manager as a compute resource for The Foreman
GNU General Public License v3.0
9 stars 24 forks source link

Can't create host in resource group from gallery image which belongs to another resource group #160

Open milanzelenka opened 2 years ago

milanzelenka commented 2 years ago

Hi,

We have problem when creating VM in resource group from image gallery in another resource group.

Steps to reproduce:

  1. create new compute resource
  2. create image in compute resource Image UUID: gallery://PUBX-RHEL-8 (which is created in azure resource group rg-azeun-X)
  3. create VM in resource group rg-azeun-X (which is the same as resource group of image gallery) => everything works fine and VM is created.
  4. createVM is in another resource group than resource group of image gallery => VM creation failed with error: "Failed to create a compute XY (Azure Resource Manager) instance app01-test-rhel8-d.azure.pubx: \"message\": \"MsRestAzure::AzureOperationError: InvalidParameter: The value of parameter imageReference.publisher is invalid."

IMHO problem is that image_reference is fetched from VM resource group name and not from original resource group name which is image belongs to.

Changing of rg_name to fixed string with original resource group name 'rg-azeun-X' in file managed_vm.rb on line 87 (in function define_image()) helps and VM is created successfully.

https://github.com/apuntamb/foreman_azure_rm/blob/6e6d4ec4d578bf637af913404fc15aa8a4e348dd/app/models/concerns/foreman_azure_rm/vm_extensions/managed_vm.rb#L87

           image_reference.id = custom_image.id
         when 'gallery'
           image_reference = ComputeModels::ImageReference.new
-          image_reference.id = sdk.fetch_gallery_image_id(rg_name, image_id)
+          image_reference.id = sdk.fetch_gallery_image_id('rg-azeun-X', image_id)
         else
           image_reference = nil
         end
evanlaar commented 1 year ago

Hi,

we're also running into this issue. The same issue also exists with custom:// images.

Maybe the image id should be stored in the database when defining it, instead of trying to rebuild it every time the image is used by foreman ?

Furthermore Azure compute galleries (gallery:// images) can optionally be shared across tenants and subscriptions and can even be made public (Currently in preview; see here) so I think storing the image id in the database might be the only solution ?

chris1984 commented 1 year ago

Yes, that is a good plan forward, I can make an issue on our sprint board and start to work on it.