//snippet-sourcedescription:[CreateKeyPair.java demonstrates how to create an Amazon Elastic Compute Cloud (Amazon EC2) key pair.]
//snippet-keyword:[AWS SDK for Java v2]
//snippet-service:[Amazon EC2]

/*
   Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
   SPDX-License-Identifier: Apache-2.0
*/
package com.example.ec2;

// snippet-start:[ec2.java2.create_key_pair.import]
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.ec2.Ec2Client;
import software.amazon.awssdk.services.ec2.model.CreateKeyPairRequest;
import software.amazon.awssdk.services.ec2.model.Ec2Exception;
// snippet-end:[ec2.java2.create_key_pair.import]

/**
 * Before running this Java V2 code example, set up your development environment, including your credentials.
 *
 * For more information, see the following documentation topic:
 *
 * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
 */
public class CreateKeyPair {

    public static void main(String[] args) {

        final String usage = "\n" +
            "Usage:\n" +
            "   <keyName> \n\n" +
            "Where:\n" +
            "   keyName - A key pair name (for example, TestKeyPair). \n\n";

        if (args.length != 1) {
            System.out.println(usage);
            System.exit(1);
        }

        String keyName = args[0];
        Region region = Region.US_EAST_1;
        Ec2Client ec2 = Ec2Client.builder()
            .region(region)
            .credentialsProvider(ProfileCredentialsProvider.create())
            .build();

        createEC2KeyPair(ec2, keyName) ;
        ec2.close();
    }

     // snippet-start:[ec2.java2.create_key_pair.main]
    public static void createEC2KeyPair(Ec2Client ec2,String keyName ) {
        try {
            CreateKeyPairRequest request = CreateKeyPairRequest.builder()
                .keyName(keyName)
                .build();

            ec2.createKeyPair(request);
            System.out.printf("Successfully created key pair named %s", keyName);

        } catch (Ec2Exception e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
     }
    // snippet-end:[ec2.java2.create_key_pair.main]
 }
