Importing CSV Data

Occasionally, you may run into situations where you have a ton of CSV data that you need to import into HighBond. In simple scenarios, you can use HighBond Bulk Importer, but for more custom scenarios or Toolkit development scenarios, you can dynamically load CSV data using Terraform

In the example below, we have a CSV that defines the configurations of our Questionnaire, which we then filter and create the appropriate resources for. It relies on the function csvdecode, but you can use the same technique with JSON (jsondecode), or YAML (yamldecode). You can read more about decoding options in Terraform's documentation here.

locals {
  # We've included this inline to create a complete example, but in practice
  # this is more likely to be loaded from a file using the "file" function.
  csv_data = <<-CSV
id,position,type,text,instruction,optional,options
1,1,section,Section 1,Section Description,,
2,2,page_break,,,,
3,3,simple,Question,Instruction,true,
4,4,choice,Choice,Instruction,false,"Safe,Not Safe"
  CSV

  questions = csvdecode(local.csv_data)
}

#
# Setup Collection and Questionnaire
#
resource highbond_collection main {
    name = "Collection"
}

resource highbond_questionnaire main {
  collection_id      = highbond_collection.main.id
  name               = "Terraform Questionnaire"
  position           = 1
  resubmittable      = true
  respondent_view    = "no_records"
}

#
# Filter per questionnaire item, and create it
#
resource highbond_questionnaire_section main {
  for_each = { 
      for q in local.questions : q.id => q
      if q.type == "section"
    }

  questionnaire_id = highbond_questionnaire.main.id
  name             = each.value.text
  description      = each.value.instruction
  position         = each.value.position
}

resource highbond_questionnaire_page_break main {
  for_each = { 
      for q in local.questions : q.id => q
      if q.type == "page_break"
    }

  questionnaire_id = highbond_questionnaire.main.id
  position         = each.value.position
}

resource highbond_question_simple main {
  for_each = { 
      for q in local.questions : q.id => q
      if q.type == "simple"
    }

  questionnaire_id = highbond_questionnaire.main.id
  text             = each.value.text
  instructions     = each.value.instruction
  optional         = each.value.optional == "true"
  item_type        = "ParagraphTextQuestion"
  position         = each.value.position
}


resource highbond_question_choice main {
  for_each = { 
      for q in local.questions : q.id => q
      if q.type == "choice"
    }

  questionnaire_id = highbond_questionnaire.main.id
  text             = each.value.text
  instructions     = each.value.instruction
  optional         = each.value.optional == "true"
  item_type        = "MultipleChoiceQuestion"
  position         = each.value.position

  dynamic options {
    for_each = toset(split(",", each.value.options))
    content {
      text = options.value
    }
  }
}