Create new Tenant from Rails Console/Automation

automate

#1

Hi everyone,

I am currently working on service request that will allow creating new tenants in CloudForms/ManageIQ. I am looking for a way to create tenant via Automation module.

I have tried following options:

tenant = $evm.vmdb(:tenant).new(:name => “Tenant A”)

or

tenant = MiqAeMethodService::MiqAeServiceTenant.new(:name => “Tenant A”)

But it always fails with following error:

ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: missing FROM-clause entry for table “id”
LINE 1: SELECT “tenants”.* FROM “tenants” WHERE “id”.“name” = $1 LI…

I have also tried to use create method, but it seems that it does not exist.

In other words, is it even possible to create manageiq objects via automation?
If not, could this be done with REST API?

Thanks in advance.


#2

Hello,
Appearently you can’t do it via the Automate ServiceModel ( aka MiqAeMethodService::MiqAeServiceTenant ). At least I did not find something obvious

To be on the save side I would create the tenant by calling the REST Endpoint /api/tenants this should do enough validation to not break something. And since you are dealing with user inputs, you want to have all the safety nets you can get :slight_smile:

Regarding other manageiq objects: You can absolutely create them via Automate.

svc = MiqAeMethodService::MiqAeServiceService.create(name: 'Service 2', description: 'lorem ipsum', miq_group: 1)
svc.display = true      # This is important, since it will be invisible by default

However if you want to you can bypass the Automation Service Model and use the rails Tenant-class directly. I was able to create a Tenant with the following piece of code:

  parent_tenant = Tenant.where(name: "<parent Tenant>").first()
  new_tenant = Tenant.create!(name: "My New Tenant", parent: parent_tenant)

#3

Hi @buc,

Thank you for detailed answer.
I’ve tried to create new tenant from rails console and it works fine.
But you are right, it’s not a good way to do it.

Here is an example using rest_client if anybody will need the same:

require ‘rest_client’

hostname = “”
username = “”
password = “”
url = “https://#{hostname}/api/tenants”

tenant_name = “<tenant_name>”
tenant_desc = “<tenant_desc>”
parent_id = “<parent_id>”

body = {
:action => “create”,
:resource => {
:name => tenant_name,
:description => tenant_desc,
:parent => {:id => parent_id}
}
}

params = {
:method => ‘post’,
:url => url,
:verify_ssl => false,
:user => username,
:password => password,
:headers => {
:content_type => “application/json”,
:accept => “application/json”
},
:payload => body.to_json
}

begin
response = RestClient::Request.new(params).execute
rescue => e
$evm.log(:error, “#{e}, #{e.message}”)
end