Exemplo n.º 1
0
        public void TestInitialize()
        {
            // Create S3 Bucket
            BucketName = "sdk-dotnet-integ-test-bucket-firehose" + DateTime.Now.Ticks;
            s3Client.PutBucket(BucketName);

            // Create IAM Role
            RoleName = "NetFirehoseTestRole" + DateTime.Now.Ticks;
            if (string.IsNullOrEmpty(TestAccountId))
            {
                Assert.Fail("TestAccountId must be specified to run these tests");
            }

            var iamCreateResponse = iamClient.CreateRole(new CreateRoleRequest
            {
                RoleName = RoleName,
                AssumeRolePolicyDocument = string.Format(FirehoseAssumeRolePolicyDocumentFormat, TestAccountId)
            });
            string roleArn = iamCreateResponse.Role.Arn;

            Assert.IsNotNull(roleArn);

            // Attach Policy to Role
            PolicyName = "NetFirehoseTestRolePolicy" + DateTime.Now.Ticks;
            iamClient.PutRolePolicy(new PutRolePolicyRequest()
            {
                PolicyDocument = string.Format(RolePolicyDocumentFormat, BucketName),
                PolicyName     = PolicyName,
                RoleName       = RoleName
            });

            // Wait for eventual consistency of role.
            Thread.Sleep(TimeSpan.FromSeconds(10));

            // Create Firehose Delivery Stream
            string bucketArn = "arn:aws:s3:::" + BucketName;

            DeliveryStreamName = "dotnet-test-delivery-stream" + DateTime.Now.Ticks;
            string deliveryStreamArn = Client.CreateDeliveryStream(new CreateDeliveryStreamRequest()
            {
                DeliveryStreamName         = DeliveryStreamName,
                S3DestinationConfiguration = new S3DestinationConfiguration()
                {
                    BucketARN = bucketArn,
                    RoleARN   = roleArn
                }
            }).DeliveryStreamARN;

            if (string.IsNullOrEmpty(deliveryStreamArn))
            {
                Assert.Fail("Expected a deliveryStreamArn value");
            }

            // Wait for Delivery Stream to be active
            DeliveryStreamStatus streamStatus = DeliveryStreamStatus.CREATING;
            var timeout = DateTime.Now.AddSeconds(120);

            while (streamStatus != DeliveryStreamStatus.ACTIVE && DateTime.Now.Ticks < timeout.Ticks)
            {
                streamStatus = Client.DescribeDeliveryStream(new DescribeDeliveryStreamRequest()
                {
                    DeliveryStreamName = DeliveryStreamName
                }).DeliveryStreamDescription.DeliveryStreamStatus;
                Assert.AreNotEqual(streamStatus, DeliveryStreamStatus.DELETING);
                Thread.Sleep(TimeSpan.FromSeconds(2));
            }
            Assert.AreNotEqual(streamStatus, DeliveryStreamStatus.CREATING, "Did not exit CREATING state within time limit.");
        }
Exemplo n.º 2
0
        public void TestPipelineOperations()
        {
            var inputBucket    = S3TestUtils.CreateBucket(s3Client);
            var outputBucket   = S3TestUtils.CreateBucket(s3Client);
            var pipelineName   = "sdktest-pipeline" + DateTime.Now.Ticks;
            var roleName       = "sdktest-ets-role" + DateTime.Now.Ticks;
            var policyName     = "Access_Policy";
            var pipelineId     = string.Empty;
            var pipelineExists = false;

            try
            {
                // Create a role with trust policy
                var role = iamClient.CreateRole(new CreateRoleRequest
                {
                    RoleName = roleName,
                    AssumeRolePolicyDocument = TrustPolicy
                }).Role;
                // Set access policy
                iamClient.PutRolePolicy(new PutRolePolicyRequest
                {
                    RoleName       = roleName,
                    PolicyDocument = AccessPolicy,
                    PolicyName     = policyName
                });

                Client.ListPipelines();

                // Create Pipeline
                var pipeline = Client.CreatePipeline(
                    new CreatePipelineRequest
                {
                    Name          = pipelineName,
                    InputBucket   = inputBucket,
                    OutputBucket  = outputBucket,
                    Notifications = new Notifications
                    {
                    },
                    Role         = role.Arn,
                    AwsKmsKeyArn = kmsKeyArn
                }).Pipeline;
                pipelineExists = true;
                Assert.IsNotNull(pipeline);
                Assert.AreEqual(pipeline.Name, pipelineName);
                Thread.Sleep(1000 * 5);
                pipelineId = pipeline.Id;

                // List Pipelines
                var pipelines = Client.ListPipelines().Pipelines;
                Assert.IsTrue(pipelines.Count > 0);
                pipelines.Contains(pipeline);

                // Get Pipeline
                var readPipelineResult = Client.ReadPipeline(
                    new ReadPipelineRequest()
                {
                    Id = pipelineId
                });
                Assert.AreEqual(readPipelineResult.Pipeline.Id, pipelineId);

                // Update pipeline
                Client.UpdatePipelineStatus(
                    new UpdatePipelineStatusRequest
                {
                    Id     = pipelineId,
                    Status = "Paused"
                });

                // Get pipeline
                readPipelineResult = Client.ReadPipeline(
                    new ReadPipelineRequest {
                    Id = pipelineId
                });
                Assert.AreEqual("Paused".ToLower(), readPipelineResult.Pipeline.Status.ToLower());

                // List jobs
                var jobs = Client.ListJobsByPipeline(
                    new ListJobsByPipelineRequest
                {
                    PipelineId = pipelineId,
                    Ascending  = "true"
                }).Jobs;

                // Remove pipeline
                Client.DeletePipeline(
                    new DeletePipelineRequest {
                    Id = pipelineId
                });
                pipelineExists = false;

                AssertExtensions.ExpectException(() =>
                {
                    readPipelineResult = Client.ReadPipeline(
                        new ReadPipelineRequest()
                    {
                        Id = pipelineId
                    });
                }, typeof(ResourceNotFoundException));
            }
            finally
            {
                s3Client.DeleteBucket(new DeleteBucketRequest {
                    BucketName = inputBucket
                });
                s3Client.DeleteBucket(new DeleteBucketRequest {
                    BucketName = outputBucket
                });

                iamClient.DeleteRolePolicy(new DeleteRolePolicyRequest
                {
                    RoleName   = roleName,
                    PolicyName = policyName
                });

                iamClient.DeleteRole(new DeleteRoleRequest
                {
                    RoleName = roleName
                });

                if (pipelineExists)
                {
                    // Remove pipeline
                    Client.DeletePipeline(new DeletePipelineRequest {
                        Id = pipelineId
                    });
                }
            }
        }
Exemplo n.º 3
0
        public void LambdaFunctionTest()
        {
            const string HELLO_SCRIPT =
                @"console.log('Loading http')
 
exports.handler = function (request, response) {
    response.write(""Hello, world!"");
    response.end();
    console.log(""Request completed"");
}";
            MemoryStream stream = new MemoryStream();

            using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Create, true))
            {
                var entry = archive.CreateEntry("helloworld.js");
                using (var entryStream = entry.Open())
                    using (var writer = new StreamWriter(entryStream))
                    {
                        writer.Write(HELLO_SCRIPT);
                    }
            }
            stream.Position = 0;

            var  functionName = "HelloWorld";
            bool uploaded     = false;

            var  iamRoleName    = "Lambda-" + DateTime.Now.Ticks;
            bool iamRoleCreated = false;

            try
            {
                var iamCreateResponse = iamClient.CreateRole(new CreateRoleRequest
                {
                    RoleName = iamRoleName,
                    AssumeRolePolicyDocument = LAMBDA_ASSUME_ROLE_POLICY
                });

                var statement = new Statement(Statement.StatementEffect.Allow);
                statement.Actions.Add(S3ActionIdentifiers.PutObject);
                statement.Actions.Add(S3ActionIdentifiers.GetObject);
                statement.Resources.Add(new Resource("*"));


                var policy = new Amazon.Auth.AccessControlPolicy.Policy();
                policy.Statements.Add(statement);

                iamClient.PutRolePolicy(new PutRolePolicyRequest
                {
                    RoleName       = iamRoleName,
                    PolicyName     = "admin",
                    PolicyDocument = policy.ToJson()
                });

                // Wait for the role and policy to propagate
                Thread.Sleep(5000);

                var uploadRequest = new UploadFunctionRequest
                {
                    FunctionName = functionName,
                    FunctionZip  = stream,
                    Handler      = "helloworld.handler",
                    Mode         = Mode.Event,
                    Runtime      = Runtime.Nodejs,
                    Role         = iamCreateResponse.Role.Arn
                };

                var uploadResponse = lambdaClient.UploadFunction(uploadRequest);
                uploaded = true;
                Assert.IsTrue(uploadResponse.CodeSize > 0);
                Assert.IsNotNull(uploadResponse.ConfigurationId);

                // List all the functions and make sure the newly uploaded function is in the collection
                var listResponse = lambdaClient.ListFunctions();
                var function     = listResponse.Functions.FirstOrDefault(x => x.FunctionName == functionName);
                Assert.IsNotNull(function);
                Assert.AreEqual("helloworld.handler", function.Handler);
                Assert.AreEqual(iamCreateResponse.Role.Arn, function.Role);

                // Get the function with a presigned URL to the uploaded code
                var getFunctionResponse = lambdaClient.GetFunction(functionName);
                Assert.AreEqual("helloworld.handler", getFunctionResponse.Configuration.Handler);
                Assert.IsNotNull(getFunctionResponse.Code.Location);

                // Get the function's configuration only
                var getFunctionConfiguration = lambdaClient.GetFunctionConfiguration(functionName);
                Assert.AreEqual("helloworld.handler", getFunctionConfiguration.Handler);

                // Call the function
                var invokeResponse = lambdaClient.InvokeAsync(functionName);
                Assert.AreEqual(invokeResponse.Status, 202); // Status Code Accepted
            }
            finally
            {
                if (uploaded)
                {
                    lambdaClient.DeleteFunction(functionName);
                }

                if (iamRoleCreated)
                {
                    iamClient.DeleteRole(new DeleteRoleRequest {
                        RoleName = iamRoleName
                    });
                }
            }
        }