クライアント ライブラリと SDK

このページでは、Google Security Operations API の Cloud クライアント ライブラリの使用を開始する方法について説明します。クライアント ライブラリを使用すると、サポートされている言語からGoogle Cloud API に簡単にアクセスできます。サーバーにリクエストを送信してGoogle Cloud API を直接利用することもできますが、クライアント ライブラリを使用すると、記述するコードの量を大幅に削減できます。

Cloud クライアント ライブラリと以前の Google API クライアント ライブラリの詳細については、クライアント ライブラリの説明をご覧ください。

クライアント ライブラリをインストールする

C++

Quickstart に沿って操作します。

C#

NuGet から Google.Cloud.Chronicle.V1 パッケージをインストールします。

詳細については、C# 開発環境の設定をご覧ください。

Go

go get cloud.google.com/go/chronicle/apiv1

詳細については、Go 開発環境の設定をご覧ください。

Java

Maven を使用している場合は、次のものを pom.xml ファイルに追加します。BOM の詳細については、Google Cloud Platform ライブラリ BOM をご覧ください。

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>libraries-bom</artifactId>
      <version>26.83.0</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-chronicle</artifactId>
  </dependency>
</dependencies>

Gradle を使用している場合は、次のものを依存関係に追加します。

implementation 'com.google.cloud:google-cloud-chronicle:0.32.0'

sbt を使用している場合は、次のものを依存関係に追加します。

libraryDependencies += "com.google.cloud" % "google-cloud-chronicle" % "0.32.0"

詳細については、Java 開発環境の設定をご覧ください。

Node.js

npm install @google-cloud/chronicle

詳細については、Node.js 開発環境の設定をご覧ください。

PHP

composer require google/cloud-chronicle

詳細については、Google Cloud での PHP の使用をご覧ください。

Python

pip install --upgrade google-cloud-chronicle

詳細については、Python 開発環境の設定をご覧ください。

Ruby

gem install google-cloud-chronicle-v1

詳細については、Ruby 開発環境の設定をご覧ください。

認証を設定する

Google Cloud API の呼び出しを認証するために、クライアント ライブラリではアプリケーションのデフォルト認証情報(ADC)がサポートされています。このライブラリは、一連の定義済みロケーションの中から認証情報を探し、それらの認証情報を使用して API へのリクエストを認証します。ADC を使用すると、アプリケーション コードを変更することなく、ローカルでの開発や本番環境など、さまざまな環境のアプリケーションで認証情報を使用できるようになります。

本番環境では、ADC の設定方法はサービスとコンテキストによって異なります。詳細については、アプリケーションのデフォルト認証情報を設定するをご覧ください。

ローカル開発環境では、Google アカウントに関連付けられている認証情報を使用して ADC を設定できます。

  1. Google Cloud CLI をインストールします。インストール後、次のコマンドを実行して Google Cloud CLI を初期化します。

    gcloud init

    外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。

  2. ローカルシェルを使用している場合は、ユーザー アカウントのローカル認証情報を作成します。

    gcloud auth application-default login

    Cloud Shell を使用している場合は、この操作を行う必要はありません。

    認証エラーが返され、外部 ID プロバイダ(IdP)を使用している場合は、 フェデレーション ID を使用して gcloud CLI にログインしていることを確認します。

    ログイン画面が表示されます。ログインすると、 ADC で使用されるローカル認証情報ファイルに認証情報が保存されます。

クライアント ライブラリを使用する

次の例は、クライアント ライブラリを使用して参照リストを一覧表示する方法を示しています。

C++

#include "google/cloud/chronicle/v1/chronicle_client.h"
#include "google/cloud/common_options.h"
#include <iostream>
#include <string>

int main(int argc, char* argv[]) {
  // TODO(developer): Replace these variables before running the sample.
  std::string const project_id = "";
  std::string const instance_id = "";
  std::string const location = "us"; 
  std::string const endpoint = "us-chronicle.googleapis.com";

  namespace chronicle = ::google::cloud::chronicle_v1;

  auto options = google::cloud::Options{}.set<google::cloud::EndpointOption>(endpoint);
  auto client = chronicle::ChronicleClient(chronicle::MakeChronicleConnection(options));

  // Construct the parent resource name
  // Format: projects/{project}/locations/{location}/instances/{instance}
  std::string parent = "projects/" + project_id + "/locations/" + location + "/instances/" + instance_id;

  google::cloud::chronicle::v1::ListReferenceListsRequest request;
  request.set_parent(parent);
  std::cout << "Listing reference lists for parent: " << parent << std::endl;
  for (auto const& reference_list : client.ListReferenceLists(request)) {
    if (!reference_list) {
      std::cerr << "Error listing reference lists: " << reference_list.status().message() << std::endl;
      return 1;
    }
    std::cout << "Name: " << reference_list->name() <<std::endl;
    std::cout << "Display Name: " << reference_list->display_name() << std::endl;
  }
  return 0;
}

C#

using Google.Cloud.Chronicle.V1;

// TODO(developer): Replace these variables before running the sample.
const string project  = "";
const string location = "us";
const string instance = "";
const string endpoint = "us-chronicle.googleapis.com";

ReferenceListServiceClient client = new ReferenceListServiceClientBuilder
{
  Endpoint = endpoint,
}.Build();

string parent = InstanceName.FromProjectLocationInstance(project, location, instance).ToString();

foreach (ReferenceList referenceList in client.ListReferenceLists(parent))
{
  Console.WriteLine($"Name:        {referenceList.Name}");
  Console.WriteLine($"Description: {referenceList.Description}");
}

Go

package main
import (
  "context"
  "fmt"
  "log"
  chronicle "cloud.google.com/go/chronicle/apiv1"
  chroniclepb "cloud.google.com/go/chronicle/apiv1/chroniclepb"
  "google.golang.org/api/iterator"
  "google.golang.org/api/option"
)

// TODO(developer): Replace these variables before running the sample.
const (
  project    = ""
  location   = "us"
  instance   = ""
  apiEndpoint = "us-chronicle.googleapis.com:443"
)

func main() {
  ctx := context.Background()
  client, err := chronicle.NewReferenceListClient(ctx, option.WithEndpoint(apiEndpoint),)
  if err != nil {
    log.Fatalf("failed to create client: %v", err)
  }
  defer client.Close()

  parent := fmt.Sprintf("projects/%s/locations/%s/instances/%s", project, location, instance)

  req := &chroniclepb.ListReferenceListsRequest{
    Parent: parent,
  }

  it := client.ListReferenceLists(ctx, req)
  for {
    resp, err := it.Next()
    if err == iterator.Done {
      break
    }
    if err != nil {
      log.Fatalf("error listing reference lists: %v", err)
    }
    fmt.Printf("Name: %s\n", resp.GetName())
    fmt.Printf("Description: %s\n", resp.GetDescription())
    fmt.Printf("---\n")
    }
}

Java

package com.example;

import com.google.cloud.chronicle.v1.InstanceName;
import com.google.cloud.chronicle.v1.ReferenceList;
import com.google.cloud.chronicle.v1.ReferenceListServiceClient;
import com.google.cloud.chronicle.v1.ReferenceListServiceSettings;

public class ListReferenceLists {
  // TODO(developer): Replace these variables before running the sample.
  private static final String PROJECT  = "";
  private static final String LOCATION = "us";
  private static final String INSTANCE = "";
  private static final String ENDPOINT = "us-chronicle.googleapis.com:443";

  public static void main(String[] args) throws Exception {
    ReferenceListServiceSettings settings = ReferenceListServiceSettings.newBuilder().setEndpoint(ENDPOINT).build();

    try (ReferenceListServiceClient client = ReferenceListServiceClient.create(settings)) {
      String parent = InstanceName.of(PROJECT, LOCATION, INSTANCE).toString();
      for (ReferenceList referenceList : client.listReferenceLists(parent).iterateAll()) {
        System.out.println("Name:        " + referenceList.getName());
        System.out.println("Description: " + referenceList.getDescription());
        System.out.println("---");
      }
    }
  }
}

Node.js

'use strict';

function main(parent) {
  const {ReferenceListServiceClient} = require('@google-cloud/chronicle').v1;

  const chronicleClient = new ReferenceListServiceClient({
    apiEndpoint: 'us-chronicle.googleapis.com',
  });

  async function callListReferenceLists() {
    // TODO(developer): Replace these variables before running the sample.
    const request = {
      parent: 'projects/<project-number>/locations/us/instances/<instance-id>'
    };

    const iterable = chronicleClient.listReferenceListsAsync(request);
    for await (const response of iterable) {
        console.log(response);
    }
  }

  callListReferenceLists();
}

process.on('unhandledRejection', err=> {
  console.error(err.message);
  process.exitCode = 1;
});
main(...process.argv.slice(2));

PHP

use Google\ApiCore\ApiException;
use Google\ApiCore\PagedListResponse;
use Google\Cloud\Chronicle\V1\Client\ReferenceListServiceClient;
use Google\Cloud\Chronicle\V1\ListReferenceListsRequest;
use Google\Cloud\Chronicle\V1\ReferenceList;

// TODO(developer): Replace these variables before running the sample.
const PROJECT  = '';
const LOCATION = 'us';
const INSTANCE = '';
const ENDPOINT = 'us-chronicle.googleapis.com';

$client = new ReferenceListServiceClient([
    'apiEndpoint' => ENDPOINT,
]);

$parent = ReferenceListServiceClient::instanceName(PROJECT, LOCATION, INSTANCE);

$request = (new ListReferenceListsRequest())->setParent($parent);

try {
    /** @var PagedListResponse $response /
    $response = $client->listReferenceLists($request);

    /* @var ReferenceList $referenceList */
    foreach ($response as $referenceList) {
        printf('Name:        %s' . PHP_EOL, $referenceList->getName());
        printf('Description: %s' . PHP_EOL, $referenceList->getDescription());
        printf('---' . PHP_EOL);
    }
} catch (ApiException $ex) {
    printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
}

Python

from google.cloud import chronicle_v1

# TODO(developer): Replace these variables before running the sample.
PROJECT_ID = ""
LOCATION = "us"
INSTANCE_ID = ""

def list_reference_lists():
  client = chronicle_v1.ReferenceListServiceClient(client_options={"api_endpoint":"us-chronicle.googleapis.com"})
  parent = f"projects/{PROJECT_ID}/locations/{LOCATION}/instances/{INSTANCE_ID}"
  response = client.list_reference_lists(parent=parent)
  for reference_list in response:
    print(f"Name: {reference_list.name}")
    print(f"Display Name: {reference_list.display_name}")

if __name__=="__main__":
  list_reference_lists()

Ruby

require "google/cloud/chronicle/v1"

# TODO(developer): Replace these variables before running the sample.
PROJECT_ID  = ""
LOCATION    = "us"
INSTANCE_ID = ""
ENDPOINT    = "us-chronicle.googleapis.com"

client = Google::Cloud::Chronicle::V1::ReferenceListService::Client.new do |config|
  config.endpoint = ENDPOINT
end

parent = "projects/#{PROJECT_ID}/locations/#{LOCATION}/instances/#{INSTANCE_ID}"

request = Google::Cloud::Chronicle::V1::ListReferenceListsRequest.new(parent: parent)

begin
  response = client.list_reference_lists request

  response.each do |reference_list|
    puts "Name:        #{reference_list.name}"
    puts "Description: #{reference_list.description}"
  end
rescue Google::Cloud::Error => e
  puts "API call failed: #{e.message}"
end

参考情報

C++

次のリストは、C++ のクライアント ライブラリに関連するその他のリソースへのリンクを示します。

C#

次のリストは、C# のクライアント ライブラリに関連するその他のリソースへのリンクを示します。

Go

次のリストは、Go のクライアント ライブラリに関連するその他のリソースへのリンクを示します。

Java

次のリストは、Java のクライアント ライブラリに関連するその他のリソースへのリンクを示します。

Node.js

次のリストは、Node.js のクライアント ライブラリに関連するその他のリソースへのリンクを示します。

PHP

次のリストは、PHP のクライアント ライブラリに関連するその他のリソースへのリンクを示します。

Python

次のリストは、Python のクライアント ライブラリに関連するその他のリソースへのリンクを示します。

Ruby

次のリストは、Ruby のクライアント ライブラリに関連するその他のリソースへのリンクを示します。