github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/cloudstack/provider.go (about)

     1  package cloudstack
     2  
     3  import (
     4  	"errors"
     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, apiURLOK := d.GetOk("api_url")
    94  	apiKey, apiKeyOK := d.GetOk("api_key")
    95  	secretKey, secretKeyOK := d.GetOk("secret_key")
    96  	config, configOK := d.GetOk("config")
    97  	profile, profileOK := d.GetOk("profile")
    98  
    99  	switch {
   100  	case apiURLOK, apiKeyOK, secretKeyOK:
   101  		if !(apiURLOK && apiKeyOK && secretKeyOK) {
   102  			return nil, errors.New("'api_url', 'api_key' and 'secret_key' should all have values")
   103  		}
   104  	case configOK, profileOK:
   105  		if !(configOK && profileOK) {
   106  			return nil, errors.New("'config' and 'profile' should both have a value")
   107  		}
   108  	default:
   109  		return nil, errors.New(
   110  			"either 'api_url', 'api_key' and 'secret_key' or 'config' and 'profile' should have values")
   111  	}
   112  
   113  	if configOK && profileOK {
   114  		cfg, err := ini.Load(config.(string))
   115  		if err != nil {
   116  			return nil, err
   117  		}
   118  
   119  		section, err := cfg.GetSection(profile.(string))
   120  		if err != nil {
   121  			return nil, err
   122  		}
   123  
   124  		apiURL = section.Key("url").String()
   125  		apiKey = section.Key("apikey").String()
   126  		secretKey = section.Key("secretkey").String()
   127  	}
   128  
   129  	cfg := Config{
   130  		APIURL:      apiURL.(string),
   131  		APIKey:      apiKey.(string),
   132  		SecretKey:   secretKey.(string),
   133  		HTTPGETOnly: d.Get("http_get_only").(bool),
   134  		Timeout:     int64(d.Get("timeout").(int)),
   135  	}
   136  
   137  	return cfg.NewClient()
   138  }