github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/rabbitmq/provider.go (about)

     1  package rabbitmq
     2  
     3  import (
     4  	"crypto/tls"
     5  	"crypto/x509"
     6  	"fmt"
     7  	"io/ioutil"
     8  	"net/http"
     9  
    10  	"github.com/michaelklishin/rabbit-hole"
    11  
    12  	"github.com/hashicorp/terraform/helper/schema"
    13  	"github.com/hashicorp/terraform/terraform"
    14  )
    15  
    16  func Provider() terraform.ResourceProvider {
    17  	return &schema.Provider{
    18  		Schema: map[string]*schema.Schema{
    19  			"endpoint": &schema.Schema{
    20  				Type:        schema.TypeString,
    21  				Required:    true,
    22  				DefaultFunc: schema.EnvDefaultFunc("RABBITMQ_ENDPOINT", nil),
    23  				ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
    24  					value := v.(string)
    25  					if value == "" {
    26  						errors = append(errors, fmt.Errorf("Endpoint must not be an empty string"))
    27  					}
    28  
    29  					return
    30  				},
    31  			},
    32  
    33  			"username": &schema.Schema{
    34  				Type:        schema.TypeString,
    35  				Required:    true,
    36  				DefaultFunc: schema.EnvDefaultFunc("RABBITMQ_USERNAME", nil),
    37  				ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
    38  					value := v.(string)
    39  					if value == "" {
    40  						errors = append(errors, fmt.Errorf("Username must not be an empty string"))
    41  					}
    42  
    43  					return
    44  				},
    45  			},
    46  
    47  			"password": &schema.Schema{
    48  				Type:        schema.TypeString,
    49  				Required:    true,
    50  				DefaultFunc: schema.EnvDefaultFunc("RABBITMQ_PASSWORD", nil),
    51  				ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
    52  					value := v.(string)
    53  					if value == "" {
    54  						errors = append(errors, fmt.Errorf("Password must not be an empty string"))
    55  					}
    56  
    57  					return
    58  				},
    59  			},
    60  
    61  			"insecure": &schema.Schema{
    62  				Type:        schema.TypeBool,
    63  				Optional:    true,
    64  				DefaultFunc: schema.EnvDefaultFunc("RABBITMQ_INSECURE", nil),
    65  			},
    66  
    67  			"cacert_file": &schema.Schema{
    68  				Type:        schema.TypeString,
    69  				Optional:    true,
    70  				DefaultFunc: schema.EnvDefaultFunc("RABBITMQ_CACERT", ""),
    71  			},
    72  		},
    73  
    74  		ResourcesMap: map[string]*schema.Resource{
    75  			"rabbitmq_binding":     resourceBinding(),
    76  			"rabbitmq_exchange":    resourceExchange(),
    77  			"rabbitmq_permissions": resourcePermissions(),
    78  			"rabbitmq_policy":      resourcePolicy(),
    79  			"rabbitmq_queue":       resourceQueue(),
    80  			"rabbitmq_user":        resourceUser(),
    81  			"rabbitmq_vhost":       resourceVhost(),
    82  		},
    83  
    84  		ConfigureFunc: providerConfigure,
    85  	}
    86  }
    87  
    88  func providerConfigure(d *schema.ResourceData) (interface{}, error) {
    89  
    90  	var username = d.Get("username").(string)
    91  	var password = d.Get("password").(string)
    92  	var endpoint = d.Get("endpoint").(string)
    93  	var insecure = d.Get("insecure").(bool)
    94  	var cacertFile = d.Get("cacert_file").(string)
    95  
    96  	// Configure TLS/SSL:
    97  	// Ignore self-signed cert warnings
    98  	// Specify a custom CA / intermediary cert
    99  	// Specify a certificate and key
   100  	tlsConfig := &tls.Config{}
   101  	if cacertFile != "" {
   102  		caCert, err := ioutil.ReadFile(cacertFile)
   103  		if err != nil {
   104  			return nil, err
   105  		}
   106  
   107  		caCertPool := x509.NewCertPool()
   108  		caCertPool.AppendCertsFromPEM(caCert)
   109  		tlsConfig.RootCAs = caCertPool
   110  	}
   111  	if insecure {
   112  		tlsConfig.InsecureSkipVerify = true
   113  	}
   114  
   115  	// Connect to RabbitMQ management interface
   116  	transport := &http.Transport{TLSClientConfig: tlsConfig}
   117  	rmqc, err := rabbithole.NewTLSClient(endpoint, username, password, transport)
   118  	if err != nil {
   119  		return nil, err
   120  	}
   121  
   122  	return rmqc, nil
   123  }