Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

codegen aws regions enums based on partitions.json #3059

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"Bodyless", "HTTPGET", "ratelimiter", "Ratelimiter", "STDMETHODCALLTYPE", "CANTSAVE", "OLECHAR", "DISPID",
"UNKNOWNNAME", "DISPPARAMS", "XMLHTTP", "comptr", "Metadataservice", "Streamfn", "HWAVEOUT", "matdesc",
"Presigner", "xindex", "errortype", "waveout", "WAVEOUTCAPSA", "ALLOWSYNC", "WAVEHDR", "MMSYSERR",
"WAVEFORMATEX", "Unprepare", "DDISABLE_IMDSV1", "SENDREQUEST", "threadpool", "stdlib", "ALLOC",
"WAVEFORMATEX", "Unprepare", "DDISABLE_IMDSV1", "SENDREQUEST", "threadpool", "stdlib", "ALLOC", "ISOE",
"isoe",
// AWS general
"Arns", "AMZN", "amzn", "Paulo", "Ningxia", "ISOB", "isob", "AWSXML", "IMDSV", "AWSSTL",
// AWS Signature
Expand Down
59 changes: 31 additions & 28 deletions src/aws-cpp-sdk-core/include/aws/core/Region.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#pragma once

#include <aws/core/Core_EXPORTS.h>

#include <aws/core/utils/memory/stl/AWSString.h>

namespace Aws
Expand All @@ -16,44 +15,48 @@ namespace Aws
*/
namespace Region
{
// AWS_GLOBAL is a pseudo region that can be used to tell SDK to use the service global endpoint if there is any.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you want to keep that comment about global?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nah now that theres several global regions, and putting the global region as the first entry of the enum adds a bit of weird custom logic that makes this change a bit messy.

also existing AWS documentation on this exists that users could refer to.

aws-global specifies the global endpoint for services that support a separate global endpoint in addition to Regional endpoints, such as AWS Security Token Service (AWS STS) and Amazon Simple Storage Service (Amazon S3).

// You can specify this region to corresponding environment variable, config file item and in your code.
// For services without global region, the request will be directed to us-east-1
static const char AWS_GLOBAL[] = "aws-global";
static const char US_EAST_1[] = "us-east-1"; // US East (N. Virginia)
static const char US_EAST_2[] = "us-east-2"; // US East (Ohio)
static const char US_WEST_1[] = "us-west-1"; // US West (N. California)
static const char US_WEST_2[] = "us-west-2"; // US West (Oregon)
static const char EU_WEST_1[] = "eu-west-1"; // EU (Ireland)
static const char EU_WEST_2[] = "eu-west-2"; // EU (London)
static const char EU_WEST_3[] = "eu-west-3"; // EU (Paris)
static const char EU_CENTRAL_1[] = "eu-central-1"; // EU (Frankfurt)
static const char EU_CENTRAL_2[] = "eu-central-2"; // EU (Zurich)
static const char EU_NORTH_1[] = "eu-north-1"; // EU (Stockholm)
static const char EU_SOUTH_1[] = "eu-south-1"; // EU (Milan)
static const char EU_SOUTH_2[] = "eu-south-2"; // EU (Spain)
static const char AF_SOUTH_1[] = "af-south-1"; // Africa (Cape Town)
static const char AP_EAST_1[] = "ap-east-1"; // Asia Pacific (Hong Kong)
static const char AP_NORTHEAST_1[] = "ap-northeast-1"; // Asia Pacific (Tokyo)
static const char AP_NORTHEAST_2[] = "ap-northeast-2"; // Asia Pacific (Seoul)
static const char AP_NORTHEAST_3[] = "ap-northeast-3"; // Asia Pacific (Osaka)
static const char AP_SOUTH_1[] = "ap-south-1"; // Asia Pacific (Mumbai)
static const char AP_SOUTH_2[] = "ap-south-2"; // Asia Pacific (Hyderabad)
static const char AP_SOUTHEAST_1[] = "ap-southeast-1"; // Asia Pacific (Singapore)
static const char AP_SOUTHEAST_2[] = "ap-southeast-2"; // Asia Pacific (Sydney)
static const char AP_SOUTHEAST_3[] = "ap-southeast-3"; // Asia Pacific (Jakarta)
static const char AP_NORTHEAST_1[] = "ap-northeast-1"; // Asia Pacific (Tokyo)
static const char AP_NORTHEAST_2[] = "ap-northeast-2"; // Asia Pacific (Seoul)
static const char AP_NORTHEAST_3[] = "ap-northeast-3"; // Asia Pacific (Osaka)
static const char AP_NORTHEAST_4[] = "ap-northeast-4"; // Asia Pacific (Melbourne)
static const char SA_EAST_1[] = "sa-east-1"; // South America (Sao Paulo)
static const char AP_SOUTHEAST_4[] = "ap-southeast-4"; // Asia Pacific (Melbourne)
static const char AWS_CN_GLOBAL[] = "aws-cn-global"; // AWS China global region
static const char AWS_GLOBAL[] = "aws-global"; // AWS Standard global region
static const char AWS_ISO_B_GLOBAL[] = "aws-iso-b-global"; // AWS ISOB (US) global region
static const char AWS_ISO_GLOBAL[] = "aws-iso-global"; // AWS ISO (US) global region
static const char AWS_US_GOV_GLOBAL[] = "aws-us-gov-global"; // AWS GovCloud (US) global region
static const char CA_CENTRAL_1[] = "ca-central-1"; // Canada (Central)
static const char CA_WEST_1[] = "ca-west-1"; // Canada West (Calgary)
static const char CN_NORTH_1[] = "cn-north-1"; // China (Beijing)
static const char CN_NORTHWEST_1[] = "cn-northwest-1"; // China (Ningxia)
static const char CA_CENTRAL_1[] = "ca-central-1"; // Canada (Central)
static const char ME_SOUTH_1[] = "me-south-1"; // Middle East (Bahrain)
static const char EU_CENTRAL_1[] = "eu-central-1"; // Europe (Frankfurt)
static const char EU_CENTRAL_2[] = "eu-central-2"; // Europe (Zurich)
static const char EU_ISOE_WEST_1[] = "eu-isoe-west-1"; // EU ISOE West
static const char EU_NORTH_1[] = "eu-north-1"; // Europe (Stockholm)
static const char EU_SOUTH_1[] = "eu-south-1"; // Europe (Milan)
static const char EU_SOUTH_2[] = "eu-south-2"; // Europe (Spain)
static const char EU_WEST_1[] = "eu-west-1"; // Europe (Ireland)
static const char EU_WEST_2[] = "eu-west-2"; // Europe (London)
static const char EU_WEST_3[] = "eu-west-3"; // Europe (Paris)
static const char IL_CENTRAL_1[] = "il-central-1"; // Israel (Tel Aviv)
static const char ME_CENTRAL_1[] = "me-central-1"; // Middle East (UAE)
static const char AF_SOUTH_1[] = "af-south-1"; // Africa (Cape Town)
static const char US_GOV_WEST_1[] = "us-gov-west-1"; // AWS GovCloud (US-West)
static const char ME_SOUTH_1[] = "me-south-1"; // Middle East (Bahrain)
static const char SA_EAST_1[] = "sa-east-1"; // South America (Sao Paulo)
static const char US_EAST_1[] = "us-east-1"; // US East (N. Virginia)
static const char US_EAST_2[] = "us-east-2"; // US East (Ohio)
static const char US_GOV_EAST_1[] = "us-gov-east-1"; // AWS GovCloud (US-East)
static const char US_ISO_EAST_1[] = "us-iso-east-1"; // US ISO East
static const char US_GOV_WEST_1[] = "us-gov-west-1"; // AWS GovCloud (US-West)
static const char US_ISO_EAST_1[] = "us-iso-east-1"; // US ISO East
static const char US_ISO_WEST_1[] = "us-iso-west-1"; // US ISO WEST
static const char US_ISOB_EAST_1[] = "us-isob-east-1"; // US ISOB East (Ohio)
static const char US_ISO_WEST_1[] = "us-iso-west-1"; // US ISO West
static const char US_WEST_1[] = "us-west-1"; // US West (N. California)
static const char US_WEST_2[] = "us-west-2"; // US West (Oregon)

// If a pseudo region, for example, aws-global or us-east-1-fips is provided, it should be converted to the region name used for signing.
Aws::String AWS_CORE_API ComputeSignerRegion(const Aws::String& region);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,21 @@

import lombok.Data;

import java.util.List;
import java.util.Map;

@Data
public class PartitionsModel {
String partitionsBlob;
@Data
public static class Partition {
private String id;
private Map<String, RegionDescription> regions;

@Data
public static class RegionDescription {
private String description;
}
}
private List<Partition> partitions;
private String partitionsBlob;
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ public static String convertToUpperCamel(String lowerCamel) {
return CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, lowerCamel);
}

public static String convertToUpperSnake(final String string) {
return CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_UNDERSCORE, string);
}

public static String computeVariableHasBeenSetName(String memberName) {
return String.format("%sHasBeenSet", computeMemberVariableName(memberName));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
import com.google.gson.GsonBuilder;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStream;


public class DirectFromC2jGenerator {

Expand Down Expand Up @@ -69,7 +66,7 @@ public ByteArrayOutputStream generateServiceSourceFromJson(String rawJson, Strin
public ByteArrayOutputStream generatePartitionsSourceFromJson(String rawJson, String languageBinding, String serviceName,
String namespace, String licenseText,
boolean generateStandalonePackage, boolean enableVirtualOperations) throws Exception {
PartitionsModel partitionsBom = new PartitionsModel();
PartitionsModel partitionsBom = parsePartitions(rawJson);
partitionsBom.setPartitionsBlob(rawJson);

return mainClientGenerator.generatePartitionsSourceFromStrBlob(partitionsBom, languageBinding, namespace, licenseText);
Expand Down Expand Up @@ -119,6 +116,17 @@ public static DefaultClientConfigs parseRawJson(final String rawJson) {
return clientConfigBom;
}

/**
* Parse rawJson into a structured Partitions object
*
* @param rawJson the raw json representation of the partitions object.
* @return a parsed object of partitions.
*/
public PartitionsModel parsePartitions(final String rawJson) {
Gson gson = new Gson();
return gson.fromJson(rawJson, PartitionsModel.class);
}

/**
* A function to generate C++ source for service client tests
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import com.amazonaws.util.awsclientgenerator.domainmodels.SdkFileEntry;
import com.amazonaws.util.awsclientgenerator.domainmodels.codegeneration.PartitionsModel;
import com.amazonaws.util.awsclientgenerator.domainmodels.codegeneration.cpp.CppViewHelper;
import com.amazonaws.util.awsclientgenerator.generators.PartitionsGenerator;
import com.amazonaws.util.awsclientgenerator.generators.exceptions.SourceGenerationFailedException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
Expand All @@ -18,8 +21,8 @@
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
Expand All @@ -32,12 +35,15 @@

public class CppPartitionsGenerator implements PartitionsGenerator {

private static String PARTITIONS_INCLUDE_TEMPLATE =
private final static String PARTITIONS_INCLUDE_TEMPLATE =
"/com/amazonaws/util/awsclientgenerator/velocity/cpp/endpoint/partitions/AWSPartitionsHeader.vm";
private static String PARTITIONS_SOURCE_TEMPLATE =
private final static String PARTITIONS_SOURCE_TEMPLATE =
"/com/amazonaws/util/awsclientgenerator/velocity/cpp/endpoint/partitions/AWSPartitionsSource.vm";
private static String PARTITIONS_HEADER_DIR_PATH = "include/aws/core/endpoint/AWSPartitions.h";
private static String PARTITIONS_SOURCE_DIR_PATH = "source/endpoint/AWSPartitions.cpp";
private final static String REGION_INCLUDE_TEMPLATE =
"/com/amazonaws/util/awsclientgenerator/velocity/cpp/endpoint/region/AWSRegionHeader.vm";
private final static String PARTITIONS_HEADER_DIR_PATH = "include/aws/core/endpoint/AWSPartitions.h";
private final static String PARTITIONS_SOURCE_DIR_PATH = "source/endpoint/AWSPartitions.cpp";
private final static String REGION_HEADER_DIR_PATH = "include/aws/core/Region.h";

protected final VelocityEngine velocityEngine;

Expand Down Expand Up @@ -81,16 +87,23 @@ public SdkFileEntry[] generateSourceFiles(PartitionsModel partitionsModel) throw
protected List<SdkFileEntry> generateModelHeaderFiles(final PartitionsModel partitionsModel) throws Exception {
VelocityContext context = createContext(partitionsModel);

Template template = velocityEngine.getTemplate(PARTITIONS_INCLUDE_TEMPLATE, StandardCharsets.UTF_8.name());
context.put("partitionsModel", partitionsModel);
context.put("serviceModel", partitionsModel); // for compatibility with generic template (Attribution.vm)

String outputFileName = PARTITIONS_HEADER_DIR_PATH;
SdkFileEntry headerFile = makeFile(template, context, outputFileName);
List<SdkFileEntry> sdkFileEntries = new ArrayList<>();
sdkFileEntries.add(headerFile);

return sdkFileEntries;
return ImmutableList.of(Pair.of(PARTITIONS_INCLUDE_TEMPLATE, PARTITIONS_HEADER_DIR_PATH),
Pair.of(REGION_INCLUDE_TEMPLATE, REGION_HEADER_DIR_PATH))
.stream()
.map(templatePair -> {
Template template = velocityEngine.getTemplate(templatePair.getKey(), StandardCharsets.UTF_8.name());
context.put("partitionsModel", partitionsModel);
context.put("serviceModel", partitionsModel); // for compatibility with generic template (Attribution.vm)
context.put("regions", partitionsModel.getPartitions().stream()
.map(partition -> partition.getRegions().entrySet())
.flatMap(Collection::stream)
.sorted(Map.Entry.comparingByKey())
.collect(Collectors.toList()));
context.put("CppViewHelper", CppViewHelper.class);

return makeFile(template, context, templatePair.getValue());
})
.collect(Collectors.toList());
}

protected List<SdkFileEntry> generateModelSourceFiles(final PartitionsModel partitionsModel) throws Exception {
Expand All @@ -117,7 +130,7 @@ protected static final VelocityContext createContext(final PartitionsModel servi
return context;
}

protected static final SdkFileEntry makeFile(Template template, VelocityContext context, String path) throws IOException {
protected static final SdkFileEntry makeFile(Template template, VelocityContext context, String path) {
StringWriter sw = new StringWriter();
template.merge(context, sw);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#parse("com/amazonaws/util/awsclientgenerator/velocity/cfamily/Attribution.vm")

#pragma once

\#include <aws/core/Core_EXPORTS.h>
\#include <aws/core/utils/memory/stl/AWSString.h>

namespace Aws
{
/**
* AWS Regions
*/
namespace Region
{
#foreach($region in $regions)
static const char ${CppViewHelper.convertToUpperSnake($region.getKey())}[] = "${region.getKey()}"; // ${region.getValue().getDescription()}
#end

// If a pseudo region, for example, aws-global or us-east-1-fips is provided, it should be converted to the region name used for signing.
Aws::String AWS_CORE_API ComputeSignerRegion(const Aws::String& region);

// A FIPs region starts with "fips-" or ends with "-fips".
bool AWS_CORE_API IsFipsRegion(const Aws::String& region);
}

} // namespace Aws

Loading