Google.Cloud.Compute.V1

Google.Cloud.Compute.V1 is a.NET client library for theCompute Engine API.

Note:This documentation is for version3.19.0 of the library.Some samples may not work with other versions.

Installation

Install theGoogle.Cloud.Compute.V1 package from NuGet. Add it toyour project in the normal way (for example by right-clicking on theproject in Visual Studio and choosing "Manage NuGet Packages...").

Authentication

When running on Google Cloud, no action needs to be taken to authenticate.

Otherwise, the simplest way of authenticating your API calls is toset up Application Default Credentials.The credentials will automatically be used to authenticate. SeeSet up Application Default Credentials for more details.

Getting started

All operations are performed through the following client classes:

Create a client instance by calling the staticCreate orCreateAsync methods. Alternatively,use the builder class associated with each client class (e.g. AcceleratorTypesClientBuilder for AcceleratorTypesClient)as an easy way of specifying custom credentials, settings, or a custom endpoint. Clients are thread-safe,and we recommend using a single instance across your entire application unless you have a particular needto configure multiple client objects separately.

Using wire values for enums

Many fields in the Compute API are effectively enums, representingone of a fixed set of values.

These are currently represented as string properties in the APIsurface, for compatibility reasons. C# enums containing the knownvalues are generated, but not used directly within the API surface.

As an example, theAddress resource has aNetworkTier property,of typestring. The set of valid values is represented by theAddress.Types.NetworkTier enum - but these are generated withC#-idiomatic names,not the strings that need to be used for theAddress.NetworkTier property.

Two approaches are provided to allow client code to refer tospecific values without hard-coding string literals in anerror-prone way. First, theComputeEnumConstants classprovides all known enum constants via nested classes. For example,theNetworkTier enum used byAddress is represented in theComputeEnumConstants.Address.NetworkTier class:

Address addressResource = new Address{    Name = addressName,    Region = region,    NetworkTier = ComputeEnumConstants.Address.NetworkTier.Premium};

This is simple for cases where client code needs to refer to asingle value known at compile-time. In situations where moreflexibility is required, the wire value corresponding to each enumvalue can be obtained usingComputeEnumHelpers.Format:

// This could be passed into a method as a parameter, for example.Address.Types.NetworkTier tier = Address.Types.NetworkTier.Premium;Address addressResource = new Address{    Name = addressName,    Region = region,    NetworkTier = ComputeEnumHelpers.Format(tier)};

The reverse operation (parsing from a wire value to an enum value)can be achieved usingComputeEnumHelpers.TryParseandComputeEnumHelpers.Parse:

if (ComputeEnumHelpers.TryParse(addressResource.NetworkTier, out Address.Types.NetworkTier tier)){    // The wire value was recognized, and tier contains the enum value.    Console.WriteLine($"Network tier: {tier}");}

(Following .NET conventions, theParse method returns the parsedenum value directly instead of using anout parameter, but throwsan exception if the value is not recognized.)

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-11-05 UTC.