2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-07-05 08:39:17 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
compute "cloud.google.com/go/compute/apiv1"
|
2022-07-29 09:00:15 -04:00
|
|
|
"github.com/spf13/afero"
|
2022-07-05 08:39:17 -04:00
|
|
|
"go.uber.org/multierr"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Client is a client for the Google Compute Engine.
|
|
|
|
type Client struct {
|
2022-07-29 09:00:15 -04:00
|
|
|
projectID string
|
2022-08-05 06:15:06 -04:00
|
|
|
projectAPI
|
2022-07-05 08:39:17 -04:00
|
|
|
instanceAPI
|
|
|
|
instanceTemplateAPI
|
|
|
|
instanceGroupManagersAPI
|
|
|
|
diskAPI
|
|
|
|
// prng is a pseudo-random number generator seeded with time. Not used for security.
|
|
|
|
prng
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new client for the Google Compute Engine.
|
2022-07-29 09:00:15 -04:00
|
|
|
func New(ctx context.Context, configPath string) (*Client, error) {
|
|
|
|
projectID, err := loadProjectID(afero.NewOsFs(), configPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-07-05 08:39:17 -04:00
|
|
|
var closers []closer
|
2022-08-05 06:15:06 -04:00
|
|
|
projectAPI, err := compute.NewProjectsRESTClient(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
closers = append(closers, projectAPI)
|
2022-07-05 08:39:17 -04:00
|
|
|
insAPI, err := compute.NewInstancesRESTClient(ctx)
|
|
|
|
if err != nil {
|
2022-08-05 06:15:06 -04:00
|
|
|
_ = closeAll(closers)
|
2022-07-05 08:39:17 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
closers = append(closers, insAPI)
|
|
|
|
templAPI, err := compute.NewInstanceTemplatesRESTClient(ctx)
|
|
|
|
if err != nil {
|
|
|
|
_ = closeAll(closers)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
closers = append(closers, templAPI)
|
|
|
|
groupAPI, err := compute.NewInstanceGroupManagersRESTClient(ctx)
|
|
|
|
if err != nil {
|
|
|
|
_ = closeAll(closers)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
closers = append(closers, groupAPI)
|
|
|
|
diskAPI, err := compute.NewDisksRESTClient(ctx)
|
|
|
|
if err != nil {
|
|
|
|
_ = closeAll(closers)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Client{
|
2022-07-29 09:00:15 -04:00
|
|
|
projectID: projectID,
|
2022-08-05 06:15:06 -04:00
|
|
|
projectAPI: projectAPI,
|
2022-07-05 08:39:17 -04:00
|
|
|
instanceAPI: insAPI,
|
|
|
|
instanceTemplateAPI: &instanceTemplateClient{templAPI},
|
|
|
|
instanceGroupManagersAPI: &instanceGroupManagersClient{groupAPI},
|
|
|
|
diskAPI: diskAPI,
|
|
|
|
prng: rand.New(rand.NewSource(int64(time.Now().Nanosecond()))),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the client's connection.
|
|
|
|
func (c *Client) Close() error {
|
|
|
|
closers := []closer{
|
2022-08-05 06:15:06 -04:00
|
|
|
c.projectAPI,
|
2022-07-05 08:39:17 -04:00
|
|
|
c.instanceAPI,
|
|
|
|
c.instanceTemplateAPI,
|
|
|
|
c.instanceGroupManagersAPI,
|
|
|
|
c.diskAPI,
|
|
|
|
}
|
|
|
|
return closeAll(closers)
|
|
|
|
}
|
|
|
|
|
|
|
|
type closer interface {
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
|
|
|
// closeAll closes all closers, even if an error occurs.
|
|
|
|
//
|
|
|
|
// Errors are collected and a composed error is returned.
|
|
|
|
func closeAll(closers []closer) error {
|
|
|
|
// Since this function is intended to be deferred, it will always call all
|
|
|
|
// close operations, even if a previous operation failed.
|
|
|
|
var errs error
|
|
|
|
for _, closer := range closers {
|
|
|
|
errs = multierr.Append(errs, closer.Close())
|
|
|
|
}
|
|
|
|
return errs
|
|
|
|
}
|