À utiliser CreateDBClusterParameterGroup avec un AWS SDK - Exemples de code de l'AWS SDK

D'autres AWS SDK exemples sont disponibles dans le GitHub dépôt AWS Doc SDK Examples.

Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.

À utiliser CreateDBClusterParameterGroup avec un AWS SDK

Les exemples de code suivants illustrent comment utiliser CreateDBClusterParameterGroup.

Les exemples d’actions sont des extraits de code de programmes de plus grande envergure et doivent être exécutés en contexte. Vous pouvez voir cette action en contexte dans l’exemple de code suivant :

.NET
AWS SDK for .NET
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

/// <summary> /// Create a custom cluster parameter group. /// </summary> /// <param name="parameterGroupFamily">The family of the parameter group.</param> /// <param name="groupName">The name for the new parameter group.</param> /// <param name="description">A description for the new parameter group.</param> /// <returns>The new parameter group object.</returns> public async Task<DBClusterParameterGroup> CreateCustomClusterParameterGroupAsync( string parameterGroupFamily, string groupName, string description) { var request = new CreateDBClusterParameterGroupRequest { DBParameterGroupFamily = parameterGroupFamily, DBClusterParameterGroupName = groupName, Description = description, }; var response = await _amazonRDS.CreateDBClusterParameterGroupAsync(request); return response.DBClusterParameterGroup; }
C++
SDKpour C++
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::RDS::RDSClient client(clientConfig); Aws::RDS::Model::CreateDBClusterParameterGroupRequest request; request.SetDBClusterParameterGroupName(CLUSTER_PARAMETER_GROUP_NAME); request.SetDBParameterGroupFamily(dbParameterGroupFamily); request.SetDescription("Example cluster parameter group."); Aws::RDS::Model::CreateDBClusterParameterGroupOutcome outcome = client.CreateDBClusterParameterGroup(request); if (outcome.IsSuccess()) { std::cout << "The DB cluster parameter group was successfully created." << std::endl; } else { std::cerr << "Error with Aurora::CreateDBClusterParameterGroup. " << outcome.GetError().GetMessage() << std::endl; return false; }
Go
SDKpour Go V2
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

import ( "context" "errors" "log" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/rds" "github.com/aws/aws-sdk-go-v2/service/rds/types" ) type DbClusters struct { AuroraClient *rds.Client } // CreateParameterGroup creates a DB cluster parameter group that is based on the specified // parameter group family. func (clusters *DbClusters) CreateParameterGroup( ctx context.Context, parameterGroupName string, parameterGroupFamily string, description string) ( *types.DBClusterParameterGroup, error) { output, err := clusters.AuroraClient.CreateDBClusterParameterGroup(ctx, &rds.CreateDBClusterParameterGroupInput{ DBClusterParameterGroupName: aws.String(parameterGroupName), DBParameterGroupFamily: aws.String(parameterGroupFamily), Description: aws.String(description), }) if err != nil { log.Printf("Couldn't create parameter group %v: %v\n", parameterGroupName, err) return nil, err } else { return output.DBClusterParameterGroup, err } }
Java
SDKpour Java 2.x
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

public static void createDBClusterParameterGroup(RdsClient rdsClient, String dbClusterGroupName, String dbParameterGroupFamily) { try { CreateDbClusterParameterGroupRequest groupRequest = CreateDbClusterParameterGroupRequest.builder() .dbClusterParameterGroupName(dbClusterGroupName) .dbParameterGroupFamily(dbParameterGroupFamily) .description("Created by using the AWS SDK for Java") .build(); CreateDbClusterParameterGroupResponse response = rdsClient.createDBClusterParameterGroup(groupRequest); System.out.println("The group name is " + response.dbClusterParameterGroup().dbClusterParameterGroupName()); } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } }
Kotlin
SDKpour Kotlin
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

suspend fun createDBClusterParameterGroup( dbClusterGroupNameVal: String?, dbParameterGroupFamilyVal: String?, ) { val groupRequest = CreateDbClusterParameterGroupRequest { dbClusterParameterGroupName = dbClusterGroupNameVal dbParameterGroupFamily = dbParameterGroupFamilyVal description = "Created by using the AWS SDK for Kotlin" } RdsClient { region = "us-west-2" }.use { rdsClient -> val response = rdsClient.createDbClusterParameterGroup(groupRequest) println("The group name is ${response.dbClusterParameterGroup?.dbClusterParameterGroupName}") } }
Python
SDKpour Python (Boto3)
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

class AuroraWrapper: """Encapsulates Aurora DB cluster actions.""" def __init__(self, rds_client): """ :param rds_client: A Boto3 Amazon Relational Database Service (Amazon RDS) client. """ self.rds_client = rds_client @classmethod def from_client(cls): """ Instantiates this class from a Boto3 client. """ rds_client = boto3.client("rds") return cls(rds_client) def create_parameter_group( self, parameter_group_name, parameter_group_family, description ): """ Creates a DB cluster parameter group that is based on the specified parameter group family. :param parameter_group_name: The name of the newly created parameter group. :param parameter_group_family: The family that is used as the basis of the new parameter group. :param description: A description given to the parameter group. :return: Data about the newly created parameter group. """ try: response = self.rds_client.create_db_cluster_parameter_group( DBClusterParameterGroupName=parameter_group_name, DBParameterGroupFamily=parameter_group_family, Description=description, ) except ClientError as err: logger.error( "Couldn't create parameter group %s. Here's why: %s: %s", parameter_group_name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return response
Rust
SDKpour Rust
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

// Select an engine family and create a custom DB cluster parameter group. rds.CreateDbClusterParameterGroup(DBParameterGroupFamily='aurora-mysql8.0') pub async fn set_engine(&mut self, engine: &str, version: &str) -> Result<(), ScenarioError> { self.engine_family = Some(engine.to_string()); self.engine_version = Some(version.to_string()); let create_db_cluster_parameter_group = self .rds .create_db_cluster_parameter_group( DB_CLUSTER_PARAMETER_GROUP_NAME, DB_CLUSTER_PARAMETER_GROUP_DESCRIPTION, engine, ) .await; match create_db_cluster_parameter_group { Ok(CreateDbClusterParameterGroupOutput { db_cluster_parameter_group: None, .. }) => { return Err(ScenarioError::with( "CreateDBClusterParameterGroup had empty response", )); } Err(error) => { if error.code() == Some("DBParameterGroupAlreadyExists") { info!("Cluster Parameter Group already exists, nothing to do"); } else { return Err(ScenarioError::new( "Could not create Cluster Parameter Group", &error, )); } } _ => { info!("Created Cluster Parameter Group"); } } Ok(()) } pub async fn create_db_cluster_parameter_group( &self, name: &str, description: &str, family: &str, ) -> Result<CreateDbClusterParameterGroupOutput, SdkError<CreateDBClusterParameterGroupError>> { self.inner .create_db_cluster_parameter_group() .db_cluster_parameter_group_name(name) .description(description) .db_parameter_group_family(family) .send() .await } #[tokio::test] async fn test_scenario_set_engine() { let mut mock_rds = MockRdsImpl::default(); mock_rds .expect_create_db_cluster_parameter_group() .with( eq("RustSDKCodeExamplesDBParameterGroup"), eq("Parameter Group created by Rust SDK Code Example"), eq("aurora-mysql"), ) .return_once(|_, _, _| { Ok(CreateDbClusterParameterGroupOutput::builder() .db_cluster_parameter_group(DbClusterParameterGroup::builder().build()) .build()) }); let mut scenario = AuroraScenario::new(mock_rds); let set_engine = scenario.set_engine("aurora-mysql", "aurora-mysql8.0").await; assert_eq!(set_engine, Ok(())); assert_eq!(Some("aurora-mysql"), scenario.engine_family.as_deref()); assert_eq!(Some("aurora-mysql8.0"), scenario.engine_version.as_deref()); } #[tokio::test] async fn test_scenario_set_engine_not_create() { let mut mock_rds = MockRdsImpl::default(); mock_rds .expect_create_db_cluster_parameter_group() .with( eq("RustSDKCodeExamplesDBParameterGroup"), eq("Parameter Group created by Rust SDK Code Example"), eq("aurora-mysql"), ) .return_once(|_, _, _| Ok(CreateDbClusterParameterGroupOutput::builder().build())); let mut scenario = AuroraScenario::new(mock_rds); let set_engine = scenario.set_engine("aurora-mysql", "aurora-mysql8.0").await; assert!(set_engine.is_err()); } #[tokio::test] async fn test_scenario_set_engine_param_group_exists() { let mut mock_rds = MockRdsImpl::default(); mock_rds .expect_create_db_cluster_parameter_group() .withf(|_, _, _| true) .return_once(|_, _, _| { Err(SdkError::service_error( CreateDBClusterParameterGroupError::DbParameterGroupAlreadyExistsFault( DbParameterGroupAlreadyExistsFault::builder().build(), ), Response::new(StatusCode::try_from(400).unwrap(), SdkBody::empty()), )) }); let mut scenario = AuroraScenario::new(mock_rds); let set_engine = scenario.set_engine("aurora-mysql", "aurora-mysql8.0").await; assert!(set_engine.is_err()); }