-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allows tfmigrate to use an Azure Blob to store history.
- Loading branch information
1 parent
70e9f7d
commit 654bdcf
Showing
9 changed files
with
380 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package azure | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob" | ||
) | ||
|
||
type Client interface { | ||
// Read an object from an Azure blob. | ||
Read(ctx context.Context, container, blob string) ([]byte, error) | ||
|
||
// Write an object onto an Azure blob. | ||
Write(ctx context.Context, container, blob string, p []byte) error | ||
} | ||
|
||
type client struct { | ||
BlobAPI *azblob.Client | ||
} | ||
|
||
// newClient returns a new instance of Client. | ||
func newClient(config *Config) (Client, error) { | ||
// If the access key isn't defined in the configuration, try to read it from the environment. | ||
if config.AccessKey == "" { | ||
config.AccessKey = os.Getenv("TFMIGRATE_AZURE_STORAGE_ACCESS_KEY") | ||
} | ||
|
||
cred, err := azblob.NewSharedKeyCredential(config.AccountName, config.AccessKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
url := fmt.Sprintf("https://%s.blob.core.windows.net/", config.AccountName) | ||
c, err := azblob.NewClientWithSharedKeyCredential(url, cred, nil) | ||
|
||
return &client{c}, err | ||
} | ||
|
||
// Read an object from an Azure blob. | ||
func (c *client) Read(ctx context.Context, container, blob string) ([]byte, error) { | ||
resp, err := c.BlobAPI.DownloadStream(ctx, container, blob, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
bs := bytes.Buffer{} | ||
r := resp.NewRetryReader(ctx, &azblob.RetryReaderOptions{}) | ||
defer r.Close() | ||
|
||
_, err = bs.ReadFrom(r) | ||
|
||
return bs.Bytes(), err | ||
} | ||
|
||
// Write an object onto an Azure blob. | ||
func (c *client) Write(ctx context.Context, container, blob string, p []byte) error { | ||
_, err := c.BlobAPI.UploadBuffer(ctx, container, blob, p, nil) | ||
|
||
return err | ||
} |
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,18 @@ | ||
package azure | ||
|
||
import "github.com/minamijoyo/tfmigrate/storage" | ||
|
||
type Config struct { | ||
AccessKey string `hcl:"access_key,optional"` | ||
AccountName string `hcl:"account_name"` | ||
ContainerName string `hcl:"container_name"` | ||
BlobName string `hcl:"blob_name,optional"` | ||
} | ||
|
||
// Config implements a storage.Config. | ||
var _ storage.Config = (*Config)(nil) | ||
|
||
// NewStorage returns a new instance of storage.Storage. | ||
func (c *Config) NewStorage() (storage.Storage, error) { | ||
return NewStorage(c, 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,46 @@ | ||
package azure | ||
|
||
import "testing" | ||
|
||
func TestConfigNewStorage(t *testing.T) { | ||
cases := []struct { | ||
desc string | ||
config *Config | ||
ok bool | ||
}{ | ||
{ | ||
desc: "valid", | ||
config: &Config{ | ||
AccountName: "tfmigrate-test", | ||
ContainerName: "tfmigrate", | ||
BlobName: "history.json", | ||
}, | ||
ok: true, | ||
}, | ||
{ | ||
desc: "valid", | ||
config: &Config{ | ||
AccessKey: "ZHVtbXkK", // expected to be a base64-encoded string | ||
AccountName: "tfmigrate-test", | ||
ContainerName: "tfmigrate", | ||
BlobName: "history.json", | ||
}, | ||
ok: true, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.desc, func(t *testing.T) { | ||
got, err := tc.config.NewStorage() | ||
if tc.ok && err != nil { | ||
t.Fatalf("unexpected err: %s", err) | ||
} | ||
if !tc.ok && err == nil { | ||
t.Fatalf("expected to return an error, but no error, got: %#v", got) | ||
} | ||
if tc.ok { | ||
_ = got.(*Storage) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.