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

feat(config-resolver): resolve hostname from variants #2980

Merged
merged 2 commits into from
Nov 4, 2021

Conversation

trivikr
Copy link
Member

@trivikr trivikr commented Nov 3, 2021

Issue

Internal JS-2929

Description

Uses variants for getting region info

  • getRegionInfo fetches regionHostname and partitionHostname from respective variants.
  • getRegionInfo throws error is hostname is undefined.

Testing

  • Unit testing
  • Functional testing
Code used for populating functional tests
// The models directory smithy models
// from https://github.com/aws/aws-sdk-js-v3/tree/main/codegen/sdk-codegen/aws-models
// The endpoints.json contains RIP endpoints.

// In JS SDK v3, the test case for SimpleDB and mobileanalytics is removed as they're deprecated.

import { readFile, readdir, writeFile } from "fs/promises";

const findVal = (object, key) => {
  let value;
  Object.keys(object).some((k) => {
    if (k === key) {
      value = object[k];
      return true;
    }
    if (object[k] && typeof object[k] === "object") {
      value = findVal(object[k], key);
      return value !== undefined;
    }
  });
  return value;
};

const getServiceInfo = (api) => {
  return findVal(api, "aws.api#service");
};

const endpointPrefixSdkIdHash = {};
const models = await readdir("models");
for (const modelFileName of models) {
  const model = JSON.parse(
    (await readFile(`models/${modelFileName}`)).toString()
  );
  const { endpointPrefix, sdkId } = getServiceInfo(model);
  endpointPrefixSdkIdHash[endpointPrefix] = sdkId;
}

// Add exceptions
endpointPrefixSdkIdHash["rds"] = "RDS";
endpointPrefixSdkIdHash["docdb"] = "DocDB";
endpointPrefixSdkIdHash["neptune"] = "Neptune";
endpointPrefixSdkIdHash["importexport"] = "S3";
endpointPrefixSdkIdHash["ioteventsdata"] = "IoT Events Data";
endpointPrefixSdkIdHash["iotsecuredtunneling"] = "IoTSecureTunneling";
endpointPrefixSdkIdHash["iotwireless"] = "IoT Wireless";
endpointPrefixSdkIdHash["mobileanalytics"] = "Pinpoint";
endpointPrefixSdkIdHash["sdb"] = "SimpleDB";

const getHostnameWithDnsSuffix = (template, dnsSuffix) =>
  template.replace("{dnsSuffix}", dnsSuffix || "{dnsSuffix}");

const getVariantsWithDnsSuffix = (variants = [], parentDnsSuffix) =>
  variants.map(({ hostname, dnsSuffix, tags }) => ({
    hostname: getHostnameWithDnsSuffix(hostname, dnsSuffix || parentDnsSuffix),
    tags,
  }));

const getHostname = (variants, { isFipsEndpoint, isDualstackEndpoint }) =>
  variants.find(
    ({ tags }) =>
      isFipsEndpoint === tags.includes("fips") &&
      isDualstackEndpoint === tags.includes("dualstack")
  )?.hostname;

const testCases = [];
const endpoints = JSON.parse((await readFile("endpoints.json")).toString());
for (const partition of endpoints.partitions) {
  const partitionDnsSuffix = partition.dnsSuffix;
  const partitionVariants = getVariantsWithDnsSuffix(
    partition.defaults.variants,
    partitionDnsSuffix
  );
  if (partition.defaults.hostname) {
    partitionVariants.push({
      hostname: getHostnameWithDnsSuffix(
        partition.defaults.hostname,
        partitionDnsSuffix
      ),
      tags: [],
    });
  }

  for (const [endpointPrefix, serviceConfig] of Object.entries(
    partition.services
  )) {
    const serviceDnsSuffix = serviceConfig.dnsSuffix;
    const serviceVariants = getVariantsWithDnsSuffix(
      serviceConfig.defaults?.variants,
      serviceDnsSuffix || partitionDnsSuffix
    );
    if (serviceConfig.hostname) {
      serviceVariants.push({
        hostname: getHostnameWithDnsSuffix(
          serviceConfig.hostname,
          serviceDnsSuffix
        ),
        tags: [],
      });
    }

    for (const isFipsEndpoint of [false, true]) {
      for (const isDualstackEndpoint of [false, true]) {
        const options = { isFipsEndpoint, isDualstackEndpoint };
        const partitionHostname = getHostname(partitionVariants, options);
        const serviceHostname = getHostname(serviceVariants, options);

        const notSupportedByService =
          serviceVariants.length > 0 && serviceHostname === undefined;
        if (notSupportedByService) {
          continue;
        }

        let hostnameFromParentCase = partitionHostname === undefined;
        let hostnameFromEndpointCase = false;

        for (const [region, regionConfig] of Object.entries(
          serviceConfig.endpoints
        )) {
          if (hostnameFromParentCase && hostnameFromEndpointCase) {
            break;
          }

          const { hostname, variants, deprecated } = regionConfig;
          if (deprecated) {
            continue;
          }

          if (!hostnameFromEndpointCase && variants) {
            if (hostname) {
              variants.push({
                hostname: getHostnameWithDnsSuffix(hostname),
                tags: [],
              });
            }

            const endpointHostname = getHostname(variants, options);
            if (endpointHostname) {
              testCases.push({
                endpointPrefix,
                sdkId: endpointPrefixSdkIdHash[endpointPrefix],
                region,
                isFipsEndpoint,
                isDualstackEndpoint,
                hostname: endpointHostname,
              });
              hostnameFromEndpointCase = true;
            }
          } else if (!hostnameFromParentCase && !variants && !hostname) {
            testCases.push({
              endpointPrefix,
              sdkId: endpointPrefixSdkIdHash[endpointPrefix],
              region,
              isFipsEndpoint,
              isDualstackEndpoint,
              hostname: (serviceHostname || partitionHostname)
                .replace("{region}", region)
                .replace("{service}", endpointPrefix),
            });
            hostnameFromParentCase = true;
          }
        }
      }
    }
  }
}
await writeFile("testCases.json", JSON.stringify(testCases, null, 2));

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@trivikr trivikr changed the title Get region info from variants feat(config-resolver): resolve hostname from variants Nov 3, 2021
@codecov-commenter
Copy link

codecov-commenter commented Nov 3, 2021

Codecov Report

Merging #2980 (f703ba7) into main (ba4386d) will increase coverage by 0.02%.
The diff coverage is 100.00%.

❗ Current head f703ba7 differs from pull request most recent head ec28c27. Consider uploading reports for the commit ec28c27 to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2980      +/-   ##
==========================================
+ Coverage   58.70%   58.72%   +0.02%     
==========================================
  Files         566      566              
  Lines       30315    30330      +15     
  Branches     7460     7469       +9     
==========================================
+ Hits        17796    17811      +15     
  Misses      12519    12519              
Impacted Files Coverage Δ
...resolver/src/regionInfo/getHostnameFromVariants.ts 100.00% <100.00%> (ø)
...es/config-resolver/src/regionInfo/getRegionInfo.ts 100.00% <100.00%> (ø)
...fig-resolver/src/regionInfo/getResolvedHostname.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ba4386d...ec28c27. Read the comment docs.

Copy link
Contributor

@AllanZhengYP AllanZhengYP left a comment

Choose a reason for hiding this comment

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

Re-approve it!

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants