| 1 |
|
| 2 |
|
| 3 |
) |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = ''' |
| 12 |
module: azure_rm_cognitivesearch_info |
| 13 |
version_added: "1.4.0" |
| 14 |
short_description: Get Azure Cognitive Search service info |
| 15 |
description: |
| 16 |
- Get info for a specific Azure Cognitive Search service or all Azure Cognitive Search service within a resource group. |
| 17 |
|
| 18 |
options: |
| 19 |
resource_group: |
| 20 |
description: |
| 21 |
- The name of the Azure resource group. |
| 22 |
type: str |
| 23 |
name: |
| 24 |
description: |
| 25 |
- The name of the Azure Cognitive Search service. |
| 26 |
type: str |
| 27 |
show_keys: |
| 28 |
description: |
| 29 |
- Retrieve admin and query keys. |
| 30 |
type: bool |
| 31 |
default: False |
| 32 |
|
| 33 |
extends_documentation_fragment: |
| 34 |
- azure.azcollection.azure |
| 35 |
|
| 36 |
author: |
| 37 |
- David Duque Hernández (@next-davidduquehernandez) |
| 38 |
''' |
| 39 |
|
| 40 |
EXAMPLES = ''' |
| 41 |
- name: Get Azure Cognitive Search info from resource group 'myResourceGroup' and name 'myAzureSearch' |
| 42 |
azure_rm_cognitivesearch_info: |
| 43 |
resource_group: myResourceGroup |
| 44 |
name: myAzureSearch |
| 45 |
|
| 46 |
- name: Get Azure Cognitive Search info from resource group 'myResourceGroup' |
| 47 |
azure_rm_cognitivesearch_info: |
| 48 |
resource_group: myResourceGroup |
| 49 |
|
| 50 |
- name: Get all Azure Cognitive Search info |
| 51 |
azure_rm_cognitivesearch_info: |
| 52 |
''' |
| 53 |
|
| 54 |
RETURN = ''' |
| 55 |
search: |
| 56 |
description: |
| 57 |
- Info for Azure Cognitive Search. |
| 58 |
returned: always |
| 59 |
type: list |
| 60 |
contains: |
| 61 |
hosting_mode: |
| 62 |
description: |
| 63 |
- Type of hosting mode selected. |
| 64 |
returned: always |
| 65 |
type: str |
| 66 |
sample: default |
| 67 |
id: |
| 68 |
description: |
| 69 |
- The unique identifier associated with this Azure Cognitive Search. |
| 70 |
returned: always |
| 71 |
type: str |
| 72 |
sample: /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName} |
| 73 |
identity: |
| 74 |
description: |
| 75 |
- The identity of the Azure Cognitive Search Service. |
| 76 |
returned: always |
| 77 |
type: dict |
| 78 |
contains: |
| 79 |
principal_id: |
| 80 |
description: |
| 81 |
- Identifier assigned. |
| 82 |
type: str |
| 83 |
sample: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 84 |
type: |
| 85 |
description: |
| 86 |
- Identity type. |
| 87 |
returned: always |
| 88 |
type: str |
| 89 |
sample: SystemAssigned |
| 90 |
sample: |
| 91 |
principal_id: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 92 |
type: SystemAssigned |
| 93 |
search_keys: |
| 94 |
description: |
| 95 |
- Admin and query keys for Azure Cognitive Search Service. |
| 96 |
type: dict |
| 97 |
contains: |
| 98 |
admin_primary: |
| 99 |
description: |
| 100 |
- Primary admin key for Azure Cognitive Search Service. |
| 101 |
type: str |
| 102 |
sample: 12345ABCDE67890FGHIJ123ABC456DEF |
| 103 |
admin_secondary: |
| 104 |
description: |
| 105 |
- Secondary admin key for Azure Cognitive Search Service. |
| 106 |
type: str |
| 107 |
sample: 12345ABCDE67890FGHIJ123ABC456DEF |
| 108 |
query: |
| 109 |
description: |
| 110 |
- List of query keys for Azure Cognitive Search Service. |
| 111 |
type: list |
| 112 |
sample: [{'key': '12345ABCDE67890FGHIJ123ABC456DEF', 'name': 'Query key'}] |
| 113 |
keys: |
| 114 |
description: |
| 115 |
- Admin and query keys for Azure Cognitive Search Service. |
| 116 |
- This return value has been deprecated and will be removed in a release after |
| 117 |
2027-05-01. Use C(search_keys) instead. |
| 118 |
type: dict |
| 119 |
contains: |
| 120 |
admin_primary: |
| 121 |
description: |
| 122 |
- Primary admin key for Azure Cognitive Search Service. |
| 123 |
type: str |
| 124 |
sample: 12345ABCDE67890FGHIJ123ABC456DEF |
| 125 |
admin_secondary: |
| 126 |
description: |
| 127 |
- Secondary admin key for Azure Cognitive Search Service. |
| 128 |
type: str |
| 129 |
sample: 12345ABCDE67890FGHIJ123ABC456DEF |
| 130 |
query: |
| 131 |
description: |
| 132 |
- List of query keys for Azure Cognitive Search Service. |
| 133 |
type: list |
| 134 |
sample: [{'key': '12345ABCDE67890FGHIJ123ABC456DEF', 'name': 'Query key'}] |
| 135 |
location: |
| 136 |
description: |
| 137 |
- The geo-location where the Azure Cognitive Search Service lives. |
| 138 |
returned: always |
| 139 |
type: str |
| 140 |
sample: West Europe |
| 141 |
name: |
| 142 |
description: |
| 143 |
- The name of the Azure Cognitive Search Service. |
| 144 |
returned: always |
| 145 |
type: str |
| 146 |
sample: myazuresearch |
| 147 |
network_rule_set: |
| 148 |
description: |
| 149 |
- Network specific rules that determine how the Azure Cognitive Search service may be reached. |
| 150 |
returned: always |
| 151 |
type: list |
| 152 |
sample: ['1.1.1.1', '8.8.8.8/31'] |
| 153 |
partition_count: |
| 154 |
description: |
| 155 |
- The number of partitions in the Azure Cognitive Search Service. |
| 156 |
returned: always |
| 157 |
type: int |
| 158 |
sample: 3 |
| 159 |
provisioning_state: |
| 160 |
description: |
| 161 |
- The state of the provisioning state of Azure Cognitive Search Service. |
| 162 |
returned: always |
| 163 |
type: str |
| 164 |
sample: succeeded |
| 165 |
public_network_access: |
| 166 |
description: |
| 167 |
- If it's allowed traffic over public interface. |
| 168 |
returned: always |
| 169 |
type: str |
| 170 |
sample: enabled |
| 171 |
replica_count: |
| 172 |
description: |
| 173 |
- The number of replicas in the Azure Cognitive Search Service. |
| 174 |
returned: always |
| 175 |
type: int |
| 176 |
sample: 3 |
| 177 |
sku: |
| 178 |
description: |
| 179 |
- The SKU of the Azure Cognitive Search Service. |
| 180 |
returned: always |
| 181 |
type: str |
| 182 |
sample: standard |
| 183 |
status: |
| 184 |
description: |
| 185 |
- The state of the Azure Cognitive Search. |
| 186 |
returned: always |
| 187 |
type: str |
| 188 |
sample: Active running |
| 189 |
tags: |
| 190 |
description: |
| 191 |
- The resource tags. |
| 192 |
returned: always |
| 193 |
type: dict |
| 194 |
sample: { "tag1":"abc" } |
| 195 |
''' |
| 196 |
|
| 197 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 198 |
|
| 199 |
try: |
| 200 |
from azure.core.exceptions import ResourceNotFoundError |
| 201 |
except ImportError: |
| 202 |
|
| 203 |
pass |
| 204 |
|
| 205 |
|
| 206 |
class AzureRMSearchInfo(AzureRMModuleBase): |
| 207 |
def __init__(self): |
| 208 |
|
| 209 |
self.module_arg_spec = dict( |
| 210 |
name=dict(type='str'), |
| 211 |
resource_group=dict(type='str'), |
| 212 |
show_keys=dict(type='bool', default=False) |
| 213 |
) |
| 214 |
|
| 215 |
self.results = dict( |
| 216 |
changed=False, |
| 217 |
search=[] |
| 218 |
) |
| 219 |
|
| 220 |
self.name = None |
| 221 |
self.resource_group = None |
| 222 |
self.show_keys = False |
| 223 |
|
| 224 |
super(AzureRMSearchInfo, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 225 |
supports_check_mode=True, |
| 226 |
supports_tags=False) |
| 227 |
|
| 228 |
def exec_module(self, **kwargs): |
| 229 |
for key in self.module_arg_spec: |
| 230 |
setattr(self, key, kwargs[key]) |
| 231 |
|
| 232 |
if self.show_keys: |
| 233 |
self.module.deprecate( |
| 234 |
"The 'keys' return key is deprecated. Use 'search_keys' instead.", |
| 235 |
date="2027-05-01", |
| 236 |
collection_name="azure.azcollection", |
| 237 |
) |
| 238 |
|
| 239 |
if self.name and not self.resource_group: |
| 240 |
self.fail("Parameter error: resource group required when filtering by name.") |
| 241 |
|
| 242 |
results = [] |
| 243 |
if self.name: |
| 244 |
results = self.get_search() |
| 245 |
elif self.resource_group: |
| 246 |
results = self.list_resource_group() |
| 247 |
else: |
| 248 |
results = self.list_all() |
| 249 |
|
| 250 |
self.results['search'] = results |
| 251 |
return self.results |
| 252 |
|
| 253 |
def get_search(self): |
| 254 |
self.log('Get properties for azure search {0}'.format(self.name)) |
| 255 |
search_obj = None |
| 256 |
account_dict = None |
| 257 |
|
| 258 |
try: |
| 259 |
search_obj = self.search_client.services.get(self.resource_group, self.name) |
| 260 |
except ResourceNotFoundError: |
| 261 |
pass |
| 262 |
|
| 263 |
if search_obj: |
| 264 |
account_dict = self.account_obj_to_dict(search_obj) |
| 265 |
|
| 266 |
return account_dict |
| 267 |
|
| 268 |
def list_resource_group(self): |
| 269 |
self.log('Get basic properties for azure search in resource group {0}'.format(self.resource_group)) |
| 270 |
search_obj = None |
| 271 |
results = list() |
| 272 |
|
| 273 |
try: |
| 274 |
search_obj = self.search_client.services.list_by_resource_group(self.resource_group) |
| 275 |
except Exception: |
| 276 |
pass |
| 277 |
|
| 278 |
if search_obj: |
| 279 |
for seach_item in search_obj: |
| 280 |
results.append(self.account_obj_to_dict(seach_item)) |
| 281 |
return results |
| 282 |
|
| 283 |
return list() |
| 284 |
|
| 285 |
def list_all(self): |
| 286 |
self.log('Get basic properties for all azure search') |
| 287 |
search_obj = None |
| 288 |
results = list() |
| 289 |
|
| 290 |
try: |
| 291 |
search_obj = self.search_client.services.list_by_subscription() |
| 292 |
except Exception: |
| 293 |
pass |
| 294 |
|
| 295 |
if search_obj: |
| 296 |
for search_item in search_obj: |
| 297 |
results.append(self.account_obj_to_dict(search_item)) |
| 298 |
return results |
| 299 |
|
| 300 |
return list() |
| 301 |
|
| 302 |
def account_obj_to_dict(self, search_obj): |
| 303 |
account_dict = dict( |
| 304 |
hosting_mode=search_obj.hosting_mode, |
| 305 |
id=search_obj.id, |
| 306 |
identity=dict(type=search_obj.identity.type if search_obj.identity else 'None'), |
| 307 |
location=search_obj.location, |
| 308 |
name=search_obj.name, |
| 309 |
network_rule_set=list(), |
| 310 |
partition_count=search_obj.partition_count, |
| 311 |
provisioning_state=search_obj.provisioning_state, |
| 312 |
public_network_access=search_obj.public_network_access, |
| 313 |
replica_count=search_obj.replica_count, |
| 314 |
sku=search_obj.sku.name, |
| 315 |
status=search_obj.status, |
| 316 |
tags=search_obj.tags |
| 317 |
) |
| 318 |
|
| 319 |
if search_obj.identity and search_obj.identity.principal_id: |
| 320 |
account_dict['identity']['principal_id'] = search_obj.identity.principal_id |
| 321 |
|
| 322 |
for rule in search_obj.network_rule_set.ip_rules: |
| 323 |
account_dict['network_rule_set'].append(rule.value) |
| 324 |
|
| 325 |
if self.show_keys: |
| 326 |
account_dict['search_keys'] = dict() |
| 327 |
|
| 328 |
admin_keys = self.search_client.admin_keys.get(self.resource_group, self.name) |
| 329 |
account_dict['search_keys']['admin_primary'] = admin_keys.primary_key |
| 330 |
account_dict['search_keys']['admin_secondary'] = admin_keys.secondary_key |
| 331 |
|
| 332 |
query_keys = self.search_client.query_keys.list_by_search_service(self.resource_group, self.name) |
| 333 |
account_dict['search_keys']['query'] = list() |
| 334 |
for key in query_keys: |
| 335 |
account_dict['search_keys']['query'].append(dict(name=key.name, key=key.key)) |
| 336 |
|
| 337 |
account_dict['keys'] = account_dict['search_keys'] |
| 338 |
|
| 339 |
return account_dict |
| 340 |
|
| 341 |
|
| 342 |
def main(): |
| 343 |
AzureRMSearchInfo() |
| 344 |
|
| 345 |
|
| 346 |
if __name__ == '__main__': |
| 347 |
main() |