2017-03-30 16:33:54 +02:00
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
2018-11-21 22:06:03 +01:00
|
|
|
"context"
|
2017-03-30 16:33:54 +02:00
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/backend"
|
|
|
|
"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"
|
2020-05-20 17:29:02 +02:00
|
|
|
"github.com/tombuildsstuff/giovanni/storage/2018-11-09/blob/blobs"
|
|
|
|
"github.com/tombuildsstuff/giovanni/storage/2018-11-09/blob/containers"
|
2017-03-30 16:33:54 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// This will be used as directory name, the odd looking colon is simply to
|
|
|
|
// reduce the chance of name conflicts with existing objects.
|
|
|
|
keyEnvPrefix = "env:"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
func (b *Backend) Workspaces() ([]string, error) {
|
2017-03-30 16:33:54 +02:00
|
|
|
prefix := b.keyName + keyEnvPrefix
|
2020-05-20 17:29:02 +02:00
|
|
|
params := containers.ListBlobsInput{
|
|
|
|
Prefix: &prefix,
|
2017-03-30 16:33:54 +02:00
|
|
|
}
|
|
|
|
|
2018-11-21 22:06:03 +01:00
|
|
|
ctx := context.TODO()
|
2020-05-20 17:29:02 +02:00
|
|
|
client, err := b.armClient.getContainersClient(ctx)
|
2018-11-21 22:06:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-20 17:29:02 +02:00
|
|
|
resp, err := client.ListBlobs(ctx, b.armClient.storageAccountName, b.containerName, params)
|
2017-03-30 16:33:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
envs := map[string]struct{}{}
|
2020-05-20 17:29:02 +02:00
|
|
|
for _, obj := range resp.Blobs.Blobs {
|
2017-03-30 16:33:54 +02:00
|
|
|
key := obj.Name
|
|
|
|
if strings.HasPrefix(key, prefix) {
|
|
|
|
name := strings.TrimPrefix(key, prefix)
|
|
|
|
// we store the state in a key, not a directory
|
|
|
|
if strings.Contains(name, "/") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
envs[name] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result := []string{backend.DefaultStateName}
|
|
|
|
for name := range envs {
|
|
|
|
result = append(result, name)
|
|
|
|
}
|
|
|
|
sort.Strings(result[1:])
|
|
|
|
return result, 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
|
|
|
func (b *Backend) DeleteWorkspace(name string) error {
|
2017-03-30 16:33:54 +02:00
|
|
|
if name == backend.DefaultStateName || name == "" {
|
|
|
|
return fmt.Errorf("can't delete default state")
|
|
|
|
}
|
|
|
|
|
2018-11-21 22:06:03 +01:00
|
|
|
ctx := context.TODO()
|
|
|
|
client, err := b.armClient.getBlobClient(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-20 17:29:02 +02:00
|
|
|
if resp, err := client.Delete(ctx, b.armClient.storageAccountName, b.containerName, b.path(name), blobs.DeleteInput{}); err != nil {
|
|
|
|
if resp.Response.StatusCode != 404 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-06-20 10:54:09 +02:00
|
|
|
|
2020-05-20 17:29:02 +02:00
|
|
|
return nil
|
2017-03-30 16:33:54 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
func (b *Backend) StateMgr(name string) (state.State, error) {
|
2018-11-21 22:06:03 +01:00
|
|
|
ctx := context.TODO()
|
|
|
|
blobClient, err := b.armClient.getBlobClient(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-03-30 16:33:54 +02:00
|
|
|
client := &RemoteClient{
|
2020-05-20 17:29:02 +02:00
|
|
|
giovanniBlobClient: *blobClient,
|
|
|
|
containerName: b.containerName,
|
|
|
|
keyName: b.path(name),
|
|
|
|
accountName: b.accountName,
|
2017-03-30 16:33:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
stateMgr := &remote.State{Client: client}
|
|
|
|
|
|
|
|
//if this isn't the default state name, we need to create the object so
|
|
|
|
//it's listed by States.
|
|
|
|
if name != backend.DefaultStateName {
|
|
|
|
// take a lock on this state while we write it
|
|
|
|
lockInfo := state.NewLockInfo()
|
|
|
|
lockInfo.Operation = "init"
|
|
|
|
lockId, err := client.Lock(lockInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to lock azure state: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Local helper function so we can call it multiple places
|
|
|
|
lockUnlock := func(parent error) error {
|
|
|
|
if err := stateMgr.Unlock(lockId); err != nil {
|
|
|
|
return fmt.Errorf(strings.TrimSpace(errStateUnlock), lockId, err)
|
|
|
|
}
|
|
|
|
return parent
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grab the value
|
|
|
|
if err := stateMgr.RefreshState(); err != nil {
|
|
|
|
err = lockUnlock(err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have no state, we have to create an 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
|
|
|
err = lockUnlock(err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := stateMgr.PersistState(); err != nil {
|
|
|
|
err = lockUnlock(err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock, the state should now be initialized
|
|
|
|
if err := lockUnlock(nil); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return stateMgr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Backend) client() *RemoteClient {
|
|
|
|
return &RemoteClient{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Backend) path(name string) string {
|
|
|
|
if name == backend.DefaultStateName {
|
|
|
|
return b.keyName
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.keyName + keyEnvPrefix + name
|
|
|
|
}
|
|
|
|
|
|
|
|
const errStateUnlock = `
|
|
|
|
Error unlocking Azure state. Lock ID: %s
|
|
|
|
|
|
|
|
Error: %s
|
|
|
|
|
|
|
|
You may have to force-unlock this state in order to use it again.
|
|
|
|
`
|