azure_rm_aks_facts.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/usr/bin/python
  2. #
  3. # Copyright (c) 2018 Yuwei Zhou, <yuwzho@microsoft.com>
  4. #
  5. # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
  6. from __future__ import absolute_import, division, print_function
  7. __metaclass__ = type
  8. ANSIBLE_METADATA = {'metadata_version': '1.1',
  9. 'status': ['preview'],
  10. 'supported_by': 'community'}
  11. DOCUMENTATION = '''
  12. ---
  13. module: azure_rm_aks_facts
  14. version_added: "2.6"
  15. short_description: Get Azure Kubernetes Service facts.
  16. description:
  17. - Get facts for a specific Azure Kubernetes Service or all Azure Kubernetes Services.
  18. options:
  19. name:
  20. description:
  21. - Limit results to a specific resource group.
  22. resource_group:
  23. description:
  24. - The resource group to search for the desired Azure Kubernetes Service
  25. tags:
  26. description:
  27. - Limit results by providing a list of tags. Format tags as 'key' or 'key:value'.
  28. extends_documentation_fragment:
  29. - azure
  30. author:
  31. - "Yuwei Zhou (@yuwzho)"
  32. '''
  33. EXAMPLES = '''
  34. - name: Get facts for one Azure Kubernetes Service
  35. azure_rm_aks_facts:
  36. name: Testing
  37. resource_group: TestRG
  38. - name: Get facts for all Azure Kubernetes Services
  39. azure_rm_aks_facts:
  40. - name: Get facts by tags
  41. azure_rm_aks_facts:
  42. tags:
  43. - testing
  44. '''
  45. RETURN = '''
  46. azure_aks:
  47. description: List of Azure Kubernetes Service dicts.
  48. returned: always
  49. type: list
  50. '''
  51. from ansible.module_utils.azure_rm_common import AzureRMModuleBase
  52. try:
  53. from msrestazure.azure_exceptions import CloudError
  54. from azure.common import AzureHttpError
  55. except:
  56. # handled in azure_rm_common
  57. pass
  58. AZURE_OBJECT_CLASS = 'managedClusters'
  59. class AzureRMManagedClusterFacts(AzureRMModuleBase):
  60. """Utility class to get Azure Kubernetes Service facts"""
  61. def __init__(self):
  62. self.module_args = dict(
  63. name=dict(type='str'),
  64. resource_group=dict(type='str'),
  65. tags=dict(type='list')
  66. )
  67. self.results = dict(
  68. changed=False,
  69. aks=[]
  70. )
  71. self.name = None
  72. self.resource_group = None
  73. self.tags = None
  74. super(AzureRMManagedClusterFacts, self).__init__(
  75. derived_arg_spec=self.module_args,
  76. supports_tags=False,
  77. facts_module=True
  78. )
  79. def exec_module(self, **kwargs):
  80. for key in self.module_args:
  81. setattr(self, key, kwargs[key])
  82. self.results['aks'] = (
  83. self.get_item() if self.name
  84. else self.list_items()
  85. )
  86. return self.results
  87. def get_item(self):
  88. """Get a single Azure Kubernetes Service"""
  89. self.log('Get properties for {0}'.format(self.name))
  90. item = None
  91. result = []
  92. try:
  93. item = self.containerservice_client.managed_clusters.get(
  94. self.resource_group, self.name)
  95. except CloudError:
  96. pass
  97. if item and self.has_tags(item.tags, self.tags):
  98. result = [self.serialize_obj(item, AZURE_OBJECT_CLASS)]
  99. return result
  100. def list_items(self):
  101. """Get all Azure Kubernetes Services"""
  102. self.log('List all Azure Kubernetes Services')
  103. try:
  104. response = self.containerservice_client.managed_clusters.list(
  105. self.resource_group)
  106. except AzureHttpError as exc:
  107. self.fail('Failed to list all items - {0}'.format(str(exc)))
  108. results = []
  109. for item in response:
  110. if self.has_tags(item.tags, self.tags):
  111. results.append(self.serialize_obj(item, AZURE_OBJECT_CLASS))
  112. return results
  113. def main():
  114. """Main module execution code path"""
  115. AzureRMManagedClusterFacts()
  116. if __name__ == '__main__':
  117. main()