def create_search_service(cmd, resource_group_name, search_service_name, sku, location=None, partition_count=0, replica_count=0,): """ Creates a Search service in the given resource group. :param resource_group_name: Name of resource group. :param search_service_name: Name of the search service. :param sku: The SKU of the search service, which determines price tier and capacity limits. :param location: Geographic location of the resource. :param partition_count: Number of partitions in the search service. :param replica_count: Number of replicas in the search service. """ from azure.mgmt.search.models import SearchService, Sku from azure.cli.command_modules.search._client_factory import cf_search_services _client = cf_search_services(cmd.cli_ctx, None) if location is None: location = _get_resource_group_location(cmd.cli_ctx, resource_group_name) _search = SearchService(location=location, sku=Sku(name=sku)) replica_count = int(replica_count) partition_count = int(partition_count) if replica_count > 0: _search.replica_count = replica_count if partition_count > 0: _search.partition_count = partition_count return _client.create_or_update(resource_group_name, search_service_name, _search)
def create_search_service(cmd, resource_group_name, search_service_name, sku, location=None, partition_count=0, replica_count=0, public_network_access="enabled", ip_rules=None, identity_type=None): """ Creates a Search service in the given resource group. :param resource_group_name: Name of resource group. :param search_service_name: Name of the search service. :param sku: The SKU of the search service, which determines price tier and capacity limits. :param location: Geographic location of the resource. :param partition_count: Number of partitions in the search service. :param replica_count: Number of replicas in the search service. :param public_network_access: Public accessibility to the search service; allowed values are "enabled" or "disabled". :param ip_rules: Public IP(v4) addresses or CIDR ranges to the search service, seperated by comma or semicolon; These IP rules are applicable only when public_network_access is "enabled". :param identity_type: The identity type; possible values include: "None", "SystemAssigned". """ from azure.mgmt.search.models import SearchService, Sku, NetworkRuleSet, IpRule, Identity from azure.cli.command_modules.search._client_factory import cf_search_services import re _client = cf_search_services(cmd.cli_ctx, None) if location is None: location = _get_resource_group_location(cmd.cli_ctx, resource_group_name) _search = SearchService(location=location, sku=Sku(name=sku.lower())) replica_count = int(replica_count) partition_count = int(partition_count) if replica_count > 0: _search.replica_count = replica_count if partition_count > 0: _search.partition_count = partition_count if (public_network_access.lower() not in ["enabled", "disabled"]): raise ValueError("SearchService.PublicNetworkAccess: only [" "enabled" ", " "disabled" "] are allowed") _search.public_network_access = public_network_access if ip_rules: _ip_rules = [] _ip_rules_array = re.split(';|,', ip_rules) for _ip_rule in _ip_rules_array: if _ip_rule: _ip_rules.append(IpRule(value=_ip_rule)) _search.network_rule_set = NetworkRuleSet(ip_rules=_ip_rules) if identity_type: _identity = Identity(type=identity_type) _search.identity = _identity return _client.begin_create_or_update(resource_group_name, search_service_name, _search)
def create_resource(self, name, **kwargs): if self.schema: schema = json.loads(self.schema) else: schema = None self.service_name = self.create_random_name() self.endpoint = "https://{}.search.windows.net".format(self.service_name) if not self.is_live: return { "api_key": "api-key", "index_name": schema["name"] if schema else None, "endpoint": self.endpoint, } group_name = self._get_resource_group(**kwargs).name from azure.mgmt.search import SearchManagementClient from azure.mgmt.search.models import ProvisioningState self.mgmt_client = self.create_mgmt_client(SearchManagementClient) # create the search service from azure.mgmt.search.models import SearchService, Sku service_config = SearchService(location="West US", sku=Sku(name="free")) resource = self.mgmt_client.services.create_or_update( group_name, self.service_name, service_config ) retries = 4 for i in range(retries): try: result = resource.result() if result.provisioning_state == ProvisioningState.succeeded: break except Exception as ex: if i == retries - 1: raise time.sleep(TIME_TO_SLEEP) time.sleep(TIME_TO_SLEEP) # note the for/else here: will raise an error if we *don't* break # above i.e. if result.provisioning state was never "Succeeded" else: raise AzureTestError("Could not create a search service") api_key = self.mgmt_client.admin_keys.get( group_name, self.service_name ).primary_key if self.schema: response = requests.post( SERVICE_URL_FMT.format(self.service_name), headers={"Content-Type": "application/json", "api-key": api_key}, data=self.schema, ) if response.status_code != 201: raise AzureTestError( "Could not create a search index {}".format(response.status_code) ) self.index_name = schema["name"] # optionally load data into the index if self.index_batch and self.schema: from azure.core.credentials import AzureKeyCredential from azure.search.documents import SearchClient from azure.search.documents._index._generated.models import IndexBatch batch = IndexBatch.deserialize(self.index_batch) index_client = SearchClient( self.endpoint, self.index_name, AzureKeyCredential(api_key) ) results = index_client.index_documents(batch) if not all(result.succeeded for result in results): raise AzureTestError("Document upload to search index failed") # Indexing is asynchronous, so if you get a 200 from the REST API, that only means that the documents are # persisted, not that they're searchable yet. The only way to check for searchability is to run queries, # and even then things are eventually consistent due to replication. In the Track 1 SDK tests, we "solved" # this by using a constant delay between indexing and querying. import time time.sleep(TIME_TO_SLEEP) return { "api_key": api_key, "index_name": self.index_name, "endpoint": self.endpoint, }