resource "random_string" "storage_account_name" {
  length  = 24
  upper   = false
  lower   = true
  numeric = true
  special = false
}

resource "azurerm_storage_account" "storage_account" {
  name                     = random_string.storage_account_name.result
  resource_group_name      = azurerm_resource_group.rg.name
  location                 = azurerm_resource_group.rg.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
}

resource "azurerm_storage_container" "objectstorage" {
  name                  = azurerm_resource_group.rg.name
  storage_account_name  = azurerm_storage_account.storage_account.name
  container_access_type = "private"
}

data "azurerm_storage_account_sas" "sas_token" {
  connection_string = azurerm_storage_account.storage_account.primary_connection_string
  https_only        = false
  resource_types {
    service   = false
    container = true
    object    = true
  }
  services {
    blob  = true
    queue = false
    table = false
    file  = false
  }
  start  = "2023-04-01T00:00:00Z"
  expiry = "9999-04-01T00:00:00Z"
  permissions {
    read    = true
    write   = true
    delete  = false
    list    = false
    add     = true
    create  = true
    update  = false
    process = false
    tag     = false
    filter  = false
  }
}

output "sas_url_query_string" {
  sensitive = true
  value     = data.azurerm_storage_account_sas.sas_token.sas
}
