2019-01-21 13:12:01 -08:00
/**
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* This file is licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License. A copy of
* the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
// snippet-sourcedescription:[CreateAndModifyClusterSubnetGroup demonstrates how to create and modify an Amazon Redshift subnet group.]
// snippet-service:[redshift]
// snippet-keyword:[Java]
2019-09-05 19:52:34 -07:00
// snippet-sourcesyntax:[java]
2019-01-21 13:12:01 -08:00
// snippet-keyword:[Amazon Redshift]
// snippet-keyword:[Code Sample]
// snippet-keyword:[CreateClusterSubnetGroup]
// snippet-keyword:[DescribeClusterSubnetGroups]
// snippet-sourcetype:[full-example]
2019-02-01 14:37:00 -08:00
// snippet-sourcedate:[2019-02-01]
2019-01-21 13:12:01 -08:00
// snippet-sourceauthor:[AWS]
// snippet-start:[redshift.java.CreateAndModifyClusterSubnetGroup.complete]
2019-02-01 14:37:00 -08:00
package com.amazonaws.services.redshift ;
2019-01-21 13:12:01 -08:00
import java.io.IOException ;
import java.util.ArrayList ;
import java.util.List ;
import com.amazonaws.services.redshift.model.* ;
public class CreateAndModifyClusterSubnetGroup {
2019-02-01 14:37:00 -08:00
public static AmazonRedshift client ;
public static String clusterSubnetGroupName = " subnet-group-name " ;
2019-01-21 13:12:01 -08:00
// You can use the VPC console to find subnet IDs to use.
public static String subnetId1 = " ***provide a subnet ID**** " ;
public static String subnetId2 = " ***provide a subnet ID**** " ;
public static void main ( String [ ] args ) throws IOException {
2019-02-01 14:37:00 -08:00
// Default client using the {@link com.amazonaws.auth.DefaultAWSCredentialsProviderChain}
client = AmazonRedshiftClientBuilder . defaultClient ( ) ;
2019-01-21 13:12:01 -08:00
try {
createClusterSubnetGroup ( ) ;
describeClusterSubnetGroups ( ) ;
modifyClusterSubnetGroup ( ) ;
} catch ( Exception e ) {
System . err . println ( " Operation failed: " + e . getMessage ( ) ) ;
}
}
private static void createClusterSubnetGroup ( ) {
CreateClusterSubnetGroupRequest request = new CreateClusterSubnetGroupRequest ( )
. withClusterSubnetGroupName ( clusterSubnetGroupName )
. withDescription ( " my cluster subnet group " )
. withSubnetIds ( subnetId1 ) ;
client . createClusterSubnetGroup ( request ) ;
System . out . println ( " Created cluster subnet group: " + clusterSubnetGroupName ) ;
}
private static void modifyClusterSubnetGroup ( ) {
// Get existing subnet list.
DescribeClusterSubnetGroupsRequest request1 = new DescribeClusterSubnetGroupsRequest ( )
. withClusterSubnetGroupName ( clusterSubnetGroupName ) ;
DescribeClusterSubnetGroupsResult result1 = client . describeClusterSubnetGroups ( request1 ) ;
List < String > subnetNames = new ArrayList < String > ( ) ;
// We can work with just the first group returned since we requested info about one group.
for ( Subnet subnet : result1 . getClusterSubnetGroups ( ) . get ( 0 ) . getSubnets ( ) ) {
subnetNames . add ( subnet . getSubnetIdentifier ( ) ) ;
}
// Add to existing subnet list.
subnetNames . add ( subnetId2 ) ;
ModifyClusterSubnetGroupRequest request = new ModifyClusterSubnetGroupRequest ( )
. withClusterSubnetGroupName ( clusterSubnetGroupName )
. withSubnetIds ( subnetNames ) ;
ClusterSubnetGroup result2 = client . modifyClusterSubnetGroup ( request ) ;
System . out . println ( " \ nSubnet group modified. " ) ;
printResultSubnetGroup ( result2 ) ;
}
private static void describeClusterSubnetGroups ( ) {
DescribeClusterSubnetGroupsRequest request = new DescribeClusterSubnetGroupsRequest ( )
. withClusterSubnetGroupName ( clusterSubnetGroupName ) ;
DescribeClusterSubnetGroupsResult result = client . describeClusterSubnetGroups ( request ) ;
printResultSubnetGroups ( result ) ;
}
private static void printResultSubnetGroups ( DescribeClusterSubnetGroupsResult result )
{
if ( result = = null )
{
System . out . println ( " \ nDescribe cluster subnet groups result is null. " ) ;
return ;
}
for ( ClusterSubnetGroup group : result . getClusterSubnetGroups ( ) )
{
printResultSubnetGroup ( group ) ;
}
}
private static void printResultSubnetGroup ( ClusterSubnetGroup group ) {
System . out . format ( " Name: %s, Description: %s \ n " , group . getClusterSubnetGroupName ( ) , group . getDescription ( ) ) ;
for ( Subnet subnet : group . getSubnets ( ) ) {
System . out . format ( " Subnet: %s, %s, %s \ n " , subnet . getSubnetIdentifier ( ) ,
subnet . getSubnetAvailabilityZone ( ) . getName ( ) , subnet . getSubnetStatus ( ) ) ;
}
}
}
// snippet-end:[redshift.java.CreateAndModifyClusterSubnetGroup.complete]