列出支持的数据库标志

此代码段展示了如何列出服务支持的数据库标志。您可以使用此命令了解哪些标志可用于在集群上进行配置。

代码示例

Go

如需向 AlloyDB 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证。


import (
	"context"
	"fmt"
	"io"

	alloydb "cloud.google.com/go/alloydb/apiv1"
	alloydbpb "cloud.google.com/go/alloydb/apiv1/alloydbpb"
	"google.golang.org/api/iterator"
)

// listSupportedDatabaseFlags lists the database flags supported by AlloyDB in a location.
func listSupportedDatabaseFlags(ctx context.Context, w io.Writer, projectID string, locationID string) error {
	parent := fmt.Sprintf("projects/%s/locations/%s", projectID, locationID)

	client, err := alloydb.NewAlloyDBAdminClient(ctx)
	if err != nil {
		return fmt.Errorf("alloydb.NewAlloyDBAdminClient: %w", err)
	}
	defer client.Close()

	req := &alloydbpb.ListSupportedDatabaseFlagsRequest{
		Parent: parent,
	}

	it := client.ListSupportedDatabaseFlags(ctx, req)
	for {
		flag, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return fmt.Errorf("it.Next: %w", err)
		}
		fmt.Fprintf(w, "Supported database flag: %s\n", flag.GetName())
	}

	return nil
}

Java

如需向 AlloyDB 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证。

import com.google.cloud.alloydb.v1.AlloyDBAdminClient;
import com.google.cloud.alloydb.v1.LocationName;
import com.google.cloud.alloydb.v1.SupportedDatabaseFlag;
import java.io.IOException;

public class ListSupportedDatabaseFlags {

  public static void listSupportedDatabaseFlags(String projectId, String locationId)
      throws IOException {
    try (AlloyDBAdminClient client = AlloyDBAdminClient.create()) {
      String parent = LocationName.of(projectId, locationId).toString();

      for (SupportedDatabaseFlag flag : client.listSupportedDatabaseFlags(parent).iterateAll()) {
        System.out.println("Flag Name: " + flag.getName());
        System.out.println("Flag Type: " + flag.getValueType());
      }
    }
  }
}

Node.js

如需向 AlloyDB 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证。

'use strict';

const {AlloyDBAdminClient} = require('@google-cloud/alloydb');
const {status} = require('@grpc/grpc-js');

const client = new AlloyDBAdminClient();

/**
 * Use the listSupportedDatabaseFlags method
 *
 * @param {string} projectId - The Google Cloud project ID (for example, 'example-project-id')
 * @param {string} locationId - The Google Cloud region where the service is located (for example, 'us-central1')
 */
async function listSupportedDatabaseFlags(projectId, locationId) {
  try {
    const parent = client.locationPath(projectId, locationId);

    const [flags] = await client.listSupportedDatabaseFlags({
      parent,
    });

    for (const flag of flags) {
      console.log(`Flag Name: ${flag.name}`);
      console.log(`  Value Type: ${flag.valueType}`);
      console.log(`  Acceptable Values: ${flag.appliesTo}`);
    }
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Specified location not found: ${locationId}`);
    } else if (err.code === status.PERMISSION_DENIED) {
      console.error('Permission denied when accessing AlloyDB flags.');
    } else {
      console.error('Failed to list supported database flags:', err);
    }
  }
}

module.exports = { listSupportedDatabaseFlags };

Python

如需向 AlloyDB 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证。

import google.api_core.exceptions
from google.cloud import alloydb_v1

client = alloydb_v1.AlloyDBAdminClient()


def list_supported_database_flags(project_id: str, location_id: str) -> None:
    """Lists supported database flags for AlloyDB in a given location.

    Args:
        project_id: The Google Cloud project ID (for example, 'example-project-id')
        location_id: The Google Cloud region where the service is located (for example, 'us-central1')
    """
    parent = f"projects/{project_id}/locations/{location_id}"

    request = alloydb_v1.ListSupportedDatabaseFlagsRequest(
        parent=parent,
    )

    try:
        page_result = client.list_supported_database_flags(request=request)
        for flag in page_result:
            print(f"Flag Name: {flag.name}")
    except google.api_core.exceptions.GoogleAPICallError as error:
        print(f"Failed to list supported database flags: {error}")

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅Google Cloud 示例浏览器。