Repository: ansible/ansible

Issue:
cloudfront_distribution module throws error on geolocation update
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
cloudfront_distribution

##### ANSIBLE VERSION
Ansible: 2.4.3.0

##### CONFIGURATION


##### OS / ENVIRONMENT
 alpine:3.7

##### SUMMARY
We have copied cloudfront_distribution and related module utils into library directory. 
When trying to update cloudfront with geolocation restriction it throws below error.

```
Request body:
            "restrictions": {
                "geo_restriction": {
                    "items": [
                        "US"
                    ],
                    "restriction_type": "whitelist"
                }
            },
            


Error updating distribution to {u'Comment': '', u'Restrictions': {'Items': ['US'], 'RestrictionType': 'whitelist', 'Quantity': 1}, u'Logging': {u'Bucke
....
Parameter validation failed:\nMissing required parameter in DistributionConfig.Restrictions: \"GeoRestriction\"\nUnknown parameter in DistributionConfig.Restrictions: \"Items\", must be one of: GeoRestriction\nUnknown parameter in DistributionConfig.Restrictions: \"RestrictionType\", must be one of: GeoRestriction\nUnknown parameter in DistributionConfig.Restrictions: \"Quantity\", must be one of: GeoRestriction
```
##### STEPS TO REPRODUCE
<!---
For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used.
-->

<!--- Paste example playbooks or commands between quotes below -->
```yaml
- name: Add restriction geo location
  cloudfront_distribution:
    state: present
    distribution_id: "{{ cloudfront_distribution_id }}"
    restrictions:
      geo_restriction:
        restriction_type: "whitelist"
        items:
          - "US"
```



Discussion:
Files identified in the description:
* [lib/ansible/modules/cloud/amazon/cloudfront_distribution.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/cloud/amazon/cloudfront_distribution.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @willthames @wilvk
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/lib/ansible/modules/cloud/amazon/cloudfront_distribution.py b/lib/ansible/modules/cloud/amazon/cloudfront_distribution.py
--- a/lib/ansible/modules/cloud/amazon/cloudfront_distribution.py
+++ b/lib/ansible/modules/cloud/amazon/cloudfront_distribution.py
@@ -1689,7 +1689,7 @@ def validate_restrictions(self, config, restrictions, purge_restrictions=False):
                                               rest not in geo_restriction_items])
             valid_restrictions = ansible_list_to_cloudfront_list(geo_restriction_items)
             valid_restrictions['restriction_type'] = geo_restriction.get('restriction_type')
-            return valid_restrictions
+            return {'geo_restriction': valid_restrictions}
         except Exception as e:
             self.module.fail_json_aws(e, msg="Error validating restrictions")
 


Test patch:
diff --git a/test/integration/targets/cloudfront_distribution/tasks/main.yml b/test/integration/targets/cloudfront_distribution/tasks/main.yml
--- a/test/integration/targets/cloudfront_distribution/tasks/main.yml
+++ b/test/integration/targets/cloudfront_distribution/tasks/main.yml
@@ -53,6 +53,23 @@
       that:
         - update_origin_http_port.changed
 
+  - name: update restrictions
+    cloudfront_distribution:
+      alias: "{{ cloudfront_alias }}"
+      restrictions:
+        geo_restriction:
+          restriction_type: "whitelist"
+          items:
+            - "US"
+      state: present
+      <<: *aws_connection_info
+    register: update_restrictions
+
+  - name: ensure restrictions was updated
+    assert:
+      that:
+        - update_restrictions.changed
+
   - name: set a random comment
     set_fact:
       comment: "{{'ABCDEFabcdef123456'|shuffle|join }}"