|
| 1 | +.. _go-template-output: |
| 2 | + |
| 3 | +================================== |
| 4 | +Customize the {+atlas-cli+} Output |
| 5 | +================================== |
| 6 | + |
| 7 | +.. default-domain:: mongodb |
| 8 | + |
| 9 | +You can customize the {+atlas-cli+} output fields and format using a Go |
| 10 | +template or a JSON path, which makes it easier to automate processes based on the |
| 11 | +output from the {+atlas-cli+}. |
| 12 | + |
| 13 | +Go templates |
| 14 | +------------ |
| 15 | + |
| 16 | +You can specify a Go template within any {+atlas-cli+} command |
| 17 | +or through a separate file. To learn more about |
| 18 | +Go templates, see `Package template <https://golang.org/pkg/text/template/>`__. |
| 19 | +To learn the types and properties available for each response, see |
| 20 | +`Atlas types <https://go.mongodb.org/atlas/mongodbatlas/>`__. |
| 21 | + |
| 22 | +Syntax |
| 23 | +~~~~~~ |
| 24 | + |
| 25 | +You can specify a template with the command using the ``--output`` or |
| 26 | +``-o`` option: |
| 27 | + |
| 28 | +.. code-block:: shell |
| 29 | + :copyable: false |
| 30 | + |
| 31 | + --output|-o go-template="{{<template>}}" |
| 32 | + |
| 33 | +Alternatively, you can specify a template through a file using the ``--output`` or |
| 34 | +``-o`` option: |
| 35 | + |
| 36 | +.. code-block:: shell |
| 37 | + :copyable: false |
| 38 | + |
| 39 | + --output|-o go-template-file="<path-to-template-file>" |
| 40 | + |
| 41 | +Examples |
| 42 | +~~~~~~~~ |
| 43 | + |
| 44 | +.. tabs:: |
| 45 | + |
| 46 | + .. tab:: Specify Template With the Command |
| 47 | + :tabid: template-command |
| 48 | + |
| 49 | + Retrieve the Number of Projects |
| 50 | + ``````````````````````````````` |
| 51 | + |
| 52 | + The following command uses a template to retrieve a count of |
| 53 | + the number of projects in the specified organization: |
| 54 | + |
| 55 | + .. code-block:: shell |
| 56 | + |
| 57 | + atlas projects ls --orgId 5ab5cedf5g5h5i5j5kl12mn4 -o go-template="Count: {{.TotalCount}}" |
| 58 | + |
| 59 | + The preceding command returns the following output: |
| 60 | + |
| 61 | + .. code-block:: shell |
| 62 | + :copyable: false |
| 63 | + |
| 64 | + Count: 2 |
| 65 | + |
| 66 | + .. _quick-start-atlas-retrieve-conection-string: |
| 67 | + |
| 68 | + Retrieve Your |service| Cluster Connection String |
| 69 | + ````````````````````````````````````````````````` |
| 70 | + |
| 71 | + |
| 72 | + The following :ref:`atlas-clusters-describe` command |
| 73 | + uses the template to retrieve the connection string for an |
| 74 | + |service| cluster named ``getStarted``. It uses the default |
| 75 | + profile for accessing |service|. |
| 76 | + |
| 77 | + .. code-block:: sh |
| 78 | + |
| 79 | + atlas clusters describe getStarted -o go-template="Parse: {{.SrvAddress}}" |
| 80 | + |
| 81 | + The previous command returns a string similar to the following: |
| 82 | + |
| 83 | + .. code-block:: sh |
| 84 | + :copyable: false |
| 85 | + |
| 86 | + Parse: mongodb+srv://getstarted.example.mongodb.net |
| 87 | + |
| 88 | + You can use the MongoDB Shell, {+mongosh+}, to connect to the |
| 89 | + ``getStarted`` |
| 90 | + cluster with the ``srvAddress`` and the :manual:`connection |
| 91 | + string |
| 92 | + </reference/connection-string/#connection-string-options>`. This |
| 93 | + example uses the connection string returned by the previous command |
| 94 | + for a user with the username ``User1``. |
| 95 | + |
| 96 | + .. code-block:: |
| 97 | + |
| 98 | + mongo "mongodb+srv://getstarted.example.mongodb.net" --username User1 --password ChangeThisPasswordToSomethingSecure |
| 99 | + |
| 100 | + .. tab:: Specify Template Through a File |
| 101 | + :tabid: template-file |
| 102 | + |
| 103 | + For example, consider the following file named ``template.tmpl``: |
| 104 | + |
| 105 | + .. code-block:: shell |
| 106 | + |
| 107 | + Projects: {{range .Results}}{{.ID}} {{end}} |
| 108 | + |
| 109 | + The following command uses the ``template.tmpl`` file to retrieve |
| 110 | + the IDs of the projects in the specified organization: |
| 111 | + |
| 112 | + .. code-block:: shell |
| 113 | + |
| 114 | + atlas projects ls --orgId 5ab5cedf5g5h5i5j5kl12mn4 -o go-template-file="template.tmpl" |
| 115 | + |
| 116 | + The preceding command returns the following output: |
| 117 | + |
| 118 | + .. code-block:: shell |
| 119 | + :copyable: false |
| 120 | + |
| 121 | + Projects: 5e2211c17a3e5a48f5497de3 5f455b39749bea5fb575dccc |
| 122 | + |
| 123 | +.. _json-path-output-option: |
| 124 | + |
| 125 | +``json-path`` Output Type |
| 126 | +------------------------- |
| 127 | + |
| 128 | +.. default-domain:: mongodb |
| 129 | + |
| 130 | +.. contents:: On this page |
| 131 | + :local: |
| 132 | + :backlinks: none |
| 133 | + :depth: 1 |
| 134 | + :class: singlecol |
| 135 | + |
| 136 | +The ``json-path`` output type limits the results of a command to the |
| 137 | +fields you specify. |
| 138 | + |
| 139 | +Usage |
| 140 | +~~~~~ |
| 141 | + |
| 142 | +When you add the ``--output`` option to a command, you can specify the |
| 143 | +type ``json-path``. You must provide ``json-path`` with an expression |
| 144 | +to evaluate against your results, which means you must be aware of the |
| 145 | +``json`` fields returned by your command. |
| 146 | + |
| 147 | +Syntax |
| 148 | +~~~~~~ |
| 149 | + |
| 150 | +.. code-block:: shell |
| 151 | + :copyable: false |
| 152 | + |
| 153 | + <command> --output|-o json-path='$<expression>' |
| 154 | + |
| 155 | +``json-path`` expressions refer to the |json| element that an {+atlas-cli+} |
| 156 | +command returns. The ``$`` character represents the root element, which |
| 157 | +is usually an object or an array. |
| 158 | + |
| 159 | +For a list of valid characters and their functions, see `JSONPath expressions <https://goessner.net/articles/JsonPath/index.html#e2>`__. |
| 160 | + |
| 161 | +Examples |
| 162 | +~~~~~~~~ |
| 163 | + |
| 164 | +Return the description of the first |api| key in a list |
| 165 | +``````````````````````````````````````````````````````` |
| 166 | + |
| 167 | +In the following example, a user retrieves their |api| keys with |
| 168 | +:ref:`atlas-organizations-apiKeys-list`. The ``json-path`` |
| 169 | +expression limits the output to the ``desc`` field of the first key, |
| 170 | +rather than returning the entire list of keys. |
| 171 | + |
| 172 | +.. code-block:: shell |
| 173 | + :copyable: false |
| 174 | + |
| 175 | + atlas organizations apikeys list --output json-path='$[0].desc' |
| 176 | + > owner_key |
| 177 | + |
| 178 | +Running the same command with ``--output json`` returns the full |json| |
| 179 | +element from the |api|. It's important to understand the |json| |
| 180 | +structure returned by a command in order to operate on it with |
| 181 | +``json-path``. |
| 182 | + |
| 183 | +Using the the following full |json| output for reference, the |
| 184 | +``--output json-path`` with the expression ``$[0].desc`` finds and |
| 185 | +returns only the value ``"owner_key"``: |
| 186 | + |
| 187 | +.. code-block:: json |
| 188 | + :copyable: false |
| 189 | + :emphasize-lines: 1-2, 4 |
| 190 | + |
| 191 | + [ //``$`` represents the outer array. |
| 192 | + { // ``[0]`` refers to the first element in the array (using a 0-based index). |
| 193 | + "id": "60e736a95d585d2c9ccf2d19", |
| 194 | + "desc": "owner_key", //``.desc`` refers to the ``desc`` field of that element. |
| 195 | + "roles": [ |
| 196 | + { |
| 197 | + "orgId": "5d961a949ccf64b4e7f53bac", |
| 198 | + "roleName": "ORG_OWNER" |
| 199 | + } |
| 200 | + ], |
| 201 | + "privateKey": "********-****-****-c4e26334754f", |
| 202 | + "publicKey": "xtfmtguk" |
| 203 | + }, |
| 204 | + { |
| 205 | + "id": "d2c9ccf2d1960e736a95d585", |
| 206 | + "desc": "member_key", |
| 207 | + "roles": [ |
| 208 | + { |
| 209 | + "orgId": "5d961a949ccf64b4e7f53bac", |
| 210 | + "roleName": "ORG_MEMBER" |
| 211 | + } |
| 212 | + ], |
| 213 | + "privateKey": "********-****-****-c4e26334754f", |
| 214 | + "publicKey": "vfgcttku" |
| 215 | + }, |
| 216 | + ] |
| 217 | + |
| 218 | +Return the description of a specific |api| key in a list |
| 219 | +```````````````````````````````````````````````````````` |
| 220 | + |
| 221 | +In the following example, a user retrieves their |api| keys with |
| 222 | +:ref:`atlas-organizations-apiKeys-list`. The ``json-path`` |
| 223 | +expression limits the output to the ``desc`` field of the specific |
| 224 | +|json| object with ``id`` ``d2c9ccf2d1960e736a95d585``. |
| 225 | + |
| 226 | +.. code-block:: shell |
| 227 | + :copyable: false |
| 228 | + |
| 229 | + atlas organizations apikeys list --output json-path='$[? @.id=="d2c9ccf2d1960e736a95d585"].desc' |
| 230 | + > member_key |
| 231 | + |
| 232 | +Return the status of a private endpoint |
| 233 | +``````````````````````````````````````` |
| 234 | + |
| 235 | +In the following example, a user retrieves information for a |
| 236 | +private endpoint with |
| 237 | +:ref:`atlas-privateEndpoints-aws-describe`. The ``json-path`` |
| 238 | +expression limits the output to the ``status`` field of the root |
| 239 | +element. |
| 240 | + |
| 241 | +.. code-block:: shell |
| 242 | + :copyable: false |
| 243 | + |
| 244 | + atlas privateendpoints aws describe 601a4044da900269480a2533 --output json-path='$.status' |
| 245 | + > WAITING_FOR_USER |
0 commit comments