Sample code for 30+ languages & platforms
Lua

Google Cloud SQL - Get Instance Information

See more Google Cloud SQL Examples

Demonstrates how to get information about your Cloud SQL instances, including instance summary information, usage data, operation logs, and database logs.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.

-- In this example, Get Google Cloud SQL OAuth2 Access Token,
-- the service account access token was saved to a text file.  This example fetches the access token from the file..
local sbToken = chilkat.StringBuilder.new()
sbToken:load_file("qa_data/tokens/google_cloud_sql_access_token.txt", "utf-8")

local http = chilkat.Http.new()

-- Implements the following CURL command:

-- curl -X GET \
-- -H "Authorization: Bearer access-token" \
-- https://www.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id

-- Use the following online tool to generate HTTP code from a CURL command
-- Convert a cURL Command to HTTP Source Code

-- Causes the "Authorization: Bearer "$(gcloud auth print-access-token)" header to be added.
http.auth_token = sbToken:get_as_string()

-- Replace "project-id" with your actual Google project ID.
-- Replace "instance-id" with your database instance ID, which is the name of your database.  (For example, when I created my test database I named it "chilkat", and therefore my instance-id is "chilkat".)
local sbResponseBody = chilkat.StringBuilder.new()
local success = http:quick_get_sb("https://www.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id", sbResponseBody)
if not success then
    print(http.last_error_text)
    return
end

local jResp = chilkat.JsonObject.new()
jResp:load_sb(sbResponseBody)
jResp.emit_compact = false

print("Response Body:")
print(jResp:emit())

local respStatusCode = http.last_status
print("Response Status Code = " .. respStatusCode)
if respStatusCode == 401 then
    print("It may be that your access token expired.")
    print("Try refreshing the access token by re-fetching it.")
end

if respStatusCode >= 400 then
    print("Response Header:")
    print(http.last_header)
    print("Failed.")
    return
end

-- Sample JSON response:
-- (Sample code for parsing the JSON response is shown below)

-- {
--   "kind": "sql#instance",
--   "state": "RUNNABLE",
--   "databaseVersion": "MYSQL_5_7",
--   "settings": {
--     "authorizedGaeApplications": [
--     ],
--     "tier": "db-f1-micro",
--     "kind": "sql#settings",
--     "pricingPlan": "PER_USE",
--     "replicationType": "SYNCHRONOUS",
--     "activationPolicy": "ALWAYS",
--     "ipConfiguration": {
--       "authorizedNetworks": [
--       ],
--       "ipv4Enabled": true
--     },
--     "locationPreference": {
--       "zone": "asia-east1-a",
--       "kind": "sql#locationPreference"
--     },
--     "dataDiskType": "PD_SSD",
--     "backupConfiguration": {
--       "startTime": "08:00",
--       "kind": "sql#backupConfiguration",
--       "enabled": true,
--       "pointInTimeEnabled": true
--     },
--     "settingsVersion": "13",
--     "storageAutoResizeLimit": "0",
--     "storageAutoResize": true,
--     "dataDiskSizeGb": "10"
--   },
--   "etag": "etag-id",
--   "ipAddresses": [
--     {
--       "type": "PRIMARY",
--       "ipAddress": "10.0.0.1"
--     }
--   ],
--   "serverCaCert": {
--     "kind": "sql#sslCert",
--     "certSerialNumber": "0",
--     "cert": "certificate-id",
--     "commonName": "C=US,O=Google\\, Inc,CN=Google Cloud SQL Server CA,dnQualifier=dn-qualifier-id",
--     "sha1Fingerprint": "sha-id",
--     "instance": "instance-id",
--     "createTime": "2019-06-28T22:46:35.052Z",
--     "expirationTime": "2029-06-25T22:47:35.052Z"
--   },
--   "instanceType": "CLOUD_SQL_INSTANCE",
--   "project": "project-id",
--   "serviceAccountEmailAddress": "service-acct-id@gcp-sa-cloud-sql.iam.gserviceaccount.com",
--   "backendType": "SECOND_GEN",
--   "selfLink": "https://www.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id",
--   "connectionName": "project-id:region:instance-id",
--   "name": "instance-id",
--   "region": "asia-east1",
--   "gceZone": "asia-east1-a"
-- }

-- Sample code for parsing the JSON response...
-- Use the following online tool to generate parsing code from sample JSON:
-- Generate Parsing Code from JSON

local v_type = ""
local ipAddress = ""

local kind = jResp:string_of("kind")
local state = jResp:string_of("state")
local databaseVersion = jResp:string_of("databaseVersion")
local settingsTier = jResp:string_of("settings.tier")
local settingsKind = jResp:string_of("settings.kind")
local settingsPricingPlan = jResp:string_of("settings.pricingPlan")
local settingsReplicationType = jResp:string_of("settings.replicationType")
local settingsActivationPolicy = jResp:string_of("settings.activationPolicy")
local settingsIpConfigurationIpv4Enabled = jResp:bool_of("settings.ipConfiguration.ipv4Enabled")
local settingsLocationPreferenceZone = jResp:string_of("settings.locationPreference.zone")
local settingsLocationPreferenceKind = jResp:string_of("settings.locationPreference.kind")
local settingsDataDiskType = jResp:string_of("settings.dataDiskType")
local settingsBackupConfigurationStartTime = jResp:string_of("settings.backupConfiguration.startTime")
local settingsBackupConfigurationKind = jResp:string_of("settings.backupConfiguration.kind")
local settingsBackupConfigurationEnabled = jResp:bool_of("settings.backupConfiguration.enabled")
local settingsBackupConfigurationPointInTimeEnabled = jResp:bool_of("settings.backupConfiguration.pointInTimeEnabled")
local settingsSettingsVersion = jResp:string_of("settings.settingsVersion")
local settingsStorageAutoResizeLimit = jResp:string_of("settings.storageAutoResizeLimit")
local settingsStorageAutoResize = jResp:bool_of("settings.storageAutoResize")
local settingsDataDiskSizeGb = jResp:string_of("settings.dataDiskSizeGb")
local etag = jResp:string_of("etag")
local serverCaCertKind = jResp:string_of("serverCaCert.kind")
local serverCaCertCertSerialNumber = jResp:string_of("serverCaCert.certSerialNumber")
local serverCaCertCert = jResp:string_of("serverCaCert.cert")
local serverCaCertCommonName = jResp:string_of("serverCaCert.commonName")
local serverCaCertSha1Fingerprint = jResp:string_of("serverCaCert.sha1Fingerprint")
local serverCaCertInstance = jResp:string_of("serverCaCert.instance")
local serverCaCertCreateTime = jResp:string_of("serverCaCert.createTime")
local serverCaCertExpirationTime = jResp:string_of("serverCaCert.expirationTime")
local instanceType = jResp:string_of("instanceType")
local project = jResp:string_of("project")
local serviceAccountEmailAddress = jResp:string_of("serviceAccountEmailAddress")
local backendType = jResp:string_of("backendType")
local selfLink = jResp:string_of("selfLink")
local connectionName = jResp:string_of("connectionName")
local name = jResp:string_of("name")
local region = jResp:string_of("region")
local gceZone = jResp:string_of("gceZone")
local i = 0
local count_i = jResp:size_of_array("settings.authorizedGaeApplications")
while i < count_i do
    jResp.i = i
    i = i + 1
end

i = 0
count_i = jResp:size_of_array("settings.ipConfiguration.authorizedNetworks")
while i < count_i do
    jResp.i = i
    i = i + 1
end

i = 0
count_i = jResp:size_of_array("ipAddresses")
while i < count_i do
    jResp.i = i
    v_type = jResp:string_of("ipAddresses[i].type")
    ipAddress = jResp:string_of("ipAddresses[i].ipAddress")
    i = i + 1
end