Skip to content

Commit

Permalink
fix: [M3-7099] - Metadata CLI command (#9665)
Browse files Browse the repository at this point in the history
## Description 📝
Fix metadata command when creating with the Linode CLI

## How to test 🧪
```
yarn test packages/manager/src/utilities/generate-cli.test.ts
```
- Go to `/linodes/create` and fill out the create form with a cloud-init compatible image and a metadata compatible region (e.g. Washington, DC)
- Enter some text in the User Data field of the Add User Data accordion
- Click the `Create Using Command Line` button and click on the `Linode CLI` tab
- Ensure that the metadata command is `--metadata.user_data="<user_data>"` instead of `--metadata '{"user_data": "<user_data>"}'`
  • Loading branch information
hana-akamai authored Sep 13, 2023
1 parent 656a0f9 commit 2423f3f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
5 changes: 5 additions & 0 deletions packages/manager/.changeset/pr-9665-fixed-1694552506393.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@linode/manager": Fixed
---

Metadata CLI command ([#9665](https://github.com/linode/manager/pull/9665))
19 changes: 12 additions & 7 deletions packages/manager/src/utilities/generate-cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,28 @@ const linodeData = {
...linodeRequest,
authorized_users: ['Linny', 'Gritty'],
backup_id: undefined,
metadata: {
user_data: 'cmVrbmpnYmloZXVma2xkbQpqZXZia2Y=',
},
stackscript_data: {
gh_username: 'linode',
},
stackscript_id: 10079,
};

const linodeDataForCLI = `
linode-cli linodes create \\
--booted ${linodeRequest.booted} \\
--image ${linodeRequest.image} \\
--label ${linodeRequest.label} \\
--region ${linodeRequest.region} \\
--root_pass ${linodeRequest.root_pass} \\
--image ${linodeRequest.image} \\
--type ${linodeRequest.type} \\
--region ${linodeRequest.region} \\
--booted ${linodeRequest.booted} \\
--stackscript_id 10079 \\
--stackscript_data '{"gh_username": "linode"}' \\
--authorized_users Linny \\
--authorized_users Gritty
--authorized_users Gritty \\
--metadata.user_data="cmVrbmpnYmloZXVma2xkbQpqZXZia2Y=" \\
--stackscript_data '{"gh_username": "linode"}' \\
--stackscript_id 10079
`.trim();

const generatedCommand = generateCLICommand(linodeData);
Expand All @@ -36,7 +41,7 @@ describe('generateCLICommand', () => {
).toBeTruthy();
});

it.skip('should return a linode-cli command with the data provided formatted as arguments', () => {
it('should return a linode-cli command with the data provided formatted as arguments', () => {
expect(generatedCommand).toMatch(linodeDataForCLI);
});

Expand Down
10 changes: 9 additions & 1 deletion packages/manager/src/utilities/generate-cli.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { UserData } from '@linode/api-v4/lib/linodes/types';

// Credit: https://github.com/xxorax/node-shell-escape
function escapeStringForCLI(s: string): string {
if (/[^A-Za-z0-9_\/:=-]/.test(s)) {
Expand Down Expand Up @@ -50,12 +52,18 @@ const parseString = (key: string, value: string) => {
const dataEntriesReduce = (acc: string[], [key, value]: JSONFieldToArray) => {
if (value === undefined || value === null) {
return acc;
} else if (Array.isArray(value)) {
}
if (Array.isArray(value)) {
if (value.length === 0) {
return acc;
}
acc.push(parseArray(key, value));
return acc;
}

if (key === 'metadata') {
const userData = value as UserData;
acc.push(` --${key}.user_data="${userData.user_data}"`);
} else if (typeof value === 'object') {
const valueAsString = convertObjectToCLIArg(value);
acc.push(` --${key} ${valueAsString}`);
Expand Down

0 comments on commit 2423f3f

Please sign in to comment.