github.com/jrperritt/terraform@v0.1.1-0.20170525065507-96f391dafc38/builtin/providers/cloudstack/provider.go (about)

     1  package cloudstack
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/go-ini/ini"
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  	"github.com/hashicorp/terraform/terraform"
     9  )
    10  
    11  // Provider returns a terraform.ResourceProvider.
    12  func Provider() terraform.ResourceProvider {
    13  	return &schema.Provider{
    14  		Schema: map[string]*schema.Schema{
    15  			"api_url": &schema.Schema{
    16  				Type:          schema.TypeString,
    17  				Optional:      true,
    18  				DefaultFunc:   schema.EnvDefaultFunc("CLOUDSTACK_API_URL", nil),
    19  				ConflictsWith: []string{"config", "profile"},
    20  			},
    21  
    22  			"api_key": &schema.Schema{
    23  				Type:          schema.TypeString,
    24  				Optional:      true,
    25  				DefaultFunc:   schema.EnvDefaultFunc("CLOUDSTACK_API_KEY", nil),
    26  				ConflictsWith: []string{"config", "profile"},
    27  			},
    28  
    29  			"secret_key": &schema.Schema{
    30  				Type:          schema.TypeString,
    31  				Optional:      true,
    32  				DefaultFunc:   schema.EnvDefaultFunc("CLOUDSTACK_SECRET_KEY", nil),
    33  				ConflictsWith: []string{"config", "profile"},
    34  			},
    35  
    36  			"config": &schema.Schema{
    37  				Type:          schema.TypeString,
    38  				Optional:      true,
    39  				ConflictsWith: []string{"api_url", "api_key", "secret_key"},
    40  			},
    41  
    42  			"profile": &schema.Schema{
    43  				Type:          schema.TypeString,
    44  				Optional:      true,
    45  				ConflictsWith: []string{"api_url", "api_key", "secret_key"},
    46  			},
    47  
    48  			"http_get_only": &schema.Schema{
    49  				Type:        schema.TypeBool,
    50  				Required:    true,
    51  				DefaultFunc: schema.EnvDefaultFunc("CLOUDSTACK_HTTP_GET_ONLY", false),
    52  			},
    53  
    54  			"timeout": &schema.Schema{
    55  				Type:        schema.TypeInt,
    56  				Required:    true,
    57  				DefaultFunc: schema.EnvDefaultFunc("CLOUDSTACK_TIMEOUT", 900),
    58  			},
    59  		},
    60  
    61  		ResourcesMap: map[string]*schema.Resource{
    62  			"cloudstack_affinity_group":       resourceCloudStackAffinityGroup(),
    63  			"cloudstack_disk":                 resourceCloudStackDisk(),
    64  			"cloudstack_egress_firewall":      resourceCloudStackEgressFirewall(),
    65  			"cloudstack_firewall":             resourceCloudStackFirewall(),
    66  			"cloudstack_instance":             resourceCloudStackInstance(),
    67  			"cloudstack_ipaddress":            resourceCloudStackIPAddress(),
    68  			"cloudstack_loadbalancer_rule":    resourceCloudStackLoadBalancerRule(),
    69  			"cloudstack_network":              resourceCloudStackNetwork(),
    70  			"cloudstack_network_acl":          resourceCloudStackNetworkACL(),
    71  			"cloudstack_network_acl_rule":     resourceCloudStackNetworkACLRule(),
    72  			"cloudstack_nic":                  resourceCloudStackNIC(),
    73  			"cloudstack_port_forward":         resourceCloudStackPortForward(),
    74  			"cloudstack_private_gateway":      resourceCloudStackPrivateGateway(),
    75  			"cloudstack_secondary_ipaddress":  resourceCloudStackSecondaryIPAddress(),
    76  			"cloudstack_security_group":       resourceCloudStackSecurityGroup(),
    77  			"cloudstack_security_group_rule":  resourceCloudStackSecurityGroupRule(),
    78  			"cloudstack_ssh_keypair":          resourceCloudStackSSHKeyPair(),
    79  			"cloudstack_static_nat":           resourceCloudStackStaticNAT(),
    80  			"cloudstack_static_route":         resourceCloudStackStaticRoute(),
    81  			"cloudstack_template":             resourceCloudStackTemplate(),
    82  			"cloudstack_vpc":                  resourceCloudStackVPC(),
    83  			"cloudstack_vpn_connection":       resourceCloudStackVPNConnection(),
    84  			"cloudstack_vpn_customer_gateway": resourceCloudStackVPNCustomerGateway(),
    85  			"cloudstack_vpn_gateway":          resourceCloudStackVPNGateway(),
    86  		},
    87  
    88  		ConfigureFunc: providerConfigure,
    89  	}
    90  }
    91  
    92  func providerConfigure(d *schema.ResourceData) (interface{}, error) {
    93  	apiURL := d.Get("api_url").(string)
    94  	apiKey := d.Get("api_key").(string)
    95  	secretKey := d.Get("secret_key").(string)
    96  
    97  	if configFile, ok := d.GetOk("config"); ok {
    98  		config, err := ini.Load(configFile.(string))
    99  		if err != nil {
   100  			return nil, err
   101  		}
   102  
   103  		section, err := config.GetSection(d.Get("profile").(string))
   104  		if err != nil {
   105  			return nil, err
   106  		}
   107  
   108  		apiURL = section.Key("url").String()
   109  		apiKey = section.Key("apikey").String()
   110  		secretKey = section.Key("secretkey").String()
   111  	}
   112  
   113  	if apiURL == "" || apiKey == "" || secretKey == "" {
   114  		return nil, fmt.Errorf("No api_url or api_key or secretKey provided")
   115  	}
   116  
   117  	config := Config{
   118  		APIURL:      apiURL,
   119  		APIKey:      apiKey,
   120  		SecretKey:   secretKey,
   121  		HTTPGETOnly: d.Get("http_get_only").(bool),
   122  		Timeout:     int64(d.Get("timeout").(int)),
   123  	}
   124  
   125  	return config.NewClient()
   126  }