nodejs-compute icon indicating copy to clipboard operation
nodejs-compute copied to clipboard

Keys of properties.labels field on instance templates are changed to camel case when creating.

Open pj opened this issue 3 years ago • 6 comments
trafficstars

Environment details

  • OS: MacOS 12.1
  • Node.js version: 16.3.1
  • npm version: 8.1.2
  • @google-cloud/compute version: 3.1.1

Steps to reproduce

When setting the properties.labels field the client library generates an error because it tries to transform the keys of the labels into camel case, which isn't valid.

The following code produces an error when run:


(async() => {
  const instanceTemplatesClient = new compute.InstanceTemplatesClient();

  await instanceTemplatesClient.insert({
    project: process.env.GCP_PROJECT, instanceTemplateResource: {
      "description": "Test",
      "name": "test-labels",
      "properties": {
        "canIpForward": false,
        "confidentialInstanceConfig": {
          "enableConfidentialCompute": false
        },
        "description": "",
        "disks": [
          {
            "autoDelete": true,
            "boot": true,
            "deviceName": "instance-template-1",
            "initializeParams": {
              "diskSizeGb": "10",
              "diskType": "pd-balanced",
              "labels": {},
              "sourceImage": "projects/debian-cloud/global/images/debian-10-buster-v20220118"
            },
            "mode": "READ_WRITE",
            "type": "PERSISTENT"
          }
        ],
        "labels": {"test-that-its-broken": ""},
        "machineType": "e2-medium",
        "metadata": {
          "items": []
        },
        "networkInterfaces": [
          {
            "accessConfigs": [
              {
                "kind": "compute#accessConfig",
                "name": "External NAT",
                "networkTier": "PREMIUM",
                "type": "ONE_TO_ONE_NAT"
              }
            ],
            "network": "projects/dev-project-lazy-test-io/global/networks/default"
          }
        ],
        "reservationAffinity": {
          "consumeReservationType": "ANY_RESERVATION"
        },
        "scheduling": {
          "automaticRestart": true,
          "onHostMaintenance": "MIGRATE",
          "preemptible": false
        },
        "serviceAccounts": [
          {
            "email": "[email protected]",
            "scopes": [
              "https://www.googleapis.com/auth/devstorage.read_only",
              "https://www.googleapis.com/auth/logging.write",
              "https://www.googleapis.com/auth/monitoring.write",
              "https://www.googleapis.com/auth/servicecontrol",
              "https://www.googleapis.com/auth/service.management.readonly",
              "https://www.googleapis.com/auth/trace.append"
            ]
          }
        ],
        "shieldedInstanceConfig": {
          "enableIntegrityMonitoring": true,
          "enableSecureBoot": false,
          "enableVtpm": true
        },
        "tags": {
          "items": []
        }
      }
    }})
})();

Generated error:

Error: Invalid value for field 'resource.properties.labels': ''. Label key 'testThatItsBroken' violates format constraints. The key must start with a lowercase character, can only contain lowercase letters, numeric characters, underscores and dashes. The key can be at most 63 characters long. International characters are allowed.
    at Function.parseHttpError (/Users/pauljohnson/Programming/lazy_cloud/server/node_modules/google-gax/src/googleError.ts:63:7)
    at decodeResponse (/Users/pauljohnson/Programming/lazy_cloud/server/node_modules/google-gax/src/fallbackRest.ts:102:45)
    at /Users/pauljohnson/Programming/lazy_cloud/server/node_modules/google-gax/src/fallbackServiceStub.ts:151:34
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

Changing the key to testthatitsbroken works.

pj avatar Feb 11 '22 05:02 pj

@pj Thanks for the issue and providing the reproduce code.

I have tested on my end. It works fine, I am able to insert with the resource.properties.labels as "test-that-its-broken". To further debugging, could you print out two items for me:

  1. npm ls google-gax: To get the dependency @google-gax version.
  2. Print out the fetchRequest body in /Users/pauljohnson/Programming/lazy_cloud/server/node_modules/google-gax/src/fallbackServiceStub.js.
console.log(fetchRequest.body)
return fetch(url, fetchRequest);

Expected output:

{"description":"Test","name":"test-labels","properties":{"disks":[{"autoDelete":true,"boot":true,"deviceName":"instance-template-1","initializeParams":{"labels":{},"diskSizeGb":"10","diskType":"pd-balanced","sourceImage":"projects/debian-cloud/global/images/debian-10-buster-v20220118"},"mode":"READ_WRITE","type":"PERSISTENT"}],"labels":{"test-that-its-broken":""},"networkInterfaces":...}

summer-ji-eng avatar Mar 08 '22 17:03 summer-ji-eng

I'm seeing this as an issue. If you use any label key that has a _ underscore, the library changes it to camelcase, and then an error is thrown stating uppercase letters are not allowed.

{
   "recommender_testing": "true"
}
Invalid value for field 'labels': ''. Label key 'recommenderTesting' violates format constraints. The key must start with a lowercase character, can only contain lowercase letters, numeric characters, underscores and dashes. The key can be at most 63 characters long. International characters are allowed.

james-lukensow avatar May 03 '22 23:05 james-lukensow

This happens after google updated the google-gax package from 3.1.2 to 3.1.3. I downgraded the version of the google-gax package to 3.1.2, and It works

ronikarapplitools avatar Jul 06 '22 07:07 ronikarapplitools

Also getting this issue creating a VM with instanceResource.labels that have - (e.g. camel-case), these are also converted to camel case (e.g. camelCase).

rhodgkins avatar Jul 07 '22 11:07 rhodgkins

google-gax issue: googleapis/gax-nodejs#1288

rhodgkins avatar Jul 08 '22 11:07 rhodgkins

This will be resolved with https://github.com/googleapis/gax-nodejs/pull/1299

sofisl avatar Jul 29 '22 13:07 sofisl