github.com/smugmug/godynamo@v0.0.0-20151122084750-7913028f6623/tests/create_table-livetest.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"github.com/smugmug/godynamo/conf"
     7  	"github.com/smugmug/godynamo/conf_file"
     8  	conf_iam "github.com/smugmug/godynamo/conf_iam"
     9  	ep "github.com/smugmug/godynamo/endpoint"
    10  	create "github.com/smugmug/godynamo/endpoints/create_table"
    11  	desc "github.com/smugmug/godynamo/endpoints/describe_table"
    12  	list "github.com/smugmug/godynamo/endpoints/list_tables"
    13  	keepalive "github.com/smugmug/godynamo/keepalive"
    14  	"github.com/smugmug/godynamo/types/attributedefinition"
    15  	"github.com/smugmug/godynamo/types/aws_strings"
    16  	"github.com/smugmug/godynamo/types/keydefinition"
    17  	"github.com/smugmug/godynamo/types/localsecondaryindex"
    18  	"log"
    19  	"net/http"
    20  	"os"
    21  )
    22  
    23  func main() {
    24  	conf_file.Read()
    25  	conf.Vals.ConfLock.RLock()
    26  	if conf.Vals.Initialized == false {
    27  		panic("the conf.Vals global conf struct has not been initialized")
    28  	}
    29  
    30  	// launch a background poller to keep conns to aws alive
    31  	if conf.Vals.Network.DynamoDB.KeepAlive {
    32  		log.Printf("launching background keepalive")
    33  		go keepalive.KeepAlive([]string{})
    34  	}
    35  
    36  	// deal with iam, or not
    37  	if conf.Vals.UseIAM {
    38  		iam_ready_chan := make(chan bool)
    39  		go conf_iam.GoIAM(iam_ready_chan)
    40  		_ = <-iam_ready_chan
    41  	}
    42  	conf.Vals.ConfLock.RUnlock()
    43  
    44  	tablename1 := "test-godynamo-livetest"
    45  	fmt.Printf("tablename: %s\n", tablename1)
    46  
    47  	var code int
    48  	var err error
    49  	var body []byte
    50  
    51  	// CREATE TABLE
    52  	create1 := create.NewCreateTable()
    53  	create1.TableName = tablename1
    54  	create1.ProvisionedThroughput.ReadCapacityUnits = 100
    55  	create1.ProvisionedThroughput.WriteCapacityUnits = 100
    56  
    57  	create1.AttributeDefinitions = append(create1.AttributeDefinitions,
    58  		attributedefinition.AttributeDefinition{AttributeName: "TheHashKey", AttributeType: ep.S})
    59  	create1.AttributeDefinitions = append(create1.AttributeDefinitions,
    60  		attributedefinition.AttributeDefinition{AttributeName: "TheRangeKey", AttributeType: ep.N})
    61  	create1.AttributeDefinitions = append(create1.AttributeDefinitions,
    62  		attributedefinition.AttributeDefinition{AttributeName: "AnAttrName", AttributeType: ep.S})
    63  	create1.KeySchema = append(create1.KeySchema,
    64  		keydefinition.KeyDefinition{AttributeName: "TheHashKey", KeyType: ep.HASH})
    65  	create1.KeySchema = append(create1.KeySchema,
    66  		keydefinition.KeyDefinition{AttributeName: "TheRangeKey", KeyType: ep.RANGE})
    67  
    68  	lsi := localsecondaryindex.NewLocalSecondaryIndex()
    69  	lsi.IndexName = "AnAttrIndex"
    70  	lsi.Projection.ProjectionType = aws_strings.KEYS_ONLY
    71  	lsi.KeySchema = append(lsi.KeySchema,
    72  		keydefinition.KeyDefinition{AttributeName: "TheHashKey", KeyType: ep.HASH})
    73  	lsi.KeySchema = append(lsi.KeySchema,
    74  		keydefinition.KeyDefinition{AttributeName: "AnAttrName", KeyType: ep.RANGE})
    75  	create1.LocalSecondaryIndexes = append(create1.LocalSecondaryIndexes, *lsi)
    76  
    77  	create_json, create_json_err := json.Marshal(create1)
    78  	if create_json_err != nil {
    79  		fmt.Printf("%v\n", create_json_err)
    80  		os.Exit(1)
    81  	}
    82  	fmt.Printf("%s\n", string(create_json))
    83  	fmt.Printf("%v\n", create1)
    84  
    85  	body, code, err = create1.EndpointReq()
    86  	fmt.Printf("%v\n%v\n,%v\n", string(body), code, err)
    87  	if err != nil || code != http.StatusOK {
    88  		fmt.Printf("create failed %d %v %s\n", code, err, string(body))
    89  		os.Exit(1)
    90  	}
    91  
    92  	var desc1 desc.Describe
    93  	desc1.TableName = tablename1
    94  	body, code, err = desc1.EndpointReq()
    95  	fmt.Printf("desc:%v\n%v\n,%v\n", string(body), code, err)
    96  	if err != nil || code != http.StatusOK {
    97  		fmt.Printf("desc failed %d %v %s\n", code, err, string(body))
    98  		os.Exit(1)
    99  	}
   100  
   101  	// WAIT FOR IT TO BE ACTIVE
   102  	fmt.Printf("checking for ACTIVE status for table....\n")
   103  	active, poll_err := desc.PollTableStatus(tablename1, desc.ACTIVE, 100)
   104  	if poll_err != nil {
   105  		fmt.Printf("poll1:%v\n", poll_err)
   106  		os.Exit(1)
   107  	}
   108  	fmt.Printf("ACTIVE:%v\n", active)
   109  
   110  	// List TABLES
   111  	var l list.List
   112  	l.ExclusiveStartTableName = ""
   113  	l.Limit = 100
   114  	body, code, err = l.EndpointReq()
   115  	if err != nil || code != http.StatusOK {
   116  		fmt.Printf("list failed %d %v %s\n", code, err, string(body))
   117  		os.Exit(1)
   118  	}
   119  	fmt.Printf("%v\n%v\n,%v\n", string(body), code, err)
   120  }