2019-02-06 18:05:50 -08:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
# include <aws/core/Aws.h>
# include <aws/redshift/RedshiftClient.h>
# include <aws/redshift/model/CreateClusterRequest.h>
# include <aws/redshift/model/CreateClusterResult.h>
# include <iostream>
/**
* Create an Amazon Redshift cluster
*/
Aws : : Redshift : : Model : : Cluster * CreateRedshiftCluster (
2019-02-08 14:39:41 -08:00
const Aws : : String & dbName ,
const Aws : : String & clusterId ,
const Aws : : String & userName ,
const Aws : : String & userPassword ,
const Aws : : String & subnetGroupName ,
const Aws : : String & nodeType ,
const int numberOfNodes ,
const Aws : : Vector < Aws : : String > & iamRoles ,
Aws : : Redshift : : Model : : Cluster & cluster ,
const int automatedSnapshotPeriod = 0 ) // 0 = disable automated snapshots
2019-02-06 18:05:50 -08:00
{
2019-02-08 14:39:41 -08:00
Aws : : Redshift : : RedshiftClient redshift ;
Aws : : Redshift : : Model : : CreateClusterRequest redshift_req ;
2019-02-06 18:05:50 -08:00
2019-02-08 14:39:41 -08:00
// Initialize all request parameters
redshift_req . SetDBName ( dbName ) ;
redshift_req . SetClusterIdentifier ( clusterId ) ;
redshift_req . SetMasterUsername ( userName ) ;
redshift_req . SetMasterUserPassword ( userPassword ) ;
redshift_req . SetClusterSubnetGroupName ( subnetGroupName ) ;
redshift_req . SetNodeType ( nodeType ) ;
redshift_req . SetNumberOfNodes ( numberOfNodes ) ;
redshift_req . SetIamRoles ( iamRoles ) ;
redshift_req . SetAutomatedSnapshotRetentionPeriod ( automatedSnapshotPeriod ) ;
2019-02-06 18:05:50 -08:00
2019-02-08 14:39:41 -08:00
// Create the cluster. Does not wait for creation to complete.
auto outcome = redshift . CreateCluster ( redshift_req ) ;
2019-02-06 18:05:50 -08:00
2019-02-08 14:39:41 -08:00
if ( ! outcome . IsSuccess ( ) )
{
std : : cerr < < " Error creating cluster. " < <
outcome . GetError ( ) . GetMessage ( ) < < std : : endl ;
return NULL ;
}
2019-02-06 18:05:50 -08:00
2019-02-08 14:39:41 -08:00
auto result = outcome . GetResult ( ) ;
cluster = result . GetCluster ( ) ;
return & cluster ;
2019-02-06 18:05:50 -08:00
}
/**
* Exercise CreateRedshiftCluster()
*/
int main ( int argc , char * * argv )
{
2019-02-08 14:39:41 -08:00
Aws : : SDKOptions options ;
Aws : : InitAPI ( options ) ;
{
// Set these configuration values before running the program
Aws : : String dbName = " dev " ;
Aws : : String clusterId = " redshift-cluster-1 " ;
Aws : : String userName = " awsuser " ;
Aws : : String userPassword = " AWSuser_01 " ;
Aws : : String subnetGroupName = " myredshiftsubnetgroup " ;
Aws : : String nodeType = " dc2.large " ;
int numberOfNodes = 2 ;
Aws : : Vector < Aws : : String > iamRoles ;
iamRoles . push_back ( " arn:aws:iam::ACCOUNT_ID:role/ROLE_NAME " ) ;
Aws : : Redshift : : Model : : Cluster cluster ;
2019-02-06 18:05:50 -08:00
2019-02-08 14:39:41 -08:00
// Create the cluster. Function returns before the cluster is fully built.
if ( ! CreateRedshiftCluster ( dbName , clusterId ,
userName , userPassword ,
subnetGroupName ,
nodeType , numberOfNodes ,
iamRoles ,
cluster ) )
{
return 1 ;
}
2019-02-06 18:05:50 -08:00
2019-02-08 14:39:41 -08:00
// Print some information about the cluster
std : : cout < < " Creating cluster with ID " < < cluster . GetClusterIdentifier ( ) < < " \n " ;
std : : cout < < " Cluster database name: " < < cluster . GetDBName ( ) < < " \n " ;
std : : cout < < " Cluster status: " < < cluster . GetClusterStatus ( ) < < std : : endl ;
}
Aws : : ShutdownAPI ( options ) ;
return 0 ;
2019-02-06 18:05:50 -08:00
}