2018-03-17 02:53:21 +01:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2019-06-05 22:12:07 +02:00
|
|
|
"time"
|
2018-03-17 02:53:21 +01:00
|
|
|
|
2019-06-05 22:12:07 +02:00
|
|
|
"github.com/hashicorp/go-cleanhttp"
|
|
|
|
"github.com/hashicorp/go-retryablehttp"
|
2021-05-17 17:42:17 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/backend"
|
2020-11-18 16:07:30 +01:00
|
|
|
"github.com/hashicorp/terraform/internal/legacy/helper/schema"
|
2020-08-11 17:43:01 +02:00
|
|
|
"github.com/hashicorp/terraform/states/remote"
|
|
|
|
"github.com/hashicorp/terraform/states/statemgr"
|
2018-03-17 02:53:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func New() backend.Backend {
|
|
|
|
s := &schema.Backend{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"address": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
2020-09-02 14:35:08 +02:00
|
|
|
DefaultFunc: schema.EnvDefaultFunc("TF_HTTP_ADDRESS", nil),
|
2018-03-17 02:53:21 +01:00
|
|
|
Description: "The address of the REST endpoint",
|
|
|
|
},
|
|
|
|
"update_method": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2020-09-02 14:35:08 +02:00
|
|
|
DefaultFunc: schema.EnvDefaultFunc("TF_HTTP_UPDATE_METHOD", "POST"),
|
2018-03-17 02:53:21 +01:00
|
|
|
Description: "HTTP method to use when updating state",
|
|
|
|
},
|
|
|
|
"lock_address": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2020-09-02 14:35:08 +02:00
|
|
|
DefaultFunc: schema.EnvDefaultFunc("TF_HTTP_LOCK_ADDRESS", nil),
|
2018-03-17 02:53:21 +01:00
|
|
|
Description: "The address of the lock REST endpoint",
|
|
|
|
},
|
|
|
|
"unlock_address": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2020-09-02 14:35:08 +02:00
|
|
|
DefaultFunc: schema.EnvDefaultFunc("TF_HTTP_UNLOCK_ADDRESS", nil),
|
2018-03-17 02:53:21 +01:00
|
|
|
Description: "The address of the unlock REST endpoint",
|
|
|
|
},
|
|
|
|
"lock_method": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2020-09-02 14:35:08 +02:00
|
|
|
DefaultFunc: schema.EnvDefaultFunc("TF_HTTP_LOCK_METHOD", "LOCK"),
|
2018-03-17 02:53:21 +01:00
|
|
|
Description: "The HTTP method to use when locking",
|
|
|
|
},
|
|
|
|
"unlock_method": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2020-09-02 14:35:08 +02:00
|
|
|
DefaultFunc: schema.EnvDefaultFunc("TF_HTTP_UNLOCK_METHOD", "UNLOCK"),
|
2018-03-17 02:53:21 +01:00
|
|
|
Description: "The HTTP method to use when unlocking",
|
|
|
|
},
|
|
|
|
"username": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2020-09-02 14:35:08 +02:00
|
|
|
DefaultFunc: schema.EnvDefaultFunc("TF_HTTP_USERNAME", nil),
|
2018-03-17 02:53:21 +01:00
|
|
|
Description: "The username for HTTP basic authentication",
|
|
|
|
},
|
|
|
|
"password": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2020-09-02 14:35:08 +02:00
|
|
|
DefaultFunc: schema.EnvDefaultFunc("TF_HTTP_PASSWORD", nil),
|
2018-03-17 02:53:21 +01:00
|
|
|
Description: "The password for HTTP basic authentication",
|
|
|
|
},
|
|
|
|
"skip_cert_verification": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: false,
|
|
|
|
Description: "Whether to skip TLS verification.",
|
|
|
|
},
|
2019-06-05 22:12:07 +02:00
|
|
|
"retry_max": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
2020-09-02 14:35:08 +02:00
|
|
|
DefaultFunc: schema.EnvDefaultFunc("TF_HTTP_RETRY_MAX", 2),
|
2019-06-05 22:12:07 +02:00
|
|
|
Description: "The number of HTTP request retries.",
|
|
|
|
},
|
|
|
|
"retry_wait_min": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
2020-09-02 14:35:08 +02:00
|
|
|
DefaultFunc: schema.EnvDefaultFunc("TF_HTTP_RETRY_WAIT_MIN", 1),
|
2019-06-05 22:12:07 +02:00
|
|
|
Description: "The minimum time in seconds to wait between HTTP request attempts.",
|
|
|
|
},
|
|
|
|
"retry_wait_max": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
2020-09-02 14:35:08 +02:00
|
|
|
DefaultFunc: schema.EnvDefaultFunc("TF_HTTP_RETRY_WAIT_MAX", 30),
|
2019-06-05 22:12:07 +02:00
|
|
|
Description: "The maximum time in seconds to wait between HTTP request attempts.",
|
|
|
|
},
|
2018-03-17 02:53:21 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
b := &Backend{Backend: s}
|
|
|
|
b.Backend.ConfigureFunc = b.configure
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
type Backend struct {
|
|
|
|
*schema.Backend
|
|
|
|
|
|
|
|
client *httpClient
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Backend) configure(ctx context.Context) error {
|
|
|
|
data := schema.FromContextBackendConfig(ctx)
|
|
|
|
|
|
|
|
address := data.Get("address").(string)
|
|
|
|
updateURL, err := url.Parse(address)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse address URL: %s", err)
|
|
|
|
}
|
|
|
|
if updateURL.Scheme != "http" && updateURL.Scheme != "https" {
|
|
|
|
return fmt.Errorf("address must be HTTP or HTTPS")
|
|
|
|
}
|
|
|
|
|
|
|
|
updateMethod := data.Get("update_method").(string)
|
|
|
|
|
|
|
|
var lockURL *url.URL
|
|
|
|
if v, ok := data.GetOk("lock_address"); ok && v.(string) != "" {
|
|
|
|
var err error
|
|
|
|
lockURL, err = url.Parse(v.(string))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse lockAddress URL: %s", err)
|
|
|
|
}
|
|
|
|
if lockURL.Scheme != "http" && lockURL.Scheme != "https" {
|
|
|
|
return fmt.Errorf("lockAddress must be HTTP or HTTPS")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lockMethod := data.Get("lock_method").(string)
|
|
|
|
|
|
|
|
var unlockURL *url.URL
|
|
|
|
if v, ok := data.GetOk("unlock_address"); ok && v.(string) != "" {
|
|
|
|
var err error
|
|
|
|
unlockURL, err = url.Parse(v.(string))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse unlockAddress URL: %s", err)
|
|
|
|
}
|
|
|
|
if unlockURL.Scheme != "http" && unlockURL.Scheme != "https" {
|
|
|
|
return fmt.Errorf("unlockAddress must be HTTP or HTTPS")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unlockMethod := data.Get("unlock_method").(string)
|
|
|
|
|
|
|
|
client := cleanhttp.DefaultPooledClient()
|
|
|
|
|
|
|
|
if data.Get("skip_cert_verification").(bool) {
|
|
|
|
// ignores TLS verification
|
|
|
|
client.Transport.(*http.Transport).TLSClientConfig = &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-05 22:12:07 +02:00
|
|
|
rClient := retryablehttp.NewClient()
|
|
|
|
rClient.HTTPClient = client
|
|
|
|
rClient.RetryMax = data.Get("retry_max").(int)
|
|
|
|
rClient.RetryWaitMin = time.Duration(data.Get("retry_wait_min").(int)) * time.Second
|
|
|
|
rClient.RetryWaitMax = time.Duration(data.Get("retry_wait_max").(int)) * time.Second
|
|
|
|
|
2018-03-17 02:53:21 +01:00
|
|
|
b.client = &httpClient{
|
|
|
|
URL: updateURL,
|
|
|
|
UpdateMethod: updateMethod,
|
|
|
|
|
|
|
|
LockURL: lockURL,
|
|
|
|
LockMethod: lockMethod,
|
|
|
|
UnlockURL: unlockURL,
|
|
|
|
UnlockMethod: unlockMethod,
|
|
|
|
|
|
|
|
Username: data.Get("username").(string),
|
|
|
|
Password: data.Get("password").(string),
|
|
|
|
|
|
|
|
// accessible only for testing use
|
2019-06-05 22:12:07 +02:00
|
|
|
Client: rClient,
|
2018-03-17 02:53:21 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-11 17:43:01 +02:00
|
|
|
func (b *Backend) StateMgr(name string) (statemgr.Full, error) {
|
2018-03-17 02:53:21 +01:00
|
|
|
if name != backend.DefaultStateName {
|
2018-10-31 16:45:03 +01:00
|
|
|
return nil, backend.ErrWorkspacesNotSupported
|
2018-03-17 02:53:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return &remote.State{Client: b.client}, 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) Workspaces() ([]string, error) {
|
2018-10-31 16:45:03 +01:00
|
|
|
return nil, backend.ErrWorkspacesNotSupported
|
2018-03-17 02:53:21 +01: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) DeleteWorkspace(string) error {
|
2018-10-31 16:45:03 +01:00
|
|
|
return backend.ErrWorkspacesNotSupported
|
2018-03-17 02:53:21 +01:00
|
|
|
}
|