0

I am looking to enable an API in all of my projects, but only create the BigQuery dataset in one project. I've tried different ways, but the dataset is being created in every project. Any suggestions on how to combine these two into one bash script so that the API is enabled in all projects, but the dataset is only created in one project?

#!/bin/bash
for project in  $(gcloud projects list --format="value(projectId)")
do
    echo "ProjectId:  $project"
    gcloud config set project $project
    gcloud services enable compute.googleapis.com  --project $project
    bq mk testdataset <insert projectid>
done
Emma
  • 27,428
  • 11
  • 44
  • 69
Lucas
  • 65
  • 1
  • 1
  • 6
  • How does your for loop know which project to only execute `bq mk ...` for? Move the command outside the loop and specify the project. – John Hanley Mar 05 '19 at 04:25
  • for I normally have the project id I want to create the dataset in. I just put that as a fill-in. If that line is "bq mk testdataset myproject" (where myproject is the project id of the project I want to create the dataset in), it still creates it in all projects. Also tried "bq mk testdataset --project myproject" which does not create the dataset in any projects. – Lucas Mar 05 '19 at 06:18
  • The command should be: `bq mk --project_id testdataset`. But why have it inside the loop if you know the project id in which you want to create the dataset? If you have N projects it will create the dataset in the first pass and then run N-1 more times just telling you "Dataset already exists". – TasosZG Mar 05 '19 at 08:49
  • @TasosZG in the most basic terms, my end goal is to copy and paste everything into cloudshell and only have to hit enter once. It will then enable the api in all projects and create that dataset in one project. So if there is a way to do that and have the dataset command outside of the loop, how would I put that together? Clearly i'm a rookie, sorry if any of this obvious! – Lucas Mar 05 '19 at 17:25

1 Answers1

1

The command to create a dataset in a specific project is:

bq mk --project_id <insert projectid> testdataset

Therefore, since you know the project id in which you want to create the dataset you can move the command outside the loop like this:

#!/bin/bash
for project in  $(gcloud projects list --format="value(projectId)")
do
    echo "ProjectId:  $project"
    gcloud services enable compute.googleapis.com  --project $project
done
bq mk --project_id <insert projectid> testdataset
TasosZG
  • 1,274
  • 1
  • 6
  • 13