0

I have a (bash) script on a server that runs as a cron job and exports all of our managed zones from our dns project in google cloud to GCS.

for dnszone in $(gcloud dns managed-zones list --project={} --format="value(NAME)")
  do
    domain=$(gcloud dns managed-zones describe $dnszone --project={} --format="value(dnsName)")
    filename="${domain}"
    gcloud dns record-sets export $filename --zone=$dnszone --project={}
  done


...

gsutil -m rsync -r -d ./ gs://bucket/$today/ 2> /dev/null

I want to move all cron jobs from the server to cloud scheduler jobs (cloud functions with pub/sub triggers tied to a cloud schedule).

I can use the client libraries to manually construct the export file in BIND format but ideally i would have an export function in the client library similar to that of the google cloud sdk.

I've been reading google's docs for the last 2 days with no solution to auto format output from a client library command. ChatGPT suggested I use the non-existent export_to_file() method of the resourcerecordset object, which wasn't helpful.

Am i missing something? Is this possible?

Paddy
  • 23
  • 4
  • Have you tried this documentation on [using Pub/Sub to trigger a Cloud Function](https://cloud.google.com/scheduler/docs/tut-pub-sub)? – Robert G Apr 27 '23 at 18:21
  • sorry if it's unclear, the cloud function/scheduler configuration is not the issue. the issue is trying to replicate the google cloud sdk command in the python client libraries because cloud functions only support go and python runtimes and offer no control over the runtime environment (ie. installing cloud sdk) – Paddy Apr 27 '23 at 18:24
  • 1
    There are several formats for DNS zone exports. The API/SDKs do not format the zone information in any particular format. You must write the code to conform to whichever format you require. I recommend not worrying about the format and simply save the raw JSON instead. The JSON is a better format IMHO. – John Hanley May 07 '23 at 00:15

1 Answers1

1

The Python SDK supports list_zones:

List zones for the project associated with this client.

The equivalent API call is ManagedZones: list:

Enumerates ManagedZones that have been created but not yet deleted.

If successful, this method returns a response body with the following structure:

{
  "kind": "dns#managedZonesListResponse",
  "header": {
    "operationId": string
  },
  "managedZones": [
    managedZones Resource
  ],
  "nextPageToken": string
}

Regarding managedZones Resource:

A zone is a subtree of the DNS namespace under one administrative responsibility. A ManagedZone is a resource that represents a DNS zone hosted by the Cloud DNS service.

{
  "kind": "dns#managedZone",
  "name": string,
  "dnsName": string,
  "description": string,
  "id": unsigned long,
  "nameServers": [
    string
  ],
  "creationTime": string,
  "dnssecConfig": {
    "kind": "dns#managedZoneDnsSecConfig",
    "state": string,
    "defaultKeySpecs": [
      {
        "kind": "dns#dnsKeySpec",
        "keyType": string,
        "algorithm": string,
        "keyLength": unsigned integer
      }
    ],
    "nonExistence": string
  },
  "nameServerSet": string,
  "visibility": string,
  "privateVisibilityConfig": {
    "kind": "dns#managedZonePrivateVisibilityConfig",
    "networks": [
      {
        "kind": "dns#managedZonePrivateVisibilityConfigNetwork",
        "networkUrl": string
      }
    ],
    "gkeClusters": [
      {
        "kind": "dns#managedZonePrivateVisibilityConfigGKECluster",
        "gkeClusterName": string
      }
    ]
  },
  "forwardingConfig": {
    "kind": "dns#managedZoneForwardingConfig",
    "targetNameServers": [
      {
        "kind": "dns#managedZoneForwardingConfigNameServerTarget",
        "ipv4Address": string,
        "forwardingPath": string
      }
    ]
  },
  "labels": {
    (key): string
  },
  "peeringConfig": {
    "kind": "dns#managedZonePeeringConfig",
    "targetNetwork": {
      "kind": "dns#managedZonePeeringConfigTargetNetwork",
      "networkUrl": string,
      "deactivateTime": string
    }
  },
  "reverseLookupConfig": {
    "kind": "dns#managedZoneReverseLookupConfig"
  },
  "serviceDirectoryConfig": {
    "kind": "dns#managedZoneServiceDirectoryConfig",
    "namespace": {
      "kind": "dns#managedZoneServiceDirectoryConfigNamespace",
      "namespaceUrl": string,
      "deletionTime": string
    }
  },
  "cloudLoggingConfig": {
    "kind": "dns#managedZoneCloudLoggingConfig",
    "enableLogging": boolean
  }
}

Therefore it should be possible to retrieve this value and save it as JSON to a file.

anothermh
  • 9,815
  • 3
  • 33
  • 52
  • This is my backup option, listing and iterating the zones to list the recordsets and details and manually format an output file to best match what export does in the SDK to produce a readily importable file. – Paddy Apr 29 '23 at 16:14