Skip to content

Commit 95d9c0b

Browse files
authored
Merge pull request #1940 from awsdocs/s3_tests
AWS CodeBuild examples (AWS SDK for JS v3)
2 parents c298279 + a036d8d commit 95d9c0b

20 files changed

+473
-167
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# AWS CodeBuild JavaScript SDK v3 code examples
2+
AWS CodeBuild is a fully-managed source control service that makes it easy for companies to host secure and highly scalable private Git repositories.
3+
## Code examples
4+
This is a workspace where you can find the following AWS SDK for JavaScript version 3 (v3) AWS CodeBuild examples:
5+
6+
- [Create a project](src/createProject.js)
7+
8+
**Note**: All code examples are written in ECMAscript 6 (ES6). For guidelines on converting to CommonJS, see
9+
[JavaScript ES6/CommonJS syntax](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/sdk-examples-javascript-syntax.html).
10+
11+
## Getting started
12+
13+
1. Clone the [AWS SDK Code Samples repo](https://github.com/awsdocs/aws-doc-sdk-examples) to your local environment. See [the Github documentation](https://docs.github.com/en/github/creating-cloning-and-archiving-repositories/cloning-a-repository) for instructions.
14+
15+
2. Install the dependencies listed in the package.json.
16+
17+
```
18+
npm install node -g
19+
cd javascriptv3/example_code/CodeBuild
20+
npm install
21+
```
22+
3. In your text editor, update user variables specified in the ```Inputs``` section of the sample file.
23+
24+
4. Run sample code:
25+
```
26+
cd src
27+
node [example name].js
28+
```
29+
30+
## Unit tests
31+
For more information see, the [README](../README.rst).
32+
33+
## Resources
34+
- [AWS SDK for JavaScript v3 repo](https://github.com/aws/aws-sdk-js-v3)
35+
- [AWS SDK for JavaScript v3 API Reference Guide](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-CodeBuild/index.html)
36+
- [AWS CodeBuild user guide](https://docs.aws.amazon.com/CodeBuild/latest/userguide/welcome.html)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
SPDX-License-Identifier: Apache-2.0
3+
ABOUT THIS NODE.JS EXAMPLE: This example works with AWS SDK for JavaScript version 3 (v3),
4+
which is codeBuild.JavaScript.createProjectV3available at https://github.com/aws/aws-sdk-js-v3. For information about how AWS CodeBuild works, see https://docs.aws.amazon.com/codebuild/latest/userguide/concepts.html.
5+
6+
Purpose:
7+
createProject.js demonstrates how to create an AWS CodeBuild project.
8+
9+
Inputs (replace in code):
10+
- TYPE_OF_BUILD_OUTPUT
11+
- COMPUTE_TYPE
12+
- IMAGE_NAME
13+
- ENVIRONMENT_TYPE
14+
- PROJECT_NAME
15+
- IAM_ROLE
16+
- SOURCE_TYPE
17+
- LOCATION_TO_BUILD_SOURCE_CODE
18+
19+
Note: This sample specifies the mimimum parameters required, and depending on your choices, alternative parameters may be required.
20+
For further details on all available parameters, see https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CodeBuild.html#createProject-property. (This is for V2, but V3 has similar
21+
parameters).
22+
23+
Running the code:
24+
node createProject.js
25+
*/
26+
// snippet-start:[codeBuild.JavaScript.createProjectV3]
27+
// Get service clients module and commands using ES6 syntax.
28+
import { CreateProjectCommand } from "@aws-sdk/client-codebuild";
29+
import { codeBuildClient } from "./libs/codeBuildClient.js";
30+
31+
// Set the bucket parameters.
32+
33+
export const params = {
34+
artifacts: { /* required */
35+
/* Required. Options include CODEPIPELINE | S3 | NO_ARTIFACTS. */
36+
type: "CODEPIPELINE"
37+
},
38+
environment: { /* required */
39+
/* Required. Options include BUILD_GENERAL1_SMALL | BUILD_GENERAL1_MEDIUM | BUILD_GENERAL1_LARGE | BUILD_GENERAL1_2XLARGE. */
40+
computeType: "BUILD_GENERAL1_SMALL",
41+
image: 'myiage', /* Required. */
42+
/* Required. Options include WINDOWS_CONTAINER | LINUX_CONTAINER | LINUX_GPU_CONTAINER | ARM_CONTAINER | WINDOWS_SERVER_2019_CONTAINER. */
43+
type: "BUILD_GENERAL1_SMALL"
44+
},
45+
name: 'mytestproject', /* Required. */
46+
/* Required. This AWS Identity and Access Management (IAM) role must have permissions to create an AWS CodeBuild project. */
47+
serviceRole: 'IAM_ROLE',
48+
source: { /* required */
49+
type: "GITHUB", /* Required. Options include CODECOMMIT | CODEPIPELINE | GITHUB | S3 | BITBUCKET | GITHUB_ENTERPRISE | NO_SOURCE */
50+
auth: {
51+
type: "OAUTH" /* Required. 'OAUTH' is the only valid value. */
52+
},
53+
location: "LOCATION_TO_BUILD_SOURCE_CODE" /* Required. */
54+
}
55+
};
56+
57+
// Create the AWS CodeBuild project.
58+
export const run = async () => {
59+
try {
60+
const data = await codeBuildClient.send(new CreateProjectCommand(params));
61+
console.log("Success", data);
62+
return data; // For unit tests.
63+
} catch (err) {
64+
console.log("Error", err);
65+
}
66+
};
67+
run();
68+
// snippet-end:[codeBuild.JavaScript.createProjectV3]
69+
// For unit tests only.
70+
// module.exports ={run, bucketParams};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
SPDX-License-Identifier: Apache-2.0
3+
ABOUT THIS NODE.JS EXAMPLE: This example works with AWS SDK for JavaScript version 3 (v3),
4+
which is available at https://github.com/aws/aws-sdk-js-v3.
5+
6+
Purpose:
7+
codeBuildClient.js is a helper function that creates the Amazon CodeBuild service clients.
8+
9+
Inputs (replace in code):
10+
- REGION
11+
12+
*/
13+
// snippet-start:[code-build.JavaScript.codeBuildClient]
14+
import { CodeBuildClient } from "@aws-sdk/client-codebuild";
15+
16+
const REGION = "REGION";
17+
18+
// Create an AWS CodeBuild service client object.
19+
const codeBuildClient = new CodeBuildClient({region: REGION});
20+
21+
export { codeBuildClient };
22+
// snippet-end:[code-build.JavaScript.codeBuildClient]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "aws-sdk-v3-codebuild-examples",
3+
"version": "1.0.0",
4+
"repository": "[email protected]/awsdocs/aws-doc-sdk-examples/tree/master/javascriptv3/example_code/codebuild.git",
5+
"author": "Brian Murray <[email protected]>",
6+
"license": "Apache 2.0",
7+
"dependencies": {
8+
"@aws-sdk/client-codebuild": "^3.3.0"
9+
},
10+
"type": "module"
11+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
presets: ['@babel/preset-env']
3+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
jest.mock("../src/libs/codeBuildClient.js");
2+
jest.mock("@aws-sdk/client-codebuild");
3+
4+
// Get service clients module and commands.
5+
import 'regenerator-runtime/runtime'
6+
import { run, params } from "../src/createProject";
7+
import { codeBuildClient } from "../src/libs/codeBuildClient";
8+
9+
describe("@aws-sdk/client-codebuild mock", () => {
10+
it("should successfully mock CodeBuild client", async () => {
11+
codeBuildClient.send.mockResolvedValue({ isMock: true });
12+
const response = await run(params);
13+
expect(response.isMock).toEqual(true);
14+
});
15+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
testEnvironment: 'node',
3+
moduleDirectories: ["node_modules"],
4+
testMatch: ["**/tests/*.test.js"],
5+
transform: {
6+
"^.+\\.(js|jsx)$": "babel-jest"
7+
}
8+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "aws-sdk-v3-cloudwatch-examples",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"repository": "[email protected]/awsdocs/aws-doc-sdk-examples/tree/master/javascriptv3/example_code/cloudwatch.git",
6+
"author": "Brian Murray <[email protected]>, Alex Forsyth <[email protected]>",
7+
"license": "Apache 2.0",
8+
"dependencies": {
9+
"@aws-sdk/client-codebuild": "^3.3.0"
10+
},
11+
"devDependencies": {
12+
"@babel/preset-env": "^7.14.4",
13+
"jest": "^26.6.3",
14+
"regenerator-runtime": "^0.13.7"
15+
},
16+
"scripts": {
17+
"test": "jest"
18+
}
19+
}

javascriptv3/example_code/codecommit/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,5 @@ For more information see, the [README](../README.rst).
4444

4545
## Resources
4646
- [AWS SDK for JavaScript v3 repo](https://github.com/aws/aws-sdk-js-v3)
47-
- [AWS SDK for JavaScript v3 Developer Guide](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/s3-examples.html)
4847
- [AWS SDK for JavaScript v3 API Reference Guide](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-codecommit/index.html)
4948
- [AWS CodeCommit user guide](https://docs.aws.amazon.com/codecommit/latest/userguide/welcome.html)

javascriptv3/example_code/codecommit/src/package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,5 @@
77
"dependencies": {
88
"@aws-sdk/client-codecommit ": "^3.3.0"
99
},
10-
"scripts": {
11-
"test": "jest"
12-
},
1310
"type": "module"
1411
}

ruby/example_code/sns/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Amazon Cloudwatch JavaScript SDK v3 code examples
2+
Amazon Cloudwatch enables you to collect, access, and correlate this data on a single platform from across all your AWS resources, applications, and services.
3+
4+
## Code examples
5+
This is a workspace where you can find the following AWS SDK for JavaScript version 3 (v3) Amazon Cloudwatch examples:
6+
7+
- [Create a subscription](./sns-ruby-example-create-subscription.rb)
8+
- [Create a topic](./sns-ruby-example-create-topic.rb)
9+
- [Enable a resource](./sns-ruby-example-enable-resource.rb)
10+
- [Send a message](./sns-ruby-example-send-message.rb)
11+
- [List subscriptions](./sns-ruby-example-show-subscriptions.rb)
12+
- [List topics](./sns-ruby-example-show-topics.rb)
13+
14+
15+
## Getting started
16+
17+
1. Clone the [AWS Code Samples repo](https://github.com/awsdocs/aws-doc-sdk-examples) to your local environment.
18+
See [the Github documentation](https://docs.github.com/en/github/creating-cloning-and-archiving-repositories/cloning-a-repository) for
19+
instructions.
20+
21+
22+
## Resources
23+
- [AWS SDK for Ruby repo](https://github.com/aws/aws-sdk-ruby) .
24+
- [AWS SDK for Ruby Developer Guide](https://docs.aws.amazon.com/sdk-for-ruby/v3/developer-guide/welcome.html)
25+
- [AWS SDK for Ruby v3 API Reference Guide](https://docs.aws.amazon.com/sdk-for-ruby/v3/api/)
26+
Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,47 @@
1-
# snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.]
2-
# snippet-sourceauthor:[Doug-AWS]
3-
# snippet-sourcedescription:[Subscribes a user to an SNS topic.]
4-
# snippet-keyword:[Amazon Simple Notification Service]
5-
# snippet-keyword:[topic method]
6-
# snippet-keyword:[Topic.subscribe method]
7-
# snippet-keyword:[Ruby]
8-
# snippet-sourcesyntax:[ruby]
9-
# snippet-service:[sns]
10-
# snippet-keyword:[Code Sample]
11-
# snippet-sourcetype:[full-example]
12-
# snippet-sourcedate:[2018-03-16]
13-
# Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
14-
#
15-
# This file is licensed under the Apache License, Version 2.0 (the "License").
16-
# You may not use this file except in compliance with the License. A copy of the
17-
# License is located at
18-
#
19-
# http://aws.amazon.com/apache2.0/
20-
#
21-
# This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
22-
# OF ANY KIND, either express or implied. See the License for the specific
23-
# language governing permissions and limitations under the License.
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# Purpose:
5+
# sns-ruby-example-create-subscription.rb demonstrates how to create an Amazon Simple Notification Services (SNS) subscription using
6+
# the AWS SDK for JavaScript (v3).
7+
8+
# Inputs:
9+
# - REGION
10+
# - SNS_TOPIC_ARN
11+
# - EMAIL_ADDRESS
12+
13+
# snippet-start:[sns.Ruby.createSubscription]
2414

2515
require 'aws-sdk-sns' # v2: require 'aws-sdk'
2616

27-
sns = Aws::SNS::Resource.new(region: 'us-west-2')
17+
def subscription_created?(sns_client, topic_arn, protocol, endpoint)
18+
19+
sns_client.subscribe(topic_arn: topic_arn, protocol: protocol, endpoint: endpoint)
20+
21+
rescue StandardError => e
22+
puts "Error while creating the subscription: #{e.message}"
23+
end
24+
25+
# Full example call:
26+
def run_me
27+
28+
protocol = 'email'
29+
endpoint = 'EMAIL_ADDRESS'
30+
topic_arn = 'TOPIC_ARN'
31+
region = 'REGION'
32+
33+
sns_client = Aws::SNS::Client.new(region: region)
34+
35+
puts "Creating the subscription."
2836

29-
topic = sns.topic('arn:aws:sns:us-west-2:123456789:MyGroovyTopic')
37+
if subscription_created?(sns_client, topic_arn, protocol, endpoint)
38+
puts 'The subscriptions was created.'
39+
else
40+
puts 'The subscription was not created. Stopping program.'
41+
exit 1
42+
end
43+
end
3044

31-
sub = topic.subscribe({
32-
protocol: 'email',
33-
endpoint: '[email protected]'
34-
})
45+
run_me if $PROGRAM_NAME == __FILE__
3546

36-
puts sub.arn
47+
# snippet-end:[sns.Ruby.createSubscription]
Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,41 @@
1-
# snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.]
2-
# snippet-sourceauthor:[Doug-AWS]
3-
# snippet-sourcedescription:[Creates an SNS topic.]
4-
# snippet-keyword:[Amazon Simple Notification Service]
5-
# snippet-keyword:[create_topic method]
6-
# snippet-keyword:[Ruby]
7-
# snippet-sourcesyntax:[ruby]
8-
# snippet-service:[sns]
9-
# snippet-keyword:[Code Sample]
10-
# snippet-sourcetype:[full-example]
11-
# snippet-sourcedate:[2018-03-16]
12-
# Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
13-
#
14-
# This file is licensed under the Apache License, Version 2.0 (the "License").
15-
# You may not use this file except in compliance with the License. A copy of the
16-
# License is located at
17-
#
18-
# http://aws.amazon.com/apache2.0/
19-
#
20-
# This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
21-
# OF ANY KIND, either express or implied. See the License for the specific
22-
# language governing permissions and limitations under the License.
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# Purpose:
5+
# sns-ruby-example-create-topic.rb demonstrates how to create an Amazon Simple Notification Services (SNS) topic using
6+
# the AWS SDK for JavaScript (v3).
7+
8+
# Inputs:
9+
# - REGION
10+
# - TOPIC_NAME
11+
12+
# snippet-start:[sns.Ruby.createTopic]
2313

2414
require 'aws-sdk-sns' # v2: require 'aws-sdk'
2515

26-
sns = Aws::SNS::Resource.new(region: 'us-west-2')
16+
def topic_created?(sns_client, topic_name)
17+
18+
sns_client.create_topic(name: topic_name)
19+
rescue StandardError => e
20+
puts "Error while creating the topic named '#{topic_name}': #{e.message}"
21+
end
22+
23+
# Full example call:
24+
def run_me
25+
topic_name = 'TOPIC_NAME'
26+
region = 'REGION'
27+
28+
sns_client = Aws::SNS::Client.new(region: region)
29+
30+
puts "Creating the topic '#{topic_name}'..."
31+
32+
if topic_created?(sns_client, topic_name)
33+
puts 'The topic was created.'
34+
else
35+
puts 'The topic was not created. Stopping program.'
36+
exit 1
37+
end
38+
end
2739

28-
topic = sns.create_topic(name: 'MyGroovyTopic')
29-
puts topic.arn
40+
run_me if $PROGRAM_NAME == __FILE__
41+
# snippet-end:[sns.Ruby.createTopic]

0 commit comments

Comments
 (0)