Azure / terraform-azurerm-search-service

Terraform module to create Azure Search service resource
MIT License
1 stars 8 forks source link

Support creating index, indexer and data source #3

Open enstulen opened 4 years ago

enstulen commented 4 years ago

Would it be possible to support adding index, indexer and data source? This would allow to index documents in Azure Blob Storage with Azure Cognitive Search. See link below with the 3 steps:

https://docs.microsoft.com/en-us/azure/search/search-howto-indexing-azure-blob-storage

I tried doing this with provisioners that sent curl requests, but it proved to be difficult. Would love to have this supported!

ctvanzandt42 commented 4 years ago

I'd also really like to see the ability to use Search as a data source.

enstulen commented 4 years ago

We did it like this with local-exec provisioners:

locals {
  search_service_name = "${var.resource_prefix}search-service"
}

resource "azurerm_storage_account" "storage_account" {
  name                     = var.blob_storage_name
  resource_group_name      = var.resource_group_name
  location                 = var.resource_group_location
  account_tier             = var.blob_storgage_tier
  account_replication_type = var.blob_storgage_replication_type
}

resource "azurerm_storage_container" "storage_container" {
  name                  = "${var.resource_prefix}storage-container"
  storage_account_name  = azurerm_storage_account.storage_account.name
  container_access_type = var.blob_storage_container_type
}

resource "local_file" "indexer" {
  content  = templatefile("${path.module}/indexer.json.tmpl", { search_service_name = local.search_service_name })
  filename = "${path.module}/indexer.json"
}

resource "local_file" "data_source" {
  content  = templatefile("${path.module}/dataSource.json.tmpl", { container_connection_string = azurerm_storage_account.storage_account.primary_connection_string, container_name = azurerm_storage_container.storage_container.name })
  filename = "${path.module}/dataSource.json"
}

resource "local_file" "index" {
  content  = templatefile("${path.module}/index.json.tmpl", {})
  filename = "${path.module}/index.json"
}

resource "azurerm_search_service" "search_service" {
  name                = local.search_service_name
  resource_group_name = var.resource_group_name
  location            = var.resource_group_location
  sku                 = var.cognitive_search_tier

  depends_on = [azurerm_storage_container.storage_container]
  //Terraform cannot create the data source, index and indexer and connect them to the search service, so this is done through 3 provisioners:

  //1. Create the data source for the search service. The data source is the storage account containing the blob container with the blobs. 
  provisioner "local-exec" {
    command = "curl -v -d \"@${local_file.data_source.filename}\" -H \"Content-Type: application/json\" -H \"api-key: ${azurerm_search_service.search_service.primary_key}\" -X POST https://${local.search_service_name}.search.windows.net/datasources?api-version=2020-06-30"
  }
  //2. Create the index for the search service. 
  provisioner "local-exec" {
    command = "curl -v -d \"@${local_file.index.filename}\" -H \"Content-Type: application/json\" -H \"api-key: ${azurerm_search_service.search_service.primary_key}\" -X POST https://${local.search_service_name}.search.windows.net/indexes?api-version=2020-06-30"
  }
  //3. Create the indexer for the search service. The data source is the storage account containing the blob container with the blobs. 
  provisioner "local-exec" {
    command = "curl -v -d \"@${local_file.indexer.filename}\" -H \"Content-Type: application/json\" -H \"api-key: ${azurerm_search_service.search_service.primary_key}\" -X POST https://${local.search_service_name}.search.windows.net/indexers?api-version=2020-06-30"
  }
}

With the parameters sent in to thejson.tmpl files like this:

{
  "name": "name",
  "type": "azureblob",
  "credentials": {
    "connectionString": "${container_connection_string}"
  },
  "container": {
    "name": "${container_name}"
  }
}