11// Copyright 2020 New Relic, Inc. All rights reserved.
22// SPDX-License-Identifier: Apache-2.0
33
4+ using System ;
45using System . Runtime . CompilerServices ;
6+ using System . Text ;
57using System . Threading . Tasks ;
6- using System ;
7- using Amazon . Runtime ;
88using Amazon . KinesisFirehose ;
99using Amazon . KinesisFirehose . Model ;
10+ using Amazon . Runtime ;
1011using Amazon . S3 ;
1112using Amazon . S3 . Model ;
12- using System . Text ;
1313
14- namespace AwsSdkTestApp . AwsSdkExercisers
14+ namespace AwsSdkTestApp . AwsSdkExercisers ;
15+
16+ public class AwsSdkFirehoseExerciser : IDisposable
1517{
16- public class AwsSdkFirehoseExerciser : IDisposable
18+ private readonly AmazonKinesisFirehoseClient _amazonKinesisFirehoseClient ;
19+ private readonly AmazonS3Client _amazonS3Client ;
20+
21+ public AwsSdkFirehoseExerciser ( )
1722 {
18- private readonly AmazonKinesisFirehoseClient _amazonKinesisFirehoseClient ;
19- private readonly AmazonS3Client _amazonS3Client ;
23+ _amazonKinesisFirehoseClient = GetKinesisFirehoseClient ( ) ;
24+ _amazonS3Client = GetS3Client ( ) ;
25+ }
26+
2027
21- public AwsSdkFirehoseExerciser ( )
28+ private AmazonKinesisFirehoseClient GetKinesisFirehoseClient ( )
29+ {
30+ // configure the client to use LocalStack
31+ // use plausible (but fake) access key and fake secret key so account id parsing can be tested
32+ var creds = new BasicAWSCredentials ( "FOOIHSHSDNNAEXAMPLE" , "MOREGIBBERISH" ) ;
33+ var config = new AmazonKinesisFirehoseConfig
2234 {
23- _amazonKinesisFirehoseClient = GetKinesisFirehoseClient ( ) ;
24- _amazonS3Client = GetS3Client ( ) ;
25- }
35+ ServiceURL = "http://localstack:4566" ,
36+ AuthenticationRegion = "us-west-2"
37+ } ;
2638
39+ var client = new AmazonKinesisFirehoseClient ( creds , config ) ;
40+ return client ;
41+ }
42+ private AmazonS3Client GetS3Client ( )
43+ {
44+ // configure the client to use LocalStack
45+ // use plausible (but fake) access key and fake secret key so account id parsing can be tested
46+ var creds = new BasicAWSCredentials ( "FOOIHSHSDNNAEXAMPLE" , "MOREGIBBERISH" ) ;
47+ var config = new AmazonS3Config
48+ {
49+ ServiceURL = "http://localstack:4566" ,
50+ AuthenticationRegion = "us-west-2" ,
51+ ForcePathStyle = true
52+ } ;
2753
28- private AmazonKinesisFirehoseClient GetKinesisFirehoseClient ( )
54+ var client = new AmazonS3Client ( creds , config ) ;
55+ return client ;
56+ }
57+
58+ [ MethodImpl ( MethodImplOptions . NoOptimization | MethodImplOptions . NoInlining ) ]
59+ public async Task < bool > CreateDeliveryStreamAsync ( string streamName , string bucketName )
60+ {
61+ // First we need to create an s3 bucket to configure as the delivery destination
62+ var createS3BucketResponse = await _amazonS3Client . PutBucketAsync ( new PutBucketRequest
2963 {
30- // configure the client to use LocalStack
31- // use plausible (but fake) access key and fake secret key so account id parsing can be tested
32- var creds = new BasicAWSCredentials ( "FOOIHSHSDNNAEXAMPLE" , "MOREGIBBERISH" ) ;
33- var config = new AmazonKinesisFirehoseConfig
34- {
35- ServiceURL = "http://localstack:4566" ,
36- AuthenticationRegion = "us-west-2"
37- } ;
64+ BucketName = bucketName ,
65+ BucketRegion = "us-west-2"
66+ } ) ;
3867
39- var client = new AmazonKinesisFirehoseClient ( creds , config ) ;
40- return client ;
41- }
42- private AmazonS3Client GetS3Client ( )
68+ if ( createS3BucketResponse . HttpStatusCode == System . Net . HttpStatusCode . OK )
4369 {
44- // configure the client to use LocalStack
45- // use plausible (but fake) access key and fake secret key so account id parsing can be tested
46- var creds = new BasicAWSCredentials ( "FOOIHSHSDNNAEXAMPLE" , "MOREGIBBERISH" ) ;
47- var config = new AmazonS3Config
70+ var s3DestinationConfiguration = new ExtendedS3DestinationConfiguration
4871 {
49- ServiceURL = "http://localstack:4566" ,
50- AuthenticationRegion = "us-west-2" ,
51- ForcePathStyle = true
72+ BucketARN = "arn:aws:s3:::" + bucketName ,
73+ RoleARN = "arn:aws:iam::000000000000:role/Firehose-Reader-Role" // per Localstack docs: https://docs.localstack.cloud/user-guide/aws/firehose/
5274 } ;
53-
54- var client = new AmazonS3Client ( creds , config ) ;
55- return client ;
56- }
57-
58- [ MethodImpl ( MethodImplOptions . NoOptimization | MethodImplOptions . NoInlining ) ]
59- public async Task < bool > CreateDeliveryStreamAsync ( string streamName , string bucketName )
60- {
61- // First we need to create an s3 bucket to configure as the delivery destination
62- var createS3BucketResponse = await _amazonS3Client . PutBucketAsync ( new PutBucketRequest
75+ var response = await _amazonKinesisFirehoseClient . CreateDeliveryStreamAsync ( new CreateDeliveryStreamRequest
6376 {
64- BucketName = bucketName ,
65- BucketRegion = "us-west-2"
77+ DeliveryStreamName = streamName ,
78+ ExtendedS3DestinationConfiguration = s3DestinationConfiguration
6679 } ) ;
6780
68- if ( createS3BucketResponse . HttpStatusCode == System . Net . HttpStatusCode . OK )
81+ if ( response . HttpStatusCode == System . Net . HttpStatusCode . OK )
6982 {
70- var s3DestinationConfiguration = new ExtendedS3DestinationConfiguration
83+ var request = new DescribeDeliveryStreamRequest
7184 {
72- BucketARN = "arn:aws:s3:::" + bucketName ,
73- RoleARN = "arn:aws:iam::000000000000:role/Firehose-Reader-Role" // per Localstack docs: https://docs.localstack.cloud/user-guide/aws/firehose/
85+ DeliveryStreamName = streamName
7486 } ;
75- var response = await _amazonKinesisFirehoseClient . CreateDeliveryStreamAsync ( new CreateDeliveryStreamRequest
76- {
77- DeliveryStreamName = streamName ,
78- ExtendedS3DestinationConfiguration = s3DestinationConfiguration
79- } ) ;
8087
81- if ( response . HttpStatusCode == System . Net . HttpStatusCode . OK )
82- {
83- var request = new DescribeDeliveryStreamRequest
84- {
85- DeliveryStreamName = streamName
86- } ;
88+ DeliveryStreamStatus status ;
8789
88- DeliveryStreamStatus status ;
90+ // Wait until the stream is ACTIVE and then report success.
91+ Console . Write ( "Waiting for stream to finish being created..." ) ;
8992
90- // Wait until the stream is ACTIVE and then report success.
91- Console . Write ( "Waiting for stream to finish being created..." ) ;
93+ int sleepDuration = 2000 ;
9294
93- int sleepDuration = 2000 ;
94-
95- var startTime = DateTime . Now ;
96- do
97- {
98- await Task . Delay ( sleepDuration ) ;
99-
100- var describeStreamResponse = await _amazonKinesisFirehoseClient . DescribeDeliveryStreamAsync ( request ) ;
101- status = describeStreamResponse . DeliveryStreamDescription . DeliveryStreamStatus ;
95+ var startTime = DateTime . Now ;
96+ do
97+ {
98+ await Task . Delay ( sleepDuration ) ;
10299
103- Console . Write ( "." ) ;
104- }
105- while ( status != "ACTIVE" && DateTime . Now - startTime < TimeSpan . FromMinutes ( 2 ) ) ;
100+ var describeStreamResponse = await _amazonKinesisFirehoseClient . DescribeDeliveryStreamAsync ( request ) ;
101+ status = describeStreamResponse . DeliveryStreamDescription . DeliveryStreamStatus ;
106102
107- return status == DeliveryStreamStatus . ACTIVE ;
108- }
109- else
110- {
111- return false ;
103+ Console . Write ( "." ) ;
112104 }
105+ while ( status != "ACTIVE" && DateTime . Now - startTime < TimeSpan . FromMinutes ( 2 ) ) ;
106+
107+ return status == DeliveryStreamStatus . ACTIVE ;
113108 }
114109 else
115110 {
116111 return false ;
117112 }
118113 }
119-
120- [ MethodImpl ( MethodImplOptions . NoOptimization | MethodImplOptions . NoInlining ) ]
121- public async Task < bool > DeleteDeliveryStreamAsync ( string name )
114+ else
122115 {
123- var response = await _amazonKinesisFirehoseClient . DeleteDeliveryStreamAsync ( new DeleteDeliveryStreamRequest
124- {
125- DeliveryStreamName = name ,
126- AllowForceDelete = true
127- } ) ;
128-
129- return response . HttpStatusCode == System . Net . HttpStatusCode . OK ;
116+ return false ;
130117 }
118+ }
131119
132- [ MethodImpl ( MethodImplOptions . NoOptimization | MethodImplOptions . NoInlining ) ]
133- public async Task < bool > ListDeliveryStreamsAsync ( )
120+ [ MethodImpl ( MethodImplOptions . NoOptimization | MethodImplOptions . NoInlining ) ]
121+ public async Task < bool > DeleteDeliveryStreamAsync ( string name )
122+ {
123+ var response = await _amazonKinesisFirehoseClient . DeleteDeliveryStreamAsync ( new DeleteDeliveryStreamRequest
134124 {
135- var response = await _amazonKinesisFirehoseClient . ListDeliveryStreamsAsync ( new ListDeliveryStreamsRequest ( ) ) ;
125+ DeliveryStreamName = name ,
126+ AllowForceDelete = true
127+ } ) ;
136128
137- if ( response . HttpStatusCode == System . Net . HttpStatusCode . OK )
138- {
139- response . DeliveryStreamNames . ForEach ( s => Console . WriteLine ( $ "Found stream name: { s } ") ) ;
140- return true ;
141- }
142- else
143- {
144- return false ;
145- }
129+ return response . HttpStatusCode == System . Net . HttpStatusCode . OK ;
130+ }
131+
132+ [ MethodImpl ( MethodImplOptions . NoOptimization | MethodImplOptions . NoInlining ) ]
133+ public async Task < bool > ListDeliveryStreamsAsync ( )
134+ {
135+ var response = await _amazonKinesisFirehoseClient . ListDeliveryStreamsAsync ( new ListDeliveryStreamsRequest ( ) ) ;
136+
137+ if ( response . HttpStatusCode == System . Net . HttpStatusCode . OK )
138+ {
139+ response . DeliveryStreamNames . ForEach ( s => Console . WriteLine ( $ "Found stream name: { s } ") ) ;
140+ return true ;
146141 }
142+ else
143+ {
144+ return false ;
145+ }
146+ }
147147
148- [ MethodImpl ( MethodImplOptions . NoOptimization | MethodImplOptions . NoInlining ) ]
149- public async Task < bool > PutRecordAsync ( string streamName , string data )
148+ [ MethodImpl ( MethodImplOptions . NoOptimization | MethodImplOptions . NoInlining ) ]
149+ public async Task < bool > PutRecordAsync ( string streamName , string data )
150+ {
151+ var response = await _amazonKinesisFirehoseClient . PutRecordAsync ( new PutRecordRequest
150152 {
151- var response = await _amazonKinesisFirehoseClient . PutRecordAsync ( new PutRecordRequest
153+ DeliveryStreamName = streamName ,
154+ Record = new Record
152155 {
153- DeliveryStreamName = streamName ,
154- Record = new Record
155- {
156- Data = new System . IO . MemoryStream ( Encoding . UTF8 . GetBytes ( data ) )
157- }
158- } ) ;
156+ Data = new System . IO . MemoryStream ( Encoding . UTF8 . GetBytes ( data ) )
157+ }
158+ } ) ;
159159
160- return response . HttpStatusCode == System . Net . HttpStatusCode . OK ;
161- }
160+ return response . HttpStatusCode == System . Net . HttpStatusCode . OK ;
161+ }
162162
163- [ MethodImpl ( MethodImplOptions . NoOptimization | MethodImplOptions . NoInlining ) ]
164- public async Task < bool > PutRecordBatchAsync ( string streamName , string data )
163+ [ MethodImpl ( MethodImplOptions . NoOptimization | MethodImplOptions . NoInlining ) ]
164+ public async Task < bool > PutRecordBatchAsync ( string streamName , string data )
165+ {
166+ var response = await _amazonKinesisFirehoseClient . PutRecordBatchAsync ( new PutRecordBatchRequest
165167 {
166- var response = await _amazonKinesisFirehoseClient . PutRecordBatchAsync ( new PutRecordBatchRequest
168+ DeliveryStreamName = streamName ,
169+ Records = new System . Collections . Generic . List < Record >
167170 {
168- DeliveryStreamName = streamName ,
169- Records = new System . Collections . Generic . List < Record >
170- {
171- new Record { Data = new System . IO . MemoryStream ( Encoding . UTF8 . GetBytes ( data ) ) } ,
172- new Record { Data = new System . IO . MemoryStream ( Encoding . UTF8 . GetBytes ( data ) ) }
173- }
174- } ) ;
171+ new Record { Data = new System . IO . MemoryStream ( Encoding . UTF8 . GetBytes ( data ) ) } ,
172+ new Record { Data = new System . IO . MemoryStream ( Encoding . UTF8 . GetBytes ( data ) ) }
173+ }
174+ } ) ;
175175
176- return response . HttpStatusCode == System . Net . HttpStatusCode . OK ;
177- }
176+ return response . HttpStatusCode == System . Net . HttpStatusCode . OK ;
177+ }
178178
179- public void Dispose ( )
180- {
181- _amazonKinesisFirehoseClient ? . Dispose ( ) ;
182- _amazonS3Client ? . Dispose ( ) ;
183- }
179+ public void Dispose ( )
180+ {
181+ _amazonKinesisFirehoseClient ? . Dispose ( ) ;
182+ _amazonS3Client ? . Dispose ( ) ;
184183 }
185- }
184+ }
0 commit comments