Beispiel #1
0
// IsTableStatusWithConf will test the equality status of a table.
func IsTableStatusWithConf(tablename string, status string, c *conf.AWS_Conf) (bool, error) {
	if !conf.IsValid(c) {
		return false, errors.New("describe_table.IsTableStatusWithConf: c is not valid")
	}
	d := ep.Endpoint(&DescribeTable{TableName: tablename})
	s_resp, s_code, s_err := authreq.RetryReq_V4WithConf(d, DESCTABLE_ENDPOINT, c)
	if s_err != nil {
		e := fmt.Sprintf("describe_table.IsTableStatus: "+
			"check on %s err %s",
			tablename, s_err.Error())
		// if not a 500 problem, don't retry
		if !ep.ServerErr(s_code) {
			return false, errors.New(e)
		}
	}
	if s_resp != nil && s_code == http.StatusOK {
		var resp_json Response
		um_err := json.Unmarshal([]byte(s_resp), &resp_json)
		if um_err != nil {
			um_msg := fmt.Sprintf("describe_table.IsTableStatus:"+
				"cannot unmarshal %s, err: %s\ncheck "+
				"table creation of %s manually",
				s_resp, um_err.Error(), tablename)
			return false, errors.New(um_msg)
		}
		return (resp_json.Table.TableStatus == status), nil
	}
	e := fmt.Sprintf("describe_table.IsTableStatus:does %s exist?", tablename)
	return false, errors.New(e)
}
Beispiel #2
0
// TableExistsWithconf test for table exists: exploit the fact that aws reports 4xx for tables that don't exist.
func (desc DescribeTable) TableExistsWithConf(c *conf.AWS_Conf) (bool, error) {
	if !conf.IsValid(c) {
		return false, errors.New("describe_table.TableExistsWithConf: c is not valid")
	}
	_, code, err := desc.EndpointReqWithConf(c)
	if err != nil {
		e := fmt.Sprintf("describe_table.TableExistsWithConf "+
			"%s", err.Error())
		return false, errors.New(e)
	}
	return (code == http.StatusOK), nil
}
Beispiel #3
0
func (delete_item *DeleteItem) EndpointReqWithConf(c *conf.AWS_Conf) ([]byte, int, error) {
	if delete_item == nil {
		return nil, 0, errors.New("delete_item.(DeleteItem)EndpointReqWithConf: receiver is nil")
	}
	if !conf.IsValid(c) {
		return nil, 0, errors.New("delete_item.EndpointReqWithConf: c is not valid")
	}
	// returns resp_body,code,err
	reqJSON, json_err := json.Marshal(delete_item)
	if json_err != nil {
		return nil, 0, json_err
	}
	return authreq.RetryReqJSON_V4WithConf(reqJSON, DELETEITEM_ENDPOINT, c)
}
Beispiel #4
0
func (query *Query) EndpointReqWithConf(c *conf.AWS_Conf) ([]byte, int, error) {
	if query == nil {
		return nil, 0, errors.New("query.(Query)EndpointReqWithConf: receiver is nil")
	}
	if !conf.IsValid(c) {
		return nil, 0, errors.New("query.EndpointReqWithConf: c is not valid")
	}
	// returns resp_body,code,err
	reqJSON, json_err := json.Marshal(query)
	if json_err != nil {
		return nil, 0, json_err
	}
	return authreq.RetryReqJSON_V4WithConf(reqJSON, QUERY_ENDPOINT, c)
}
Beispiel #5
0
func (list_tables *ListTables) EndpointReqWithConf(c *conf.AWS_Conf) ([]byte, int, error) {
	if list_tables == nil {
		return nil, 0, errors.New("list_tables.(ListTables)EndpointReqWithConf: receiver is nil")
	}
	if !conf.IsValid(c) {
		return nil, 0, errors.New("list_tables.EndpointReqWithConf: c is not valid")
	}
	// returns resp_body,code,err
	reqJSON, json_err := json.Marshal(list_tables)
	if json_err != nil {
		return nil, 0, json_err
	}
	return authreq.RetryReqJSON_V4WithConf(reqJSON, LISTTABLE_ENDPOINT, c)
}
Beispiel #6
0
func (describe_table *DescribeTable) EndpointReqWithConf(c *conf.AWS_Conf) ([]byte, int, error) {
	if describe_table == nil {
		return nil, 0, errors.New("describe_table.(DescribeTable)EndpointReqWithConf: receiver is nil")
	}
	if !conf.IsValid(c) {
		return nil, 0, errors.New("describe_table.EndpointReqWithConf: c is not valid")
	}
	// returns resp_body,code,err
	reqJSON, json_err := json.Marshal(describe_table)
	if json_err != nil {
		return nil, 0, json_err
	}
	return authreq.RetryReqJSON_V4WithConf(reqJSON, DESCTABLE_ENDPOINT, c)
}
func (batch_write_item *BatchWriteItem) EndpointReqWithConf(c *conf.AWS_Conf) ([]byte, int, error) {
	if batch_write_item == nil {
		return nil, 0, errors.New("batch_write_item.(BatchWriteItem)EndpointReqWithConf: receiver is nil")
	}
	if !conf.IsValid(c) {
		return nil, 0, errors.New("batch_write_item.EndpointReqWithConf: c is not valid")
	}
	// returns resp_body,code,err
	reqJSON, json_err := json.Marshal(batch_write_item)
	if json_err != nil {
		return nil, 0, json_err
	}
	return authreq.RetryReqJSON_V4WithConf(reqJSON, BATCHWRITE_ENDPOINT, c)
}
Beispiel #8
0
func (scan *Scan) EndpointReqWithConf(c *conf.AWS_Conf) ([]byte, int, error) {
	if scan == nil {
		return nil, 0, errors.New("scan.(Scan)EndpointReqWithConf: receiver is nil")
	}
	if !conf.IsValid(c) {
		return nil, 0, errors.New("scan.EndpointReqWithConf: c is not valid")
	}
	// returns resp_body,code,err
	reqJSON, json_err := json.Marshal(scan)
	if json_err != nil {
		return nil, 0, json_err
	}
	return authreq.RetryReqJSON_V4WithConf(reqJSON, SCAN_ENDPOINT, c)
}
Beispiel #9
0
// DoBatchGet is an endpoint request handler for BatchGetItem that supports arbitrarily-sized
// BatchGetItem struct instances.
// These are split in a list of conforming BatchGetItem instances
// via `Split` and the concurrently dispatched to DynamoDB,
// with the resulting responses stitched together. May break your provisioning.
func (b *BatchGetItem) DoBatchGetWithConf(c *conf.AWS_Conf) ([]byte, int, error) {
	if b == nil {
		return nil, 0, errors.New("batch_get_item.DoBatchGetWithConf: receiver is nil")
	}
	if !conf.IsValid(c) {
		return nil, 0, errors.New("batch_get_item.DoBatchGetWithConf: c is not valid")
	}
	bs, split_err := Split(b)
	if split_err != nil {
		e := fmt.Sprintf("batch_get_item.DoBatchGet: split failed: %s", split_err.Error())
		return nil, 0, errors.New(e)
	}
	resps := make(chan ep.Endpoint_Response, len(bs))
	for _, bi := range bs {
		go func(bi_ BatchGetItem) {
			body, code, err := bi_.RetryBatchGetWithConf(0, c)
			resps <- ep.Endpoint_Response{Body: body, Code: code, Err: err}
		}(bi)
	}
	combined_resp := NewResponse()
	for i := 0; i < len(bs); i++ {
		resp := <-resps
		if resp.Err != nil {
			return nil, 0, resp.Err
		} else if resp.Code != http.StatusOK {
			e := fmt.Sprintf("batch_get_item.DoBatchGetWithConf (%d): code %d",
				i, resp.Code)
			return nil, resp.Code, errors.New(e)
		} else {
			var r Response
			um_err := json.Unmarshal(resp.Body, &r)
			if um_err != nil {
				e := fmt.Sprintf("batch_get_item.DoBatchGetWithConf (%d): %s on \n%s",
					i, um_err.Error(), string(resp.Body))
				return nil, 0, errors.New(e)
			}
			// merge the responses from this call and the recursive one
			_ = combineResponseMetadata(combined_resp, &r)
			_ = combineResponses(combined_resp, &r)
		}
	}
	body, marshal_err := json.Marshal(*combined_resp)
	if marshal_err != nil {
		return nil, 0, marshal_err
	}
	return body, http.StatusOK, nil
}
Beispiel #10
0
// PollTableStatusWithConf allows the caller to poll a table for a specific status.
func PollTableStatusWithConf(tablename string, status string, tries int, c *conf.AWS_Conf) (bool, error) {
	if !conf.IsValid(c) {
		return false, errors.New("describe_table.PollTableStatusWithConf: c is not valid")
	}
	// aws docs informs us to poll the describe endpoint until the table
	// "status" is status for this tablename
	wait := time.Duration(2 * time.Second)

	for i := 0; i < tries; i++ {
		active, err := IsTableStatusWithConf(tablename, status, c)
		if err != nil {
			e := fmt.Sprintf("describe_table.PollStatus:%s",
				err.Error())
			return false, errors.New(e)
		}
		if active {
			return active, nil
		}
		time.Sleep(wait) // wait for table to become ACTIVE
	}
	return false, nil
}
Beispiel #11
0
// RawReqWithConf will sign and transmit the request to the AWS DynamoDB endpoint.
// reqJSON is the json request
// amzTarget is the dynamoDB endpoint
// c is the configuration struct
// returns []byte respBody, string aws reqID, int http code, error
func RawReqWithConf(reqJSON []byte, amzTarget string, c *conf.AWS_Conf) ([]byte, string, int, error) {
	if !conf.IsValid(c) {
		return nil, "", 0, errors.New("auth_v4.RawReqWithConf: conf not valid")
	}
	// shadow conf vars in a read lock to minimize contention
	var our_c conf.AWS_Conf
	cp_err := our_c.Copy(c)
	if cp_err != nil {
		return nil, "", 0, cp_err
	}
	return rawReqAll(
		reqJSON,
		amzTarget,
		our_c.UseIAM,
		our_c.Network.DynamoDB.URL,
		our_c.Network.DynamoDB.Host,
		our_c.Network.DynamoDB.Port,
		our_c.Network.DynamoDB.Zone,
		our_c.IAM.Credentials.Secret,
		our_c.IAM.Credentials.AccessKey,
		our_c.IAM.Credentials.Token,
		our_c.Auth.Secret,
		our_c.Auth.AccessKey)
}