Skip to content

Commit

Permalink
Add password property for protected Excel worksheet
Browse files Browse the repository at this point in the history
  • Loading branch information
Ariel Abuel (NCS) committed Jan 5, 2022
1 parent 213e930 commit d1edde7
Show file tree
Hide file tree
Showing 11 changed files with 287 additions and 113 deletions.
9 changes: 8 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,11 @@ release:
# If you want to manually examine the release before its live, uncomment this line:
# draft: true
changelog:
skip: true
filters:
exclude:
- '^docs:'
- '^test:'
- Merge pull request
- Merge branch
- go mod tidy

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ TEST?=$$(go list ./... | grep -v 'vendor')
HOSTNAME=aa
NAMESPACE=test
NAME=config
VERSION=0.2.1
VERSION=0.2.2
BINARY=terraform-provider-${NAME}
OS_ARCH=linux_amd64

Expand Down
195 changes: 159 additions & 36 deletions config/data_source_config_common_functions.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package config

import (
"encoding/csv"
"encoding/json"
"fmt"
"io"
"strings"

"github.com/360EntSecGroup-Skylar/excelize/v2"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"gopkg.in/yaml.v2"
)

func dataSourceFilterSchema() *schema.Schema {
Expand Down Expand Up @@ -37,10 +42,26 @@ func dataSourceLookupSchema() *schema.Schema {
Type: schema.TypeString,
Required: true,
},
"excel": {
Type: schema.TypeString,
Optional: true,
},
"password": {
Type: schema.TypeString,
Optional: true,
},
"worksheet": {
Type: schema.TypeString,
Optional: true,
},
"yaml": {
Type: schema.TypeString,
Optional: true,
},
"json": {
Type: schema.TypeString,
Optional: true,
},
"key_column": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -70,22 +91,56 @@ func buildConfigDataSourceFilters(set *schema.Set) []map[string]interface{} {
return filters
}

func buildConfigDataSourceLookup(set *schema.Set) []map[string]interface{} {
func buildConfigDataSourceLookup(set *schema.Set) ([]map[string]interface{}, error) {
var lookup []map[string]interface{}
for _, v := range set.List() {
m := v.(map[string]interface{})
mvalue := make(map[string]interface{})
mvalue["Column"] = m["column"].(string)
if m["worksheet"] != nil {

source := 0
if m["worksheet"].(string) != "" {
source++
}
if m["json"].(string) != "" {
source++
}
if m["yaml"].(string) != "" {
source++
}
if source > 1 {
return nil, fmt.Errorf("only 1 type of lookup source is required (worksheet/json/yaml)")
}

if m["worksheet"].(string) != "" {
if m["excel"].(string) != "" {
mvalue["Excel"] = m["excel"].(string)
}
if m["password"].(string) != "" {
mvalue["Password"] = m["password"].(string)
}
mvalue["Worksheet"] = m["worksheet"].(string)
} else {
mvalue["Excel"] = nil
mvalue["Worksheet"] = nil
mvalue["Password"] = nil
}
if m["json"].(string) != "" {
mvalue["Json"] = m["json"].(string)
} else {
mvalue["Json"] = nil
}
if m["yaml"].(string) != "" {
mvalue["Yaml"] = m["yaml"].(string)
} else {
mvalue["Yaml"] = nil
}

mvalue["Key"] = m["key_column"].(string)
mvalue["Value"] = m["value_column"].(string)
lookup = append(lookup, mvalue)
}
return lookup
return lookup, nil
}

func checkLookupValue(lookup []map[string]interface{}, key string) bool {
Expand All @@ -97,48 +152,76 @@ func checkLookupValue(lookup []map[string]interface{}, key string) bool {
return false
}

func getLookupValue(lookup []map[string]interface{}, excel_file, default_worksheet string, key string, value string) (string, error) {
func getLookupValue(lookup []map[string]interface{}, default_excel string, default_password string, default_worksheet string, key string, value string) (string, error) {
var lookupValue = ""
for _, lv := range lookup {
if lv["Column"].(string) == key {
f, err := excelize.OpenFile(excel_file)
if err != nil {
return "", err
}
worksheet := ""
if lv["Worksheet"].(string) != "" {
worksheet = lv["Worksheet"].(string)
} else {
worksheet = default_worksheet
}
rows, err := f.GetRows(worksheet)
if err != nil {
return "", fmt.Errorf(fmt.Sprintf("%v", rows))
}
if lv["Json"] != nil {
jd, _ := stringToInterface(lv["Json"].(string))
jsondata := jd.(map[interface{}]interface{})
if jsondata[value] != nil {
lookupValue = jsondata[value].(string)
} else {
lookupValue = ""
}
} else if lv["Yaml"] != nil {
yd, _ := stringToInterface(lv["Yaml"].(string))
yamldata := yd.(map[interface{}]interface{})
if yamldata[value] != nil {
lookupValue = yamldata[value].(string)
} else {
lookupValue = ""
}
} else if lv["Worksheet"] != nil {
excel_file := default_excel
excel_pass := default_password
if lv["Excel"] != nil {
excel_file = lv["Excel"].(string)
}
if lv["Password"] != nil {
excel_pass = lv["Password"].(string)
}
f, err := excelize.OpenFile(excel_file, excelize.Options{Password: excel_pass})
if err != nil {
return "", err
}
worksheet := ""
if lv["Worksheet"].(string) != "" {
worksheet = lv["Worksheet"].(string)
} else {
worksheet = default_worksheet
}
rows, err := f.GetRows(worksheet)
if err != nil {
return "", fmt.Errorf(fmt.Sprintf("%v", rows))
}

columns := len(rows[0])
columns := len(rows[0])

// get column of key
header := rows[0]
column_key := 0
column_value := -1
for i := 0; i < columns; i++ {
if header[i] == key {
column_key = i
}
if header[i] == lv["Value"] {
column_value = i
// get column of key
header := rows[0]
column_key := 0
column_value := -1
for i := 0; i < columns; i++ {
if header[i] == key {
column_key = i
}
if header[i] == lv["Value"] {
column_value = i
}
}
}
// get row of key
if column_value >= 0 {
for _, row := range rows {
if row[column_key] == value {
lookupValue = row[column_value]
// get row of key
if column_value >= 0 {
for _, row := range rows {
if row[column_key] == value {
lookupValue = row[column_value]
}
}
} else {
lookupValue = ""
}
} else {
return "", fmt.Errorf("lookup value not found")
lookupValue = ""
}
}
}
Expand Down Expand Up @@ -166,3 +249,43 @@ func stringInList(s string, list []string) bool {
}
return false
}

func stringToInterface(s string) (interface{}, error) {
var v interface{}

// Try if the string is yaml
err := yaml.Unmarshal([]byte(s), &v)
if err != nil {
// Try if the string is json
err = json.Unmarshal([]byte(s), &v)
if err != nil {
return nil, fmt.Errorf("unable to parse string using yaml or json")
}
}
return v, nil
}

func stringToMap(s string) ([]map[string]string, error) {
r := csv.NewReader(strings.NewReader(s))
rows := []map[string]string{}
var header []string
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
if header == nil {
header = record
} else {
dict := map[string]string{}
for i := range header {
dict[header[i]] = record[i]
}
rows = append(rows, dict)
}
}
return rows, nil
}
Loading

0 comments on commit d1edde7

Please sign in to comment.