Created
August 4, 2016 16:05
-
-
Save kondor6c/963f063042cad47797af598282d18f67 to your computer and use it in GitHub Desktop.
Amazon Route53 JSON to a CSV in golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/csv" | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"net" | |
"os" | |
"strconv" | |
) | |
type AWSjson struct { | |
ResourceRecordSets []struct { | |
ResourceRecords []struct { | |
Value string `json:"Value"` | |
} `json:"ResourceRecords"` | |
Type []string `json:"Type"` | |
Name []string `json:"Name"` | |
TTL int `json:"TTL"` | |
} `json:"ResourceRecordSets"` | |
} | |
func check(e error) { | |
if e != nil { | |
panic(e) | |
} | |
} | |
//I took this from Go's blog, but I mostly understand it, below was my attempt | |
//sliceGrow := make([]string, len(WriteRow), (cap(WriteRow)+1+cap(valDat)*2)) | |
func AppendString(slice []string, data []string) []string { | |
m := len(slice) | |
n := m + len(data) | |
if n > cap(slice) { | |
newSlice := make([]string, (n+1)*2) //I do not know why we double it | |
copy(newSlice, slice) | |
slice = newSlice | |
} | |
slice = slice[0:n] //I do not know why we need to specify the range til n | |
copy(slice[m:n], data) | |
return slice | |
} | |
/* I should make these functions, and this should work, but isn't | |
func mapify_string(mapper interface{}) { | |
return mapper.(map[string]interface{}) | |
} | |
func mapify_list(mapper interface{}) { | |
return mapper.(map[]interface{}) | |
} | |
*/ | |
func main() { | |
dat, err := ioutil.ReadFile("old_zone_file.json") | |
check(err) | |
f, erro := os.Create("/tmp/data.csv") | |
check(erro) | |
defer f.Close() | |
csvWriter := csv.NewWriter(f) | |
csvWriter.Write([]string{"Name", "Type", "TTL", "Value(s)"}) | |
var AmazonJson interface{} | |
json.Unmarshal(dat, &AmazonJson) | |
AmazonMapped := AmazonJson.(map[string]interface{}) | |
AmazonReMapped := AmazonMapped["ResourceRecordSets"].([]interface{}) | |
for _, obj := range AmazonReMapped { | |
valDat := make([]string, 1) | |
objMap, _ := obj.(map[string]interface{}) | |
csvName := objMap["Name"].(string) | |
csvTTL := strconv.FormatFloat(objMap["TTL"].(float64), 'f', -1, 64) | |
csvType := objMap["Type"].(string) | |
WriteRow := []string{csvType, csvTTL, csvName} | |
objList, _ := objMap["ResourceRecords"].([]interface{}) | |
for _, val := range objList { | |
objValue, _ := val.(map[string]interface{}) | |
valDat = append(valDat, string(objValue["Value"].(string))) | |
/*so many strings! this is due the fact that it returns a mapped object, the | |
string method says I just want the string of the indexed reference instead | |
of interface{}, the string func is correcting the assertation? still | |
don't fully know :-( */ | |
} | |
fmt.Println(net.LookupHost(csvName)) | |
WriteRow = AppendString(WriteRow, valDat) | |
csvWriter.Write(WriteRow) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment