Skip to content

Commit

Permalink
Merge pull request #27 from Omnevo/add-custom-headers
Browse files Browse the repository at this point in the history
add ability to provide custom headers
  • Loading branch information
rfavreau authored Dec 6, 2024
2 parents 04d5955 + 2f03a50 commit b9a4901
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
5 changes: 5 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ provider "rabbitmq" {
endpoint = "http://127.0.0.1:15672"
username = "guest"
password = "guest"
headers = {
"X-Custom-Header" = "CustomValue"
}
}
# Create a virtual host
Expand Down Expand Up @@ -48,5 +52,6 @@ $ sudo rabbitmq-plugins enable rabbitmq_management
- `cacert_file` (String) The path to a custom CA / intermediate certificate. This can also be sourced from the `RABBITMQ_CACERT` Environment Variable.
- `clientcert_file` (String) The path to the X.509 client certificate. This can also be sourced from the `RABBITMQ_CLIENTCERT` Environment Variable.
- `clientkey_file` (String) The path to the private key. This can also be sourced from the `RABBITMQ_CLIENTKEY` Environment Variable.
- `headers` (Map of String) Custom headers to include in HTTP requests. This should be a map of header names to values.
- `insecure` (Boolean) Trust self-signed certificates. This can also be sourced from the `RABBITMQ_INSECURE` Environment Variable.
- `proxy` (String) The URL of a proxy through which to send HTTP requests to the RabbitMQ server. This can also be sourced from the `RABBITMQ_PROXY` Environment Variable. If not set, the default `HTTP_PROXY`/`HTTPS_PROXY` will be used instead.
36 changes: 32 additions & 4 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,22 @@ import (
"net/url"
"os"

rabbithole "github.com/michaelklishin/rabbit-hole/v2"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
rabbithole "github.com/michaelklishin/rabbit-hole/v2"
)

type customHeaderRoundTripper struct {
headers map[string]string
transport http.RoundTripper
}

func (c *customHeaderRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
for key, value := range c.headers {
req.Header.Add(key, value)
}
return c.transport.RoundTrip(req)
}

func New() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -95,6 +106,13 @@ func New() *schema.Provider {
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("RABBITMQ_PROXY", ""),
},

"headers": {
Description: "Custom headers to include in HTTP requests. This should be a map of header names to values.",
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},

ResourcesMap: map[string]*schema.Resource{
Expand Down Expand Up @@ -130,6 +148,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
var clientcertFile = d.Get("clientcert_file").(string)
var clientkeyFile = d.Get("clientkey_file").(string)
var proxy = d.Get("proxy").(string)
var headers = d.Get("headers").(map[string]interface{})

// Configure TLS/SSL:
// Ignore self-signed cert warnings
Expand Down Expand Up @@ -166,7 +185,11 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
}
}

// Connect to RabbitMQ management interface
customHeaders := make(map[string]string)
for k, v := range headers {
customHeaders[k] = v.(string)
}

transport := &http.Transport{
TLSClientConfig: tlsConfig,
Proxy: func(req *http.Request) (*url.URL, error) {
Expand All @@ -178,7 +201,12 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
},
}

rmqc, err := rabbithole.NewTLSClient(endpoint, username, password, transport)
customTransport := &customHeaderRoundTripper{
headers: customHeaders,
transport: transport,
}

rmqc, err := rabbithole.NewTLSClient(endpoint, username, password, customTransport)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit b9a4901

Please sign in to comment.