Skip to content

Commit 152e72f

Browse files
committed
Merge pull request hashicorp#773 from ceh/issue-691
helper/schema: fix panic when validating composite schema type
2 parents 5b08cd6 + 4893eb8 commit 152e72f

26 files changed

Lines changed: 131 additions & 50 deletions

examples/aws-count/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
The count parameter on resources can simplify configurations
44
and let you scale resources by simply incrementing a number.
55

6-
Additionally, variables can be used to expand a list of resources
6+
Additionally, variables can be used to expand an array of resources
77
for use elsewhere.
88

99
As with all examples, just copy and paste the example and run

helper/schema/schema.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ type Schema struct {
142142
// element type is a complex structure, potentially with its own lifecycle.
143143
Elem interface{}
144144

145-
// The follow fields are only valid for a TypeSet type.
145+
// The following fields are only valid for a TypeSet type.
146146
//
147147
// Set defines a function to determine the unique ID of an item so that
148148
// a proper set can be built.
@@ -902,7 +902,7 @@ func (m schemaMap) validate(
902902
"%s: this field cannot be set", k)}
903903
}
904904

905-
return m.validatePrimitive(k, raw, schema, c)
905+
return m.validateType(k, raw, schema, c)
906906
}
907907

908908
func (m schemaMap) validateList(
@@ -915,7 +915,7 @@ func (m schemaMap) validateList(
915915
rawV := reflect.ValueOf(raw)
916916
if rawV.Kind() != reflect.Slice {
917917
return nil, []error{fmt.Errorf(
918-
"%s: should be a list", k)}
918+
"%s: should be an array", k)}
919919
}
920920

921921
// Now build the []interface{}
@@ -936,8 +936,7 @@ func (m schemaMap) validateList(
936936
// This is a sub-resource
937937
ws2, es2 = m.validateObject(key, t.Schema, c)
938938
case *Schema:
939-
// This is some sort of primitive
940-
ws2, es2 = m.validatePrimitive(key, raw, t, c)
939+
ws2, es2 = m.validateType(key, raw, t, c)
941940
}
942941

943942
if len(ws2) > 0 {
@@ -1041,12 +1040,6 @@ func (m schemaMap) validatePrimitive(
10411040
}
10421041

10431042
switch schema.Type {
1044-
case TypeSet:
1045-
fallthrough
1046-
case TypeList:
1047-
return m.validateList(k, raw, schema, c)
1048-
case TypeMap:
1049-
return m.validateMap(k, raw, schema, c)
10501043
case TypeBool:
10511044
// Verify that we can parse this as the correct type
10521045
var n bool
@@ -1071,3 +1064,20 @@ func (m schemaMap) validatePrimitive(
10711064

10721065
return nil, nil
10731066
}
1067+
1068+
func (m schemaMap) validateType(
1069+
k string,
1070+
raw interface{},
1071+
schema *Schema,
1072+
c *terraform.ResourceConfig) ([]string, []error) {
1073+
switch schema.Type {
1074+
case TypeSet:
1075+
fallthrough
1076+
case TypeList:
1077+
return m.validateList(k, raw, schema, c)
1078+
case TypeMap:
1079+
return m.validateMap(k, raw, schema, c)
1080+
default:
1081+
return m.validatePrimitive(k, raw, schema, c)
1082+
}
1083+
}

helper/schema/schema_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2503,6 +2503,45 @@ func TestSchemaMap_Validate(t *testing.T) {
25032503

25042504
Err: true,
25052505
},
2506+
2507+
{
2508+
Schema: map[string]*Schema{
2509+
"security_groups": &Schema{
2510+
Type: TypeSet,
2511+
Optional: true,
2512+
Computed: true,
2513+
ForceNew: true,
2514+
Elem: &Schema{Type: TypeString},
2515+
Set: func(v interface{}) int {
2516+
return len(v.(string))
2517+
},
2518+
},
2519+
},
2520+
2521+
Config: map[string]interface{}{
2522+
"security_groups": []interface{}{"${var.foo}"},
2523+
},
2524+
2525+
Err: false,
2526+
},
2527+
2528+
{
2529+
Schema: map[string]*Schema{
2530+
"security_groups": &Schema{
2531+
Type: TypeSet,
2532+
Optional: true,
2533+
Computed: true,
2534+
ForceNew: true,
2535+
Elem: &Schema{Type: TypeString},
2536+
},
2537+
},
2538+
2539+
Config: map[string]interface{}{
2540+
"security_groups": "${var.foo}",
2541+
},
2542+
2543+
Err: true,
2544+
},
25062545
}
25072546

25082547
for i, tc := range cases {

terraform/context_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4148,6 +4148,22 @@ func TestContextPlan_varMultiCountOne(t *testing.T) {
41484148
}
41494149
}
41504150

4151+
func TestContextPlan_varListErr(t *testing.T) {
4152+
m := testModule(t, "plan-var-list-err")
4153+
p := testProvider("aws")
4154+
ctx := testContext(t, &ContextOpts{
4155+
Module: m,
4156+
Providers: map[string]ResourceProviderFactory{
4157+
"aws": testProviderFuncFixed(p),
4158+
},
4159+
})
4160+
4161+
_, err := ctx.Plan(nil)
4162+
if err == nil {
4163+
t.Fatal("should error")
4164+
}
4165+
}
4166+
41514167
func TestContextRefresh(t *testing.T) {
41524168
p := testProvider("aws")
41534169
m := testModule(t, "refresh-basic")
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
provider "aws" {
2+
access_key = "a"
3+
secret_key = "b"
4+
region = "us-east-1"
5+
}
6+
7+
resource "aws_instance" "foo" {
8+
ami = "ami-foo"
9+
instance_type = "t2.micro"
10+
security_groups = "${aws_security_group.foo.name}"
11+
}
12+
13+
resource "aws_security_group" "foo" {
14+
name = "foobar"
15+
description = "foobar"
16+
}

website/source/docs/configuration/interpolation.html.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ will interpolate the ID attribute from the "aws\_instance"
3232
resource named "web". If the resource has a `count` attribute set,
3333
you can access individual attributes with a zero-based index, such
3434
as `${aws_instance.web.0.id}`. You can also use the splat syntax
35-
to get a list of all the attributes: `${aws_instance.web.*.id}`.
35+
to get an array of all the attributes: `${aws_instance.web.*.id}`.
3636
This is documented in more detail in the
3737
[resource configuration page](/docs/configuration/resources.html).
3838

@@ -68,16 +68,16 @@ The supported built-in functions are:
6868
in this file are _not_ interpolated. The contents of the file are
6969
read as-is.
7070

71-
* `join(delim, list)` - Joins the list with the delimiter. A list is
71+
* `join(delim, array)` - Joins the array with the delimiter. An array is
7272
only possible with splat variables from resources with a count
7373
greater than one. Example: `join(",", aws_instance.foo.*.id)`
7474

7575
* `lookup(map, key)` - Performs a dynamic lookup into a mapping
7676
variable.
7777

78-
* `element(list, index)` - Returns a single element from a list
78+
* `element(array, index)` - Returns a single element from an array
7979
at the given index. If the index is greater than the number of
8080
elements, this function will wrap using a standard mod algorithm.
81-
A list is only possible with splat variables from resources with
81+
An array is only possible with splat variables from resources with
8282
a count greater than one.
8383
Example: `element(aws_subnet.foo.*.id, count.index)`

website/source/docs/configuration/resources.html.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ There are **meta-parameters** available to all resources:
4848
[interpolation](/docs/configuration/interpolation.html) to reference
4949
the current count index in your resource.
5050

51-
* `depends_on` (list of strings) - Explicit dependencies that this
51+
* `depends_on` (array of strings) - Explicit dependencies that this
5252
resource has. These dependencies will be created before this
5353
resource. The dependencies are in the format of `TYPE.NAME`,
5454
for example `aws_instance.web`.

website/source/docs/providers/aws/r/autoscale.html.markdown

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ The following arguments are supported:
3333
* `name` - (Required) The name of the auto scale group.
3434
* `max_size` - (Required) The maximum size of the auto scale group.
3535
* `min_size` - (Required) The minimum size of the auto scale group.
36-
* `availability_zones` - (Required) A list of AZs to launch resources in.
36+
* `availability_zones` - (Required) An array of AZs to launch resources in.
3737
* `launch_configuration` - (Required) The ID of the launch configuration to use.
3838
* `health_check_grace_period` - (Optional) Time after instance comes into service before checking health.
3939
* `health_check_type` - (Optional) "EC2" or "ELB". Controls how health checking is done.
4040
* `desired_capacity` - (Optional) The number of Amazon EC2 instances that should be running in the group.
4141
* `force_delete` - (Optional) Allows deleting the autoscaling group without waiting
4242
for all instances in the pool to terminate.
43-
* `load_balancers` (Optional) A list of load balancer names to add to the autoscaling
43+
* `load_balancers` (Optional) An array of load balancer names to add to the autoscaling
4444
group names.
45-
* `vpc_zone_identifier` (Optional) A list of subnet IDs to launch resources in.
46-
* `termination_policies` (Optional) A list of policies to decide how the instances in the auto scale group should be terminated.
45+
* `vpc_zone_identifier` (Optional) An array of subnet IDs to launch resources in.
46+
* `termination_policies` (Optional) An array of policies to decide how the instances in the auto scale group should be terminated.
4747

4848
## Attributes Reference
4949

website/source/docs/providers/aws/r/db_instance.html.markdown

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ The following arguments are supported:
5050
* `multi_az` - (Optional) Specifies if the RDS instance is multi-AZ
5151
* `port` - (Optional) The port on which the DB accepts connections.
5252
* `publicly_accessible` - (Optional) Bool to control if instance is publicly accessible.
53-
* `vpc_security_group_ids` - (Optional) List of VPC security groups to associate.
53+
* `vpc_security_group_ids` - (Optional) Array of VPC security groups to associate.
5454
* `skip_final_snapshot` - (Optional) Enables skipping the final snapshot on deletion.
55-
* `security_group_names` - (Optional) List of DB Security Groups to associate.
55+
* `security_group_names` - (Optional) Array of DB Security Groups to associate.
5656
* `db_subnet_group_name` - (Optional) Name of DB subnet group
5757
* `parameter_group_name` - (Optional) Name of the DB parameter group to associate.
5858

website/source/docs/providers/aws/r/db_parameter_group.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ The following arguments are supported:
3535
* `name` - (Required) The name of the DB parameter group.
3636
* `family` - (Required) The family of the DB parameter group.
3737
* `description` - (Required) The description of the DB parameter group.
38-
* `parameter` - (Optional) A list of DB parameters to apply.
38+
* `parameter` - (Optional) An array of DB parameters to apply.
3939

4040
Parameter blocks support the following:
4141

0 commit comments

Comments
 (0)