Post data to create new stack

Just came through the following question about how to create a stack using catalog api and I have tried what @vincent suggested but I am facing a problem with the POST step.

Note: I am using v2-beta api so I am doing the POST request against this URL: /v2-beta/projects/<projectID>/stacks and previously I tried the v1 api /v1/projects/<projectID>/environments which have the same result.

The data i sent:

 {'environment': {'public_port': 8035}, 'rancherCompose': u'.catalog:\n  name: "Wordpress"\n  version: "v0.1-educaas1"\n  description: "Blog tool, publishing platform and CMS"\n  uuid: Wordpress-0\n  minimum_rancher_version: v0.51.0\n  questions:    \n    - variable: public_port\n      description: "public port to access the wordpress site"\n      label: "Public Port"\n      required: true\n      default: "80"\n      type: "int"\n\n\nwordpress:\n', 'name': 'API tester', 'dockerCompose': u'wordpress:\n  image: wordpress\n  links:\n    - db:mysql\n  ports:\n    - ${public_port}:80\n\ndb:\n  image: mariadb\n  environment:\n    MYSQL_ROOT_PASSWORD: example\n'}

And the response I got:

{"id":"da2a00ba-3ac3-447a-9581-c9920a6a0588",
 "type":"error","links":{},"actions: {},"status":422,"code":"InvalidFormat",
 "message":null,"detail":null,"fieldName":"environment","baseType":"error"}

The response is telling me that there is an error with the datatype of environment which I pass it as a dict after debugging a little I noticed that the Rancher API is seeing the value as string like this:

{
  "environment": "public_port",
  "name": "API tester",
  "rancherCompose": ".catalog:\n  name: \"Wordpress\"\n  version: \"v0.1-educaas1\"\n  description: \"Blog tool, publishing platform and CMS\"\n  uuid: Wordpress-0\n  minimum_rancher_version: v0.51.0\n  questions:    \n    - variable: public_port\n      description: \"public port to access the wordpress site\"\n      label: \"Public Port\"\n      required: true\n      default: \"80\"\n      type: \"int\"\n\n\nwordpress:\n",
  "dockerCompose": "wordpress:\n  image: wordpress\n  links:\n    - db:mysql\n  ports:\n    - ${public_port}:80\n\ndb:\n  image: mariadb\n  environment:\n    MYSQL_ROOT_PASSWORD: example\n"
}

I don’t know why the value changed even if i printed the payload before passing it to the POST request to make sure that I am passing the correct format.
Rancher version is 1.2.1

1 Like

I don’t know what you’re using to serialize the JSON object but seems like it doesn’t support nested objects or something… The audit log is the raw body that was received so it must be getting sent wrong.

2 Likes

Thanks for pointing me in the right direction ! I made it work I was serializing the JSON in a wrong way.

I have added startOnCreate in order to start the service immediately. Is there any required value should I pass it other than that ?

I got this error while creating a stack.

I tried:

curl -u “key:key” -X POST -d ‘{“name”:“HGDGGG”}’ ‘http://server_ip:8080/v2-beta/stack’

Response:

{“id”:“7e795f30-f5f5-4f3d-add0-109e970403f6”,“type”:“error”,“links”:{},“actions”:{},“status”:405,“code”:“Method not allowed”,“message”:“Method not allowed”,“detail”:null,“baseType”:“error”}

What is the issue here and how to resolve it??

@discobot @vincent

Stacks belong to an environment (project), you are using a user API key which has access to many projects so you need to post to /v2-beta/projects/your_project_id/stacks.

1 Like