Create Amazon Service Template via REST API

gaprindashvili

#1

I’ve been trying to create a service template that deploys a discovered AWS EC2 AMI via the REST API. I’m having no luck and I’ve been trying for days. I’d really appreciate any help on this.

I’m using the following URL https://miq/api/service_templates (POST) and supplying it:

{
“name”: “My Atomic Service Template2”,
“service_type”: “atomic”,
“prov_type”: “amazon”,
“display”: false,
“config_info”: {
“service_template_request”: true,
“provision_type”: [“amazon”, “Amazon”],
“customize_enabled”: [“enabled”],
“miq_request_dialog_name”: “miq_provision_amazon_dialogs_template”,
“catalog_id”: “3”,
“st_prov_type”: “amazon”,
“service_type”: “atomic”,
“dialog_id”: “2”,
“placement_auto”: [true, 1],
“number_of_vms”: [1, “1”],
“src_vm_id”: [25842, “Windows 10 RS2 ENT en_us (x64)”],
“vm_name”: “test_rest_aRZlFIJa3ZAAA”,
“schedule_type”: [“immediately”, “Immediately on Approval”],
“src_ems_id”: [2, “AWS (Ireland)”],
“provision”: {
“fqname”: “/Service/Provisioning/StateMachines/ServiceProvision_Template/CatalogItemInitialization”,
“dialog_id”: “4”
},
“retirement”: {
“fqname”: “/Service/Retirement/StateMachines/ServiceRetirement/Default”,
“dialog_id”: “4”
}
}
}

I get the following error though:

“error”: {
“kind”: “bad_request”,
“message”: “Could not create Service Template - undefined method ``base_class’ for FalseClass:Class”,
“klass”: “Api::BadRequestError”
}
}

I’ve read and tired various examples:

Nothing seems to work. The interesting thing is that if I omit the “src_vm_id” I get a created service template in the UI, but the “Request” tabs are not visible and the UI will not allow me to edit the service template.

Thanks
James.


#2

It seems that display should be a string-type.
"display": "false"

At least according to this API example here


#3

Hi @buc - Thanks. I changed the type to a string and still get the error. I wondering if this is bug?
I’m running gaprindashvili-6 as an AWS AMI appliance.


#4

I’ve still not had any luck with this. Is anyone able to help? Thanks.


#5

Sorry, I couldn’t get my hands on a non-production API yesterday.
Are you sure your JSON is valid? Because it works for me, with the exact Request you posted. The API Request returns OK. I tested it with Gaprindashvili-6 and Hammer-RC1 (both run as docker container)

  • However my service_templates are not editable in the UI (probably because I don’t have a AWS provider, nor dialogs, nor anything else referenced by the service_template).
  • I do get the Request-Info-Tab, however it says “Request is missing for selected item”.
Request

curl -k --user admin:smartvm https://localhost:8443/api/service_templates -XPOST --data @invalid.json
cat invalid.json

    {
  "name": "My Atomic Service Template",
  "service_type": "atomic",
  "prov_type": "amazon",
  "display": false,
  "config_info": {
    "service_template_request": true,
    "provision_type": [
      "amazon",
      "Amazon"
    ],
    "customize_enabled": [
      "enabled"
    ],
    "miq_request_dialog_name": "miq_provision_amazon_dialogs_template",
    "catalog_id": "3",
    "st_prov_type": "amazon",
    "service_type": "atomic",
    "dialog_id": "2",
    "placement_auto": [
      true,
      1
    ],
    "number_of_vms": [
      1,
      "1"
    ],
    "src_vm_id": [
      25842,
      "Windows 10 RS2 ENT en_us (x64)"
    ],
    "vm_name": "test_rest_aRZlFIJa3ZAAA",
    "schedule_type": [
      "immediately",
      "Immediately on Approval"
    ],
    "src_ems_id": [
      2,
      "AWS (Ireland)"
    ],
    "provision": {
      "fqname": "/Service/Provisioning/StateMachines/ServiceProvision_Template/CatalogItemInitialization",
      "dialog_id": "4"
    },
    "retirement": {
      "fqname": "/Service/Retirement/StateMachines/ServiceRetirement/Default",
      "dialog_id": "4"
    }
  }
}

Have you tried creating the service_template in the UI , to see if it differs from the data you are sending?

I know we ran into this problem with the service_template not being editable in the UI and the missing request, when importing catalog items with the rhtconsulting rake-tasks. However I think updating existing service_templates work.
Can you work around the problem by manually creating a simple service_template and updating that via the API?


#6

Hi @buc

Thanks for getting back to me again. Much appreciated.

I’ve checked that the JSON is valid and have done quite extensive testing of the scenarios.

  1. Creating a new service_template from the API, specifying a real amazon template (which was discovered from the amazon provider) returns an error.
  2. Creating a new service_template from the API, specifying a non-real template (i.e. a random id that does not exist as a template) gets created OK, but is not editable from the UI and does not have a default template to provision when its requested from the catalog.
  3. Updating a top level field (e.g. description) of an existing service_template (which was created from the UI) from the API, works OK.
    4 Updating a ‘config_info’ field (e.g. scr_vm_id) of an existing service_template (which was created from the UI) from the API, returns an error.

I’ve since discovered that the default template is actually a ‘special’ provision_request, which is left in a ‘pending_approval’ state with the type of ‘MiqProvisionRequestTemplate’. By doing some ugly matching of names, I can find the corresponding provision_request and successfully update the ‘scr_vm_id’ field with a new template, and it updates the linked service_template.

What I still can’t do though is, create a new service_template with an associated ‘special’ provision_request from the API.

I’ve got a lot of templates to create (over 100) as service_templates and I would really like the whole process automated.

Thanks.