github.com/smugmug/godynamo@v0.0.0-20151122084750-7913028f6623/tests/item_operations-param-conf-livetest.go (about) 1 package main 2 3 import ( 4 "encoding/base64" 5 "encoding/json" 6 "fmt" 7 "github.com/smugmug/godynamo/conf" 8 "github.com/smugmug/godynamo/conf_file" 9 conf_iam "github.com/smugmug/godynamo/conf_iam" 10 delete_item "github.com/smugmug/godynamo/endpoints/delete_item" 11 get_item "github.com/smugmug/godynamo/endpoints/get_item" 12 put_item "github.com/smugmug/godynamo/endpoints/put_item" 13 update_item "github.com/smugmug/godynamo/endpoints/update_item" 14 keepalive "github.com/smugmug/godynamo/keepalive" 15 "github.com/smugmug/godynamo/types/attributevalue" 16 "log" 17 "net/http" 18 "os" 19 ) 20 21 // this test program runs a bunch of item-oriented operations. 22 23 func main() { 24 25 // this is the same as item-operations-livetest except here we demonstrate using a parameterized conf 26 home := os.Getenv("HOME") 27 home_conf_file := home + string(os.PathSeparator) + "." + conf.CONF_NAME 28 home_conf, home_conf_err := conf_file.ReadConfFile(home_conf_file) 29 if home_conf_err != nil { 30 panic("cannot read conf from " + home_conf_file) 31 } 32 home_conf.ConfLock.RLock() 33 if home_conf.Initialized == false { 34 panic("conf struct has not been initialized") 35 } 36 37 // launch a background poller to keep conns to aws alive 38 if home_conf.Network.DynamoDB.KeepAlive { 39 log.Printf("launching background keepalive") 40 go keepalive.KeepAlive([]string{}) 41 } 42 43 // deal with iam, or not 44 if home_conf.UseIAM { 45 iam_ready_chan := make(chan bool) 46 go conf_iam.GoIAM(iam_ready_chan) 47 _ = <-iam_ready_chan 48 } 49 home_conf.ConfLock.RUnlock() 50 51 tn := "test-godynamo-livetest" 52 tablename1 := tn 53 fmt.Printf("tablename1: %s\n", tablename1) 54 55 var code int 56 var err error 57 var body []byte 58 59 // INSERT SINGLE ITEM 60 hk := "a-hash-key" 61 rk := "1" 62 put1 := put_item.NewPutItem() 63 put1.TableName = tablename1 64 av1 := attributevalue.NewAttributeValue() 65 av2 := attributevalue.NewAttributeValue() 66 av3 := attributevalue.NewAttributeValue() 67 av4 := attributevalue.NewAttributeValue() 68 av5 := attributevalue.NewAttributeValue() 69 av6 := attributevalue.NewAttributeValue() 70 av7 := attributevalue.NewAttributeValue() 71 av1.S = hk 72 av2.N = rk 73 av3.InsertSS("pk1_a") 74 av3.InsertSS("pk1_c") 75 av4.InsertNS("1") 76 av4.InsertNS_float64(2) 77 av4.InsertNS("3") 78 av4.InsertNS("-7.2432342") 79 av5.N = "1" 80 av6.B = base64.StdEncoding.EncodeToString([]byte("hello")) 81 av7.InsertBS(base64.StdEncoding.EncodeToString([]byte("hello"))) 82 av7.InsertBS(base64.StdEncoding.EncodeToString([]byte("there"))) 83 put1.Item["TheHashKey"] = av1 84 put1.Item["TheRangeKey"] = av2 85 put1.Item["stringlist"] = av3 86 put1.Item["numlist"] = av4 87 put1.Item["num"] = av5 88 put1.Item["byte"] = av6 89 put1.Item["bytelist"] = av7 90 91 body, code, err = put1.EndpointReqWithConf(home_conf) 92 if err != nil || code != http.StatusOK { 93 fmt.Printf("put1 failed %d %v %s\n", code, err, string(body)) 94 os.Exit(1) 95 } 96 fmt.Printf("%v\n%v\n,%v\n", string(body), code, err) 97 98 // GET THAT ITEM 99 get1 := get_item.NewGetItem() 100 get1.TableName = tablename1 101 get1.Key["TheHashKey"] = av1 102 get1.Key["TheRangeKey"] = av2 103 104 get_json, get_json_err := json.Marshal(get1) 105 if get_json_err != nil { 106 fmt.Printf("%v\n", get_json_err) 107 os.Exit(1) 108 } 109 fmt.Printf("%s\n", string(get_json)) 110 111 body, code, err = get1.EndpointReqWithConf(home_conf) 112 if err != nil || code != http.StatusOK { 113 fmt.Printf("get failed %d %v %s\n", code, err, string(body)) 114 os.Exit(1) 115 } 116 fmt.Printf("%v\n%v\n,%v\n", string(body), code, err) 117 118 var gr get_item.Response 119 um_err := json.Unmarshal([]byte(body), &gr) 120 if um_err != nil { 121 fmt.Printf("get resp unmarshal failed %s\n", um_err.Error()) 122 os.Exit(1) 123 } 124 fmt.Printf("%v\n", string(body)) 125 126 // UPDATE THAT ITEM 127 up1 := update_item.NewUpdateItem() 128 new_attr_val := "new string here" 129 up1.TableName = tablename1 130 up1.Key["TheHashKey"] = av1 131 up1.Key["TheRangeKey"] = av2 132 133 up1.AttributeUpdates = attributevalue.NewAttributeValueUpdateMap() 134 up_avu := attributevalue.NewAttributeValueUpdate() 135 up_avu.Action = update_item.ACTION_PUT 136 up_avu.Value = &attributevalue.AttributeValue{S: new_attr_val} 137 up1.AttributeUpdates["new_string"] = up_avu 138 139 del_avu := attributevalue.NewAttributeValueUpdate() 140 del_avu.Action = update_item.ACTION_DEL 141 del_avu.Value = attributevalue.NewAttributeValue() 142 del_avu.Value.InsertSS("pk1_a") 143 up1.AttributeUpdates["stringlist"] = del_avu 144 145 del2_avu := attributevalue.NewAttributeValueUpdate() 146 del2_avu.Action = update_item.ACTION_DEL 147 del2_avu.Value = &attributevalue.AttributeValue{} 148 up1.AttributeUpdates["byte"] = del2_avu 149 150 add_avu := attributevalue.NewAttributeValueUpdate() 151 add_avu.Action = update_item.ACTION_ADD 152 add_avu.Value = &attributevalue.AttributeValue{N: "4"} 153 up1.AttributeUpdates["num"] = add_avu 154 155 up1.ReturnValues = update_item.RETVAL_ALL_NEW 156 157 update_item_json, update_item_json_err := json.Marshal(up1) 158 if update_item_json_err != nil { 159 fmt.Printf("%v\n", update_item_json_err) 160 os.Exit(1) 161 } 162 fmt.Printf("%s\n", string(update_item_json)) 163 164 body, code, err = up1.EndpointReqWithConf(home_conf) 165 if err != nil || code != http.StatusOK { 166 fmt.Printf("update item failed %d %v %s\n", code, err, string(body)) 167 os.Exit(1) 168 } 169 fmt.Printf("%v\n%v\n,%v\n", string(body), code, err) 170 171 var ur update_item.Response 172 um_err = json.Unmarshal([]byte(body), &ur) 173 if um_err != nil { 174 fmt.Printf("update resp unmarshal failed %s\n", um_err.Error()) 175 os.Exit(1) 176 } 177 178 // GET IT AGAIN 179 body, code, err = get1.EndpointReqWithConf(home_conf) 180 if err != nil || code != http.StatusOK { 181 fmt.Printf("get failed %d %v %s\n", code, err, string(body)) 182 os.Exit(1) 183 } 184 fmt.Printf("%v\n%v\n,%v\n", string(body), code, err) 185 186 // DELETE THE ITEM 187 del1 := delete_item.NewDeleteItem() 188 del1.TableName = tablename1 189 del1.Key["TheHashKey"] = av1 190 del1.Key["TheRangeKey"] = av2 191 192 del1.ReturnValues = delete_item.RETVAL_ALL_OLD 193 body, code, err = del1.EndpointReqWithConf(home_conf) 194 if err != nil || code != http.StatusOK { 195 fmt.Printf("delete item failed %d %v %s\n", code, err, string(body)) 196 os.Exit(1) 197 } 198 fmt.Printf("%v\n%v\n,%v\n", string(body), code, err) 199 200 fmt.Printf("PASSED\n") 201 }