github.com/alexissmirnov/terraform@v0.4.3-0.20150423153700-1ef9731a2f14/builtin/providers/aws/provider.go (about)

     1  package aws
     2  
     3  import (
     4  	"github.com/hashicorp/terraform/helper/hashcode"
     5  	"github.com/hashicorp/terraform/helper/schema"
     6  	"github.com/hashicorp/terraform/terraform"
     7  )
     8  
     9  // Provider returns a terraform.ResourceProvider.
    10  func Provider() terraform.ResourceProvider {
    11  	// TODO: Move the validation to this, requires conditional schemas
    12  	// TODO: Move the configuration to this, requires validation
    13  
    14  	return &schema.Provider{
    15  		Schema: map[string]*schema.Schema{
    16  			"access_key": &schema.Schema{
    17  				Type:     schema.TypeString,
    18  				Required: true,
    19  				DefaultFunc: schema.MultiEnvDefaultFunc([]string{
    20  					"AWS_ACCESS_KEY",
    21  					"AWS_ACCESS_KEY_ID",
    22  				}, nil),
    23  				Description: descriptions["access_key"],
    24  			},
    25  
    26  			"secret_key": &schema.Schema{
    27  				Type:     schema.TypeString,
    28  				Required: true,
    29  				DefaultFunc: schema.MultiEnvDefaultFunc([]string{
    30  					"AWS_SECRET_KEY",
    31  					"AWS_SECRET_ACCESS_KEY",
    32  				}, nil),
    33  				Description: descriptions["secret_key"],
    34  			},
    35  
    36  			"token": &schema.Schema{
    37  				Type:     schema.TypeString,
    38  				Optional: true,
    39  				DefaultFunc: schema.MultiEnvDefaultFunc([]string{
    40  					"AWS_SESSION_TOKEN",
    41  				}, ""),
    42  				Description: descriptions["token"],
    43  			},
    44  
    45  			"region": &schema.Schema{
    46  				Type:     schema.TypeString,
    47  				Required: true,
    48  				DefaultFunc: schema.MultiEnvDefaultFunc([]string{
    49  					"AWS_REGION",
    50  					"AWS_DEFAULT_REGION",
    51  				}, nil),
    52  				Description:  descriptions["region"],
    53  				InputDefault: "us-east-1",
    54  			},
    55  
    56  			"allowed_account_ids": &schema.Schema{
    57  				Type:          schema.TypeSet,
    58  				Elem:          &schema.Schema{Type: schema.TypeString},
    59  				Optional:      true,
    60  				ConflictsWith: []string{"forbidden_account_ids"},
    61  				Set: func(v interface{}) int {
    62  					return hashcode.String(v.(string))
    63  				},
    64  			},
    65  
    66  			"forbidden_account_ids": &schema.Schema{
    67  				Type:          schema.TypeSet,
    68  				Elem:          &schema.Schema{Type: schema.TypeString},
    69  				Optional:      true,
    70  				ConflictsWith: []string{"allowed_account_ids"},
    71  				Set: func(v interface{}) int {
    72  					return hashcode.String(v.(string))
    73  				},
    74  			},
    75  		},
    76  
    77  		ResourcesMap: map[string]*schema.Resource{
    78  			"aws_autoscaling_group":            resourceAwsAutoscalingGroup(),
    79  			"aws_db_instance":                  resourceAwsDbInstance(),
    80  			"aws_db_parameter_group":           resourceAwsDbParameterGroup(),
    81  			"aws_db_security_group":            resourceAwsDbSecurityGroup(),
    82  			"aws_db_subnet_group":              resourceAwsDbSubnetGroup(),
    83  			"aws_eip":                          resourceAwsEip(),
    84  			"aws_elb":                          resourceAwsElb(),
    85  			"aws_instance":                     resourceAwsInstance(),
    86  			"aws_internet_gateway":             resourceAwsInternetGateway(),
    87  			"aws_key_pair":                     resourceAwsKeyPair(),
    88  			"aws_launch_configuration":         resourceAwsLaunchConfiguration(),
    89  			"aws_main_route_table_association": resourceAwsMainRouteTableAssociation(),
    90  			"aws_network_acl":                  resourceAwsNetworkAcl(),
    91  			"aws_network_interface":            resourceAwsNetworkInterface(),
    92  			"aws_route53_record":               resourceAwsRoute53Record(),
    93  			"aws_route53_zone":                 resourceAwsRoute53Zone(),
    94  			"aws_route_table":                  resourceAwsRouteTable(),
    95  			"aws_route_table_association":      resourceAwsRouteTableAssociation(),
    96  			"aws_s3_bucket":                    resourceAwsS3Bucket(),
    97  			"aws_security_group":               resourceAwsSecurityGroup(),
    98  			"aws_subnet":                       resourceAwsSubnet(),
    99  			"aws_vpc":                          resourceAwsVpc(),
   100  			"aws_vpc_peering_connection":       resourceAwsVpcPeeringConnection(),
   101  			"aws_vpn_gateway":                  resourceAwsVpnGateway(),
   102  		},
   103  
   104  		ConfigureFunc: providerConfigure,
   105  	}
   106  }
   107  
   108  var descriptions map[string]string
   109  
   110  func init() {
   111  	descriptions = map[string]string{
   112  		"region": "The region where AWS operations will take place. Examples\n" +
   113  			"are us-east-1, us-west-2, etc.",
   114  
   115  		"access_key": "The access key for API operations. You can retrieve this\n" +
   116  			"from the 'Security & Credentials' section of the AWS console.",
   117  
   118  		"secret_key": "The secret key for API operations. You can retrieve this\n" +
   119  			"from the 'Security & Credentials' section of the AWS console.",
   120  
   121  		"token": "session token. A session token is only required if you are\n" +
   122  			"using temporary security credentials.",
   123  	}
   124  }
   125  
   126  func providerConfigure(d *schema.ResourceData) (interface{}, error) {
   127  	config := Config{
   128  		AccessKey: d.Get("access_key").(string),
   129  		SecretKey: d.Get("secret_key").(string),
   130  		Token:     d.Get("token").(string),
   131  		Region:    d.Get("region").(string),
   132  	}
   133  
   134  	if v, ok := d.GetOk("allowed_account_ids"); ok {
   135  		config.AllowedAccountIds = v.(*schema.Set).List()
   136  	}
   137  
   138  	if v, ok := d.GetOk("forbidden_account_ids"); ok {
   139  		config.ForbiddenAccountIds = v.(*schema.Set).List()
   140  	}
   141  
   142  	return config.Client()
   143  }