Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Google Cloud SQL - Start Database Instance

See more Google Cloud SQL Examples

Demonstrates how to start a Google Cloud SQL database instance.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Http,
  Chilkat.StringBuilder,
  Chilkat.HttpResponse,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  sbToken: TStringBuilder;
  http: THttp;
  json: TJsonObject;
  sbRequestBody: TStringBuilder;
  resp: THttpResponse;
  sbResponseBody: TStringBuilder;
  jResp: TJsonObject;
  respStatusCode: Integer;
  kind: string;
  targetLink: string;
  status: string;
  user: string;
  insertTime: string;
  operationType: string;
  name: string;
  targetId: string;
  selfLink: string;
  targetProject: string;

begin
  success := False;

  //  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..
  sbToken := TStringBuilder.Create;
  sbToken.LoadFile('qa_data/tokens/google_cloud_sql_access_token.txt','utf-8');

  http := THttp.Create;

  //  Implements the following CURL command:

  //  curl -X PATCH \
  //  -H "Authorization: Bearer "$(gcloud auth print-access-token) \
  //  -H "Content-Type: application/json; charset=utf-8" \
  //  -d '{
  //    "settings": {
  //      "activationPolicy": "ALWAYS" 
  //    }
  //  }' \
  //  https://www.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id

  //  Use this online tool to generate code from sample JSON:
  //  Generate Code to Create JSON

  //  The following JSON is sent in the request body.

  //  {
  //    "settings": {
  //      "activationPolicy": "ALWAYS"
  //    }
  //  }

  //  Use "ALWAYS" to start an instance.  Use "NEVER" to stop an instance.
  json := TJsonObject.Create;
  json.UpdateString('settings.activationPolicy','ALWAYS');

  //  Causes the "Authorization: Bearer "$(gcloud auth print-access-token)" header to be added.
  http.AuthToken := sbToken.GetAsString();

  http.SetRequestHeader('Content-Type','application/json; charset=utf-8');

  sbRequestBody := TStringBuilder.Create;
  json.EmitSb(sbRequestBody);

  //  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".)
  resp := THttpResponse.Create;
  success := http.HttpSb('PATCH','https://www.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id',sbRequestBody,'utf-8','application/json',resp);
  if (success = False) then
    begin
      WriteLn(http.LastErrorText);
      Exit;
    end;

  sbResponseBody := TStringBuilder.Create;
  resp.GetBodySb(sbResponseBody);
  jResp := TJsonObject.Create;
  jResp.LoadSb(sbResponseBody);
  jResp.EmitCompact := False;

  WriteLn('Response Body:');
  WriteLn(jResp.Emit());

  respStatusCode := resp.StatusCode;
  WriteLn('Response Status Code = ' + respStatusCode);
  if (respStatusCode = 401) then
    begin
      WriteLn('It may be that your access token expired.');
      WriteLn('Try refreshing the access token by re-fetching it.');
    end;
  if (respStatusCode >= 400) then
    begin
      WriteLn('Response Header:');
      WriteLn(resp.Header);
      WriteLn('Failed.');
      Exit;
    end;

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

  //  {
  //    "kind": "sql#operation",
  //    "targetLink": "https://www.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id",
  //    "status": "PENDING",
  //    "user": "user@example.com",
  //    "insertTime": "2020-01-20T21:30:35.667Z",
  //    "operationType": "UPDATE",
  //    "name": "operation-id",
  //    "targetId": "instance-id",
  //    "selfLink": "https://www.googleapis.com/sql/v1beta4/projects/project-id/operations/operation-id",
  //    "targetProject": "project-id"
  //  }

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

  kind := jResp.StringOf('kind');
  targetLink := jResp.StringOf('targetLink');
  status := jResp.StringOf('status');
  user := jResp.StringOf('user');
  insertTime := jResp.StringOf('insertTime');
  operationType := jResp.StringOf('operationType');
  name := jResp.StringOf('name');
  targetId := jResp.StringOf('targetId');
  selfLink := jResp.StringOf('selfLink');
  targetProject := jResp.StringOf('targetProject');


  sbToken.Free;
  http.Free;
  json.Free;
  sbRequestBody.Free;
  resp.Free;
  sbResponseBody.Free;
  jResp.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.