github.com/peterbale/terraform@v0.9.0-beta2.0.20170315142748-5723acd55547/builtin/providers/icinga2/provider.go (about)

     1  package icinga2
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"os"
     7  
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"github.com/lrsmith/go-icinga2-api/iapi"
    11  )
    12  
    13  func Provider() terraform.ResourceProvider {
    14  	return &schema.Provider{
    15  		Schema: map[string]*schema.Schema{
    16  			"api_url": &schema.Schema{
    17  				Type:        schema.TypeString,
    18  				Required:    true,
    19  				DefaultFunc: schema.EnvDefaultFunc("ICINGA2_API_URL", nil),
    20  				Description: descriptions["api_url"],
    21  			},
    22  			"api_user": &schema.Schema{
    23  				Type:        schema.TypeString,
    24  				Required:    true,
    25  				DefaultFunc: schema.EnvDefaultFunc("ICINGA2_API_USER", nil),
    26  				Description: descriptions["api_user"],
    27  			},
    28  			"api_password": &schema.Schema{
    29  				Type:        schema.TypeString,
    30  				Required:    true,
    31  				DefaultFunc: schema.EnvDefaultFunc("ICINGA2_API_PASSWORD", nil),
    32  				Description: descriptions["api_password"],
    33  			},
    34  			"insecure_skip_tls_verify": &schema.Schema{
    35  				Type:        schema.TypeBool,
    36  				Optional:    true,
    37  				DefaultFunc: EnvBoolDefaultFunc("ICINGA2_INSECURE_SKIP_TLS_VERIFY", false),
    38  				Description: descriptions["insecure_skip_tls_verify"],
    39  			},
    40  		},
    41  		ResourcesMap: map[string]*schema.Resource{
    42  			"icinga2_host":         resourceIcinga2Host(),
    43  			"icinga2_hostgroup":    resourceIcinga2Hostgroup(),
    44  			"icinga2_checkcommand": resourceIcinga2Checkcommand(),
    45  			"icinga2_service":      resourceIcinga2Service(),
    46  		},
    47  		ConfigureFunc: configureProvider,
    48  	}
    49  }
    50  
    51  func configureProvider(d *schema.ResourceData) (interface{}, error) {
    52  
    53  	config, _ := iapi.New(
    54  		d.Get("api_user").(string),
    55  		d.Get("api_password").(string),
    56  		d.Get("api_url").(string),
    57  		d.Get("insecure_skip_tls_verify").(bool),
    58  	)
    59  
    60  	err := validateURL(d.Get("api_url").(string))
    61  
    62  	if err := config.Connect(); err != nil {
    63  		return nil, err
    64  	}
    65  
    66  	return config, err
    67  }
    68  
    69  var descriptions map[string]string
    70  
    71  func init() {
    72  	descriptions = map[string]string{
    73  		"api_url":                  "The address of the Icinga2 server.\n",
    74  		"api_user":                 "The user to authenticate to the Icinga2 Server as.\n",
    75  		"api_password":             "The password for authenticating to the Icinga2 server.\n",
    76  		"insecure_skip_tls_verify": "Disable TLS verify when connecting to Icinga2 Server\n",
    77  	}
    78  }
    79  
    80  func validateURL(urlString string) error {
    81  
    82  	//ICINGA2_API_URL=https://127.0.0.1:4665/v1
    83  	tokens, err := url.Parse(urlString)
    84  	if err != nil {
    85  		return err
    86  	}
    87  
    88  	if tokens.Scheme != "https" {
    89  		return fmt.Errorf("Error : Requests are only allowed to use the HTTPS protocol so that traffic remains encrypted.")
    90  	}
    91  
    92  	if tokens.Path != "/v1" {
    93  		return fmt.Errorf("Error : Invalid API version %s specified. Only v1 is currently supported.", tokens.Path)
    94  	}
    95  
    96  	return nil
    97  }
    98  
    99  // EnvBoolDefaultFunc is a helper function that returns
   100  func EnvBoolDefaultFunc(k string, dv interface{}) schema.SchemaDefaultFunc {
   101  	return func() (interface{}, error) {
   102  		if v := os.Getenv(k); v == "true" {
   103  			return true, nil
   104  		}
   105  
   106  		return false, nil
   107  	}
   108  }