Skip to content

Commit

Permalink
GCP: Simplify the use of ENV var's.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmiddelkoop committed Feb 3, 2022
1 parent 0d114c5 commit 9c77425
Showing 1 changed file with 60 additions and 135 deletions.
195 changes: 60 additions & 135 deletions content/GCP/04_intro_to_cli.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,16 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"id": "3ce9e6fb-9f5f-42e1-8512-35c0564f8e85",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"998995103712-compute@developer.gserviceaccount.com\n"
"Your active configuration is: [cloudshell-2071]\n",
"learner@class.internet2.edu\n"
]
}
],
Expand All @@ -130,14 +131,15 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 2,
"id": "cfb10587-e007-45ae-839f-7bcfe088c711",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Your active configuration is: [cloudshell-2071]\n",
"essentials-learner\n"
]
}
Expand All @@ -156,7 +158,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 3,
"id": "617325c9-d853-4291-a1db-938ab9439fee",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -250,7 +252,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 5,
"id": "b8f74fe9-0943-4bd9-a91b-da09c001f8a1",
"metadata": {},
"outputs": [],
Expand All @@ -260,7 +262,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 6,
"id": "b937f6c7-3697-4bc0-8e32-da0b0faec593",
"metadata": {},
"outputs": [
Expand All @@ -286,7 +288,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 7,
"id": "bae0587c-0379-40e3-9c31-16ba5505487a",
"metadata": {},
"outputs": [
Expand All @@ -304,15 +306,15 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 8,
"id": "52b4add5-0d18-4faf-a304-b7e0ee8ba82e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"essentials\n"
"cs-194566722441-default\n"
]
}
],
Expand All @@ -330,15 +332,15 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 9,
"id": "776806e4-209f-45fa-b14e-f771f5d5089d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Thu Feb 3 20:10:51 UTC 2022\n"
"Thu 03 Feb 2022 09:50:31 PM UTC\n"
]
}
],
Expand All @@ -348,15 +350,15 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 10,
"id": "560fde63-0eb8-4c52-9787-4debfe35ec23",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Date: Thu Feb 3 20:10:52 UTC 2022\n"
"Date: Thu 03 Feb 2022 09:50:31 PM UTC\n"
]
}
],
Expand Down Expand Up @@ -384,14 +386,15 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 11,
"id": "18f40620-8537-42b5-948f-dd581e604a36",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Your active configuration is: [cloudshell-2071]\n",
"essentials-learner\n"
]
}
Expand All @@ -402,19 +405,27 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 12,
"id": "a959f857-8a20-4356-bc30-dbf8c75be96d",
"metadata": {
"tags": []
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Your active configuration is: [cloudshell-2071]\n"
]
}
],
"source": [
"PROJECT=\"$(gcloud config get-value project)\""
"PROJECT=$(gcloud config get-value project)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 13,
"id": "c1d27e89-cc69-4979-82c4-79c3fb36bccb",
"metadata": {},
"outputs": [
Expand All @@ -435,7 +446,17 @@
"id": "9d3a8504-a75c-4945-ab54-85f1d9a7e2b2",
"metadata": {},
"source": [
"Note the \"Your active configuration is: ...\" line is a \"warning\" and not actually part of the output of the program."
"Note the \"Your active configuration is: ...\" line is a \"warning\" and not actually part of the output of the program.\n",
"\n",
"````{admonition} Advanced Tip\n",
":class: tip\n",
"\n",
"You can also use \n",
"```\n",
"PROJECT=$(gcloud config list --format='value(core.project)')\n",
"```\n",
"to get the project id without the warning.\n",
"````"
]
},
{
Expand All @@ -448,7 +469,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 14,
"id": "31b01f78-94b1-44f0-a2af-d657058941b2",
"metadata": {},
"outputs": [
Expand All @@ -471,109 +492,15 @@
"gcloud projects describe $PROJECT"
]
},
{
"cell_type": "markdown",
"id": "ed2ab9ce-6b0a-4939-88c7-8103e29413b7",
"metadata": {},
"source": [
"Note the extraneous message about the active configuration produced by the `gcloud config get-value project` command. A slightly different way to access the data can be used to remove the extraneous message by using the following command:"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "845b2aaf-0032-4043-a40a-d2482693d23e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[core]\n",
"account = 998995103712-compute@developer.gserviceaccount.com\n",
"disable_usage_reporting = True\n",
"project = essentials-learner\n",
"\n",
"Your active configuration is: [default]\n"
]
}
],
"source": [
"gcloud config list"
]
},
{
"cell_type": "markdown",
"id": "d919b26d-2f64-4a43-9c76-34f7798fe853",
"metadata": {},
"source": [
"We can now use the `--format='value()'` option to get specific values. The following command returns the project number without the extraneous message."
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "9714ebe2-2921-498b-9e0e-69ec6a85896a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"essentials-learner\n"
]
}
],
"source": [
"gcloud config list --format='value(core.project)'"
]
},
{
"cell_type": "markdown",
"id": "ba660d2b-5ddf-449b-8049-56a40f2e366b",
"metadata": {},
"source": [
"Note where the `core` part comes in the output of the `gcloud config list` command."
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "7d803ddd-acca-4ad1-933a-4d6e6044def6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"essentials-learner\n"
]
}
],
"source": [
"PROJECT=$(gcloud config list --format='value(core.project)')\n",
"echo $PROJECT"
]
},
{
"cell_type": "markdown",
"id": "d40b2c7b-7331-412d-80e1-f1a77204075a",
"metadata": {},
"source": [
"We will use this form from now on. Since we know the Project ID only contains numbers, letters, and dashes with no spaces there is no need to use quotes."
]
},
{
"cell_type": "markdown",
"id": "270e0abb-d5e9-476a-af91-060cc0ea0349",
"metadata": {},
"source": [
"```{admonition} Exercise\n",
"\n",
" * What command shows the project number? (hint: we used it already)\n",
" * What is the name of this variable? (hint: it is case sensitive)\n",
" * What command shows *just* the Project Number (hint: use `--format`)\n",
" * Set the `PROJECT_NUMBER` environment variable with the correct value. (hint: be careful with punctuation (single and double quotes) and matching brackets and punctuation)\n",
" * What command shows the account? (hint: we used it already)\n",
" * Set the `ACCOUNT` environment variable with the correct value. (hint: be careful with punctuation (single and double quotes) and matching brackets and punctuation)\n",
"```"
]
},
Expand All @@ -584,12 +511,12 @@
"source": [
"## Formating and Scripting Output (Optional)\n",
"\n",
"The following example shows how to output the configuration as `JSON`. The command `jq` can also be used to parse the JSON data."
"The following example shows how to output the configuration as `JSON`. The command `jq` can also be used to parse the JSON data in addition to the `--format` option."
]
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 15,
"id": "678a1735-8cad-403c-b302-639de1fb0f4b",
"metadata": {
"tags": []
Expand All @@ -599,42 +526,40 @@
"name": "stdout",
"output_type": "stream",
"text": [
"bash: jq: command not found\n"
"\u001b[1;39m{\n",
" \u001b[0m\u001b[34;1m\"createTime\"\u001b[0m\u001b[1;39m: \u001b[0m\u001b[0;32m\"2022-01-14T19:03:26.486Z\"\u001b[0m\u001b[1;39m,\n",
" \u001b[0m\u001b[34;1m\"lifecycleState\"\u001b[0m\u001b[1;39m: \u001b[0m\u001b[0;32m\"ACTIVE\"\u001b[0m\u001b[1;39m,\n",
" \u001b[0m\u001b[34;1m\"name\"\u001b[0m\u001b[1;39m: \u001b[0m\u001b[0;32m\"essentials-learner\"\u001b[0m\u001b[1;39m,\n",
" \u001b[0m\u001b[34;1m\"parent\"\u001b[0m\u001b[1;39m: \u001b[0m\u001b[1;39m{\n",
" \u001b[0m\u001b[34;1m\"id\"\u001b[0m\u001b[1;39m: \u001b[0m\u001b[0;32m\"1072231596131\"\u001b[0m\u001b[1;39m,\n",
" \u001b[0m\u001b[34;1m\"type\"\u001b[0m\u001b[1;39m: \u001b[0m\u001b[0;32m\"folder\"\u001b[0m\u001b[1;39m\n",
" \u001b[1;39m}\u001b[0m\u001b[1;39m,\n",
" \u001b[0m\u001b[34;1m\"projectId\"\u001b[0m\u001b[1;39m: \u001b[0m\u001b[0;32m\"essentials-learner\"\u001b[0m\u001b[1;39m,\n",
" \u001b[0m\u001b[34;1m\"projectNumber\"\u001b[0m\u001b[1;39m: \u001b[0m\u001b[0;32m\"998995103712\"\u001b[0m\u001b[1;39m\n",
"\u001b[1;39m}\u001b[0m\n"
]
},
{
"ename": "",
"evalue": "127",
"output_type": "error",
"traceback": []
}
],
"source": [
"gcloud config list --format=json |jq"
"gcloud projects describe $PROJECT --format=json | jq"
]
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 16,
"id": "b0dec5ed-dc7f-4003-b228-f6605119ce47",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"bash: jq: command not found\n"
"998995103712\n"
]
},
{
"ename": "",
"evalue": "127",
"output_type": "error",
"traceback": []
}
],
"source": [
"gcloud config list --format=json |jq -r .core.project"
"gcloud projects describe $PROJECT --format=json | jq -r .projectNumber"
]
}
],
Expand Down

0 comments on commit 9c77425

Please sign in to comment.