Bug 1053471

Summary: Fail to parse the env vars of an array of hashes when setting multiple env vars in one API call using SET_UNSET_ENVIRONMENT_VARIABLES, ADD_CARTRIDGE, or ADD_APPLICATION APIs
Product: OpenShift Online Reporter: Zhe Wang <zhewang>
Component: MasterAssignee: Lili Nader <lnader>
Status: CLOSED NOTABUG QA Contact: libra bugs <libra-bugs>
Severity: medium Docs Contact:
Priority: medium    
Version: 2.xCC: ccoleman, mfisher, xtian
Target Milestone: ---   
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2014-01-15 19:43:48 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Zhe Wang 2014-01-15 10:15:32 UTC
Description of problem:
When invoking SET_UNSET_ENVIRONMENT_VARIABLES API to operate on a batch of user-defined env vars, environment_variables_controller fails to parse the params of YAML format, which is officially documented in the description of the API:

{
                        "default_value": null, 
                        "description": "Add/Update/Delete application environment variables, e.g. Add/Update: [{'name':'FOO', 'value':'123'}, {'name':'BAR', 'value':'abc'}], {Delete: [{'name':'FOO'}, {'name':'BAR'}]}", 
                        "name": "environment_variables", 
                        "type": "array", 
                        "valid_options": []
                    }

Version-Release number of selected component (if applicable):
devenv_4224

How reproducible:
always

Steps to Reproduce:
1. create an app
2. look up its SET_UNSET_ENVIRONMENT_VARIABLES API
3. add some env vars with the API using the example given in its description
curl -k -u user:password https://<BROKER_DNS>/broker/rest/application/52d64852afb999beeb000007/environment-variables -XPATCH -d environment_variables="[{'name':'FOO', 'value':'123'}, {'name':'BAR', 'value':'abc'}]" | python -m json.tool

Actual results:
When adding the BAR and FOO env vars in the example, it fails with the error below:

"api_version": 1.6, 
    "data": null, 
    "messages": [
        {
            "exit_code": 186, 
            "field": "environment_variables", 
            "index": null, 
            "severity": "error", 
            "text": "Invalid environment variables: expected array of hashes"
        }
    ], 

Expected results:
Operating on a batch of env vars with SET_UNSET_ENVIRONMENT_VARIABLES API should be successful, with the param format given in the example. Otherwise, we need to update the documentation of this API.

Additional info:

Comment 1 Zhe Wang 2014-01-15 10:38:51 UTC
Similar problem also exists in the following APIs:

ADD_APPLICATION (with multiple env vars)
e.g., curl -k -u  user:password https://<BROKER_DNS>/broker/rest/domain/stg621/applications -XPOST -d name=py273 -d cartridges[][name]=python-2.7 -d environment_variables=[{'name':'FOO', 'value':'123'}, {'name':'BAR', 'value':'abc'}] | python -m json.tool

ADD_CARTRIDGE (with multiple env vars):
e.g., curl -k -u  user:password https://<BROKER_DNS>/broker/rest/application/52d635f96993b9bb88000007/cartridges -d name=mongodb-2.2 -d environment_variables="[{'name':'FOO', 'value':'123'}, {'name':'BAR', 'value':'abc'}]" | python -m json.tool

Comment 2 Zhe Wang 2014-01-15 13:49:37 UTC
I also tried with the following format, but none of these succeeded. Maybe we need to change the type of environment_variables to string, or change the description somehow.


1. without white spaces in the value of environment_variables

-d environment_variables=[{'name':'BAR','value':'123'},{'name':'FOO','value':'abc'}] 

curl -k -u user:password 
https://<BROKER>/broker/rest/application/52d68ddd819d378008000007/environment-variables -d environment_variables=[{'name':'BAR', 'value':'123'},{'name':'FOO', 'value':'abc'}] -XPATCH | python -m json.tool

2. -d environment_variables[]=[{'name':'BAR', 'value':'123'},{'name':'FOO', 'value':'abc'}]

3. -d environment_variables[0]={'name':'BAR', 'value':'123'} 

4. using escaping chars
-d environment_variables=\[\{\'name\'\:\'BAR\'\,\'value\'\:\'123\'\}\,\{\'name\'\:\'FOO\'\,\'value\'\:\'abc\'\}\]

5. using a json obj:
{'environment_variables': [{'name':'BAR','value':'123'},{'name':'FOO','value':'abc'}]}

Comment 3 Clayton Coleman 2014-01-15 19:43:48 UTC
You have to post as valid JSON

curl -X PATCH -H "Content-Type: application/json" -d "{'environment_variables': [{'name':'BAR','value':'123'},{'name':'FOO','value':'abc'}]}"

That works for me.

-d environment_variables=[...] is not a valid JSON document, you'd have to be using:

environment_variables[][name]=a&environment_variables[][value]=b&....