tessl install tessl/golang-cloud-google-com--go--compute@1.53.0Go client library for Google Cloud Compute Engine API providing programmatic access to manage virtual machines, disks, networks, and other compute resources
Network clients manage VPC networks, subnets, firewall rules, routes, and Cloud Router instances for comprehensive network topology control.
The NetworksClient manages Virtual Private Cloud (VPC) networks including network creation, VPC peering, and routing configuration.
func NewNetworksRESTClient(ctx context.Context, opts ...option.ClientOption) (*NetworksClient, error)Example:
import (
"context"
compute "cloud.google.com/go/compute/apiv1"
)
ctx := context.Background()
client, err := compute.NewNetworksRESTClient(ctx)
if err != nil {
// handle error
}
defer client.Close()// Get retrieves a specific VPC network
func (c *NetworksClient) Get(ctx context.Context, req *computepb.GetNetworkRequest, opts ...gax.CallOption) (*computepb.Network, error)
// List returns an iterator over VPC networks
func (c *NetworksClient) List(ctx context.Context, req *computepb.ListNetworksRequest, opts ...gax.CallOption) *NetworkIterator
// Insert creates a new VPC network
func (c *NetworksClient) Insert(ctx context.Context, req *computepb.InsertNetworkRequest, opts ...gax.CallOption) (*Operation, error)
// Delete deletes a VPC network
func (c *NetworksClient) Delete(ctx context.Context, req *computepb.DeleteNetworkRequest, opts ...gax.CallOption) (*Operation, error)
// Patch updates a VPC network
func (c *NetworksClient) Patch(ctx context.Context, req *computepb.PatchNetworkRequest, opts ...gax.CallOption) (*Operation, error)
// SwitchToCustomMode converts auto-mode network to custom-mode
func (c *NetworksClient) SwitchToCustomMode(ctx context.Context, req *computepb.SwitchToCustomModeNetworkRequest, opts ...gax.CallOption) (*Operation, error)Example - Create VPC Network:
import (
"cloud.google.com/go/compute/apiv1/computepb"
"google.golang.org/protobuf/proto"
)
insertReq := &computepb.InsertNetworkRequest{
Project: "my-project",
NetworkResource: &computepb.Network{
Name: proto.String("my-vpc"),
AutoCreateSubnetworks: proto.Bool(false),
RoutingConfig: &computepb.NetworkRoutingConfig{
RoutingMode: proto.String("REGIONAL"),
},
},
}
op, err := client.Insert(ctx, insertReq)
if err != nil {
// handle error
}
err = op.Wait(ctx)// AddPeering creates a VPC peering connection
func (c *NetworksClient) AddPeering(ctx context.Context, req *computepb.AddPeeringNetworkRequest, opts ...gax.CallOption) (*Operation, error)
// RemovePeering removes a VPC peering connection
func (c *NetworksClient) RemovePeering(ctx context.Context, req *computepb.RemovePeeringNetworkRequest, opts ...gax.CallOption) (*Operation, error)
// RequestRemovePeering requests removal of a peering (for CONSENSUS update_strategy)
func (c *NetworksClient) RequestRemovePeering(ctx context.Context, req *computepb.RequestRemovePeeringNetworkRequest, opts ...gax.CallOption) (*Operation, error)
// UpdatePeering updates peering configuration
func (c *NetworksClient) UpdatePeering(ctx context.Context, req *computepb.UpdatePeeringNetworkRequest, opts ...gax.CallOption) (*Operation, error)
// ListPeeringRoutes lists routes exchanged over VPC peering
func (c *NetworksClient) ListPeeringRoutes(ctx context.Context, req *computepb.ListPeeringRoutesNetworksRequest, opts ...gax.CallOption) *ExchangedPeeringRouteIteratorExample - Add VPC Peering:
peeringReq := &computepb.AddPeeringNetworkRequest{
Project: "my-project",
Network: "my-vpc",
NetworksAddPeeringRequestResource: &computepb.NetworksAddPeeringRequest{
Name: proto.String("peer-to-other-vpc"),
PeerNetwork: proto.String("projects/other-project/global/networks/other-vpc"),
AutoCreateRoutes: proto.Bool(true),
},
}
op, err := client.AddPeering(ctx, peeringReq)// GetEffectiveFirewalls lists effective firewall rules for a network
func (c *NetworksClient) GetEffectiveFirewalls(ctx context.Context, req *computepb.GetEffectiveFirewallsNetworkRequest, opts ...gax.CallOption) (*computepb.NetworksGetEffectiveFirewallsResponse, error)// GetIamPolicy retrieves the IAM policy for a network
func (c *NetworksClient) GetIamPolicy(ctx context.Context, req *computepb.GetIamPolicyNetworkRequest, opts ...gax.CallOption) (*computepb.Policy, error)
// SetIamPolicy sets the IAM policy for a network
func (c *NetworksClient) SetIamPolicy(ctx context.Context, req *computepb.SetIamPolicyNetworkRequest, opts ...gax.CallOption) (*computepb.Policy, error)
// TestIamPermissions tests IAM permissions for a network
func (c *NetworksClient) TestIamPermissions(ctx context.Context, req *computepb.TestIamPermissionsNetworkRequest, opts ...gax.CallOption) (*computepb.TestPermissionsResponse, error)The SubnetworksClient manages subnets within VPC networks, including IP range configuration and private Google access.
func NewSubnetworksRESTClient(ctx context.Context, opts ...option.ClientOption) (*SubnetworksClient, error)// Get retrieves a specific subnet
func (c *SubnetworksClient) Get(ctx context.Context, req *computepb.GetSubnetworkRequest, opts ...gax.CallOption) (*computepb.Subnetwork, error)
// List returns an iterator over subnets in a region
func (c *SubnetworksClient) List(ctx context.Context, req *computepb.ListSubnetworksRequest, opts ...gax.CallOption) *SubnetworkIterator
// AggregatedList returns an iterator over subnets across all regions
func (c *SubnetworksClient) AggregatedList(ctx context.Context, req *computepb.AggregatedListSubnetworksRequest, opts ...gax.CallOption) *SubnetworksScopedListPairIterator
// Insert creates a new subnet
func (c *SubnetworksClient) Insert(ctx context.Context, req *computepb.InsertSubnetworkRequest, opts ...gax.CallOption) (*Operation, error)
// Delete deletes a subnet
func (c *SubnetworksClient) Delete(ctx context.Context, req *computepb.DeleteSubnetworkRequest, opts ...gax.CallOption) (*Operation, error)
// Patch updates a subnet
func (c *SubnetworksClient) Patch(ctx context.Context, req *computepb.PatchSubnetworkRequest, opts ...gax.CallOption) (*Operation, error)Example - Create Subnet:
insertReq := &computepb.InsertSubnetworkRequest{
Project: "my-project",
Region: "us-central1",
SubnetworkResource: &computepb.Subnetwork{
Name: proto.String("my-subnet"),
Network: proto.String("projects/my-project/global/networks/my-vpc"),
IpCidrRange: proto.String("10.0.0.0/24"),
PrivateIpGoogleAccess: proto.Bool(true),
},
}
op, err := client.Insert(ctx, insertReq)// ExpandIpCidrRange expands the primary IP range of a subnet
func (c *SubnetworksClient) ExpandIpCidrRange(ctx context.Context, req *computepb.ExpandIpCidrRangeSubnetworkRequest, opts ...gax.CallOption) (*Operation, error)
// SetPrivateIpGoogleAccess enables or disables Private Google Access
func (c *SubnetworksClient) SetPrivateIpGoogleAccess(ctx context.Context, req *computepb.SetPrivateIpGoogleAccessSubnetworkRequest, opts ...gax.CallOption) (*Operation, error)// GetIamPolicy retrieves the IAM policy for a subnet
func (c *SubnetworksClient) GetIamPolicy(ctx context.Context, req *computepb.GetIamPolicySubnetworkRequest, opts ...gax.CallOption) (*computepb.Policy, error)
// SetIamPolicy sets the IAM policy for a subnet
func (c *SubnetworksClient) SetIamPolicy(ctx context.Context, req *computepb.SetIamPolicySubnetworkRequest, opts ...gax.CallOption) (*computepb.Policy, error)
// TestIamPermissions tests IAM permissions for a subnet
func (c *SubnetworksClient) TestIamPermissions(ctx context.Context, req *computepb.TestIamPermissionsSubnetworkRequest, opts ...gax.CallOption) (*computepb.TestPermissionsResponse, error)The FirewallsClient manages firewall rules for VPC networks, controlling ingress and egress traffic.
func NewFirewallsRESTClient(ctx context.Context, opts ...option.ClientOption) (*FirewallsClient, error)// Get retrieves a specific firewall rule
func (c *FirewallsClient) Get(ctx context.Context, req *computepb.GetFirewallRequest, opts ...gax.CallOption) (*computepb.Firewall, error)
// List returns an iterator over firewall rules
func (c *FirewallsClient) List(ctx context.Context, req *computepb.ListFirewallsRequest, opts ...gax.CallOption) *FirewallIterator
// Insert creates a new firewall rule
func (c *FirewallsClient) Insert(ctx context.Context, req *computepb.InsertFirewallRequest, opts ...gax.CallOption) (*Operation, error)
// Update updates a firewall rule (full replacement)
func (c *FirewallsClient) Update(ctx context.Context, req *computepb.UpdateFirewallRequest, opts ...gax.CallOption) (*Operation, error)
// Patch partially updates a firewall rule
func (c *FirewallsClient) Patch(ctx context.Context, req *computepb.PatchFirewallRequest, opts ...gax.CallOption) (*Operation, error)
// Delete deletes a firewall rule
func (c *FirewallsClient) Delete(ctx context.Context, req *computepb.DeleteFirewallRequest, opts ...gax.CallOption) (*Operation, error)Example - Create Firewall Rule:
insertReq := &computepb.InsertFirewallRequest{
Project: "my-project",
FirewallResource: &computepb.Firewall{
Name: proto.String("allow-ssh"),
Network: proto.String("projects/my-project/global/networks/my-vpc"),
Direction: proto.String("INGRESS"),
Priority: proto.Int32(1000),
SourceRanges: []string{"0.0.0.0/0"},
Allowed: []*computepb.Allowed{
{
IPProtocol: proto.String("tcp"),
Ports: []string{"22"},
},
},
TargetTags: []string{"ssh-enabled"},
},
}
op, err := client.Insert(ctx, insertReq)Example - Allow HTTP/HTTPS Traffic:
insertReq := &computepb.InsertFirewallRequest{
Project: "my-project",
FirewallResource: &computepb.Firewall{
Name: proto.String("allow-web"),
Network: proto.String("projects/my-project/global/networks/my-vpc"),
Direction: proto.String("INGRESS"),
Priority: proto.Int32(1000),
SourceRanges: []string{"0.0.0.0/0"},
Allowed: []*computepb.Allowed{
{
IPProtocol: proto.String("tcp"),
Ports: []string{"80", "443"},
},
},
},
}
op, err := client.Insert(ctx, insertReq)The RoutesClient manages custom routes within VPC networks.
func NewRoutesRESTClient(ctx context.Context, opts ...option.ClientOption) (*RoutesClient, error)// Get retrieves a specific route
func (c *RoutesClient) Get(ctx context.Context, req *computepb.GetRouteRequest, opts ...gax.CallOption) (*computepb.Route, error)
// List returns an iterator over routes
func (c *RoutesClient) List(ctx context.Context, req *computepb.ListRoutesRequest, opts ...gax.CallOption) *RouteIterator
// Insert creates a new route
func (c *RoutesClient) Insert(ctx context.Context, req *computepb.InsertRouteRequest, opts ...gax.CallOption) (*Operation, error)
// Delete deletes a route
func (c *RoutesClient) Delete(ctx context.Context, req *computepb.DeleteRouteRequest, opts ...gax.CallOption) (*Operation, error)Example - Create Custom Route:
insertReq := &computepb.InsertRouteRequest{
Project: "my-project",
RouteResource: &computepb.Route{
Name: proto.String("custom-route"),
Network: proto.String("projects/my-project/global/networks/my-vpc"),
DestRange: proto.String("192.168.0.0/24"),
NextHopInstance: proto.String("projects/my-project/zones/us-central1-a/instances/nat-instance"),
Priority: proto.Int32(1000),
},
}
op, err := client.Insert(ctx, insertReq)The RoutersClient manages Cloud Router instances for dynamic routing and NAT.
func NewRoutersRESTClient(ctx context.Context, opts ...option.ClientOption) (*RoutersClient, error)// Get retrieves a specific Cloud Router
func (c *RoutersClient) Get(ctx context.Context, req *computepb.GetRouterRequest, opts ...gax.CallOption) (*computepb.Router, error)
// List returns an iterator over Cloud Routers in a region
func (c *RoutersClient) List(ctx context.Context, req *computepb.ListRoutersRequest, opts ...gax.CallOption) *RouterIterator
// AggregatedList returns an iterator over Cloud Routers across all regions
func (c *RoutersClient) AggregatedList(ctx context.Context, req *computepb.AggregatedListRoutersRequest, opts ...gax.CallOption) *RoutersScopedListPairIterator
// Insert creates a new Cloud Router
func (c *RoutersClient) Insert(ctx context.Context, req *computepb.InsertRouterRequest, opts ...gax.CallOption) (*Operation, error)
// Update updates a Cloud Router
func (c *RoutersClient) Update(ctx context.Context, req *computepb.UpdateRouterRequest, opts ...gax.CallOption) (*Operation, error)
// Patch partially updates a Cloud Router
func (c *RoutersClient) Patch(ctx context.Context, req *computepb.PatchRouterRequest, opts ...gax.CallOption) (*Operation, error)
// Delete deletes a Cloud Router
func (c *RoutersClient) Delete(ctx context.Context, req *computepb.DeleteRouterRequest, opts ...gax.CallOption) (*Operation, error)Example - Create Cloud Router:
insertReq := &computepb.InsertRouterRequest{
Project: "my-project",
Region: "us-central1",
RouterResource: &computepb.Router{
Name: proto.String("my-router"),
Network: proto.String("projects/my-project/global/networks/my-vpc"),
Bgp: &computepb.RouterBgp{
Asn: proto.Uint32(64512),
},
},
}
op, err := client.Insert(ctx, insertReq)// GetRouterStatus retrieves the runtime status of a Cloud Router
func (c *RoutersClient) GetRouterStatus(ctx context.Context, req *computepb.GetRouterStatusRouterRequest, opts ...gax.CallOption) (*computepb.RouterStatusResponse, error)
// GetNatMappingInfo lists NAT mapping information
func (c *RoutersClient) GetNatMappingInfo(ctx context.Context, req *computepb.GetNatMappingInfoRoutersRequest, opts ...gax.CallOption) *VmEndpointNatMappingsIterator
// Preview previews router configuration
func (c *RoutersClient) Preview(ctx context.Context, req *computepb.PreviewRouterRequest, opts ...gax.CallOption) (*computepb.RoutersPreviewResponse, error)type Network struct {
AutoCreateSubnetworks *bool
CreationTimestamp *string
Description *string
EnableUlaInternalIpv6 *bool
FirewallPolicy *string
GatewayIPv4 *string
Id *uint64
InternalIpv6Range *string
Kind *string
Mtu *int32
Name *string
NetworkFirewallPolicyEnforcementOrder *string
Peerings []*NetworkPeering
RoutingConfig *NetworkRoutingConfig
SelfLink *string
Subnetworks []string
}type Subnetwork struct {
CreationTimestamp *string
Description *string
EnableFlowLogs *bool
ExternalIpv6Prefix *string
Fingerprint *string
GatewayAddress *string
Id *uint64
InternalIpv6Prefix *string
IpCidrRange *string
Ipv6AccessType *string
Ipv6CidrRange *string
Kind *string
LogConfig *SubnetworkLogConfig
Name *string
Network *string
PrivateIpGoogleAccess *bool
PrivateIpv6GoogleAccess *string
Purpose *string
Region *string
Role *string
SecondaryIpRanges []*SubnetworkSecondaryRange
SelfLink *string
StackType *string
State *string
}type Firewall struct {
Allowed []*Allowed
CreationTimestamp *string
Denied []*Denied
Description *string
DestinationRanges []string
Direction *string
Disabled *bool
Id *uint64
Kind *string
LogConfig *FirewallLogConfig
Name *string
Network *string
Priority *int32
SelfLink *string
SourceRanges []string
SourceServiceAccounts []string
SourceTags []string
TargetServiceAccounts []string
TargetTags []string
}
type Allowed struct {
IPProtocol *string // tcp, udp, icmp, esp, ah, sctp, or protocol number
Ports []string // Port ranges like "80" or "8000-8100"
}
type Denied struct {
IPProtocol *string // tcp, udp, icmp, esp, ah, sctp, or protocol number
Ports []string // Port ranges like "80" or "8000-8100"
}type Route struct {
AsPaths []*RouteAsPath
CreationTimestamp *string
Description *string
DestRange *string
Id *uint64
Kind *string
Name *string
Network *string
NextHopGateway *string
NextHopHub *string
NextHopIlb *string
NextHopInstance *string
NextHopIp *string
NextHopNetwork *string
NextHopPeering *string
NextHopVpnTunnel *string
Priority *int32
RouteStatus *string
RouteType *string
SelfLink *string
Tags []string
Warnings []*Warnings
}For advanced firewall features, see:
See Security Clients Documentation for Cloud Armor security policies.