获取谷歌云身份的所有群组使用 gcloud 命令

huangapple go评论65阅读模式
英文:

How to get all groups from google cloud identity using gcloud command

问题

I am using the gcloud beta command to search the groups in GCP.

The problem is I have 1700 groups, and after x number of groups, the command generates a nextpagetoken and I have to enter it manually to rerun the command. Is there any way I can automate it? i.e., store the next page token in a variable and pass it to the following command.

gcloud beta identity groups search --organization="5487965215" --labels="cloudidentity.googleapis.com/groups.discussion_forum" --page-size=3

英文:

I am using the gcloud beta command to search the groups in GCP.

The problem is I have 1700 groups, and after x number of groups command generates nextpagetoken and I have to enter it manually to rerun the command. Is there any way I can automate it? i.e, store the next page token in a variable and pass it to the following command.

gcloud beta identity groups search --organization="5487965215"  --labels="cloudidentity.googleapis.com/groups.discussion_forum" --page-size=3

答案1

得分: 1

实施可以以不同的方式进行,其中之一是使用一个循环来从响应中获取nextPageToken

您可以在Cloud Shell上运行此脚本并更改组织ID。组名称将保存在group.txt文件中。

# 设置
ORGANIZATION_ID="..."

# 获取组列表
echo -n > groups.txt
GCLOUD_ARG_PAGE_TOKEN=""
RUN=1
while [ $RUN == 1 ] ; do
  gcloud beta identity groups search \
    --organization="${ORGANIZATION_ID}" \
    --labels="cloudidentity.googleapis.com/groups.discussion_forum" \
    --format=json \
    --page-size=1000 \
    ${GCLOUD_ARG_PAGE_TOKEN} \
    > response.json < response.json jq -r "[0].groups[].groupKey.id" >> groups.txt  NEXT_PAGE_TOKEN="$(<response.json jq -r '[0].nextPageToken')"
  if [ "$NEXT_PAGE_TOKEN" == "null" ] ; then
    GCLOUD_ARG_PAGE_TOKEN=""
    RUN=0
  else
    GCLOUD_ARG_PAGE_TOKEN="--page-token=$NEXT_PAGE_TOKEN"
  fi
done

这是您提供的代码部分的翻译。

英文:

Implementation can be done in different ways, one of them using a while loop that retrieves nextPageToken from response.

You can run this script on the Cloud Shell and change the Org ID. Group names will be saved on group.txt file

# setup
ORGANIZATION_ID=&quot;...&quot; 

# get groups list
echo -n &gt; groups.txt
GCLOUD_ARG_PAGE_TOKEN=&quot;&quot;
RUN=1
while [ $RUN == 1 ] ; do
&#160; gcloud beta identity groups search \
&#160; &#160; --organization=&quot;${ORGANIZATION_ID}&quot; \
&#160; &#160; --labels=&quot;cloudidentity.googleapis.com/groups.discussion_forum&quot; \
&#160; &#160; --format=json \
&#160; &#160; --page-size=1000 \
&#160; &#160; ${GCLOUD_ARG_PAGE_TOKEN} \
&#160; &gt; response.json &#160; &lt;response.json jq -r &quot;.[0].groups[].groupKey.id&quot; &gt;&gt; groups.txt &#160; NEXT_PAGE_TOKEN=&quot;$(&lt;response.json jq -r &#39;.[0].nextPageToken&#39;)&quot;
&#160; if [ &quot;$NEXT_PAGE_TOKEN&quot; == &quot;null&quot; ] ; then
&#160; &#160; GCLOUD_ARG_PAGE_TOKEN=&quot;&quot;
&#160; &#160; RUN=0
&#160; else
&#160; &#160; GCLOUD_ARG_PAGE_TOKEN=&quot;--page-token=$NEXT_PAGE_TOKEN&quot;
&#160; fi
done

huangapple
  • 本文由 发表于 2023年4月13日 23:07:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76007031.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定