2017-03-30 16:33:54 +02:00
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
2020-05-20 17:29:02 +02:00
|
|
|
"context"
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
"encoding/base64"
|
2017-03-30 16:33:54 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-06-25 11:50:16 +02:00
|
|
|
"log"
|
|
|
|
|
2018-11-21 22:06:03 +01:00
|
|
|
"github.com/hashicorp/go-multierror"
|
|
|
|
"github.com/hashicorp/go-uuid"
|
2020-06-25 11:50:16 +02:00
|
|
|
"github.com/tombuildsstuff/giovanni/storage/2018-11-09/blob/blobs"
|
|
|
|
|
2017-03-30 16:33:54 +02:00
|
|
|
"github.com/hashicorp/terraform/state"
|
|
|
|
"github.com/hashicorp/terraform/state/remote"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
leaseHeader = "x-ms-lease-id"
|
|
|
|
// Must be lower case
|
|
|
|
lockInfoMetaKey = "terraformlockid"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RemoteClient struct {
|
2020-05-20 17:29:02 +02:00
|
|
|
giovanniBlobClient blobs.Client
|
|
|
|
accountName string
|
|
|
|
containerName string
|
|
|
|
keyName string
|
|
|
|
leaseID string
|
2020-06-25 11:50:16 +02:00
|
|
|
snapshot bool
|
2017-03-30 16:33:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RemoteClient) Get() (*remote.Payload, error) {
|
2020-05-20 17:29:02 +02:00
|
|
|
options := blobs.GetInput{}
|
2017-09-04 13:04:43 +02:00
|
|
|
if c.leaseID != "" {
|
2020-05-20 17:29:02 +02:00
|
|
|
options.LeaseID = &c.leaseID
|
2017-09-04 13:04:43 +02:00
|
|
|
}
|
|
|
|
|
2020-05-20 17:29:02 +02:00
|
|
|
ctx := context.TODO()
|
|
|
|
blob, err := c.giovanniBlobClient.Get(ctx, c.accountName, c.containerName, c.keyName, options)
|
2017-03-30 16:33:54 +02:00
|
|
|
if err != nil {
|
2020-05-20 17:29:02 +02:00
|
|
|
if blob.Response.StatusCode == 404 {
|
|
|
|
return nil, nil
|
2017-03-30 16:33:54 +02:00
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
payload := &remote.Payload{
|
2020-05-20 17:29:02 +02:00
|
|
|
Data: blob.Contents,
|
2017-03-30 16:33:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If there was no data, then return nil
|
|
|
|
if len(payload.Data) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return payload, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RemoteClient) Put(data []byte) error {
|
2020-05-20 17:29:02 +02:00
|
|
|
getOptions := blobs.GetPropertiesInput{}
|
|
|
|
setOptions := blobs.SetPropertiesInput{}
|
|
|
|
putOptions := blobs.PutBlockBlobInput{}
|
2017-03-30 16:33:54 +02:00
|
|
|
|
2020-05-20 17:29:02 +02:00
|
|
|
options := blobs.GetInput{}
|
2017-03-30 16:33:54 +02:00
|
|
|
if c.leaseID != "" {
|
2020-05-20 17:29:02 +02:00
|
|
|
options.LeaseID = &c.leaseID
|
|
|
|
getOptions.LeaseID = &c.leaseID
|
|
|
|
setOptions.LeaseID = &c.leaseID
|
|
|
|
putOptions.LeaseID = &c.leaseID
|
2017-03-30 16:33:54 +02:00
|
|
|
}
|
|
|
|
|
2020-05-20 17:29:02 +02:00
|
|
|
ctx := context.TODO()
|
2020-06-25 11:50:16 +02:00
|
|
|
|
|
|
|
if c.snapshot {
|
|
|
|
snapshotInput := blobs.SnapshotInput{LeaseID: options.LeaseID}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Snapshotting existing Blob %q (Container %q / Account %q)", c.keyName, c.containerName, c.accountName)
|
|
|
|
if _, err := c.giovanniBlobClient.Snapshot(ctx, c.accountName, c.containerName, c.keyName, snapshotInput); err != nil {
|
|
|
|
return fmt.Errorf("error snapshotting Blob %q (Container %q / Account %q): %+v", c.keyName, c.containerName, c.accountName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Print("[DEBUG] Created blob snapshot")
|
|
|
|
}
|
|
|
|
|
2020-05-20 17:29:02 +02:00
|
|
|
blob, err := c.giovanniBlobClient.GetProperties(ctx, c.accountName, c.containerName, c.keyName, getOptions)
|
2017-09-04 13:04:43 +02:00
|
|
|
if err != nil {
|
2020-05-20 17:29:02 +02:00
|
|
|
if blob.StatusCode != 404 {
|
2017-09-04 13:04:43 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-20 17:29:02 +02:00
|
|
|
contentType := "application/json"
|
|
|
|
putOptions.Content = &data
|
|
|
|
putOptions.ContentType = &contentType
|
|
|
|
putOptions.MetaData = blob.MetaData
|
|
|
|
_, err = c.giovanniBlobClient.PutBlockBlob(ctx, c.accountName, c.containerName, c.keyName, putOptions)
|
2017-03-30 16:33:54 +02:00
|
|
|
|
2020-05-20 17:29:02 +02:00
|
|
|
return err
|
2017-03-30 16:33:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RemoteClient) Delete() error {
|
2020-05-20 17:29:02 +02:00
|
|
|
options := blobs.DeleteInput{}
|
2017-06-20 10:54:09 +02:00
|
|
|
|
2017-03-30 16:33:54 +02:00
|
|
|
if c.leaseID != "" {
|
2020-05-20 17:29:02 +02:00
|
|
|
options.LeaseID = &c.leaseID
|
2017-03-30 16:33:54 +02:00
|
|
|
}
|
|
|
|
|
2020-05-20 17:29:02 +02:00
|
|
|
ctx := context.TODO()
|
|
|
|
resp, err := c.giovanniBlobClient.Delete(ctx, c.accountName, c.containerName, c.keyName, options)
|
|
|
|
if err != nil {
|
|
|
|
if resp.Response.StatusCode != 404 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2017-03-30 16:33:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RemoteClient) Lock(info *state.LockInfo) (string, error) {
|
|
|
|
stateName := fmt.Sprintf("%s/%s", c.containerName, c.keyName)
|
|
|
|
info.Path = stateName
|
|
|
|
|
|
|
|
if info.ID == "" {
|
|
|
|
lockID, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
info.ID = lockID
|
|
|
|
}
|
|
|
|
|
|
|
|
getLockInfoErr := func(err error) error {
|
|
|
|
lockInfo, infoErr := c.getLockInfo()
|
|
|
|
if infoErr != nil {
|
|
|
|
err = multierror.Append(err, infoErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &state.LockError{
|
|
|
|
Err: err,
|
|
|
|
Info: lockInfo,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-20 17:29:02 +02:00
|
|
|
leaseOptions := blobs.AcquireLeaseInput{
|
|
|
|
ProposedLeaseID: &info.ID,
|
|
|
|
LeaseDuration: -1,
|
|
|
|
}
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
|
|
|
// obtain properties to see if the blob lease is already in use. If the blob doesn't exist, create it
|
|
|
|
properties, err := c.giovanniBlobClient.GetProperties(ctx, c.accountName, c.containerName, c.keyName, blobs.GetPropertiesInput{})
|
2017-03-30 16:33:54 +02:00
|
|
|
if err != nil {
|
2020-05-20 17:29:02 +02:00
|
|
|
// error if we had issues getting the blob
|
|
|
|
if properties.Response.StatusCode != 404 {
|
2017-03-30 16:33:54 +02:00
|
|
|
return "", getLockInfoErr(err)
|
|
|
|
}
|
2020-05-20 17:29:02 +02:00
|
|
|
// if we don't find the blob, we need to build it
|
2017-03-30 16:33:54 +02:00
|
|
|
|
2020-05-20 17:29:02 +02:00
|
|
|
contentType := "application/json"
|
|
|
|
putGOptions := blobs.PutBlockBlobInput{
|
|
|
|
ContentType: &contentType,
|
2017-03-30 16:33:54 +02:00
|
|
|
}
|
|
|
|
|
2020-05-20 17:29:02 +02:00
|
|
|
_, err = c.giovanniBlobClient.PutBlockBlob(ctx, c.accountName, c.containerName, c.keyName, putGOptions)
|
2017-03-30 16:33:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", getLockInfoErr(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-20 17:29:02 +02:00
|
|
|
// if the blob is already locked then error
|
|
|
|
if properties.LeaseStatus == blobs.Locked {
|
|
|
|
return "", getLockInfoErr(fmt.Errorf("state blob is already locked"))
|
|
|
|
}
|
|
|
|
|
|
|
|
leaseID, err := c.giovanniBlobClient.AcquireLease(ctx, c.accountName, c.containerName, c.keyName, leaseOptions)
|
|
|
|
if err != nil {
|
|
|
|
return "", getLockInfoErr(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
info.ID = leaseID.LeaseID
|
|
|
|
c.leaseID = leaseID.LeaseID
|
2017-03-30 16:33:54 +02:00
|
|
|
|
|
|
|
if err := c.writeLockInfo(info); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return info.ID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RemoteClient) getLockInfo() (*state.LockInfo, error) {
|
2020-05-20 17:29:02 +02:00
|
|
|
options := blobs.GetPropertiesInput{}
|
|
|
|
if c.leaseID != "" {
|
|
|
|
options.LeaseID = &c.leaseID
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
blob, err := c.giovanniBlobClient.GetProperties(ctx, c.accountName, c.containerName, c.keyName, options)
|
2017-03-30 16:33:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-20 17:29:02 +02:00
|
|
|
raw := blob.MetaData[lockInfoMetaKey]
|
2017-03-30 16:33:54 +02:00
|
|
|
if raw == "" {
|
2017-09-04 13:04:43 +02:00
|
|
|
return nil, fmt.Errorf("blob metadata %q was empty", lockInfoMetaKey)
|
2017-03-30 16:33:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
data, err := base64.StdEncoding.DecodeString(raw)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
lockInfo := &state.LockInfo{}
|
|
|
|
err = json.Unmarshal(data, lockInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return lockInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// writes info to blob meta data, deletes metadata entry if info is nil
|
|
|
|
func (c *RemoteClient) writeLockInfo(info *state.LockInfo) error {
|
2020-05-20 17:29:02 +02:00
|
|
|
ctx := context.TODO()
|
|
|
|
blob, err := c.giovanniBlobClient.GetProperties(ctx, c.accountName, c.containerName, c.keyName, blobs.GetPropertiesInput{LeaseID: &c.leaseID})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-30 16:33:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if info == nil {
|
2020-05-20 17:29:02 +02:00
|
|
|
delete(blob.MetaData, lockInfoMetaKey)
|
2017-03-30 16:33:54 +02:00
|
|
|
} else {
|
|
|
|
value := base64.StdEncoding.EncodeToString(info.Marshal())
|
2020-05-20 17:29:02 +02:00
|
|
|
blob.MetaData[lockInfoMetaKey] = value
|
2017-03-30 16:33:54 +02:00
|
|
|
}
|
|
|
|
|
2020-05-20 17:29:02 +02:00
|
|
|
opts := blobs.SetMetaDataInput{
|
|
|
|
LeaseID: &c.leaseID,
|
|
|
|
MetaData: blob.MetaData,
|
2017-03-30 16:33:54 +02:00
|
|
|
}
|
2020-05-20 17:29:02 +02:00
|
|
|
|
|
|
|
_, err = c.giovanniBlobClient.SetMetaData(ctx, c.accountName, c.containerName, c.keyName, opts)
|
|
|
|
return err
|
2017-03-30 16:33:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RemoteClient) Unlock(id string) error {
|
|
|
|
lockErr := &state.LockError{}
|
|
|
|
|
|
|
|
lockInfo, err := c.getLockInfo()
|
|
|
|
if err != nil {
|
|
|
|
lockErr.Err = fmt.Errorf("failed to retrieve lock info: %s", err)
|
|
|
|
return lockErr
|
|
|
|
}
|
|
|
|
lockErr.Info = lockInfo
|
|
|
|
|
|
|
|
if lockInfo.ID != id {
|
|
|
|
lockErr.Err = fmt.Errorf("lock id %q does not match existing lock", id)
|
|
|
|
return lockErr
|
|
|
|
}
|
|
|
|
|
2018-11-22 17:47:35 +01:00
|
|
|
c.leaseID = lockInfo.ID
|
2017-03-30 16:33:54 +02:00
|
|
|
if err := c.writeLockInfo(nil); err != nil {
|
|
|
|
lockErr.Err = fmt.Errorf("failed to delete lock info from metadata: %s", err)
|
|
|
|
return lockErr
|
|
|
|
}
|
|
|
|
|
2020-05-20 17:29:02 +02:00
|
|
|
ctx := context.TODO()
|
|
|
|
_, err = c.giovanniBlobClient.ReleaseLease(ctx, c.accountName, c.containerName, c.keyName, id)
|
2017-03-30 16:33:54 +02:00
|
|
|
if err != nil {
|
|
|
|
lockErr.Err = err
|
|
|
|
return lockErr
|
|
|
|
}
|
|
|
|
|
|
|
|
c.leaseID = ""
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|