Search code examples
cloudcontrol

How to add a config value / environment variable to a cloudControl deployment using the API


I would like to add an environment variable, also called config in this context, to my cloudControl deployment. However, I can't use the provided command-line tool cctrlapp, but have to use the REST API. Their API can be accessed at https://api.cloudcontrol.com and is documented at https://api.cloudcontrol.com/doc/.

My problem now is that the section of the documentation that refers to the environment variables is empty!

Here is what I have done so far:

  1. Application of the type nodejs created
  2. Default deployment created
  3. Tried to add the env. variable

I figured out that I would have to send a POST request to https://api.cloudcontrol.com/app/{APP_NAME}/deployment/{DEPLOYMENT_NAME}/addon. According to the implementation of cctrlapp (see app.py and cclib.py) I would infer the following JSON data is required:

{"addon":"config.free","options":{"key":"value"}}

Now I'm stuck. In this case, the request returns:

Bad Request {"parameters": "Invalid parameters given."}

If I use a slightly modified object

{"addon":"config.free","settings":{"key":"value"}}

I receive this message:

[ERROR] Missing config variables

Does anyone know how to apply such a key-value pair using only the API?


Solution

  • From looking at https://github.com/cloudControl/pycclib/blob/master/pycclib/cclib.py#L422, I think you do need to use "options" and the value of options should be a json encoded dictionary (https://github.com/cloudControl/cctrl/blob/master/cctrl/addonoptionhelpers.py#L82).

    The remaining parts are not json encoded however, but are just normal POST parameters (https://github.com/cloudControl/pycclib/blob/master/pycclib/cclib.py#L931). A working request for reference (equivalent to config.add x=1):

    POST /app/myappname/deployment/default/addon/ HTTP/1.1
    content-length: 50
    accept-encoding: compress, gzip
    user-agent: pycclib/1.5.5
    host: api.cloudcontrolled.com
    content-type: application/x-www-form-urlencoded
    authorization: cc_auth_token="thetoken"
    
    options=%7B%22x%22%3A+%221%22%7D&addon=config.free