Repository: ansible/ansible-modules-extras

Issue:
hipchat: module fails when API return 204 HTTP status code
<!--- Verify first that your issue/request is not already reported in GitHub -->
##### ISSUE TYPE

<!--- Pick one below and delete the rest: -->
- Bug Report
##### COMPONENT NAME

<!--- Name of the plugin/module/task -->

Hipchat
##### ANSIBLE VERSION

```
ansible 2.1.0.0 (stable-2.1 c7cae3b08c) last updated 2016/05/02 11:16:23 (GMT +000)
  lib/ansible/modules/core: (detached HEAD babb97e473) last updated 2016/05/02 11:16:30 (GMT +000)
  lib/ansible/modules/extras: (detached HEAD 1d180c7707) last updated 2016/05/02 11:16:34 (GMT +000)
```
##### CONFIGURATION

<!---
Mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->
##### OS / ENVIRONMENT

<!---
Mention the OS you are running Ansible from, and the OS you are
managing, or say “N/A” for anything that is not platform-specific.
-->
##### SUMMARY

Module fails when Hipchat REST API v2 returns a non `200` HTTP status code
##### STEPS TO REPRODUCE

<!---
For bugs, show exactly how to reproduce the problem.
For new features, show how the feature would be used.
-->

```
- name: Notify hipchat
  hipchat:
    api: "https://api.hipchat.com/v2"
    color: gray
    room: "{{ item.room }}"
    token: "{{ item.token }}"
    msg: "hello world"
    msg_format: html
  with_items: "{{ hipchat }}"
```

<!--- You can also paste gist.github.com links for larger files -->
##### EXPECTED RESULTS

<!--- What did you expect to happen when running the steps above? -->

A successful return from module when Hipchat REST API v2 returns a 2xx status code
##### ACTUAL RESULTS

<!--- What actually happened? If possible run with high verbosity (-vvvv) -->

```
failed: [xxx.xxx.xxx.xxx] (item={u'token': u'OAUTH_TOKEN', u'room': u'Bot chat'}) => {"failed": true, "invocation": {"module_args": {"api": "https://api.hipchat.com/v2", "color": "gray", "msg": "hello world", "msg_format": "html", "msg_from": "Ansible", "notify": true, "room": "Bot chat", "token": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "validate_certs": true}, "module_name": "hipchat"}, "item": {"room": "Bot chat", "token": "OAUTH_TOKEN"}, "msg": "failed to send message, return status=204"}
```

hipchat: module fails when API return 204 HTTP status code
<!--- Verify first that your issue/request is not already reported in GitHub -->
##### ISSUE TYPE

<!--- Pick one below and delete the rest: -->
- Bug Report
##### COMPONENT NAME

<!--- Name of the plugin/module/task -->

Hipchat
##### ANSIBLE VERSION

```
ansible 2.1.0.0 (stable-2.1 c7cae3b08c) last updated 2016/05/02 11:16:23 (GMT +000)
  lib/ansible/modules/core: (detached HEAD babb97e473) last updated 2016/05/02 11:16:30 (GMT +000)
  lib/ansible/modules/extras: (detached HEAD 1d180c7707) last updated 2016/05/02 11:16:34 (GMT +000)
```
##### CONFIGURATION

<!---
Mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->
##### OS / ENVIRONMENT

<!---
Mention the OS you are running Ansible from, and the OS you are
managing, or say “N/A” for anything that is not platform-specific.
-->
##### SUMMARY

Module fails when Hipchat REST API v2 returns a non `200` HTTP status code
##### STEPS TO REPRODUCE

<!---
For bugs, show exactly how to reproduce the problem.
For new features, show how the feature would be used.
-->

```
- name: Notify hipchat
  hipchat:
    api: "https://api.hipchat.com/v2"
    color: gray
    room: "{{ item.room }}"
    token: "{{ item.token }}"
    msg: "hello world"
    msg_format: html
  with_items: "{{ hipchat }}"
```

<!--- You can also paste gist.github.com links for larger files -->
##### EXPECTED RESULTS

<!--- What did you expect to happen when running the steps above? -->

A successful return from module when Hipchat REST API v2 returns a 2xx status code
##### ACTUAL RESULTS

<!--- What actually happened? If possible run with high verbosity (-vvvv) -->

```
failed: [xxx.xxx.xxx.xxx] (item={u'token': u'OAUTH_TOKEN', u'room': u'Bot chat'}) => {"failed": true, "invocation": {"module_args": {"api": "https://api.hipchat.com/v2", "color": "gray", "msg": "hello world", "msg_format": "html", "msg_from": "Ansible", "notify": true, "room": "Bot chat", "token": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "validate_certs": true}, "module_name": "hipchat"}, "item": {"room": "Bot chat", "token": "OAUTH_TOKEN"}, "msg": "failed to send message, return status=204"}
```



Discussion:
Yup, looks like the module only considers a 200 a success.
https://github.com/ansible/ansible-modules-extras/blob/devel/notification/hipchat.py#L157

https://www.hipchat.com/docs/apiv2/method/send_room_notification does show 204 as the expected return code  (Even though 204 is missing from https://developer.atlassian.com/hipchat/guide/hipchat-rest-api/api-response-codes at the moment)

Yup, looks like the module only considers a 200 a success.
https://github.com/ansible/ansible-modules-extras/blob/devel/notification/hipchat.py#L157

https://www.hipchat.com/docs/apiv2/method/send_room_notification does show 204 as the expected return code  (Even though 204 is missing from https://developer.atlassian.com/hipchat/guide/hipchat-rest-api/api-response-codes at the moment)


Resolving patch:
diff --git a/notification/hipchat.py b/notification/hipchat.py
--- a/notification/hipchat.py
+++ b/notification/hipchat.py
@@ -155,7 +155,10 @@ def send_msg_v2(module, token, room, msg_from, msg, msg_format='text',
         module.exit_json(changed=False)
 
     response, info = fetch_url(module, url, data=data, headers=headers, method='POST')
-    if info['status'] == 200:
+
+    # https://www.hipchat.com/docs/apiv2/method/send_room_notification shows
+    # 204 to be the expected result code.
+    if info['status'] in [200, 204]:
         return response.read()
     else:
         module.fail_json(msg="failed to send message, return status=%s" % str(info['status']))


Test patch: