mirror of https://github.com/hashicorp/terraform
Merge pull request #13871 from fatmcgav/swift_move_to_backend
Move Swift remote state to backendpull/15415/head
commit
5066fa2151
@ -0,0 +1,325 @@
|
||||
package swift
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gophercloud/gophercloud"
|
||||
"github.com/gophercloud/gophercloud/openstack"
|
||||
|
||||
"github.com/hashicorp/terraform/backend"
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
tf_openstack "github.com/terraform-providers/terraform-provider-openstack/openstack"
|
||||
)
|
||||
|
||||
// New creates a new backend for Swift remote state.
|
||||
func New() backend.Backend {
|
||||
s := &schema.Backend{
|
||||
Schema: map[string]*schema.Schema{
|
||||
"auth_url": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
DefaultFunc: schema.EnvDefaultFunc("OS_AUTH_URL", nil),
|
||||
Description: descriptions["auth_url"],
|
||||
},
|
||||
|
||||
"user_id": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
DefaultFunc: schema.EnvDefaultFunc("OS_USER_ID", ""),
|
||||
Description: descriptions["user_name"],
|
||||
},
|
||||
|
||||
"user_name": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
DefaultFunc: schema.EnvDefaultFunc("OS_USERNAME", ""),
|
||||
Description: descriptions["user_name"],
|
||||
},
|
||||
|
||||
"tenant_id": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
|
||||
"OS_TENANT_ID",
|
||||
"OS_PROJECT_ID",
|
||||
}, ""),
|
||||
Description: descriptions["tenant_id"],
|
||||
},
|
||||
|
||||
"tenant_name": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
|
||||
"OS_TENANT_NAME",
|
||||
"OS_PROJECT_NAME",
|
||||
}, ""),
|
||||
Description: descriptions["tenant_name"],
|
||||
},
|
||||
|
||||
"password": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Sensitive: true,
|
||||
DefaultFunc: schema.EnvDefaultFunc("OS_PASSWORD", ""),
|
||||
Description: descriptions["password"],
|
||||
},
|
||||
|
||||
"token": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
DefaultFunc: schema.EnvDefaultFunc("OS_AUTH_TOKEN", ""),
|
||||
Description: descriptions["token"],
|
||||
},
|
||||
|
||||
"domain_id": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
|
||||
"OS_USER_DOMAIN_ID",
|
||||
"OS_PROJECT_DOMAIN_ID",
|
||||
"OS_DOMAIN_ID",
|
||||
}, ""),
|
||||
Description: descriptions["domain_id"],
|
||||
},
|
||||
|
||||
"domain_name": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
|
||||
"OS_USER_DOMAIN_NAME",
|
||||
"OS_PROJECT_DOMAIN_NAME",
|
||||
"OS_DOMAIN_NAME",
|
||||
"OS_DEFAULT_DOMAIN",
|
||||
}, ""),
|
||||
Description: descriptions["domain_name"],
|
||||
},
|
||||
|
||||
"region_name": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
DefaultFunc: schema.EnvDefaultFunc("OS_REGION_NAME", ""),
|
||||
Description: descriptions["region_name"],
|
||||
},
|
||||
|
||||
"insecure": &schema.Schema{
|
||||
Type: schema.TypeBool,
|
||||
Optional: true,
|
||||
DefaultFunc: schema.EnvDefaultFunc("OS_INSECURE", ""),
|
||||
Description: descriptions["insecure"],
|
||||
},
|
||||
|
||||
"endpoint_type": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
DefaultFunc: schema.EnvDefaultFunc("OS_ENDPOINT_TYPE", ""),
|
||||
},
|
||||
|
||||
"cacert_file": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
DefaultFunc: schema.EnvDefaultFunc("OS_CACERT", ""),
|
||||
Description: descriptions["cacert_file"],
|
||||
},
|
||||
|
||||
"cert": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
DefaultFunc: schema.EnvDefaultFunc("OS_CERT", ""),
|
||||
Description: descriptions["cert"],
|
||||
},
|
||||
|
||||
"key": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
DefaultFunc: schema.EnvDefaultFunc("OS_KEY", ""),
|
||||
Description: descriptions["key"],
|
||||
},
|
||||
|
||||
"path": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Description: descriptions["path"],
|
||||
Deprecated: "Use container instead",
|
||||
ConflictsWith: []string{"container"},
|
||||
},
|
||||
|
||||
"container": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Description: descriptions["container"],
|
||||
},
|
||||
|
||||
"archive_path": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Description: descriptions["archive_path"],
|
||||
Deprecated: "Use archive_container instead",
|
||||
ConflictsWith: []string{"archive_container"},
|
||||
},
|
||||
|
||||
"archive_container": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Description: descriptions["archive_container"],
|
||||
},
|
||||
|
||||
"expire_after": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Description: descriptions["expire_after"],
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
result := &Backend{Backend: s}
|
||||
result.Backend.ConfigureFunc = result.configure
|
||||
return result
|
||||
}
|
||||
|
||||
var descriptions map[string]string
|
||||
|
||||
func init() {
|
||||
descriptions = map[string]string{
|
||||
"auth_url": "The Identity authentication URL.",
|
||||
|
||||
"user_name": "Username to login with.",
|
||||
|
||||
"user_id": "User ID to login with.",
|
||||
|
||||
"tenant_id": "The ID of the Tenant (Identity v2) or Project (Identity v3)\n" +
|
||||
"to login with.",
|
||||
|
||||
"tenant_name": "The name of the Tenant (Identity v2) or Project (Identity v3)\n" +
|
||||
"to login with.",
|
||||
|
||||
"password": "Password to login with.",
|
||||
|
||||
"token": "Authentication token to use as an alternative to username/password.",
|
||||
|
||||
"domain_id": "The ID of the Domain to scope to (Identity v3).",
|
||||
|
||||
"domain_name": "The name of the Domain to scope to (Identity v3).",
|
||||
|
||||
"region_name": "The name of the Region to use.",
|
||||
|
||||
"insecure": "Trust self-signed certificates.",
|
||||
|
||||
"cacert_file": "A Custom CA certificate.",
|
||||
|
||||
"endpoint_type": "The catalog endpoint type to use.",
|
||||
|
||||
"cert": "A client certificate to authenticate with.",
|
||||
|
||||
"key": "A client private key to authenticate with.",
|
||||
|
||||
"path": "Swift container path to use.",
|
||||
|
||||
"container": "Swift container to create",
|
||||
|
||||
"archive_path": "Swift container path to archive state to.",
|
||||
|
||||
"archive_container": "Swift container to archive state to.",
|
||||
|
||||
"expire_after": "Archive object expiry duration.",
|
||||
}
|
||||
}
|
||||
|
||||
type Backend struct {
|
||||
*schema.Backend
|
||||
|
||||
// Fields below are set from configure
|
||||
client *gophercloud.ServiceClient
|
||||
archive bool
|
||||
archiveContainer string
|
||||
expireSecs int
|
||||
container string
|
||||
}
|
||||
|
||||
func (b *Backend) configure(ctx context.Context) error {
|
||||
if b.client != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Grab the resource data
|
||||
data := schema.FromContextBackendConfig(ctx)
|
||||
|
||||
config := &tf_openstack.Config{
|
||||
CACertFile: data.Get("cacert_file").(string),
|
||||
ClientCertFile: data.Get("cert").(string),
|
||||
ClientKeyFile: data.Get("key").(string),
|
||||
DomainID: data.Get("domain_id").(string),
|
||||
DomainName: data.Get("domain_name").(string),
|
||||
EndpointType: data.Get("endpoint_type").(string),
|
||||
IdentityEndpoint: data.Get("auth_url").(string),
|
||||
Insecure: data.Get("insecure").(bool),
|
||||
Password: data.Get("password").(string),
|
||||
Token: data.Get("token").(string),
|
||||
TenantID: data.Get("tenant_id").(string),
|
||||
TenantName: data.Get("tenant_name").(string),
|
||||
Username: data.Get("user_name").(string),
|
||||
UserID: data.Get("user_id").(string),
|
||||
}
|
||||
|
||||
if err := config.LoadAndValidate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Assign Container
|
||||
b.container = data.Get("container").(string)
|
||||
if b.container == "" {
|
||||
// Check deprecated field
|
||||
b.container = data.Get("path").(string)
|
||||
}
|
||||
|
||||
// Enable object archiving?
|
||||
if archiveContainer, ok := data.GetOk("archive_container"); ok {
|
||||
log.Printf("[DEBUG] Archive_container set, enabling object versioning")
|
||||
b.archive = true
|
||||
b.archiveContainer = archiveContainer.(string)
|
||||
} else if archivePath, ok := data.GetOk("archive_path"); ok {
|
||||
log.Printf("[DEBUG] Archive_path set, enabling object versioning")
|
||||
b.archive = true
|
||||
b.archiveContainer = archivePath.(string)
|
||||
}
|
||||
|
||||
// Enable object expiry?
|
||||
if expireRaw, ok := data.GetOk("expire_after"); ok {
|
||||
expire := expireRaw.(string)
|
||||
log.Printf("[DEBUG] Requested that remote state expires after %s", expire)
|
||||
|
||||
if strings.HasSuffix(expire, "d") {
|
||||
log.Printf("[DEBUG] Got a days expire after duration. Converting to hours")
|
||||
days, err := strconv.Atoi(expire[:len(expire)-1])
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error converting expire_after value %s to int: %s", expire, err)
|
||||
}
|
||||
|
||||
expire = fmt.Sprintf("%dh", days*24)
|
||||
log.Printf("[DEBUG] Expire after %s hours", expire)
|
||||
}
|
||||
|
||||
expireDur, err := time.ParseDuration(expire)
|
||||
if err != nil {
|
||||
log.Printf("[DEBUG] Error parsing duration %s: %s", expire, err)
|
||||
return fmt.Errorf("Error parsing expire_after duration '%s': %s", expire, err)
|
||||
}
|
||||
log.Printf("[DEBUG] Seconds duration = %d", int(expireDur.Seconds()))
|
||||
b.expireSecs = int(expireDur.Seconds())
|
||||
}
|
||||
|
||||
objClient, err := openstack.NewObjectStorageV1(config.OsClient, gophercloud.EndpointOpts{
|
||||
Region: data.Get("region_name").(string),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
b.client = objClient
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -0,0 +1,31 @@
|
||||
package swift
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/terraform/backend"
|
||||
"github.com/hashicorp/terraform/state"
|
||||
"github.com/hashicorp/terraform/state/remote"
|
||||
)
|
||||
|
||||
func (b *Backend) States() ([]string, error) {
|
||||
return nil, backend.ErrNamedStatesNotSupported
|
||||
}
|
||||
|
||||
func (b *Backend) DeleteState(name string) error {
|
||||
return backend.ErrNamedStatesNotSupported
|
||||
}
|
||||
|
||||
func (b *Backend) State(name string) (state.State, error) {
|
||||
if name != backend.DefaultStateName {
|
||||
return nil, backend.ErrNamedStatesNotSupported
|
||||
}
|
||||
|
||||
client := &RemoteClient{
|
||||
client: b.client,
|
||||
container: b.container,
|
||||
archive: b.archive,
|
||||
archiveContainer: b.archiveContainer,
|
||||
expireSecs: b.expireSecs,
|
||||
}
|
||||
|
||||
return &remote.State{Client: client}, nil
|
||||
}
|
||||
@ -0,0 +1,259 @@
|
||||
package swift
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gophercloud/gophercloud"
|
||||
"github.com/gophercloud/gophercloud/openstack/objectstorage/v1/containers"
|
||||
"github.com/gophercloud/gophercloud/openstack/objectstorage/v1/objects"
|
||||
"github.com/gophercloud/gophercloud/pagination"
|
||||
"github.com/hashicorp/terraform/backend"
|
||||
"github.com/hashicorp/terraform/state/remote"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
)
|
||||
|
||||
// verify that we are doing ACC tests or the Swift tests specifically
|
||||
func testACC(t *testing.T) {
|
||||
skip := os.Getenv("TF_ACC") == "" && os.Getenv("TF_SWIFT_TEST") == ""
|
||||
if skip {
|
||||
t.Log("swift backend tests require setting TF_ACC or TF_SWIFT_TEST")
|
||||
t.Skip()
|
||||
}
|
||||
t.Log("swift backend acceptance tests enabled")
|
||||
}
|
||||
|
||||
func TestBackend_impl(t *testing.T) {
|
||||
var _ backend.Backend = new(Backend)
|
||||
}
|
||||
|
||||
func testAccPreCheck(t *testing.T) {
|
||||
v := os.Getenv("OS_AUTH_URL")
|
||||
if v == "" {
|
||||
t.Fatal("OS_AUTH_URL must be set for acceptance tests")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackendConfig(t *testing.T) {
|
||||
testACC(t)
|
||||
|
||||
// Build config
|
||||
config := map[string]interface{}{
|
||||
"archive_container": "test-tfstate-archive",
|
||||
"container": "test-tfstate",
|
||||
}
|
||||
|
||||
b := backend.TestBackendConfig(t, New(), config).(*Backend)
|
||||
|
||||
if b.container != "test-tfstate" {
|
||||
t.Fatal("Incorrect path was provided.")
|
||||
}
|
||||
if b.archiveContainer != "test-tfstate-archive" {
|
||||
t.Fatal("Incorrect archivepath was provided.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackend(t *testing.T) {
|
||||
testACC(t)
|
||||
|
||||
container := fmt.Sprintf("terraform-state-swift-test-%x", time.Now().Unix())
|
||||
|
||||
b := backend.TestBackendConfig(t, New(), map[string]interface{}{
|
||||
"container": container,
|
||||
}).(*Backend)
|
||||
|
||||
defer deleteSwiftContainer(t, b.client, container)
|
||||
|
||||
backend.TestBackend(t, b, nil)
|
||||
}
|
||||
|
||||
func TestBackendPath(t *testing.T) {
|
||||
testACC(t)
|
||||
|
||||
path := fmt.Sprintf("terraform-state-swift-test-%x", time.Now().Unix())
|
||||
t.Logf("[DEBUG] Generating backend config")
|
||||
b := backend.TestBackendConfig(t, New(), map[string]interface{}{
|
||||
"path": path,
|
||||
}).(*Backend)
|
||||
t.Logf("[DEBUG] Backend configured")
|
||||
|
||||
defer deleteSwiftContainer(t, b.client, path)
|
||||
|
||||
t.Logf("[DEBUG] Testing Backend")
|
||||
|
||||
// Generate some state
|
||||
state1 := terraform.NewState()
|
||||
// state1Lineage := state1.Lineage
|
||||
t.Logf("state1 lineage = %s, serial = %d", state1.Lineage, state1.Serial)
|
||||
|
||||
// RemoteClient to test with
|
||||
client := &RemoteClient{
|
||||
client: b.client,
|
||||
archive: b.archive,
|
||||
archiveContainer: b.archiveContainer,
|
||||
container: b.container,
|
||||
}
|
||||
|
||||
stateMgr := &remote.State{Client: client}
|
||||
stateMgr.WriteState(state1)
|
||||
if err := stateMgr.PersistState(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := stateMgr.RefreshState(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Add some state
|
||||
state1.AddModuleState(&terraform.ModuleState{
|
||||
Path: []string{"root"},
|
||||
Outputs: map[string]*terraform.OutputState{
|
||||
"bar": &terraform.OutputState{
|
||||
Type: "string",
|
||||
Sensitive: false,
|
||||
Value: "baz",
|
||||
},
|
||||
},
|
||||
})
|
||||
stateMgr.WriteState(state1)
|
||||
if err := stateMgr.PersistState(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestBackendArchive(t *testing.T) {
|
||||
testACC(t)
|
||||
|
||||
container := fmt.Sprintf("terraform-state-swift-test-%x", time.Now().Unix())
|
||||
archiveContainer := fmt.Sprintf("%s_archive", container)
|
||||
|
||||
b := backend.TestBackendConfig(t, New(), map[string]interface{}{
|
||||
"archive_container": archiveContainer,
|
||||
"container": container,
|
||||
}).(*Backend)
|
||||
|
||||
defer deleteSwiftContainer(t, b.client, container)
|
||||
defer deleteSwiftContainer(t, b.client, archiveContainer)
|
||||
|
||||
// Generate some state
|
||||
state1 := terraform.NewState()
|
||||
// state1Lineage := state1.Lineage
|
||||
t.Logf("state1 lineage = %s, serial = %d", state1.Lineage, state1.Serial)
|
||||
|
||||
// RemoteClient to test with
|
||||
client := &RemoteClient{
|
||||
client: b.client,
|
||||
archive: b.archive,
|
||||
archiveContainer: b.archiveContainer,
|
||||
container: b.container,
|
||||
}
|
||||
|
||||
stateMgr := &remote.State{Client: client}
|
||||
stateMgr.WriteState(state1)
|
||||
if err := stateMgr.PersistState(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := stateMgr.RefreshState(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Add some state
|
||||
state1.AddModuleState(&terraform.ModuleState{
|
||||
Path: []string{"root"},
|
||||
Outputs: map[string]*terraform.OutputState{
|
||||
"bar": &terraform.OutputState{
|
||||
Type: "string",
|
||||
Sensitive: false,
|
||||
Value: "baz",
|
||||
},
|
||||
},
|
||||
})
|
||||
stateMgr.WriteState(state1)
|
||||
if err := stateMgr.PersistState(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
archiveObjects := getSwiftObjectNames(t, b.client, archiveContainer)
|
||||
t.Logf("archiveObjects len = %d. Contents = %+v", len(archiveObjects), archiveObjects)
|
||||
if len(archiveObjects) != 1 {
|
||||
t.Fatalf("Invalid number of archive objects. Expected 1, got %d", len(archiveObjects))
|
||||
}
|
||||
|
||||
// Download archive state to validate
|
||||
archiveData := downloadSwiftObject(t, b.client, archiveContainer, archiveObjects[0])
|
||||
t.Logf("Archive data downloaded... Looks like: %+v", archiveData)
|
||||
archiveState, err := terraform.ReadState(archiveData)
|
||||
if err != nil {
|
||||
t.Fatalf("Error Reading State: %s", err)
|
||||
}
|
||||
|
||||
t.Logf("Archive state lineage = %s, serial = %d, lineage match = %t", archiveState.Lineage, archiveState.Serial, stateMgr.State().SameLineage(archiveState))
|
||||
if !stateMgr.State().SameLineage(archiveState) {
|
||||
t.Fatal("Got a different lineage")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Helper function to download an object in a Swift container
|
||||
func downloadSwiftObject(t *testing.T, osClient *gophercloud.ServiceClient, container, object string) (data io.Reader) {
|
||||
t.Logf("Attempting to download object %s from container %s", object, container)
|
||||
res := objects.Download(osClient, container, object, nil)
|
||||
if res.Err != nil {
|
||||
t.Fatalf("Error downloading object: %s", res.Err)
|
||||
}
|
||||
data = res.Body
|
||||
return
|
||||
}
|
||||
|
||||
// Helper function to get a list of objects in a Swift container
|
||||
func getSwiftObjectNames(t *testing.T, osClient *gophercloud.ServiceClient, container string) (objectNames []string) {
|
||||
_ = objects.List(osClient, container, nil).EachPage(func(page pagination.Page) (bool, error) {
|
||||
|
||||
// Get a slice of object names
|
||||
names, err := objects.ExtractNames(page)
|
||||
if err != nil {
|
||||
t.Fatalf("Error extracting object names from page: %s", err)
|
||||
}
|
||||
for _, object := range names {
|
||||
objectNames = append(objectNames, object)
|
||||
}
|
||||
|
||||
return true, nil
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Helper function to delete Swift container
|
||||
func deleteSwiftContainer(t *testing.T, osClient *gophercloud.ServiceClient, container string) {
|
||||
warning := "WARNING: Failed to delete the test Swift container. It may have been left in your Openstack account and may incur storage charges. (error was %s)"
|
||||
|
||||
// Remove any objects
|
||||
deleteSwiftObjects(t, osClient, container)
|
||||
|
||||
// Delete the container
|
||||
deleteResult := containers.Delete(osClient, container)
|
||||
if deleteResult.Err != nil {
|
||||
if _, ok := deleteResult.Err.(gophercloud.ErrDefault404); !ok {
|
||||
t.Fatalf(warning, deleteResult.Err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to delete Swift objects within a container
|
||||
func deleteSwiftObjects(t *testing.T, osClient *gophercloud.ServiceClient, container string) {
|
||||
// Get a slice of object names
|
||||
objectNames := getSwiftObjectNames(t, osClient, container)
|
||||
|
||||
for _, object := range objectNames {
|
||||
result := objects.Delete(osClient, container, object, nil)
|
||||
if result.Err != nil {
|
||||
t.Fatalf("Error deleting object %s from container %s: %s", object, container, result.Err)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,115 @@
|
||||
package swift
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/md5"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/gophercloud/gophercloud"
|
||||
"github.com/gophercloud/gophercloud/openstack/objectstorage/v1/containers"
|
||||
"github.com/gophercloud/gophercloud/openstack/objectstorage/v1/objects"
|
||||
|
||||
"github.com/hashicorp/terraform/state/remote"
|
||||
)
|
||||
|
||||
const (
|
||||
TFSTATE_NAME = "tfstate.tf"
|
||||
TFSTATE_LOCK_NAME = "tfstate.lock"
|
||||
)
|
||||
|
||||
// RemoteClient implements the Client interface for an Openstack Swift server.
|
||||
type RemoteClient struct {
|
||||
client *gophercloud.ServiceClient
|
||||
container string
|
||||
archive bool
|
||||
archiveContainer string
|
||||
expireSecs int
|
||||
}
|
||||
|
||||
func (c *RemoteClient) Get() (*remote.Payload, error) {
|
||||
log.Printf("[DEBUG] Getting object %s in container %s", TFSTATE_NAME, c.container)
|
||||
result := objects.Download(c.client, c.container, TFSTATE_NAME, nil)
|
||||
|
||||
// Extract any errors from result
|
||||
_, err := result.Extract()
|
||||
|
||||
// 404 response is to be expected if the object doesn't already exist!
|
||||
if _, ok := err.(gophercloud.ErrDefault404); ok {
|
||||
log.Println("[DEBUG] Object doesn't exist to download.")
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
bytes, err := result.ExtractContent()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
hash := md5.Sum(bytes)
|
||||
payload := &remote.Payload{
|
||||
Data: bytes,
|
||||
MD5: hash[:md5.Size],
|
||||
}
|
||||
|
||||
return payload, nil
|
||||
}
|
||||
|
||||
func (c *RemoteClient) Put(data []byte) error {
|
||||
if err := c.ensureContainerExists(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG] Putting object %s in container %s", TFSTATE_NAME, c.container)
|
||||
reader := bytes.NewReader(data)
|
||||
createOpts := objects.CreateOpts{
|
||||
Content: reader,
|
||||
}
|
||||
|
||||
if c.expireSecs != 0 {
|
||||
log.Printf("[DEBUG] ExpireSecs = %d", c.expireSecs)
|
||||
createOpts.DeleteAfter = c.expireSecs
|
||||
}
|
||||
|
||||
result := objects.Create(c.client, c.container, TFSTATE_NAME, createOpts)
|
||||
|
||||
return result.Err
|
||||
}
|
||||
|
||||
func (c *RemoteClient) Delete() error {
|
||||
log.Printf("[DEBUG] Deleting object %s in container %s", TFSTATE_NAME, c.container)
|
||||
result := objects.Delete(c.client, c.container, TFSTATE_NAME, nil)
|
||||
return result.Err
|
||||
}
|
||||
|
||||
func (c *RemoteClient) ensureContainerExists() error {
|
||||
containerOpts := &containers.CreateOpts{}
|
||||
|
||||
if c.archive {
|
||||
log.Printf("[DEBUG] Creating archive container %s", c.archiveContainer)
|
||||
result := containers.Create(c.client, c.archiveContainer, nil)
|
||||
if result.Err != nil {
|
||||
log.Printf("[DEBUG] Error creating archive container %s: %s", c.archiveContainer, result.Err)
|
||||
return result.Err
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG] Enabling Versioning on container %s", c.container)
|
||||
containerOpts.VersionsLocation = c.archiveContainer
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG] Creating container %s", c.container)
|
||||
result := containers.Create(c.client, c.container, containerOpts)
|
||||
if result.Err != nil {
|
||||
return result.Err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func multiEnv(ks []string) string {
|
||||
for _, k := range ks {
|
||||
if v := os.Getenv(k); v != "" {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@ -0,0 +1,33 @@
|
||||
package swift
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/terraform/backend"
|
||||
"github.com/hashicorp/terraform/state/remote"
|
||||
)
|
||||
|
||||
func TestRemoteClient_impl(t *testing.T) {
|
||||
var _ remote.Client = new(RemoteClient)
|
||||
}
|
||||
|
||||
func TestRemoteClient(t *testing.T) {
|
||||
testACC(t)
|
||||
|
||||
container := fmt.Sprintf("terraform-state-swift-test-%x", time.Now().Unix())
|
||||
|
||||
b := backend.TestBackendConfig(t, New(), map[string]interface{}{
|
||||
"container": container,
|
||||
}).(*Backend)
|
||||
|
||||
state, err := b.State(backend.DefaultStateName)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
defer deleteSwiftContainer(t, b.client, container)
|
||||
|
||||
remote.TestClient(t, state.(*remote.State).Client)
|
||||
}
|
||||
@ -1,362 +0,0 @@
|
||||
package remote
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/md5"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gophercloud/gophercloud"
|
||||
"github.com/gophercloud/gophercloud/openstack"
|
||||
"github.com/gophercloud/gophercloud/openstack/objectstorage/v1/containers"
|
||||
"github.com/gophercloud/gophercloud/openstack/objectstorage/v1/objects"
|
||||
tf_openstack "github.com/terraform-providers/terraform-provider-openstack/openstack"
|
||||
)
|
||||
|
||||
const TFSTATE_NAME = "tfstate.tf"
|
||||
|
||||
// SwiftClient implements the Client interface for an Openstack Swift server.
|
||||
type SwiftClient struct {
|
||||
client *gophercloud.ServiceClient
|
||||
authurl string
|
||||
cacert string
|
||||
cert string
|
||||
domainid string
|
||||
domainname string
|
||||
insecure bool
|
||||
key string
|
||||
password string
|
||||
path string
|
||||
region string
|
||||
tenantid string
|
||||
tenantname string
|
||||
userid string
|
||||
username string
|
||||
token string
|
||||
archive bool
|
||||
archivepath string
|
||||
expireSecs int
|
||||
}
|
||||
|
||||
func swiftFactory(conf map[string]string) (Client, error) {
|
||||
client := &SwiftClient{}
|
||||
|
||||
if err := client.validateConfig(conf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func (c *SwiftClient) validateConfig(conf map[string]string) (err error) {
|
||||
authUrl, ok := conf["auth_url"]
|
||||
if !ok {
|
||||
authUrl = os.Getenv("OS_AUTH_URL")
|
||||
if authUrl == "" {
|
||||
return fmt.Errorf("missing 'auth_url' configuration or OS_AUTH_URL environment variable")
|
||||
}
|
||||
}
|
||||
c.authurl = authUrl
|
||||
|
||||
username, ok := conf["user_name"]
|
||||
if !ok {
|
||||
username = os.Getenv("OS_USERNAME")
|
||||
}
|
||||
c.username = username
|
||||
|
||||
userID, ok := conf["user_id"]
|
||||
if !ok {
|
||||
userID = os.Getenv("OS_USER_ID")
|
||||
}
|
||||
c.userid = userID
|
||||
|
||||
token, ok := conf["token"]
|
||||
if !ok {
|
||||
token = os.Getenv("OS_AUTH_TOKEN")
|
||||
}
|
||||
c.token = token
|
||||
|
||||
password, ok := conf["password"]
|
||||
if !ok {
|
||||
password = os.Getenv("OS_PASSWORD")
|
||||
|
||||
}
|
||||
c.password = password
|
||||
if password == "" && token == "" {
|
||||
return fmt.Errorf("missing either password or token configuration or OS_PASSWORD or OS_AUTH_TOKEN environment variable")
|
||||
}
|
||||
|
||||
region, ok := conf["region_name"]
|
||||
if !ok {
|
||||
region = os.Getenv("OS_REGION_NAME")
|
||||
}
|
||||
c.region = region
|
||||
|
||||
tenantID, ok := conf["tenant_id"]
|
||||
if !ok {
|
||||
tenantID = multiEnv([]string{
|
||||
"OS_TENANT_ID",
|
||||
"OS_PROJECT_ID",
|
||||
})
|
||||
}
|
||||
c.tenantid = tenantID
|
||||
|
||||
tenantName, ok := conf["tenant_name"]
|
||||
if !ok {
|
||||
tenantName = multiEnv([]string{
|
||||
"OS_TENANT_NAME",
|
||||
"OS_PROJECT_NAME",
|
||||
})
|
||||
}
|
||||
c.tenantname = tenantName
|
||||
|
||||
domainID, ok := conf["domain_id"]
|
||||
if !ok {
|
||||
domainID = multiEnv([]string{
|
||||
"OS_USER_DOMAIN_ID",
|
||||
"OS_PROJECT_DOMAIN_ID",
|
||||
"OS_DOMAIN_ID",
|
||||
})
|
||||
}
|
||||
c.domainid = domainID
|
||||
|
||||
domainName, ok := conf["domain_name"]
|
||||
if !ok {
|
||||
domainName = multiEnv([]string{
|
||||
"OS_USER_DOMAIN_NAME",
|
||||
"OS_PROJECT_DOMAIN_NAME",
|
||||
"OS_DOMAIN_NAME",
|
||||
"DEFAULT_DOMAIN",
|
||||
})
|
||||
}
|
||||
c.domainname = domainName
|
||||
|
||||
path, ok := conf["path"]
|
||||
if !ok || path == "" {
|
||||
return fmt.Errorf("missing 'path' configuration")
|
||||
}
|
||||
c.path = path
|
||||
|
||||
if archivepath, ok := conf["archive_path"]; ok {
|
||||
log.Printf("[DEBUG] Archivepath set, enabling object versioning")
|
||||
c.archive = true
|
||||
c.archivepath = archivepath
|
||||
}
|
||||
|
||||
if expire, ok := conf["expire_after"]; ok {
|
||||
log.Printf("[DEBUG] Requested that remote state expires after %s", expire)
|
||||
|
||||
if strings.HasSuffix(expire, "d") {
|
||||
log.Printf("[DEBUG] Got a days expire after duration. Converting to hours")
|
||||
days, err := strconv.Atoi(expire[:len(expire)-1])
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error converting expire_after value %s to int: %s", expire, err)
|
||||
}
|
||||
|
||||
expire = fmt.Sprintf("%dh", days*24)
|
||||
log.Printf("[DEBUG] Expire after %s hours", expire)
|
||||
}
|
||||
|
||||
expireDur, err := time.ParseDuration(expire)
|
||||
if err != nil {
|
||||
log.Printf("[DEBUG] Error parsing duration %s: %s", expire, err)
|
||||
return fmt.Errorf("Error parsing expire_after duration '%s': %s", expire, err)
|
||||
}
|
||||
log.Printf("[DEBUG] Seconds duration = %d", int(expireDur.Seconds()))
|
||||
c.expireSecs = int(expireDur.Seconds())
|
||||
}
|
||||
|
||||
c.insecure = false
|
||||
raw, ok := conf["insecure"]
|
||||
if !ok {
|
||||
raw = os.Getenv("OS_INSECURE")
|
||||
}
|
||||
if raw != "" {
|
||||
v, err := strconv.ParseBool(raw)
|
||||
if err != nil {
|
||||
return fmt.Errorf("'insecure' and 'OS_INSECURE' could not be parsed as bool: %s", err)
|
||||
}
|
||||
c.insecure = v
|
||||
}
|
||||
|
||||
cacertFile, ok := conf["cacert_file"]
|
||||
if !ok {
|
||||
cacertFile = os.Getenv("OS_CACERT")
|
||||
}
|
||||
c.cacert = cacertFile
|
||||
|
||||
cert, ok := conf["cert"]
|
||||
if !ok {
|
||||
cert = os.Getenv("OS_CERT")
|
||||
}
|
||||
c.cert = cert
|
||||
|
||||
key, ok := conf["key"]
|
||||
if !ok {
|
||||
key = os.Getenv("OS_KEY")
|
||||
}
|
||||
c.key = key
|
||||
|
||||
ao := gophercloud.AuthOptions{
|
||||
IdentityEndpoint: c.authurl,
|
||||
UserID: c.userid,
|
||||
Username: c.username,
|
||||
TenantID: c.tenantid,
|
||||
TenantName: c.tenantname,
|
||||
Password: c.password,
|
||||
TokenID: c.token,
|
||||
DomainID: c.domainid,
|
||||
DomainName: c.domainname,
|
||||
}
|
||||
|
||||
provider, err := openstack.NewClient(ao.IdentityEndpoint)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
config := &tls.Config{}
|
||||
|
||||
if c.cacert != "" {
|
||||
caCert, err := ioutil.ReadFile(c.cacert)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
caCertPool := x509.NewCertPool()
|
||||
caCertPool.AppendCertsFromPEM(caCert)
|
||||
config.RootCAs = caCertPool
|
||||
}
|
||||
|
||||
if c.insecure {
|
||||
log.Printf("[DEBUG] Insecure mode set")
|
||||
config.InsecureSkipVerify = true
|
||||
}
|
||||
|
||||
if c.cert != "" && c.key != "" {
|
||||
cert, err := tls.LoadX509KeyPair(c.cert, c.key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
config.Certificates = []tls.Certificate{cert}
|
||||
config.BuildNameToCertificate()
|
||||
}
|
||||
|
||||
// if OS_DEBUG is set, log the requests and responses
|
||||
var osDebug bool
|
||||
if os.Getenv("OS_DEBUG") != "" {
|
||||
osDebug = true
|
||||
}
|
||||
|
||||
transport := &http.Transport{Proxy: http.ProxyFromEnvironment, TLSClientConfig: config}
|
||||
provider.HTTPClient = http.Client{
|
||||
Transport: &tf_openstack.LogRoundTripper{
|
||||
Rt: transport,
|
||||
OsDebug: osDebug,
|
||||
},
|
||||
}
|
||||
|
||||
err = openstack.Authenticate(provider, ao)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.client, err = openstack.NewObjectStorageV1(provider, gophercloud.EndpointOpts{
|
||||
Region: c.region,
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *SwiftClient) Get() (*Payload, error) {
|
||||
result := objects.Download(c.client, c.path, TFSTATE_NAME, nil)
|
||||
|
||||
// Extract any errors from result
|
||||
_, err := result.Extract()
|
||||
|
||||
// 404 response is to be expected if the object doesn't already exist!
|
||||
if _, ok := err.(gophercloud.ErrDefault404); ok {
|
||||
log.Printf("[DEBUG] Container doesn't exist to download.")
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
bytes, err := result.ExtractContent()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
hash := md5.Sum(bytes)
|
||||
payload := &Payload{
|
||||
Data: bytes,
|
||||
MD5: hash[:md5.Size],
|
||||
}
|
||||
|
||||
return payload, nil
|
||||
}
|
||||
|
||||
func (c *SwiftClient) Put(data []byte) error {
|
||||
if err := c.ensureContainerExists(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG] Creating object %s at path %s", TFSTATE_NAME, c.path)
|
||||
reader := bytes.NewReader(data)
|
||||
createOpts := objects.CreateOpts{
|
||||
Content: reader,
|
||||
}
|
||||
|
||||
if c.expireSecs != 0 {
|
||||
log.Printf("[DEBUG] ExpireSecs = %d", c.expireSecs)
|
||||
createOpts.DeleteAfter = c.expireSecs
|
||||
}
|
||||
|
||||
result := objects.Create(c.client, c.path, TFSTATE_NAME, createOpts)
|
||||
|
||||
return result.Err
|
||||
}
|
||||
|
||||
func (c *SwiftClient) Delete() error {
|
||||
result := objects.Delete(c.client, c.path, TFSTATE_NAME, nil)
|
||||
return result.Err
|
||||
}
|
||||
|
||||
func (c *SwiftClient) ensureContainerExists() error {
|
||||
containerOpts := &containers.CreateOpts{}
|
||||
|
||||
if c.archive {
|
||||
log.Printf("[DEBUG] Creating container %s", c.archivepath)
|
||||
result := containers.Create(c.client, c.archivepath, nil)
|
||||
if result.Err != nil {
|
||||
log.Printf("[DEBUG] Error creating container %s: %s", c.archivepath, result.Err)
|
||||
return result.Err
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG] Enabling Versioning on container %s", c.path)
|
||||
containerOpts.VersionsLocation = c.archivepath
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG] Creating container %s", c.path)
|
||||
result := containers.Create(c.client, c.path, containerOpts)
|
||||
if result.Err != nil {
|
||||
return result.Err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func multiEnv(ks []string) string {
|
||||
for _, k := range ks {
|
||||
if v := os.Getenv(k); v != "" {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@ -1,31 +0,0 @@
|
||||
package remote
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSwiftClient_impl(t *testing.T) {
|
||||
var _ Client = new(SwiftClient)
|
||||
}
|
||||
|
||||
func TestSwiftClient(t *testing.T) {
|
||||
os_auth_url := os.Getenv("OS_AUTH_URL")
|
||||
if os_auth_url == "" {
|
||||
t.Skipf("skipping, OS_AUTH_URL and friends must be set")
|
||||
}
|
||||
|
||||
if _, err := http.Get(os_auth_url); err != nil {
|
||||
t.Skipf("skipping, unable to reach %s: %s", os_auth_url, err)
|
||||
}
|
||||
|
||||
client, err := swiftFactory(map[string]string{
|
||||
"path": "swift_test",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("bad: %s", err)
|
||||
}
|
||||
|
||||
testClient(t, client)
|
||||
}
|
||||
49
vendor/github.com/terraform-providers/terraform-provider-openstack/openstack/config.go
generated
vendored
49
vendor/github.com/terraform-providers/terraform-provider-openstack/openstack/config.go
generated
vendored
12
vendor/github.com/terraform-providers/terraform-provider-openstack/openstack/provider.go
generated
vendored
12
vendor/github.com/terraform-providers/terraform-provider-openstack/openstack/provider.go
generated
vendored
13
vendor/github.com/terraform-providers/terraform-provider-openstack/openstack/util.go
generated
vendored
13
vendor/github.com/terraform-providers/terraform-provider-openstack/openstack/util.go
generated
vendored
Loading…
Reference in new issue