-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #152 from kanisterio/sync
Add GCP snapshot volume support to kanister
- Loading branch information
Showing
17 changed files
with
724 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package gcepd | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"golang.org/x/oauth2" | ||
"golang.org/x/oauth2/google" | ||
compute "google.golang.org/api/compute/v1" | ||
) | ||
|
||
// Client is a wrapper for Client client | ||
type Client struct { | ||
Service *compute.Service | ||
ProjectID string | ||
} | ||
|
||
// NewClient returns a Client struct | ||
func NewClient(ctx context.Context, servicekey string) (*Client, error) { | ||
var err error | ||
var creds *google.Credentials | ||
if len(servicekey) > 0 { | ||
creds, err = google.CredentialsFromJSON(ctx, []byte(servicekey), compute.ComputeScope) | ||
} else { | ||
creds, err = google.FindDefaultCredentials(ctx, compute.ComputeScope) | ||
} | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
// NOTE: Ashlie is not sure how long this will work for since a comment at | ||
// https://godoc.org/golang.org/x/oauth2#NewClient | ||
// states that the client in the context will only be used for getting a | ||
// token. | ||
httpClient := &http.Client{Transport: http.DefaultTransport} | ||
ctx = context.WithValue(ctx, oauth2.HTTPClient, httpClient) | ||
|
||
client := oauth2.NewClient(ctx, creds.TokenSource) | ||
|
||
service, err := compute.New(client) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Client{ | ||
Service: service, | ||
ProjectID: creds.ProjectID, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package gcepd | ||
|
||
import ( | ||
"context" | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
. "gopkg.in/check.v1" | ||
|
||
"github.com/kanisterio/kanister/pkg/blockstorage" | ||
) | ||
|
||
// Hook up gocheck into the "go test" runner. | ||
func Test(t *testing.T) { TestingT(t) } | ||
|
||
type ClientSuite struct{} | ||
|
||
var _ = Suite(&ClientSuite{}) | ||
|
||
func (s *ClientSuite) SetUpSuite(c *C) {} | ||
|
||
func (s *ClientSuite) TestClient(c *C) { | ||
var zone string | ||
filename := s.GetEnvOrSkip(c, blockstorage.GoogleCloudCreds) | ||
b, err := ioutil.ReadFile(filename) | ||
c.Assert(err, IsNil) | ||
gCli, err := NewClient(context.Background(), string(b)) | ||
c.Assert(err, IsNil) | ||
c.Assert(gCli.Service, NotNil) | ||
c.Assert(*gCli, FitsTypeOf, Client{}) | ||
// Get zone | ||
zone = s.GetEnvOrSkip(c, blockstorage.GoogleCloudZone) | ||
_, err = gCli.Service.Disks.List(gCli.ProjectID, zone).Do() | ||
c.Assert(err, IsNil) | ||
} | ||
|
||
func (s *ClientSuite) GetEnvOrSkip(c *C, varName string) string { | ||
v := os.Getenv(varName) | ||
// Ensure the variable is set | ||
if v == "" { | ||
c.Skip("Required environment variable " + varName + " is not set") | ||
} | ||
return v | ||
} |
Oops, something went wrong.