From 306cbd833dcdef8528038e028c0e27074c80095e Mon Sep 17 00:00:00 2001
From: Chris Houseknecht <chouseknecht@ansible.com>
Date: Wed, 27 Apr 2016 17:50:03 -0400
Subject: [PATCH] Adding new module azure_rm_resourcegroup_facts (#3487)

---
 .../azure/azure_rm_resourcegroup_facts.py     | 173 ++++++++++++++++++
 1 file changed, 173 insertions(+)
 create mode 100644 lib/ansible/modules/cloud/azure/azure_rm_resourcegroup_facts.py

diff --git a/lib/ansible/modules/cloud/azure/azure_rm_resourcegroup_facts.py b/lib/ansible/modules/cloud/azure/azure_rm_resourcegroup_facts.py
new file mode 100644
index 00000000000..8bd6b8b375e
--- /dev/null
+++ b/lib/ansible/modules/cloud/azure/azure_rm_resourcegroup_facts.py
@@ -0,0 +1,173 @@
+#!/usr/bin/python
+#
+# Copyright (c) 2016 Matt Davis, <mdavis@ansible.com>
+#                    Chris Houseknecht, <house@redhat.com>
+#
+# This file is part of Ansible
+#
+# Ansible is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Ansible is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+DOCUMENTATION = '''
+---
+module: azure_rm_resouregroup_facts
+
+version_added: "2.1"
+
+short_description: Get resource group facts.
+
+description:
+    - Get facts for a specific resource group or all resource groups.
+
+options:
+    name:
+        description:
+            - Limit results to a specific resource group.
+        required: false
+        default: null
+    tags:
+        description:
+            - Limit results by providing a list of tags. Format tags as 'key' or 'key:value'.
+        required: false
+        default: null
+
+extends_documentation_fragment:
+    - azure
+
+author:
+    - "Chris Houseknecht (@chouseknecht)"
+    - "Matt Davis (@nitzmahone)"
+
+'''
+
+EXAMPLES = '''
+    - name: Get facts for one resource group
+      azure_rm_resourcegroup_facts:
+        name: Testing
+
+    - name: Get facts for all resource groups
+      azure_rm_securitygroup_facts:
+
+    - name: Get facts by tags
+      azure_rm_resourcegroup_facts:
+        tags:
+          - testing
+          - foo:bar
+'''
+RETURN = '''
+changed:
+    description: Whether or not the object was changed.
+    returned: always
+    type: bool
+    sample: False
+objects:
+    description: List containing a set of facts for each selected object.
+    returned: always
+    type: list
+    sample: [{
+        "id": "/subscriptions/XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX/resourceGroups/Testing",
+        "location": "westus",
+        "name": "Testing",
+        "properties": {
+            "provisioningState": "Succeeded"
+        },
+        "tags": {
+            "delete": "never",
+            "testing": "testing"
+        }
+    }]
+'''
+
+from ansible.module_utils.basic import *
+from ansible.module_utils.azure_rm_common import *
+
+try:
+    from msrestazure.azure_exceptions import CloudError
+    from azure.common import AzureMissingResourceHttpError, AzureHttpError
+except:
+    # This is handled in azure_rm_common
+    pass
+
+
+AZURE_OBJECT_CLASS = 'ResourceGroup'
+
+
+class AzureRMResourceGroupFacts(AzureRMModuleBase):
+
+    def __init__(self):
+
+        self.module_arg_spec = dict(
+            name=dict(type='str'),
+            tags=dict(type='list')
+        )
+
+        self.results = dict(
+            changed=False,
+            results=[]
+        )
+
+        self.name = None
+        self.tags = None
+
+        super(AzureRMResourceGroupFacts, self).__init__(self.module_arg_spec,
+                                                        supports_tags=False,
+                                                        facts_module=True)
+
+    def exec_module(self, **kwargs):
+
+        for key in self.module_arg_spec:
+            setattr(self, key, kwargs[key])
+
+        if self.name:
+            self.results['objects'] = self.get_item()
+        else:
+            self.results['objects'] = self.list_items()
+
+        return self.results
+
+    def get_item(self):
+        self.log('Get properties for {0}'.format(self.name))
+        item = None
+        result = []
+
+        try:
+            item = self.rm_client.resource_groups.get(self.name)
+        except CloudError:
+            pass
+
+        if item and self.has_tags(item.tags, self.tags):
+            result = [self.serialize_obj(item, AZURE_OBJECT_CLASS)]
+
+        return result
+
+    def list_items(self):
+        self.log('List all items')
+        try:
+            response = self.rm_client.resource_groups.list()
+        except AzureHttpError as exc:
+            self.fail("Failed to list all items - {1}".format(str(exc)))
+
+        results = []
+        for item in response:
+            if self.has_tags(item.tags, self.tags):
+                results.append(self.serialize_obj(item, AZURE_OBJECT_CLASS))
+        return results
+
+
+def main():
+    AzureRMResourceGroupFacts()
+
+if __name__ == '__main__':
+    main()
+