Skip to content

Commit

Permalink
Merge pull request awsdocs#1660 from knabben/master
Browse files Browse the repository at this point in the history
Added go v2 version of Kinesis PutRecord
  • Loading branch information
Doug-AWS authored Feb 24, 2021
2 parents 27b05d7 + 9e2e428 commit eb69899
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 0 deletions.
1 change: 1 addition & 0 deletions gov2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/aws/aws-sdk-go-v2/config v0.4.0
github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v0.1.0
github.com/aws/aws-sdk-go-v2/feature/dynamodb/expression v0.1.0
github.com/aws/aws-sdk-go-v2/service/kinesis v0.31.0
github.com/aws/aws-sdk-go-v2/service/cloudwatch v0.31.0
github.com/aws/aws-sdk-go-v2/service/cloudwatchevents v0.31.0
github.com/aws/aws-sdk-go-v2/service/dynamodb v0.31.0
Expand Down
1 change: 1 addition & 0 deletions gov2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v0.2.0 h1:xeqwfWQGg3
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v0.2.0/go.mod h1:Ef71w/O9Ulhxj8gD9Pq2S0lXMvyNzFLMRuIxWpDRQxk=
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v0.4.0 h1:OiRO8lneLP7wJ80dqAwHpIRcMInUO04HzcQUVtPwk3U=
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v0.4.0/go.mod h1:fMsE4Rr6OkxQUmZxi4Amj8LS0BakLZw05Rog0jYStQM=
github.com/aws/aws-sdk-go-v2/service/kinesis v0.31.0/go.mod h1:OYBQZvaQy8tR59aqEYerLKGbdfkbxTOgqLpQWvlg6Oc=
github.com/aws/aws-sdk-go-v2/service/kms v0.31.0 h1:ju4tqKfhT+6AG39E+bUsZcaqkLhEW573RKbvwJo8G9M=
github.com/aws/aws-sdk-go-v2/service/kms v0.31.0/go.mod h1:NcamFVUaE0JeTLqM9GbDL03yoUWrXCufXQ4gMrCT0Lc=
github.com/aws/aws-sdk-go-v2/service/s3 v0.31.0 h1:KDBpodyszhL8F83QYv36qBzDj99oPlt1coZNGSFKTXU=
Expand Down
62 changes: 62 additions & 0 deletions gov2/kinesis/PutRecord/PutRecordv2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX - License - Identifier: Apache - 2.0
// snippet-start:[kinesis.go-v2.PutRecord]
package main

import (
"context"
"flag"
"fmt"

"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/kinesis"
)

// KinesisPutRecordAPI defines the interface for the PutRecord function.
// We use this interface to test the function using a mocked service.
type KinesisPutRecordAPI interface {
PutRecord(ctx context.Context,
params *kinesis.PutRecordInput,
optFns ...func(*kinesis.Options)) (*kinesis.PutRecordOutput, error)
}

// MakePutRecord creates an Amazon Kinesis (Amazon Kinesis) stream record.
// Inputs:
// c is the context of the method call, which includes the AWS Region.
// api is the interface that defines the method call.
// input defines the input arguments to the service call.
// Output:
// If success, a PutRecordOutput object containing the result of the service call and nil.
// Otherwise, nil and an error from the call to PutRecordOutput.
func MakePutRecord(c context.Context, api KinesisPutRecordAPI, input *kinesis.PutRecordInput) (*kinesis.PutRecordOutput, error) {
return api.PutRecord(c, input)
}

func main() {
stream := flag.String("s", "", "The name of the stream")
partition := flag.String("k", "", "The identifier of the partition key")
payload := flag.String("p", "", "The payload")
flag.Parse()

cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
panic("configuration error, " + err.Error())
}

client := kinesis.NewFromConfig(cfg)

input := &kinesis.PutRecordInput{
Data: []byte(*payload),
PartitionKey: partition,
StreamName: stream,
}

results, err := MakePutRecord(context.TODO(), client, input)
if err != nil {
fmt.Println(err.Error())
}

fmt.Println(results.SequenceNumber)
}

// snippet-end:[kinesis.go-v2.PutRecord]
81 changes: 81 additions & 0 deletions gov2/kinesis/PutRecord/PutRecordv2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package main

import (
"context"
"encoding/json"
"errors"
"io/ioutil"
"testing"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/kinesis"
)

type KinesisPutRecordImpl struct{}

func (pr KinesisPutRecordImpl) PutRecord(ctx context.Context,
params *kinesis.PutRecordInput,
optFns ...func(*kinesis.Options)) (*kinesis.PutRecordOutput, error) {

output := &kinesis.PutRecordOutput{
ShardId: aws.String("shard-01"),
}

return output, nil
}

type Config struct {
StreamName string `json:"StreamName"`
}

var configFileName = "config.json"

var globalConfig Config

func populateConfiguration(t *testing.T) error {
content, err := ioutil.ReadFile(configFileName)
if err != nil {
return err
}

text := string(content)

err = json.Unmarshal([]byte(text), &globalConfig)
if err != nil {
return err
}

if globalConfig.StreamName == "" {
msg := "You must specify a value for StreamName in " + configFileName
return errors.New(msg)
}

return nil
}

func TestPutRecord(t *testing.T) {
thisTime := time.Now()
nowString := thisTime.Format("2006-01-02 15:04:05 Monday")
t.Log("Starting unit test at " + nowString)

err := populateConfiguration(t)
if err != nil {
t.Fatal(err)
}

input := kinesis.PutRecordInput{
StreamName: &globalConfig.StreamName,
}

api := &KinesisPutRecordImpl{}

resp, err := MakePutRecord(context.Background(), *api, &input)
if err != nil {
t.Log("Got an error ...:")
t.Log(err)
return
}

t.Log("Put record on shard " + *resp.ShardId)
}
5 changes: 5 additions & 0 deletions gov2/kinesis/PutRecord/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"StreamName": "fakestream",
"PartitionKey": "shard-0",
"Data": "payload"
}
54 changes: 54 additions & 0 deletions gov2/kinesis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# AWS SDK for Go V2 code examples for Amazon Kinesis

## Purpose

These examples demonstrates how to perform several Kinesis operations
using version 2 of the AWS SDK for Go.

## Prerequisites

You must have an AWS account, and have your default credentials and AWS Region
configured as described in
[Configuring the AWS SDK for Go](https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html)
in the AWS SDK for Go Developer Guide.

## Running the code

### PutRecord/PutRecordv2.go

This example creates an Amazon EC2 image.

`go run PutRecordv2.go -s STREAM -k PARTITION-KEY -p PAYLOAD

- _STREAM_ is the Kinesis stream name.
- _PARTITION-KEY_ is the partition ID.
- _PAYLOAD_ is the content to be published.

The unit test accepts similar values in _config.json_.

## Running the unit tests

Unit tests should delete any resources they create.
However, they might result in charges to your
AWS account.

To run a unit test, enter:

`go test`

You should see something like the following,
where PATH is the path to the folder containing the Go files:

```sh
PASS
ok PATH 6.593s
```

If you want to see any log messages, enter:

`go test -v`

You should see some additional log messages.
The last two lines should be similar to the previous output shown.

Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0
7 changes: 7 additions & 0 deletions gov2/kinesis/metadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
files:
- path: PutRecord/PutRecordv2.go
services:
- kinesis
- path: PutRecord/PutRecordv2_test.go
services:
- kinesis

0 comments on commit eb69899

Please sign in to comment.