2017-03-30 16:33:54 +02:00
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
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"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
|
2017-06-20 10:54:09 +02:00
|
|
|
"github.com/Azure/azure-sdk-for-go/storage"
|
2017-03-30 16:33:54 +02:00
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
|
|
|
uuid "github.com/hashicorp/go-uuid"
|
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
|
|
|
|
2017-03-30 16:33:54 +02:00
|
|
|
"github.com/hashicorp/terraform/state"
|
|
|
|
"github.com/hashicorp/terraform/state/remote"
|
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
|
|
|
"github.com/hashicorp/terraform/states"
|
2017-03-30 16:33:54 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
leaseHeader = "x-ms-lease-id"
|
|
|
|
// Must be lower case
|
|
|
|
lockInfoMetaKey = "terraformlockid"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RemoteClient struct {
|
|
|
|
blobClient storage.BlobStorageClient
|
|
|
|
containerName string
|
|
|
|
keyName string
|
|
|
|
leaseID string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RemoteClient) Get() (*remote.Payload, error) {
|
2017-06-20 10:54:09 +02:00
|
|
|
containerReference := c.blobClient.GetContainerReference(c.containerName)
|
|
|
|
blobReference := containerReference.GetBlobReference(c.keyName)
|
|
|
|
options := &storage.GetBlobOptions{}
|
2017-09-04 13:04:43 +02:00
|
|
|
|
|
|
|
if c.leaseID != "" {
|
|
|
|
options.LeaseID = c.leaseID
|
|
|
|
}
|
|
|
|
|
2017-06-20 10:54:09 +02:00
|
|
|
blob, err := blobReference.Get(options)
|
2017-03-30 16:33:54 +02:00
|
|
|
if err != nil {
|
|
|
|
if storErr, ok := err.(storage.AzureStorageServiceError); ok {
|
|
|
|
if storErr.Code == "BlobNotFound" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer blob.Close()
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
if _, err := io.Copy(buf, blob); err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to read remote state: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
payload := &remote.Payload{
|
|
|
|
Data: buf.Bytes(),
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2017-09-04 13:04:43 +02:00
|
|
|
getOptions := &storage.GetBlobMetadataOptions{}
|
2017-06-20 10:54:09 +02:00
|
|
|
setOptions := &storage.SetBlobPropertiesOptions{}
|
|
|
|
putOptions := &storage.PutBlobOptions{}
|
|
|
|
|
|
|
|
containerReference := c.blobClient.GetContainerReference(c.containerName)
|
|
|
|
blobReference := containerReference.GetBlobReference(c.keyName)
|
|
|
|
|
|
|
|
blobReference.Properties.ContentType = "application/json"
|
|
|
|
blobReference.Properties.ContentLength = int64(len(data))
|
2017-03-30 16:33:54 +02:00
|
|
|
|
|
|
|
if c.leaseID != "" {
|
2017-09-04 13:04:43 +02:00
|
|
|
getOptions.LeaseID = c.leaseID
|
2017-06-20 10:54:09 +02:00
|
|
|
setOptions.LeaseID = c.leaseID
|
|
|
|
putOptions.LeaseID = c.leaseID
|
2017-03-30 16:33:54 +02:00
|
|
|
}
|
|
|
|
|
2017-09-04 13:04:43 +02:00
|
|
|
exists, err := blobReference.Exists()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
err = blobReference.GetMetadata(getOptions)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-20 10:54:09 +02:00
|
|
|
reader := bytes.NewReader(data)
|
2017-03-30 16:33:54 +02:00
|
|
|
|
2017-09-04 13:04:43 +02:00
|
|
|
err = blobReference.CreateBlockBlobFromReader(reader, putOptions)
|
2017-03-30 16:33:54 +02:00
|
|
|
if err != nil {
|
2017-06-20 10:54:09 +02:00
|
|
|
return err
|
2017-03-30 16:33:54 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 10:54:09 +02:00
|
|
|
return blobReference.SetProperties(setOptions)
|
2017-03-30 16:33:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RemoteClient) Delete() error {
|
2017-06-20 10:54:09 +02:00
|
|
|
containerReference := c.blobClient.GetContainerReference(c.containerName)
|
|
|
|
blobReference := containerReference.GetBlobReference(c.keyName)
|
|
|
|
options := &storage.DeleteBlobOptions{}
|
|
|
|
|
2017-03-30 16:33:54 +02:00
|
|
|
if c.leaseID != "" {
|
2017-06-20 10:54:09 +02:00
|
|
|
options.LeaseID = c.leaseID
|
2017-03-30 16:33:54 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 10:54:09 +02:00
|
|
|
return blobReference.Delete(options)
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-20 10:54:09 +02:00
|
|
|
containerReference := c.blobClient.GetContainerReference(c.containerName)
|
|
|
|
blobReference := containerReference.GetBlobReference(c.keyName)
|
|
|
|
leaseID, err := blobReference.AcquireLease(-1, info.ID, &storage.LeaseOptions{})
|
2017-03-30 16:33:54 +02:00
|
|
|
if err != nil {
|
|
|
|
if storErr, ok := err.(storage.AzureStorageServiceError); ok && storErr.Code != "BlobNotFound" {
|
|
|
|
return "", getLockInfoErr(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// failed to lock as there was no state blob, write empty state
|
|
|
|
stateMgr := &remote.State{Client: c}
|
|
|
|
|
|
|
|
// ensure state is actually empty
|
|
|
|
if err := stateMgr.RefreshState(); err != nil {
|
|
|
|
return "", fmt.Errorf("Failed to refresh state before writing empty state for locking: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Print("[DEBUG] Could not lock as state blob did not exist, creating with empty state")
|
|
|
|
|
|
|
|
if v := stateMgr.State(); v == nil {
|
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
|
|
|
if err := stateMgr.WriteState(states.NewState()); err != nil {
|
2017-03-30 16:33:54 +02:00
|
|
|
return "", fmt.Errorf("Failed to write empty state for locking: %s", err)
|
|
|
|
}
|
|
|
|
if err := stateMgr.PersistState(); err != nil {
|
|
|
|
return "", fmt.Errorf("Failed to persist empty state for locking: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-20 10:54:09 +02:00
|
|
|
leaseID, err = blobReference.AcquireLease(-1, info.ID, &storage.LeaseOptions{})
|
2017-03-30 16:33:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", getLockInfoErr(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
info.ID = leaseID
|
|
|
|
c.leaseID = leaseID
|
|
|
|
|
|
|
|
if err := c.writeLockInfo(info); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return info.ID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RemoteClient) getLockInfo() (*state.LockInfo, error) {
|
2017-06-20 10:54:09 +02:00
|
|
|
containerReference := c.blobClient.GetContainerReference(c.containerName)
|
|
|
|
blobReference := containerReference.GetBlobReference(c.keyName)
|
|
|
|
err := blobReference.GetMetadata(&storage.GetBlobMetadataOptions{})
|
2017-03-30 16:33:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-06-20 10:54:09 +02:00
|
|
|
raw := blobReference.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 {
|
2017-06-20 10:54:09 +02:00
|
|
|
containerReference := c.blobClient.GetContainerReference(c.containerName)
|
|
|
|
blobReference := containerReference.GetBlobReference(c.keyName)
|
2017-09-04 13:04:43 +02:00
|
|
|
err := blobReference.GetMetadata(&storage.GetBlobMetadataOptions{
|
|
|
|
LeaseID: c.leaseID,
|
|
|
|
})
|
2017-03-30 16:33:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if info == nil {
|
2017-06-20 10:54:09 +02:00
|
|
|
delete(blobReference.Metadata, lockInfoMetaKey)
|
2017-03-30 16:33:54 +02:00
|
|
|
} else {
|
|
|
|
value := base64.StdEncoding.EncodeToString(info.Marshal())
|
2017-06-20 10:54:09 +02:00
|
|
|
blobReference.Metadata[lockInfoMetaKey] = value
|
2017-03-30 16:33:54 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 10:54:09 +02:00
|
|
|
opts := &storage.SetBlobMetadataOptions{
|
|
|
|
LeaseID: c.leaseID,
|
2017-03-30 16:33:54 +02:00
|
|
|
}
|
2017-06-20 10:54:09 +02:00
|
|
|
return blobReference.SetMetadata(opts)
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.writeLockInfo(nil); err != nil {
|
|
|
|
lockErr.Err = fmt.Errorf("failed to delete lock info from metadata: %s", err)
|
|
|
|
return lockErr
|
|
|
|
}
|
|
|
|
|
2017-06-20 10:54:09 +02:00
|
|
|
containerReference := c.blobClient.GetContainerReference(c.containerName)
|
|
|
|
blobReference := containerReference.GetBlobReference(c.keyName)
|
|
|
|
err = blobReference.ReleaseLease(id, &storage.LeaseOptions{})
|
2017-03-30 16:33:54 +02:00
|
|
|
if err != nil {
|
|
|
|
lockErr.Err = err
|
|
|
|
return lockErr
|
|
|
|
}
|
|
|
|
|
|
|
|
c.leaseID = ""
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|