Skip to content

Commit

Permalink
fixes for handling worksheet without data
Browse files Browse the repository at this point in the history
  • Loading branch information
Ariel Abuel (NCS) committed Sep 1, 2021
1 parent 51ac3f0 commit 46b9d8f
Showing 1 changed file with 41 additions and 26 deletions.
67 changes: 41 additions & 26 deletions config/data_source_configuration_workbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,41 +119,53 @@ func dataSourceConfigurationItemRead(ctx context.Context, d *schema.ResourceData
return diag.FromErr(err)
}
csv_string = csvstring
// return diag.FromErr(fmt.Errorf(fmt.Sprintf("%v", "The configuration item \""+configuration_item+"\" has no data")))
}

// convert the csv to map
csv, err := stringToMap(csv_string)
if err != nil {
return diag.FromErr(err)
}

// get all unique configuration items
items := unique(getConfigurationItems(csv, col_config_item))

// convert the schema to map
var map_yaml interface{}
if config_schema != "" {
map_yaml, err = stringToInterface(config_schema)
if csv_string != "" {
// convert the csv to map
// var csv []map[string]string
csv, err := stringToMap(csv_string)
if err != nil {
return diag.FromErr(err)
}
} else {
map_yaml, err = createDefaultMapping(items, csv, col_config_item)
if err != nil {
return diag.FromErr(err)

// get all unique configuration items
items := unique(getConfigurationItems(csv, col_config_item))

// convert the schema to map
var map_yaml interface{}
if config_schema != "" {
map_yaml, err = stringToInterface(config_schema)
if err != nil {
return diag.FromErr(err)
}
} else {
map_yaml, err = createDefaultMapping(items, csv, col_config_item)
if err != nil {
return diag.FromErr(err)
}
}
}
mapping := map_yaml.(map[interface{}]interface{})
mapping := map_yaml.(map[interface{}]interface{})

// remap all csv headers based on mapping configuration
records := reMapData(csv, mapping["config_schema"], filters, col_config_item)
// remap all csv headers based on mapping configuration
records := reMapData(csv, mapping["config_schema"], filters, col_config_item)

// get the transformed data
data := getItemData(records, items, col_config_item)
// get the transformed data
data := getItemData(records, items, col_config_item)

// set the data to the attribute json
if err := d.Set("json", data); err != nil {
return diag.FromErr(err)
// set the data to the attribute json
if err := d.Set("json", data); err != nil {
return diag.FromErr(err)
}
} else {
// set the data to the attribute json
if configuration_item == "" {
configuration_item = sheet_name
}
if err := d.Set("json", "{\""+configuration_item+"\": []}"); err != nil {
return diag.FromErr(err)
}
}

d.SetId(strconv.FormatInt(time.Now().Unix(), 10))
Expand Down Expand Up @@ -253,6 +265,9 @@ func excelToCSV(excel_file string, sheet_name string, start_column string, end_c
}
csv = append(csv, sb.String())
}
if len(csv) == 1 {
return "", err
}
return strings.Join(csv, "\n"), err
}

Expand Down

0 comments on commit 46b9d8f

Please sign in to comment.