github.com/smugmug/godynamo@v0.0.0-20151122084750-7913028f6623/endpoints/create_table/create_table.go (about) 1 // Support for the DynamoDB CreateTable endpoint. 2 // 3 // example use: 4 // 5 // see tests/create_table-livestest.go 6 // 7 package create_table 8 9 import ( 10 "encoding/json" 11 "errors" 12 "github.com/smugmug/godynamo/authreq" 13 "github.com/smugmug/godynamo/aws_const" 14 "github.com/smugmug/godynamo/conf" 15 "github.com/smugmug/godynamo/types/attributedefinition" 16 "github.com/smugmug/godynamo/types/globalsecondaryindex" 17 "github.com/smugmug/godynamo/types/keydefinition" 18 "github.com/smugmug/godynamo/types/localsecondaryindex" 19 "github.com/smugmug/godynamo/types/provisionedthroughput" 20 ) 21 22 const ( 23 ENDPOINT_NAME = "CreateTable" 24 CREATETABLE_ENDPOINT = aws_const.ENDPOINT_PREFIX + ENDPOINT_NAME 25 ) 26 27 type CreateTable struct { 28 AttributeDefinitions attributedefinition.AttributeDefinitions 29 GlobalSecondaryIndexes []globalsecondaryindex.GlobalSecondaryIndex `json:",omitempty"` 30 KeySchema keydefinition.KeySchema 31 LocalSecondaryIndexes []localsecondaryindex.LocalSecondaryIndex `json:",omitempty"` 32 ProvisionedThroughput provisionedthroughput.ProvisionedThroughput 33 TableName string 34 } 35 36 func NewCreateTable() *CreateTable { 37 c := new(CreateTable) 38 c.AttributeDefinitions = make(attributedefinition.AttributeDefinitions, 0) 39 c.GlobalSecondaryIndexes = make([]globalsecondaryindex.GlobalSecondaryIndex, 0) 40 c.KeySchema = make(keydefinition.KeySchema, 0) 41 c.LocalSecondaryIndexes = make([]localsecondaryindex.LocalSecondaryIndex, 0) 42 return c 43 } 44 45 // Create is an alias for backwards compatibility 46 type Create CreateTable 47 48 func NewCreate() *Create { 49 create_table := NewCreateTable() 50 create := Create(*create_table) 51 return &create 52 } 53 54 type Request CreateTable 55 56 type Response struct { 57 TableDescription struct { 58 AttributeDefinitions attributedefinition.AttributeDefinitions `json:",omitempty"` 59 CreationDateTime float64 `json:",omitempty"` 60 GlobalSecondaryIndexes []globalsecondaryindex.GlobalSecondaryIndexDesc `json:",omitempty"` 61 ItemCount uint64 `json:",omitempty"` 62 KeySchema keydefinition.KeySchema `json:",omitempty"` 63 LocalSecondaryIndexes []localsecondaryindex.LocalSecondaryIndexDesc `json:",omitempty"` 64 ProvisionedThroughput provisionedthroughput.ProvisionedThroughputDesc `json:",omitempty"` 65 TableName string 66 TableSizeBytes uint64 `json:",omitempty"` 67 TableStatus string 68 } 69 } 70 71 func NewResponse() *Response { 72 r := new(Response) 73 r.TableDescription.AttributeDefinitions = make(attributedefinition.AttributeDefinitions, 0) 74 r.TableDescription.GlobalSecondaryIndexes = make([]globalsecondaryindex.GlobalSecondaryIndexDesc, 0) 75 r.TableDescription.KeySchema = make(keydefinition.KeySchema, 0) 76 r.TableDescription.LocalSecondaryIndexes = make([]localsecondaryindex.LocalSecondaryIndexDesc, 0) 77 78 return r 79 } 80 81 // These implementations of EndpointReq use a parameterized conf. 82 83 func (create_table *CreateTable) EndpointReqWithConf(c *conf.AWS_Conf) ([]byte, int, error) { 84 if create_table == nil { 85 return nil, 0, errors.New("create_table.(CreateTable)EndpointReqWithConf: receiver is nil") 86 } 87 if !conf.IsValid(c) { 88 return nil, 0, errors.New("create_table.EndpointReqWithConf: c is not valid") 89 } 90 // returns resp_body,code,err 91 reqJSON, json_err := json.Marshal(create_table) 92 if json_err != nil { 93 return nil, 0, json_err 94 } 95 return authreq.RetryReqJSON_V4WithConf(reqJSON, CREATETABLE_ENDPOINT, c) 96 } 97 98 func (create *Create) EndpointReqWithConf(c *conf.AWS_Conf) ([]byte, int, error) { 99 if create == nil { 100 return nil, 0, errors.New("create_table.(Create)EndpointReqWithConf: receiver is nil") 101 } 102 create_table := CreateTable(*create) 103 return create_table.EndpointReqWithConf(c) 104 } 105 106 func (req *Request) EndpointReqWithConf(c *conf.AWS_Conf) ([]byte, int, error) { 107 if req == nil { 108 return nil, 0, errors.New("create_table.(Request)EndpointReqWithConf: receiver is nil") 109 } 110 create_table := CreateTable(*req) 111 return create_table.EndpointReqWithConf(c) 112 } 113 114 // These implementations of EndpointReq use the global conf. 115 116 func (create_table *CreateTable) EndpointReq() ([]byte, int, error) { 117 if create_table == nil { 118 return nil, 0, errors.New("create_table.(CreateTable)EndpointReq: receiver is nil") 119 } 120 return create_table.EndpointReqWithConf(&conf.Vals) 121 } 122 123 func (create *Create) EndpointReq() ([]byte, int, error) { 124 if create == nil { 125 return nil, 0, errors.New("create_table.(Create)EndpointReq: receiver is nil") 126 } 127 create_table := CreateTable(*create) 128 return create_table.EndpointReqWithConf(&conf.Vals) 129 } 130 131 func (req *Request) EndpointReq() ([]byte, int, error) { 132 if req == nil { 133 return nil, 0, errors.New("create_table.(Request)EndpointReq: receiver is nil") 134 } 135 create_table := CreateTable(*req) 136 return create_table.EndpointReqWithConf(&conf.Vals) 137 } 138 139 // ValidTable is a local validator that helps callers determine if a table name is too long. 140 func ValidTableName(t string) bool { 141 l := len([]byte(t)) 142 return (l > 3) && (l < 256) 143 }