Astronet-K2
Astronet-K2

Reputation: 49

create gcloud configuration from json file

I have downloaded my gcloud configs as json (with gcloud config configurations list --format=json). Then I picked up one config out from the collection returned, and updated all the features/flags I want in that json and saved as a file. Now I want to create a new configuration with this updated JSON file, how can I do that? Thanks in advance!

Upvotes: 0

Views: 2256

Answers (2)

Gerb
Gerb

Reputation: 496

I'm assuming that you are on a Unix-like machine, and that your configurations are stored in ~/.config/gcloud/configurations.

I'm also assuming that you created the original JSON output by running something similar to the following, and then editing the file in-place:

gcloud config configurations activate SOME_CONFIG
gcloud config list --format=json > newconfig.json

First, create the new configuration that you want to populate:

gcloud config configurations create newconfig

You can confirm that this file was created by running:

ls ~/.config/gcloud/configurations/config_newconfig

Next, use this hidden command to convert the JSON file to config format, and overwrite the configuration file:

gcloud meta list-from-json newconfig.json --format=config > ~/.config/gcloud/configurations/config_newconfig

You can confirm that things are as expected by running:

gcloud config configurations activate newconfig
gcloud config list

Upvotes: 2

DazWilkin
DazWilkin

Reputation: 40136

Please test this thoroughly; improvements welcome

Requires: jq

  • filter by (configuration) name=="foo"
  • convert .properties object into an array {"key":.., "value":[...]}
  • map to {"section":...,"properties"...} converting values to an array and filtering null
  • map the resulting {"section":...,"properties":{"key":...,"value":...}
  • into a gcloud config set {section}/${properties[key]} ${properties[value]}
  • returning the array as a list of strings

NOTE There ought to be a way to do this with a single map: capturing the section value and then for each property key:value pair (where value != null) emitting the gcloud config set command

FILTER='.[]
|select(.name=="foo")
|.properties|to_entries
|map(
  {
    "section": .key,
    "properties":.value|to_entries|.[]|select(.value!=null)
  }
)
|map(
  "gcloud config set "+.section+"/"+.properties["key"]+" "+.properties["value"]
)
|.[]'

# Or cat /path/to/your/saved.json
gcloud config configurations list --format=json \
| jq -r "${FILTER}"

Example: https://jqplay.org/s/ZO_6urQma8

You could pipe the output into a script and check the script before running it.

Upvotes: 2

Related Questions