Skip to content

Commit a0a6635

Browse files
authored
Merge pull request #1470 from scmacdon/master
Update the CodeDeploy Java V2 examples
2 parents be532ef + d30c4f9 commit a0a6635

File tree

8 files changed

+146
-232
lines changed

8 files changed

+146
-232
lines changed
Lines changed: 67 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,67 @@
1-
//snippet-sourcedescription:[CreateApplication.java demonstrates how to create an application.]
2-
//snippet-keyword:[Java]
3-
//snippet-keyword:[Code Sample]
4-
//snippet-keyword:[AWS CodeDeploy
5-
//snippet-service:[AWS CodeDeploy]
6-
//snippet-sourcetype:[full-example]
7-
//snippet-sourcedate:[10/3/2020]
8-
//snippet-sourceauthor:[scmacdon AWS]
9-
10-
/*
11-
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
12-
*
13-
* Licensed under the Apache License, Version 2.0 (the "License").
14-
* You may not use this file except in compliance with the License.
15-
* A copy of the License is located at
16-
*
17-
* http://aws.amazon.com/apache2.0
18-
*
19-
* or in the "license" file accompanying this file. This file is distributed
20-
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
21-
* express or implied. See the License for the specific language governing
22-
* permissions and limitations under the License.
23-
*/
24-
package com.example.deploy;
25-
26-
// snippet-start:[codedeploy.java2.create_app.import]
27-
import software.amazon.awssdk.regions.Region;
28-
import software.amazon.awssdk.services.codedeploy.CodeDeployClient;
29-
import software.amazon.awssdk.services.codedeploy.model.CodeDeployException;
30-
import software.amazon.awssdk.services.codedeploy.model.ComputePlatform;
31-
import software.amazon.awssdk.services.codedeploy.model.CreateApplicationRequest;
32-
import software.amazon.awssdk.services.codedeploy.model.CreateApplicationResponse;
33-
// snippet-end:[codedeploy.java2.create_app.import]
34-
35-
public class CreateApplication {
36-
37-
public static void main(String[] args) {
38-
39-
final String USAGE = "\n" +
40-
"Usage:\n" +
41-
" CreateApplication <appName> \n\n" +
42-
"Where:\n" +
43-
" appName - the name of the application \n";
44-
45-
if (args.length < 1) {
46-
System.out.println(USAGE);
47-
System.exit(1);
48-
}
49-
50-
/* Read the name from command args*/
51-
String appName = args[0];
52-
53-
Region region = Region.US_EAST_1;
54-
CodeDeployClient deployClient = CodeDeployClient.builder()
55-
.region(region)
56-
.build();
57-
58-
// Create the application
59-
createApp(deployClient, appName);
60-
}
61-
62-
// snippet-start:[codedeploy.java2.create_app.main]
63-
public static void createApp(CodeDeployClient deployClient, String appName) {
64-
try {
65-
CreateApplicationRequest applicationRequest = CreateApplicationRequest.builder()
66-
.applicationName(appName)
67-
.computePlatform(ComputePlatform.SERVER)
68-
.build();
69-
70-
CreateApplicationResponse applicationResponse = deployClient.createApplication(applicationRequest);
71-
String appId = applicationResponse.applicationId();
72-
System.out.println("The application ID is "+appId);
73-
74-
} catch (CodeDeployException e) {
75-
System.err.println(e.awsErrorDetails().errorMessage());
76-
System.exit(1);
77-
}
78-
}
79-
// snippet-end:[codedeploy.java2.create_app.main]
80-
}
1+
//snippet-sourcedescription:[CreateApplication.java demonstrates how to create an application.]
2+
//snippet-keyword:[AWS SDK for Java v2]
3+
//snippet-keyword:[Code Sample]
4+
//snippet-keyword:[AWS CodeDeploy
5+
//snippet-sourcetype:[full-example]
6+
//snippet-sourcedate:[11/3/2020]
7+
//snippet-sourceauthor:[scmacdon AWS]
8+
9+
/*
10+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
11+
SPDX-License-Identifier: Apache-2.0
12+
*/
13+
package com.example.deploy;
14+
15+
// snippet-start:[codedeploy.java2.create_app.import]
16+
import software.amazon.awssdk.regions.Region;
17+
import software.amazon.awssdk.services.codedeploy.CodeDeployClient;
18+
import software.amazon.awssdk.services.codedeploy.model.CodeDeployException;
19+
import software.amazon.awssdk.services.codedeploy.model.ComputePlatform;
20+
import software.amazon.awssdk.services.codedeploy.model.CreateApplicationRequest;
21+
import software.amazon.awssdk.services.codedeploy.model.CreateApplicationResponse;
22+
// snippet-end:[codedeploy.java2.create_app.import]
23+
24+
public class CreateApplication {
25+
26+
public static void main(String[] args) {
27+
28+
final String USAGE = "\n" +
29+
"Usage:\n" +
30+
" CreateApplication <appName> \n\n" +
31+
"Where:\n" +
32+
" appName - the name of the application. \n";
33+
34+
if (args.length != 1) {
35+
System.out.println(USAGE);
36+
System.exit(1);
37+
}
38+
39+
String appName = args[0];
40+
Region region = Region.US_EAST_1;
41+
CodeDeployClient deployClient = CodeDeployClient.builder()
42+
.region(region)
43+
.build();
44+
45+
createApp(deployClient, appName);
46+
deployClient.close();
47+
}
48+
49+
// snippet-start:[codedeploy.java2.create_app.main]
50+
public static void createApp(CodeDeployClient deployClient, String appName) {
51+
try {
52+
CreateApplicationRequest applicationRequest = CreateApplicationRequest.builder()
53+
.applicationName(appName)
54+
.computePlatform(ComputePlatform.SERVER)
55+
.build();
56+
57+
CreateApplicationResponse applicationResponse = deployClient.createApplication(applicationRequest);
58+
String appId = applicationResponse.applicationId();
59+
System.out.println("The application ID is "+appId);
60+
61+
} catch (CodeDeployException e) {
62+
System.err.println(e.awsErrorDetails().errorMessage());
63+
System.exit(1);
64+
}
65+
}
66+
// snippet-end:[codedeploy.java2.create_app.main]
67+
}

javav2/example_code/codedeploy/src/main/java/com/example/deploy/CreateDeploymentGroup.java

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
11
//snippet-sourcedescription:[CreateDeploymentGroup.java demonstrates how to create a deployment group.]
2-
//snippet-keyword:[Java]
2+
//snippet-keyword:[AWS SDK for Java v2]
33
//snippet-keyword:[Code Sample]
44
//snippet-keyword:[AWS CodeDeploy
5-
//snippet-service:[AWS CodeDeploy]
65
//snippet-sourcetype:[full-example]
7-
//snippet-sourcedate:[10/3/2020]
6+
//snippet-sourcedate:[11/3/2020]
87
//snippet-sourceauthor:[scmacdon AWS]
8+
99
/*
10-
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
11-
*
12-
* Licensed under the Apache License, Version 2.0 (the "License").
13-
* You may not use this file except in compliance with the License.
14-
* A copy of the License is located at
15-
*
16-
* http://aws.amazon.com/apache2.0
17-
*
18-
* or in the "license" file accompanying this file. This file is distributed
19-
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
20-
* express or implied. See the License for the specific language governing
21-
* permissions and limitations under the License.
22-
*/
10+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
11+
SPDX-License-Identifier: Apache-2.0
12+
*/
2313

2414
package com.example.deploy;
2515

@@ -44,33 +34,33 @@ public static void main(String[] args) {
4434

4535
final String USAGE = "\n" +
4636
"Usage:\n" +
47-
" CreateDeploymentGroup <deploymentGroupName><appName><serviceRoleArn><tagKey><tagValue> \n\n" +
37+
" CreateDeploymentGroup <deploymentGroupName> <appName> <serviceRoleArn> <tagKey> <tagValue> \n\n" +
4838
"Where:\n" +
49-
" deploymentGroupName - the name of the deployment group \n" +
50-
" appName - the name of the application \n" +
51-
" serviceRoleArn - a service role Amazon Resource Name (ARN) that allows AWS CodeDeploy to act on the user's behalf \n" +
52-
" tagKey - the tag filter key (ie, AppName) \n"+
53-
" tagValue - the tag filter value (ie, mywebapp)\n";
39+
" deploymentGroupName - the name of the deployment group. \n" +
40+
" appName - the name of the application. \n" +
41+
" serviceRoleArn - a service role Amazon Resource Name (ARN) that allows AWS CodeDeploy to act on the user's behalf. \n" +
42+
" tagKey - the tag filter key (ie, AppName). \n"+
43+
" tagValue - the tag filter value (ie, mywebapp).\n";
5444

55-
if (args.length < 5) {
45+
if (args.length != 5) {
5646
System.out.println(USAGE);
5747
System.exit(1);
5848
}
5949

60-
Region region = Region.US_EAST_1;
61-
CodeDeployClient deployClient = CodeDeployClient.builder()
62-
.region(region)
63-
.build();
64-
65-
/* Read the name from command args*/
6650
String deploymentGroupName = args[0] ;
6751
String appName = args[1];
6852
String serviceRoleArn= args[2];
6953
String tagKey=args[3];
7054
String tagValue=args[4];
7155

56+
Region region = Region.US_EAST_1;
57+
CodeDeployClient deployClient = CodeDeployClient.builder()
58+
.region(region)
59+
.build();
60+
7261
String groupId = createNewDeploymentGroup(deployClient, deploymentGroupName, appName, serviceRoleArn, tagKey, tagValue );
7362
System.out.println("The group deployment ID is "+groupId);
63+
deployClient.close();
7464
}
7565

7666
// snippet-start:[codedeploy.java2.create_deployment_group.main]

javav2/example_code/codedeploy/src/main/java/com/example/deploy/DeleteApplication.java

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
11
//snippet-sourcedescription:[DeleteApplication.java demonstrates how to delete an application.]
2-
//snippet-keyword:[Java]
2+
//snippet-keyword:[AWS SDK for Java v2]
33
//snippet-keyword:[Code Sample]
44
//snippet-keyword:[AWS CodeDeploy
5-
//snippet-service:[AWS CodeDeploy]
65
//snippet-sourcetype:[full-example]
7-
//snippet-sourcedate:[10/3/2020]
6+
//snippet-sourcedate:[11/3/2020]
87
//snippet-sourceauthor:[scmacdon AWS]
8+
99
/*
10-
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
11-
*
12-
* Licensed under the Apache License, Version 2.0 (the "License").
13-
* You may not use this file except in compliance with the License.
14-
* A copy of the License is located at
15-
*
16-
* http://aws.amazon.com/apache2.0
17-
*
18-
* or in the "license" file accompanying this file. This file is distributed
19-
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
20-
* express or implied. See the License for the specific language governing
21-
* permissions and limitations under the License.
22-
*/
10+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
11+
SPDX-License-Identifier: Apache-2.0
12+
*/
2313

2414
package com.example.deploy;
2515

@@ -38,23 +28,21 @@ public static void main(String[] args) {
3828
"Usage:\n" +
3929
" DeleteApplication <appName> \n\n" +
4030
"Where:\n" +
41-
" appName - the name of the application \n";
31+
" appName - the name of the application. \n";
4232

43-
if (args.length < 1) {
33+
if (args.length != 1) {
4434
System.out.println(USAGE);
4535
System.exit(1);
4636
}
4737

48-
/* Read the name from command args*/
4938
String appName = args[0];
50-
5139
Region region = Region.US_EAST_1;
5240
CodeDeployClient deployClient = CodeDeployClient.builder()
5341
.region(region)
5442
.build();
5543

56-
// Delete the specified application
5744
delApplication(deployClient, appName);
45+
deployClient.close();
5846
}
5947

6048
// snippet-start:[codedeploy.java2.delete_app.main]

javav2/example_code/codedeploy/src/main/java/com/example/deploy/DeleteDeploymentGroup.java

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
11
//snippet-sourcedescription:[DeleteDeploymentGroup.java demonstrates how to delete a deployment group.]
2-
//snippet-keyword:[Java]
2+
//snippet-keyword:[AWS SDK for Java v2]
33
//snippet-keyword:[Code Sample]
44
//snippet-keyword:[AWS CodeDeploy
5-
//snippet-service:[AWS CodeDeploy]
65
//snippet-sourcetype:[full-example]
7-
//snippet-sourcedate:[10/3/2020]
6+
//snippet-sourcedate:[11/3/2020]
87
//snippet-sourceauthor:[scmacdon AWS]
8+
99
/*
10-
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
11-
*
12-
* Licensed under the Apache License, Version 2.0 (the "License").
13-
* You may not use this file except in compliance with the License.
14-
* A copy of the License is located at
15-
*
16-
* http://aws.amazon.com/apache2.0
17-
*
18-
* or in the "license" file accompanying this file. This file is distributed
19-
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
20-
* express or implied. See the License for the specific language governing
21-
* permissions and limitations under the License.
22-
*/
10+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
11+
SPDX-License-Identifier: Apache-2.0
12+
*/
2313

2414
package com.example.deploy;
2515

@@ -36,17 +26,16 @@ public static void main(String[] args) {
3626

3727
final String USAGE = "\n" +
3828
"Usage:\n" +
39-
" DeleteDeploymentGroup <appName><deploymentGroupName>\n\n" +
29+
" DeleteDeploymentGroup <appName> <deploymentGroupName>\n\n" +
4030
"Where:\n" +
41-
" appName - the name of the application \n"+
42-
" deploymentGroupName - the name of the deployment group \n";
31+
" appName - the name of the application. \n"+
32+
" deploymentGroupName - the name of the deployment group. \n";
4333

44-
if (args.length < 2) {
34+
if (args.length != 2) {
4535
System.out.println(USAGE);
4636
System.exit(1);
4737
}
4838

49-
/* Read the name from command args*/
5039
String appName = args[0];
5140
String deploymentGroupName = args[1];
5241

@@ -56,6 +45,7 @@ public static void main(String[] args) {
5645
.build();
5746

5847
delDeploymentGroup(deployClient, appName, deploymentGroupName);
48+
deployClient.close();
5949
}
6050

6151
// snippet-start:[codedeploy.java2.delete_group.main]

0 commit comments

Comments
 (0)