github.com/keshavdv/terraform@v0.7.0-rc2.0.20160711232630-d69256dcb425/builtin/providers/aws/provider.go (about) 1 package aws 2 3 import ( 4 "bytes" 5 "fmt" 6 7 "github.com/hashicorp/terraform/helper/hashcode" 8 "github.com/hashicorp/terraform/helper/mutexkv" 9 "github.com/hashicorp/terraform/helper/schema" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 // Provider returns a terraform.ResourceProvider. 14 func Provider() terraform.ResourceProvider { 15 // TODO: Move the validation to this, requires conditional schemas 16 // TODO: Move the configuration to this, requires validation 17 18 // The actual provider 19 return &schema.Provider{ 20 Schema: map[string]*schema.Schema{ 21 "access_key": &schema.Schema{ 22 Type: schema.TypeString, 23 Optional: true, 24 Default: "", 25 Description: descriptions["access_key"], 26 }, 27 28 "secret_key": &schema.Schema{ 29 Type: schema.TypeString, 30 Optional: true, 31 Default: "", 32 Description: descriptions["secret_key"], 33 }, 34 35 "profile": &schema.Schema{ 36 Type: schema.TypeString, 37 Optional: true, 38 Default: "", 39 Description: descriptions["profile"], 40 }, 41 42 "shared_credentials_file": &schema.Schema{ 43 Type: schema.TypeString, 44 Optional: true, 45 Default: "", 46 Description: descriptions["shared_credentials_file"], 47 }, 48 49 "token": &schema.Schema{ 50 Type: schema.TypeString, 51 Optional: true, 52 Default: "", 53 Description: descriptions["token"], 54 }, 55 56 "region": &schema.Schema{ 57 Type: schema.TypeString, 58 Required: true, 59 DefaultFunc: schema.MultiEnvDefaultFunc([]string{ 60 "AWS_REGION", 61 "AWS_DEFAULT_REGION", 62 }, nil), 63 Description: descriptions["region"], 64 InputDefault: "us-east-1", 65 }, 66 67 "max_retries": &schema.Schema{ 68 Type: schema.TypeInt, 69 Optional: true, 70 Default: 11, 71 Description: descriptions["max_retries"], 72 }, 73 74 "allowed_account_ids": &schema.Schema{ 75 Type: schema.TypeSet, 76 Elem: &schema.Schema{Type: schema.TypeString}, 77 Optional: true, 78 ConflictsWith: []string{"forbidden_account_ids"}, 79 Set: schema.HashString, 80 }, 81 82 "forbidden_account_ids": &schema.Schema{ 83 Type: schema.TypeSet, 84 Elem: &schema.Schema{Type: schema.TypeString}, 85 Optional: true, 86 ConflictsWith: []string{"allowed_account_ids"}, 87 Set: schema.HashString, 88 }, 89 90 "dynamodb_endpoint": &schema.Schema{ 91 Type: schema.TypeString, 92 Optional: true, 93 Default: "", 94 Description: descriptions["dynamodb_endpoint"], 95 }, 96 97 "kinesis_endpoint": &schema.Schema{ 98 Type: schema.TypeString, 99 Optional: true, 100 Default: "", 101 Description: descriptions["kinesis_endpoint"], 102 }, 103 "endpoints": endpointsSchema(), 104 105 "insecure": &schema.Schema{ 106 Type: schema.TypeBool, 107 Optional: true, 108 Default: false, 109 Description: descriptions["insecure"], 110 }, 111 }, 112 113 DataSourcesMap: map[string]*schema.Resource{ 114 "aws_ami": dataSourceAwsAmi(), 115 "aws_availability_zones": dataSourceAwsAvailabilityZones(), 116 "aws_iam_policy_document": dataSourceAwsIamPolicyDocument(), 117 "aws_s3_bucket_object": dataSourceAwsS3BucketObject(), 118 }, 119 120 ResourcesMap: map[string]*schema.Resource{ 121 "aws_ami": resourceAwsAmi(), 122 "aws_ami_copy": resourceAwsAmiCopy(), 123 "aws_ami_from_instance": resourceAwsAmiFromInstance(), 124 "aws_api_gateway_account": resourceAwsApiGatewayAccount(), 125 "aws_api_gateway_api_key": resourceAwsApiGatewayApiKey(), 126 "aws_api_gateway_authorizer": resourceAwsApiGatewayAuthorizer(), 127 "aws_api_gateway_deployment": resourceAwsApiGatewayDeployment(), 128 "aws_api_gateway_integration": resourceAwsApiGatewayIntegration(), 129 "aws_api_gateway_integration_response": resourceAwsApiGatewayIntegrationResponse(), 130 "aws_api_gateway_method": resourceAwsApiGatewayMethod(), 131 "aws_api_gateway_method_response": resourceAwsApiGatewayMethodResponse(), 132 "aws_api_gateway_model": resourceAwsApiGatewayModel(), 133 "aws_api_gateway_resource": resourceAwsApiGatewayResource(), 134 "aws_api_gateway_rest_api": resourceAwsApiGatewayRestApi(), 135 "aws_app_cookie_stickiness_policy": resourceAwsAppCookieStickinessPolicy(), 136 "aws_autoscaling_group": resourceAwsAutoscalingGroup(), 137 "aws_autoscaling_notification": resourceAwsAutoscalingNotification(), 138 "aws_autoscaling_policy": resourceAwsAutoscalingPolicy(), 139 "aws_autoscaling_schedule": resourceAwsAutoscalingSchedule(), 140 "aws_cloudformation_stack": resourceAwsCloudFormationStack(), 141 "aws_cloudfront_distribution": resourceAwsCloudFrontDistribution(), 142 "aws_cloudfront_origin_access_identity": resourceAwsCloudFrontOriginAccessIdentity(), 143 "aws_cloudtrail": resourceAwsCloudTrail(), 144 "aws_cloudwatch_event_rule": resourceAwsCloudWatchEventRule(), 145 "aws_cloudwatch_event_target": resourceAwsCloudWatchEventTarget(), 146 "aws_cloudwatch_log_group": resourceAwsCloudWatchLogGroup(), 147 "aws_cloudwatch_log_metric_filter": resourceAwsCloudWatchLogMetricFilter(), 148 "aws_cloudwatch_log_subscription_filter": resourceAwsCloudwatchLogSubscriptionFilter(), 149 "aws_autoscaling_lifecycle_hook": resourceAwsAutoscalingLifecycleHook(), 150 "aws_cloudwatch_metric_alarm": resourceAwsCloudWatchMetricAlarm(), 151 "aws_codedeploy_app": resourceAwsCodeDeployApp(), 152 "aws_codedeploy_deployment_group": resourceAwsCodeDeployDeploymentGroup(), 153 "aws_codecommit_repository": resourceAwsCodeCommitRepository(), 154 "aws_customer_gateway": resourceAwsCustomerGateway(), 155 "aws_db_event_subscription": resourceAwsDbEventSubscription(), 156 "aws_db_instance": resourceAwsDbInstance(), 157 "aws_db_option_group": resourceAwsDbOptionGroup(), 158 "aws_db_parameter_group": resourceAwsDbParameterGroup(), 159 "aws_db_security_group": resourceAwsDbSecurityGroup(), 160 "aws_db_subnet_group": resourceAwsDbSubnetGroup(), 161 "aws_directory_service_directory": resourceAwsDirectoryServiceDirectory(), 162 "aws_dynamodb_table": resourceAwsDynamoDbTable(), 163 "aws_ebs_volume": resourceAwsEbsVolume(), 164 "aws_ecr_repository": resourceAwsEcrRepository(), 165 "aws_ecr_repository_policy": resourceAwsEcrRepositoryPolicy(), 166 "aws_ecs_cluster": resourceAwsEcsCluster(), 167 "aws_ecs_service": resourceAwsEcsService(), 168 "aws_ecs_task_definition": resourceAwsEcsTaskDefinition(), 169 "aws_efs_file_system": resourceAwsEfsFileSystem(), 170 "aws_efs_mount_target": resourceAwsEfsMountTarget(), 171 "aws_eip": resourceAwsEip(), 172 "aws_eip_association": resourceAwsEipAssociation(), 173 "aws_elasticache_cluster": resourceAwsElasticacheCluster(), 174 "aws_elasticache_parameter_group": resourceAwsElasticacheParameterGroup(), 175 "aws_elasticache_security_group": resourceAwsElasticacheSecurityGroup(), 176 "aws_elasticache_subnet_group": resourceAwsElasticacheSubnetGroup(), 177 "aws_elastic_beanstalk_application": resourceAwsElasticBeanstalkApplication(), 178 "aws_elastic_beanstalk_configuration_template": resourceAwsElasticBeanstalkConfigurationTemplate(), 179 "aws_elastic_beanstalk_environment": resourceAwsElasticBeanstalkEnvironment(), 180 "aws_elasticsearch_domain": resourceAwsElasticSearchDomain(), 181 "aws_elastictranscoder_pipeline": resourceAwsElasticTranscoderPipeline(), 182 "aws_elastictranscoder_preset": resourceAwsElasticTranscoderPreset(), 183 "aws_elb": resourceAwsElb(), 184 "aws_elb_attachment": resourceAwsElbAttachment(), 185 "aws_flow_log": resourceAwsFlowLog(), 186 "aws_glacier_vault": resourceAwsGlacierVault(), 187 "aws_iam_access_key": resourceAwsIamAccessKey(), 188 "aws_iam_account_password_policy": resourceAwsIamAccountPasswordPolicy(), 189 "aws_iam_group_policy": resourceAwsIamGroupPolicy(), 190 "aws_iam_group": resourceAwsIamGroup(), 191 "aws_iam_group_membership": resourceAwsIamGroupMembership(), 192 "aws_iam_group_policy_attachment": resourceAwsIamGroupPolicyAttachment(), 193 "aws_iam_instance_profile": resourceAwsIamInstanceProfile(), 194 "aws_iam_policy": resourceAwsIamPolicy(), 195 "aws_iam_policy_attachment": resourceAwsIamPolicyAttachment(), 196 "aws_iam_role_policy_attachment": resourceAwsIamRolePolicyAttachment(), 197 "aws_iam_role_policy": resourceAwsIamRolePolicy(), 198 "aws_iam_role": resourceAwsIamRole(), 199 "aws_iam_saml_provider": resourceAwsIamSamlProvider(), 200 "aws_iam_server_certificate": resourceAwsIAMServerCertificate(), 201 "aws_iam_user_policy_attachment": resourceAwsIamUserPolicyAttachment(), 202 "aws_iam_user_policy": resourceAwsIamUserPolicy(), 203 "aws_iam_user_ssh_key": resourceAwsIamUserSshKey(), 204 "aws_iam_user": resourceAwsIamUser(), 205 "aws_instance": resourceAwsInstance(), 206 "aws_internet_gateway": resourceAwsInternetGateway(), 207 "aws_key_pair": resourceAwsKeyPair(), 208 "aws_kinesis_firehose_delivery_stream": resourceAwsKinesisFirehoseDeliveryStream(), 209 "aws_kinesis_stream": resourceAwsKinesisStream(), 210 "aws_kms_alias": resourceAwsKmsAlias(), 211 "aws_kms_key": resourceAwsKmsKey(), 212 "aws_lambda_function": resourceAwsLambdaFunction(), 213 "aws_lambda_event_source_mapping": resourceAwsLambdaEventSourceMapping(), 214 "aws_lambda_alias": resourceAwsLambdaAlias(), 215 "aws_lambda_permission": resourceAwsLambdaPermission(), 216 "aws_launch_configuration": resourceAwsLaunchConfiguration(), 217 "aws_lb_cookie_stickiness_policy": resourceAwsLBCookieStickinessPolicy(), 218 "aws_main_route_table_association": resourceAwsMainRouteTableAssociation(), 219 "aws_nat_gateway": resourceAwsNatGateway(), 220 "aws_network_acl": resourceAwsNetworkAcl(), 221 "aws_default_network_acl": resourceAwsDefaultNetworkAcl(), 222 "aws_network_acl_rule": resourceAwsNetworkAclRule(), 223 "aws_network_interface": resourceAwsNetworkInterface(), 224 "aws_opsworks_application": resourceAwsOpsworksApplication(), 225 "aws_opsworks_stack": resourceAwsOpsworksStack(), 226 "aws_opsworks_java_app_layer": resourceAwsOpsworksJavaAppLayer(), 227 "aws_opsworks_haproxy_layer": resourceAwsOpsworksHaproxyLayer(), 228 "aws_opsworks_static_web_layer": resourceAwsOpsworksStaticWebLayer(), 229 "aws_opsworks_php_app_layer": resourceAwsOpsworksPhpAppLayer(), 230 "aws_opsworks_rails_app_layer": resourceAwsOpsworksRailsAppLayer(), 231 "aws_opsworks_nodejs_app_layer": resourceAwsOpsworksNodejsAppLayer(), 232 "aws_opsworks_memcached_layer": resourceAwsOpsworksMemcachedLayer(), 233 "aws_opsworks_mysql_layer": resourceAwsOpsworksMysqlLayer(), 234 "aws_opsworks_ganglia_layer": resourceAwsOpsworksGangliaLayer(), 235 "aws_opsworks_custom_layer": resourceAwsOpsworksCustomLayer(), 236 "aws_opsworks_instance": resourceAwsOpsworksInstance(), 237 "aws_placement_group": resourceAwsPlacementGroup(), 238 "aws_proxy_protocol_policy": resourceAwsProxyProtocolPolicy(), 239 "aws_rds_cluster": resourceAwsRDSCluster(), 240 "aws_rds_cluster_instance": resourceAwsRDSClusterInstance(), 241 "aws_rds_cluster_parameter_group": resourceAwsRDSClusterParameterGroup(), 242 "aws_redshift_cluster": resourceAwsRedshiftCluster(), 243 "aws_redshift_security_group": resourceAwsRedshiftSecurityGroup(), 244 "aws_redshift_parameter_group": resourceAwsRedshiftParameterGroup(), 245 "aws_redshift_subnet_group": resourceAwsRedshiftSubnetGroup(), 246 "aws_route53_delegation_set": resourceAwsRoute53DelegationSet(), 247 "aws_route53_record": resourceAwsRoute53Record(), 248 "aws_route53_zone_association": resourceAwsRoute53ZoneAssociation(), 249 "aws_route53_zone": resourceAwsRoute53Zone(), 250 "aws_route53_health_check": resourceAwsRoute53HealthCheck(), 251 "aws_route": resourceAwsRoute(), 252 "aws_route_table": resourceAwsRouteTable(), 253 "aws_route_table_association": resourceAwsRouteTableAssociation(), 254 "aws_ses_active_receipt_rule_set": resourceAwsSesActiveReceiptRuleSet(), 255 "aws_ses_receipt_filter": resourceAwsSesReceiptFilter(), 256 "aws_ses_receipt_rule": resourceAwsSesReceiptRule(), 257 "aws_ses_receipt_rule_set": resourceAwsSesReceiptRuleSet(), 258 "aws_s3_bucket": resourceAwsS3Bucket(), 259 "aws_s3_bucket_object": resourceAwsS3BucketObject(), 260 "aws_s3_bucket_notification": resourceAwsS3BucketNotification(), 261 "aws_security_group": resourceAwsSecurityGroup(), 262 "aws_security_group_rule": resourceAwsSecurityGroupRule(), 263 "aws_spot_instance_request": resourceAwsSpotInstanceRequest(), 264 "aws_spot_fleet_request": resourceAwsSpotFleetRequest(), 265 "aws_sqs_queue": resourceAwsSqsQueue(), 266 "aws_sns_topic": resourceAwsSnsTopic(), 267 "aws_sns_topic_subscription": resourceAwsSnsTopicSubscription(), 268 "aws_subnet": resourceAwsSubnet(), 269 "aws_volume_attachment": resourceAwsVolumeAttachment(), 270 "aws_vpc_dhcp_options_association": resourceAwsVpcDhcpOptionsAssociation(), 271 "aws_vpc_dhcp_options": resourceAwsVpcDhcpOptions(), 272 "aws_vpc_peering_connection": resourceAwsVpcPeeringConnection(), 273 "aws_vpc": resourceAwsVpc(), 274 "aws_vpc_endpoint": resourceAwsVpcEndpoint(), 275 "aws_vpn_connection": resourceAwsVpnConnection(), 276 "aws_vpn_connection_route": resourceAwsVpnConnectionRoute(), 277 "aws_vpn_gateway": resourceAwsVpnGateway(), 278 }, 279 ConfigureFunc: providerConfigure, 280 } 281 } 282 283 var descriptions map[string]string 284 285 func init() { 286 descriptions = map[string]string{ 287 "region": "The region where AWS operations will take place. Examples\n" + 288 "are us-east-1, us-west-2, etc.", 289 290 "access_key": "The access key for API operations. You can retrieve this\n" + 291 "from the 'Security & Credentials' section of the AWS console.", 292 293 "secret_key": "The secret key for API operations. You can retrieve this\n" + 294 "from the 'Security & Credentials' section of the AWS console.", 295 296 "profile": "The profile for API operations. If not set, the default profile\n" + 297 "created with `aws configure` will be used.", 298 299 "shared_credentials_file": "The path to the shared credentials file. If not set\n" + 300 "this defaults to ~/.aws/credentials.", 301 302 "token": "session token. A session token is only required if you are\n" + 303 "using temporary security credentials.", 304 305 "max_retries": "The maximum number of times an AWS API request is\n" + 306 "being executed. If the API request still fails, an error is\n" + 307 "thrown.", 308 309 "dynamodb_endpoint": "Use this to override the default endpoint URL constructed from the `region`.\n" + 310 "It's typically used to connect to dynamodb-local.", 311 312 "kinesis_endpoint": "Use this to override the default endpoint URL constructed from the `region`.\n" + 313 "It's typically used to connect to kinesalite.", 314 315 "iam_endpoint": "Use this to override the default endpoint URL constructed from the `region`.\n", 316 317 "ec2_endpoint": "Use this to override the default endpoint URL constructed from the `region`.\n", 318 319 "elb_endpoint": "Use this to override the default endpoint URL constructed from the `region`.\n", 320 321 "insecure": "Explicitly allow the provider to perform \"insecure\" SSL requests. If omitted," + 322 "default value is `false`", 323 } 324 } 325 326 func providerConfigure(d *schema.ResourceData) (interface{}, error) { 327 config := Config{ 328 AccessKey: d.Get("access_key").(string), 329 SecretKey: d.Get("secret_key").(string), 330 Profile: d.Get("profile").(string), 331 CredsFilename: d.Get("shared_credentials_file").(string), 332 Token: d.Get("token").(string), 333 Region: d.Get("region").(string), 334 MaxRetries: d.Get("max_retries").(int), 335 DynamoDBEndpoint: d.Get("dynamodb_endpoint").(string), 336 KinesisEndpoint: d.Get("kinesis_endpoint").(string), 337 Insecure: d.Get("insecure").(bool), 338 } 339 340 endpointsSet := d.Get("endpoints").(*schema.Set) 341 342 for _, endpointsSetI := range endpointsSet.List() { 343 endpoints := endpointsSetI.(map[string]interface{}) 344 config.IamEndpoint = endpoints["iam"].(string) 345 config.Ec2Endpoint = endpoints["ec2"].(string) 346 config.ElbEndpoint = endpoints["elb"].(string) 347 } 348 349 if v, ok := d.GetOk("allowed_account_ids"); ok { 350 config.AllowedAccountIds = v.(*schema.Set).List() 351 } 352 353 if v, ok := d.GetOk("forbidden_account_ids"); ok { 354 config.ForbiddenAccountIds = v.(*schema.Set).List() 355 } 356 357 return config.Client() 358 } 359 360 // This is a global MutexKV for use within this plugin. 361 var awsMutexKV = mutexkv.NewMutexKV() 362 363 func endpointsSchema() *schema.Schema { 364 return &schema.Schema{ 365 Type: schema.TypeSet, 366 Optional: true, 367 Elem: &schema.Resource{ 368 Schema: map[string]*schema.Schema{ 369 "iam": &schema.Schema{ 370 Type: schema.TypeString, 371 Optional: true, 372 Default: "", 373 Description: descriptions["iam_endpoint"], 374 }, 375 376 "ec2": &schema.Schema{ 377 Type: schema.TypeString, 378 Optional: true, 379 Default: "", 380 Description: descriptions["ec2_endpoint"], 381 }, 382 383 "elb": &schema.Schema{ 384 Type: schema.TypeString, 385 Optional: true, 386 Default: "", 387 Description: descriptions["elb_endpoint"], 388 }, 389 }, 390 }, 391 Set: endpointsToHash, 392 } 393 } 394 395 func endpointsToHash(v interface{}) int { 396 var buf bytes.Buffer 397 m := v.(map[string]interface{}) 398 buf.WriteString(fmt.Sprintf("%s-", m["iam"].(string))) 399 buf.WriteString(fmt.Sprintf("%s-", m["ec2"].(string))) 400 buf.WriteString(fmt.Sprintf("%s-", m["elb"].(string))) 401 402 return hashcode.String(buf.String()) 403 }