Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Instance Template network definition to match changes to Instance. #980

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 31 additions & 20 deletions builtin/providers/google/resource_compute_instance_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ func resourceComputeInstanceTemplate() *schema.Resource {
ForceNew: true,
},

// TODO: Constraint either source or other disk params
"disk": &schema.Schema{
Type: schema.TypeList,
Required: true,
Expand Down Expand Up @@ -133,22 +132,30 @@ func resourceComputeInstanceTemplate() *schema.Resource {
},
},

"network": &schema.Schema{
"network_interface": &schema.Schema{
Type: schema.TypeList,
Required: true,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"source": &schema.Schema{
"network": &schema.Schema{
Type: schema.TypeString,
ForceNew: true,
Required: true,
ForceNew: true,
},

"address": &schema.Schema{
Type: schema.TypeString,
ForceNew: true,
"access_config": &schema.Schema{
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"nat_ip": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Optional: true,
},
},
},
},
},
},
Expand Down Expand Up @@ -290,31 +297,35 @@ func buildDisks(d *schema.ResourceData, meta interface{}) []*compute.AttachedDis

func buildNetworks(d *schema.ResourceData, meta interface{}) (error, []*compute.NetworkInterface) {
// Build up the list of networks
networksCount := d.Get("network.#").(int)
networks := make([]*compute.NetworkInterface, 0, networksCount)
networksCount := d.Get("network_interface.#").(int)
networkInterfaces := make([]*compute.NetworkInterface, 0, networksCount)
for i := 0; i < networksCount; i++ {
prefix := fmt.Sprintf("network.%d", i)
prefix := fmt.Sprintf("network_interface.%d", i)

source := "global/networks/default"
if v, ok := d.GetOk(prefix + ".source"); ok {
if v, ok := d.GetOk(prefix + ".network"); ok {
if v.(string) != "default" {
source = v.(string)
}
}

// Build the interface
// Build the networkInterface
var iface compute.NetworkInterface
iface.AccessConfigs = []*compute.AccessConfig{
&compute.AccessConfig{
iface.Network = source

accessConfigsCount := d.Get(prefix + ".access_config.#").(int)
iface.AccessConfigs = make([]*compute.AccessConfig, accessConfigsCount)
for j := 0; j < accessConfigsCount; j++ {
acPrefix := fmt.Sprintf("%s.access_config.%d", prefix, j)
iface.AccessConfigs[j] = &compute.AccessConfig{
Type: "ONE_TO_ONE_NAT",
NatIP: d.Get(prefix + ".address").(string),
},
NatIP: d.Get(acPrefix + ".nat_ip").(string),
}
}
iface.Network = source

networks = append(networks, &iface)
networkInterfaces = append(networkInterfaces, &iface)
}
return nil, networks
return nil, networkInterfaces
}

func resourceComputeInstanceTemplateCreate(d *schema.ResourceData, meta interface{}) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ resource "google_compute_instance_template" "foobar" {
boot = true
}

network {
source = "default"
network_interface {
network = "default"
}

metadata {
Expand All @@ -241,9 +241,11 @@ resource "google_compute_instance_template" "foobar" {
source_image = "debian-7-wheezy-v20140814"
}

network {
source = "default"
address = "${google_compute_address.foo.address}"
network_interface {
network = "default"
access_config {
nat_ip = "${google_compute_address.foo.address}"
}
}

metadata {
Expand All @@ -268,8 +270,8 @@ resource "google_compute_instance_template" "foobar" {
boot = false
}

network {
source = "default"
network_interface {
network = "default"
}

metadata {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ resource "google_compute_instance_template" "foobar" {
boot = false
}

network {
source = "default"
network_interface {
network = "default"
}

metadata {
Expand Down Expand Up @@ -82,7 +82,7 @@ The following arguments are supported:
* `metadata` - (Optional) Metadata key/value pairs to make available from
within instances created from this template.

* `network` - (Required) Networks to attach to instances created from this template.
* `network_interface` - (Required) Networks to attach to instances created from this template.
This can be specified multiple times for multiple networks. Structure is
documented below.

Expand Down Expand Up @@ -130,12 +130,20 @@ The `disk` block supports:

* `type` - (Optional) The GCE disk type.

The `network` block supports:
The `network_interface` block supports:

* `source` - (Required) The name of the network to attach this interface to.
* `network` - (Required) The name of the network to attach this interface to.

* `address` - (Optional) The IP address of a reserved IP address to assign
to this interface.
* `access_config` - (Optional) Access configurations, i.e. IPs via which this instance can be
accessed via the Internet. Omit to ensure that the instance is not accessible from the Internet
(this means that ssh provisioners will not work unless you are running Terraform can send traffic to
the instance's network (e.g. via tunnel or because it is running on another cloud instance on that
network). This block can be repeated multiple times. Structure documented below.

The `access_config` block supports:

* `nat_ip` - (Optional) The IP address that will be 1:1 mapped to the instance's network ip. If not
given, one will be generated.

The `service_account` block supports:

Expand Down