aws / aws-sdk

Landing page for the AWS SDKs on GitHub
https://aws.amazon.com/tools/
Other
71 stars 14 forks source link

convertion from string to enum fails #781

Open nmesika-te opened 1 month ago

nmesika-te commented 1 month ago

Describe the bug

When issuing an ec2::DescribeNetworkInterfaces, the returned NetworkInterfaceType is a string that the sdk can convert to an Enum using an internal map. However, the value returned by the API for Nat Gateway does not match the value in the map. The result is that the Enum value returned does not correctly reflect the type of the interface (an "unknown to sdk" Enum value is returned)

Here are the returned values from services/ec2/src/main/resources/codegen-resources/service-2.json:46316:

    "NetworkInterfaceType":{
      "type":"string",
      "enum":[
        "interface",
        "natGateway",
        "efa",
        "trunk",
        "load_balancer",
        "network_load_balancer",
        "vpc_endpoint",
        "branch",
        "transit_gateway",
        "lambda",
        "quicksight",
        "global_accelerator_managed",
        "api_gateway_managed",
        "gateway_load_balancer",
        "gateway_load_balancer_endpoint",
        "iot_rules_managed",
        "aws_codestar_connections_managed"
      ]
    },

Note that natGateway is camel cased while the others use underscores. Following is the the XML response from the AWS API for a specific interface. Note the last line <interfaceType>nat_gateway</interfaceType>

        <item>
            <networkInterfaceId>eni-0c712113537506cd7</networkInterfaceId>
            <subnetId>subnet-0a415aa89455cf4f8</subnetId>
            <vpcId>vpc-04f987fff6675ec10</vpcId>
            <availabilityZone>us-west-1a</availabilityZone>
            <description>Interface for NAT Gateway nat-06a39644e236c7926</description>
            <ownerId>036476006320</ownerId>
            <requesterId>213236389121</requesterId>
            <requesterManaged>true</requesterManaged>
            <status>in-use</status>
            <macAddress>06:c6:2a:ae:e4:23</macAddress>
            <privateIpAddress>10.56.5.210</privateIpAddress>
            <privateDnsName>ip-10-56-5-210.us-west-1.compute.internal</privateDnsName>
            <sourceDestCheck>false</sourceDestCheck>
            <groupSet/>
            <attachment>
                <attachmentId>ela-attach-fcc879c1</attachmentId>
                <instanceOwnerId>amazon-aws</instanceOwnerId>
                <deviceIndex>1</deviceIndex>
                <status>attached</status>
                <deleteOnTermination>false</deleteOnTermination>
            </attachment>
            <association>
                <publicIp>50.18.200.169</publicIp>
                <publicDnsName>ec2-50-18-200-169.us-west-1.compute.amazonaws.com</publicDnsName>
                <ipOwnerId>036476006320</ipOwnerId>
                <allocationId>eipalloc-0f749c8c6fe1d8101</allocationId>
                <associationId>eipassoc-a56ec57f</associationId>
                <natEnabled>true</natEnabled>
            </association>
            <tagSet/>
            <privateIpAddressesSet>
                <item>
                    <privateIpAddress>10.56.5.210</privateIpAddress>
                    <privateDnsName>ip-10-56-5-210.us-west-1.compute.internal</privateDnsName>
                    <primary>true</primary>
                    <association>
                        <publicIp>50.18.200.169</publicIp>
                        <publicDnsName>ec2-50-18-200-169.us-west-1.compute.amazonaws.com</publicDnsName>
                        <ipOwnerId>036476006320</ipOwnerId>
                        <allocationId>eipalloc-0f749c8c6fe1d8101</allocationId>
                        <associationId>eipassoc-a56ec57f</associationId>
                        <natEnabled>true</natEnabled>
                    </association>
                </item>
            </privateIpAddressesSet>
            <ipv6AddressesSet/>
            <interfaceType>nat_gateway</interfaceType>
        </item>

Expected Behavior

The Network Interface Type should resolve to NAT_GATEWAY.

Current Behavior

The Network Interface Type resolves to UNKNOWN_TO_SDK_VERSION

Reproduction Steps

        try(var client = Ec2Client.builder().build()) {
            var req = DescribeNetworkInterfacesRequest.builder().build();

            var resp = client.describeNetworkInterfaces(req);
            for(NetworkInterface nwif : resp.networkInterfaces()) {
                var ifType = nwif.interfaceType();
                var ifTypeName = nwif.interfaceTypeAsString();
                if (ifTypeName != null && ifTypeName.toLowerCase().contains("nat")) {
                    System.out.println(ifTypeName);
                    System.out.println(ifType);
                }
            }
        }

Response is:

nat_gateway
null

Indicating that the string internally stored by the SDK is nat_gateway and the convertion to Enum fails. This is due to the incorrect string defined here:

public enum NetworkInterfaceType {
    INTERFACE("interface"),
    NAT_GATEWAY("natGateway"),  <======= should be nat_gateway
    EFA("efa"),
    TRUNK("trunk"),
    LOAD_BALANCER("load_balancer"),
    NETWORK_LOAD_BALANCER("network_load_balancer"),
    VPC_ENDPOINT("vpc_endpoint"),
    BRANCH("branch"),
    TRANSIT_GATEWAY("transit_gateway"),
    LAMBDA("lambda"),
    QUICKSIGHT("quicksight"),
    GLOBAL_ACCELERATOR_MANAGED("global_accelerator_managed"),
    API_GATEWAY_MANAGED("api_gateway_managed"),
    GATEWAY_LOAD_BALANCER("gateway_load_balancer"),
    GATEWAY_LOAD_BALANCER_ENDPOINT("gateway_load_balancer_endpoint"),
    IOT_RULES_MANAGED("iot_rules_managed"),
    AWS_CODESTAR_CONNECTIONS_MANAGED("aws_codestar_connections_managed"),
    UNKNOWN_TO_SDK_VERSION((String)null);

    private static final Map<String, NetworkInterfaceType> VALUE_MAP = EnumUtils.uniqueIndex(NetworkInterfaceType.class, NetworkInterfaceType::toString);

Possible Solution

Update services/ec2/src/main/resources/codegen-resources/service-2.json:46316 to replace"natGateway"with"nat_gateway"`.

Additional Information/Context

No response

AWS Java SDK version used

2.26.18

JDK version used

openjdk version "17.0.9" 2023-10-17

Operating System and version

Linux 5.10.219-208.866.amzn2.x86_64 aws/aws-sdk-java-v2#1 SMP Tue Jun 18 14:00:06 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

nmesika-te commented 1 month ago

Here's the code I used to reproduce the issue (and a similar issue with internet gateways which was apparently reported for the go-sdk as well.

https://github.com/nmesika-te/aws-sdk-test/blob/main/src/main/java/org/example/Main.java#L24

bhoradc commented 1 month ago

Hi @nmesika-te,

Thank you for reporting this issue. I am able to reproduce this scenario, where I get a NULL value for EC2's DescribeNetworkInterfaces [interfaceType()](https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/ec2/model/NetworkInterface.html#interfaceType()) call for nat_gateway.

These enumeration constants are generated from service model shared by EC2 service team. I will submit an internal ticket to the service team for further investigation and fix.

Also, since this something that has to be fixed at service level, I will transfer this to our Shared SDK repo for further tracking purpose.

Regards, Chaitanya

bhoradc commented 1 month ago

V1454285308