Rust
Rust
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 Rust Downloads
// 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..
let sb_token = chilkat::StringBuilder::new();
let _ = sb_token.load_file("qa_data/tokens/google_cloud_sql_access_token.txt", "utf-8");
let 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.set_auth_token(&sb_token.get_as_string().unwrap_or_default());
// 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".)
let sb_response_body = chilkat::StringBuilder::new();
if http.quick_get_sb("https://www.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id", &sb_response_body).is_err() {
println!("{}", http.last_error_text());
return;
}
let j_resp = chilkat::JsonObject::new();
let _ = j_resp.load_sb(&sb_response_body);
j_resp.set_emit_compact(false);
println!("Response Body:");
println!("{}", j_resp.emit().unwrap_or_default());
let resp_status_code = http.last_status();
println!("Response Status Code = {}", resp_status_code);
if resp_status_code == 401 {
println!("It may be that your access token expired.");
println!("Try refreshing the access token by re-fetching it.");
}
if resp_status_code >= 400 {
println!("Response Header:");
println!("{}", http.last_header());
println!("Failed.");
return;
}
// 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
let kind = j_resp.string_of("kind").unwrap_or_default();
let state = j_resp.string_of("state").unwrap_or_default();
let database_version = j_resp.string_of("databaseVersion").unwrap_or_default();
let settings_tier = j_resp.string_of("settings.tier").unwrap_or_default();
let settings_kind = j_resp.string_of("settings.kind").unwrap_or_default();
let settings_pricing_plan = j_resp.string_of("settings.pricingPlan").unwrap_or_default();
let settings_replication_type = j_resp.string_of("settings.replicationType").unwrap_or_default();
let settings_activation_policy = j_resp.string_of("settings.activationPolicy").unwrap_or_default();
let settings_ip_configuration_ipv4_enabled = j_resp.bool_of("settings.ipConfiguration.ipv4Enabled");
let settings_location_preference_zone = j_resp.string_of("settings.locationPreference.zone").unwrap_or_default();
let settings_location_preference_kind = j_resp.string_of("settings.locationPreference.kind").unwrap_or_default();
let settings_data_disk_type = j_resp.string_of("settings.dataDiskType").unwrap_or_default();
let settings_backup_configuration_start_time = j_resp.string_of("settings.backupConfiguration.startTime").unwrap_or_default();
let settings_backup_configuration_kind = j_resp.string_of("settings.backupConfiguration.kind").unwrap_or_default();
let settings_backup_configuration_enabled = j_resp.bool_of("settings.backupConfiguration.enabled");
let settings_backup_configuration_point_in_time_enabled = j_resp.bool_of("settings.backupConfiguration.pointInTimeEnabled");
let settings_settings_version = j_resp.string_of("settings.settingsVersion").unwrap_or_default();
let settings_storage_auto_resize_limit = j_resp.string_of("settings.storageAutoResizeLimit").unwrap_or_default();
let settings_storage_auto_resize = j_resp.bool_of("settings.storageAutoResize");
let settings_data_disk_size_gb = j_resp.string_of("settings.dataDiskSizeGb").unwrap_or_default();
let etag = j_resp.string_of("etag").unwrap_or_default();
let server_ca_cert_kind = j_resp.string_of("serverCaCert.kind").unwrap_or_default();
let server_ca_cert_cert_serial_number = j_resp.string_of("serverCaCert.certSerialNumber").unwrap_or_default();
let server_ca_cert_cert = j_resp.string_of("serverCaCert.cert").unwrap_or_default();
let server_ca_cert_common_name = j_resp.string_of("serverCaCert.commonName").unwrap_or_default();
let server_ca_cert_sha1_fingerprint = j_resp.string_of("serverCaCert.sha1Fingerprint").unwrap_or_default();
let server_ca_cert_instance = j_resp.string_of("serverCaCert.instance").unwrap_or_default();
let server_ca_cert_create_time = j_resp.string_of("serverCaCert.createTime").unwrap_or_default();
let server_ca_cert_expiration_time = j_resp.string_of("serverCaCert.expirationTime").unwrap_or_default();
let instance_type = j_resp.string_of("instanceType").unwrap_or_default();
let project = j_resp.string_of("project").unwrap_or_default();
let service_account_email_address = j_resp.string_of("serviceAccountEmailAddress").unwrap_or_default();
let backend_type = j_resp.string_of("backendType").unwrap_or_default();
let self_link = j_resp.string_of("selfLink").unwrap_or_default();
let connection_name = j_resp.string_of("connectionName").unwrap_or_default();
let name = j_resp.string_of("name").unwrap_or_default();
let region = j_resp.string_of("region").unwrap_or_default();
let gce_zone = j_resp.string_of("gceZone").unwrap_or_default();
let mut i = 0;
let mut count_i = j_resp.size_of_array("settings.authorizedGaeApplications");
while i < count_i {
j_resp.set_i(i);
i = i + 1;
}
i = 0;
count_i = j_resp.size_of_array("settings.ipConfiguration.authorizedNetworks");
while i < count_i {
j_resp.set_i(i);
i = i + 1;
}
i = 0;
count_i = j_resp.size_of_array("ipAddresses");
while i < count_i {
j_resp.set_i(i);
let _ = j_resp.string_of("ipAddresses[i].type").unwrap_or_default();
let _ = j_resp.string_of("ipAddresses[i].ipAddress").unwrap_or_default();
i = i + 1;
}