Monday, 9 November 2020

OCI Command Line JSON File Data

Part of my day job is to manage PeopleSoft system running on Oracle Cloud Infrastructure (OCI).  I use the OCI API extensively in PeopleCode and Python but I don't often use the Command Line Interface (CLI).

Just wanted to call out an issue I hit recently when trying to pass in a JSON file as input to a CLI command.

In this example I'm using the CLI to update an existing Security List.  Security Lists in OCI define IP and Port based rules allowing traffic in or out of a subnet.

You can see an example of a network Ingress (Inbound) rule below.

Let's use the CLI to modify the Destination Port Number of this Rule from 8000 to 8001.

The CLI command for this is:

oci network security-list update --security-list-id <ocid of Security List> --ingress-security-rules file://myNewRule.json

JSON can be supplied on the command line as a string input.  However, if you're running CLI on Windows then you need to escape the " in the string (e.g. /"). It's much easier to include the JSON in a file and then input that to the --ingress-security-rules parameter.

But what does the shape of this JSON file need to be?  Well we don't need to guess or try and key from some online documentation as there's a handy parameter which will generate a JSON file template --generate-param-json

oci network security-list update  --generate-param-json-input ingress-security-rules > myNewRule.json

I'm using the standard redirect symbol > to send the output of the command to a file.  A sample fragment of this input template looks like this:

This template makes it easy to create the JSON to make the change we want.

WARNINGWhen a Security List is updated the existing Security List is replaced with the inbound definition.  You get a warning of this when you execute the command.

I ran the command and this is what I got:

Parameter 'ingress_security_rules' must be in JSON format.

But my file is in JSON format.  A few hours of tinkering around and my file is still in JSON format, looks valid but still throwing this error.

Turns out the File Encoding matters.  My JSON input file was in UCS-2 LE BOM.  This got set when I redirected the output of the OCI command which was running in the Windows PowerShell.  This file encoding is obsolete and not appreciated by the OCI CLI.   If I execute the command to redirect the template JSON to file in a regular Command Prompt window then I get a supported encoding of UTF-8 which works.

No comments: