Skip to content

Instantly share code, notes, and snippets.

@rezamt
Created December 2, 2018 00:53

Revisions

  1. rezamt created this gist Dec 2, 2018.
    90 changes: 90 additions & 0 deletions gcloud-fiters.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,90 @@
    List all Google Compute Engine instance resources:

    $ gcloud compute instances list

    List Compute Engine instance resources that have machineType f1-micro:

    $ gcloud compute instances list --filter="machineType:f1-micro"

    List Compute Engine instance resources with zone prefix us and not
    MachineType f1-micro:

    $ gcloud compute instances list \
    --filter="zone ~ ^us AND -machineType:f1-micro"

    List Compute Engine instance resources with tag my-tag:

    $ gcloud compute instances list --filter="tags.items=my-tag"

    List Compute Engine instance resources with tag my-tag or my-other-tag:

    $ gcloud compute instances list \
    --filter="tags.items=(my-tag,my-other-tag)"

    List Compute Engine instance resources with tag my-tag and my-other-tag:

    $ gcloud compute instances list \
    --filter="tags.items=my-tag AND tags.items=my-other-tag"

    List Compute Engine instance resources which either have tag my-tag but not
    my-other-tag or have tag alternative-tag:

    $ gcloud compute instances list \
    --filter="(tags.items=my-tag AND -tags.items=my-other-tag) OR\
    tags.items=alternative-tag"

    List Compute Engine instance resources with label my-label with any value:

    $ gcloud compute instances list --filter="labels.my-label:*"

    List in JSON format those projects where the labels match specific values
    (e.g. label.env is 'test' and label.version is alpha):

    $ gcloud projects list --format="json" \
    --filter="labels.env=test AND labels.version=alpha"

    List projects that were created on and after a specific date:

    $ gcloud projects list \
    --format="table(projectNumber,projectId,createTime)" \
    --filter="createTime>=2018-01-15"

    List projects that were created on and after a specific date and time and
    sort from oldest to newest (with dates and times listed according to the
    local timezone):

    $ gcloud projects list \
    --format="table(projectNumber,projectId,createTime.date(tz=LOCAL\
    ))" --filter="createTime>=2018-01-15T12:00:00" --sort-by=createTime

    List projects that were created within the last two weeks, using ISO8601
    durations:

    $ gcloud projects list \
    --format="table(projectNumber,projectId,createTime)" \
    --filter="createTime>-P2W"

    For more about ISO8601 durations, see:
    https://en.wikipedia.org/wiki/ISO_8601

    This table shows : operator pattern matching:

    PATTERN VALUE MATCHES DEPRECATED_MATCHES
    abc* abcpdqxyz True True
    abc abcpdqxyz False True
    pdq* abcpdqxyz False False
    pdq abcpdqxyz False True
    xyz* abcpdqxyz False False
    xyz abcpdqxyz False True
    * abcpdqxyz True True
    * <None> False False
    * <''> False False
    * <otherwise> True True
    abc* abc.pdq.xyz True True
    abc abc.pdq.xyz True True
    abc.pdq abc.pdq.xyz True True
    pdq* abc.pdq.xyz True False
    pdq abc.pdq.xyz True True
    pdq.xyz abc.pdq.xyz True True
    xyz* abc.pdq.xyz True False
    xyz abc.pdq.xyz True True