github.com/openshift/installer@v1.4.17/pkg/asset/quota/gcp/client.go (about)

     1  package gcp
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/pkg/errors"
     7  	computev1 "google.golang.org/api/compute/v1"
     8  	"google.golang.org/api/option"
     9  
    10  	gcpconfig "github.com/openshift/installer/pkg/asset/installconfig/gcp"
    11  )
    12  
    13  // MachineTypeGetter returns the machine type info for a type in a zone using GCP API.
    14  type MachineTypeGetter interface {
    15  	GetMachineType(zone string, machineType string) (*computev1.MachineType, error)
    16  }
    17  
    18  // Client is GCP client for calculating quota constraint.
    19  type Client struct {
    20  	computeSvc *computev1.Service
    21  
    22  	projectID string
    23  }
    24  
    25  // NewClient returns Client using the context and session.
    26  func NewClient(ctx context.Context, sess *gcpconfig.Session, projectID string) (*Client, error) {
    27  	svc, err := computev1.NewService(ctx, option.WithCredentials(sess.Credentials))
    28  	if err != nil {
    29  		return nil, errors.Wrap(err, "failed to create compute service")
    30  	}
    31  	return &Client{computeSvc: svc, projectID: projectID}, nil
    32  }
    33  
    34  // GetMachineType returns the machine type info for a type in a zone using the client.
    35  func (c *Client) GetMachineType(zone string, machineType string) (*computev1.MachineType, error) {
    36  	return c.computeSvc.MachineTypes.Get(c.projectID, zone, machineType).Context(context.TODO()).Do()
    37  }