コード例 #1
0
ファイル: get_item-livetest.go プロジェクト: nabeken/godynamo
func main() {
	conf_file.Read()
	if conf.Vals.Initialized == false {
		panic("the conf.Vals global conf struct has not been initialized")
	}

	iam_ready_chan := make(chan bool)
	go conf_iam.GoIAM(iam_ready_chan)
	iam_ready := <-iam_ready_chan
	if iam_ready {
		fmt.Printf("using iam\n")
	} else {
		fmt.Printf("not using iam\n")
	}

	for i := 1; i <= 300; i++ {
		var get1 get.Request
		get1.TableName = "test-godynamo-livetest"
		get1.Key = make(ep.Item)
		k := fmt.Sprintf("AHashKey%d", i)
		v := fmt.Sprintf("%d", i)
		get1.Key["TheHashKey"] = ep.AttributeValue{S: k}
		get1.Key["TheRangeKey"] = ep.AttributeValue{N: v}
		body, code, err := get1.EndpointReq()
		if err != nil || code != http.StatusOK {
			fmt.Printf("get failed %d %v %s\n", code, err, body)
		}
		fmt.Printf("%s\n", string(body))
	}
}
コード例 #2
0
ファイル: ddb.go プロジェクト: Glimmerville/Heartbleed
/* Fetch the record from the Cache.
 *
 * returns the record and if the record should be considered "OK" to use.
 * An OK record is a valid, non-expired cache stored entry.
 */
func Check(host string) (CacheReply, bool) {
	getr := get.Request{
		TableName: CACHE_TAB,
		Key:       make(ep.Item),
	}
	getr.Key["hostname"] = ep.AttributeValue{S: host}
	body, code, err := getr.EndpointReq()
	if err != nil {
		log.Printf("[cache] ERROR: %s", err.Error())
		return CacheReply{}, false
	}
	if code != http.StatusOK {
		return CacheReply{}, false
	}

	// get the time from the body
	// log.Printf("####CACHE_GET: %s %d\n", string(body), len(body))
	if len(body) < 3 {
		// record not found
		return CacheReply{}, false
	}

	var gr get.Response
	var reply CacheReply
	if err = json.Unmarshal([]byte(body), &gr); err == nil {
		reply.Status, err = strconv.ParseInt(gr.Item["Status"].N, 10, 64)
		if err != nil {
			// unparsable status
			log.Printf("[cache] ERROR: Bad Record %s, %s", host, err.Error())
			return CacheReply{}, false
		}

		reply.Host = gr.Item["hostname"].S
		reply.Error = gr.Item["Error"].S
		if reply.Error == "---" {
			reply.Error = ""
		}
		reply.Data = gr.Item["Data"].S
		if reply.Data == "---" {
			reply.Data = ""
		}

		reply.LastUpdate, err = strconv.ParseInt(gr.Item["Mtime"].N, 10, 64)
		if err != nil {
			// unparsable time
			log.Printf("[cache] ERROR: Bad Record %s, %s", host, err.Error())
			return CacheReply{}, false
		}
		if reply.LastUpdate < time.Now().UTC().Add(-expiry).Unix() {
			// record has expired.
			return reply, false
		}
		// log.Printf("gr %+v", reply)
	}

	return reply, true
}
コード例 #3
0
func main() {
	// conf file must be read in before anything else, to initialize permissions etc
	conf_file.Read()
	if conf.Vals.Initialized == false {
		panic("the conf.Vals global conf struct has not been initialized")
	}

	// deal with iam, or not
	iam_ready_chan := make(chan bool)
	go conf_iam.GoIAM(iam_ready_chan)
	iam_ready := <-iam_ready_chan
	if iam_ready {
		fmt.Printf("using iam\n")
	} else {
		fmt.Printf("not using iam\n")
	}

	tn := "test-godynamo-livetest"
	tablename1 := tn
	fmt.Printf("tablename1: %s\n", tablename1)

	var code int
	var err error
	var body string

	// INSERT SINGLE ITEM
	hk := "a-hash-key"
	rk := "1"
	var put1 put_item.Request
	put1.TableName = tablename1
	var av1, av2, av3, av4, av5, av6, av7 ep.AttributeValue
	av1.S = hk
	av2.N = rk
	av3.SS = []string{"pk1_a", "pk1_b", "pk1_c"}
	av4.NS = []string{"1", "2", "3", "-7.234234234234234e+09"}
	av5.N = "1"
	av6.B = base64.StdEncoding.EncodeToString([]byte("hello"))
	av7.BS = []string{base64.StdEncoding.EncodeToString([]byte("hello")),
		base64.StdEncoding.EncodeToString([]byte("there"))}
	put1.Item = make(ep.Item)
	put1.Item["TheHashKey"] = av1
	put1.Item["TheRangeKey"] = av2
	put1.Item["stringlist"] = av3
	put1.Item["numlist"] = av4
	put1.Item["num"] = av5
	put1.Item["byte"] = av6
	put1.Item["bytelist"] = av7
	// recommended to make sure pk does not exist yet in table
	// (i.e. this will be a new item)
	put1.Expected = make(ep.Expected)
	put1.Expected["t1.hk"] = ep.Constraints{Exists: false}
	body, code, err = put1.EndpointReq()
	if err != nil || code != http.StatusOK {
		fmt.Printf("put1 failed %d %v %s\n", code, err, body)
		os.Exit(1)
	}

	// GET THAT ITEM
	var get1 get_item.Request
	get1.TableName = tablename1
	get1.Key = make(ep.Item)
	get1.Key["TheHashKey"] = av1
	get1.Key["TheRangeKey"] = av2

	get_json, get_json_err := json.Marshal(get1)
	if get_json_err != nil {
		fmt.Printf("%v\n", get_json_err)
		os.Exit(1)
	}
	fmt.Printf("%s\n", string(get_json))

	body, code, err = get1.EndpointReq()
	if err != nil || code != http.StatusOK {
		fmt.Printf("get failed %d %v %s\n", code, err, body)
		os.Exit(1)
	}
	var gr get_item.Response
	um_err := json.Unmarshal([]byte(body), &gr)
	if um_err != nil {
		fmt.Printf("get resp unmarshal failed %s\n", um_err.Error())
		os.Exit(1)
	}

	// USE PUT TO REPLACE THAT ITEM CONDITIONALLY
	// **SET IT UP TO FAIL**
	var put2 put_item.Request
	put2.TableName = tablename1
	var av1_2, av2_2, av3_2, av4_2, av5_2, av6_2 ep.AttributeValue
	av1_2.S = hk
	av2_2.N = rk
	av3_2.SS = []string{"pk1_d", "pk1_e", "pk1_f"}
	av4_2.NS = []string{"4", "5", "6"}
	av5_2.N = "2"
	av6_2.S = "hello there"
	put2.Item = make(ep.Item)
	put2.Item["TheHashKey"] = av1_2
	put2.Item["TheRangeKey"] = av2_2
	put2.Item["stringlist"] = av3_2
	put2.Item["numlist"] = av4_2
	put2.Item["num"] = av5_2
	put2.Item["string"] = av6_2
	put2.Item["byte"] = av6
	put2.Item["bytelist"] = av7
	put2.Expected = make(ep.Expected)
	put2.Expected["TheHashKey"] = ep.Constraints{Exists: false}
	body, code, err = put2.EndpointReq()
	if err == nil && code == http.StatusOK {
		fmt.Printf("put2 should have failed %d %v %s\n", code, err, body)
		os.Exit(1)
	}

	// NOW MAKE IT SO THAT PUT WORKS
	put2.Expected = make(ep.Expected)
	put2.ReturnValues = put_item.RETVAL_ALL_OLD
	put2.Expected["num"] = ep.Constraints{Exists: true, Value: av5}
	body, code, err = put2.EndpointReq()
	if err != nil || code != http.StatusOK {
		fmt.Printf("put2 item failed %d %v %s\n", code, err, body)
		os.Exit(1)
	}

	// UPDATE THAT ITEM
	var up1 update_item.Request
	new_attr_val := "new string here"
	up1.TableName = tablename1
	up1.Key = make(ep.Item)
	up1.Key["TheHashKey"] = av1
	up1.Key["TheRangeKey"] = av2

	up1.AttributeUpdates = make(update_item.AttributeUpdates)
	up1.AttributeUpdates["new_string"] =
		update_item.AttributeAction{Value: ep.AttributeValue{S: new_attr_val}, Action: update_item.ACTION_PUT}
	var del_stringlist ep.AttributeValue
	del_stringlist.SS = []string{"pk1_a"}
	up1.AttributeUpdates["stringlist"] =
		update_item.AttributeAction{Value: del_stringlist, Action: update_item.ACTION_DEL}
	up1.AttributeUpdates["byte"] = update_item.AttributeAction{Value: ep.AttributeValue{}, Action: update_item.ACTION_DEL}
	up1.AttributeUpdates["num"] = update_item.AttributeAction{Value: ep.AttributeValue{N: "4"}, Action: update_item.ACTION_ADD}
	up1.ReturnValues = update_item.RETVAL_ALL_NEW

	update_item_json, update_item_json_err := json.Marshal(up1)
	if update_item_json_err != nil {
		fmt.Printf("%v\n", update_item_json_err)
		os.Exit(1)
	}
	fmt.Printf("%s\n", string(update_item_json))

	body, code, err = up1.EndpointReq()
	if err != nil || code != http.StatusOK {
		fmt.Printf("update item failed %d %v %s\n", code, err, body)
		os.Exit(1)
	}

	var ur update_item.Response
	um_err = json.Unmarshal([]byte(body), &ur)
	if um_err != nil {
		fmt.Printf("update resp unmarshal failed %s\n", um_err.Error())
		os.Exit(1)
	}

	// GET IT AGAIN
	body, code, err = get1.EndpointReq()
	if err != nil || code != http.StatusOK {
		fmt.Printf("get failed %d %v %s\n", code, err, body)
		os.Exit(1)
	}

	// DELETE THE ITEM
	var del1 delete_item.Request
	del1.Key = make(ep.Item)
	del1.TableName = tablename1
	del1.Key["TheHashKey"] = av1
	del1.Key["TheRangeKey"] = av2

	del1.Expected = make(ep.Expected)
	del1.Expected["num"] = ep.Constraints{Exists: true, Value: ep.AttributeValue{N: "6"}}
	del1.ReturnValues = delete_item.RETVAL_ALL_OLD
	body, code, err = del1.EndpointReq()
	if err != nil || code != http.StatusOK {
		fmt.Printf("delete item failed %d %v %s\n", code, err, body)
		os.Exit(1)
	}
}