Skip to content

Commit

Permalink
Allow resource_compute_address to select a specific internal IP
Browse files Browse the repository at this point in the history
  • Loading branch information
Nic Cope committed Oct 27, 2017
1 parent 15973d6 commit 5b7a769
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
9 changes: 9 additions & 0 deletions google/resource_compute_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var (
computeAddressLinkRegex = regexp.MustCompile("projects/(.+)/regions/(.+)/addresses/(.+)$")
AddressBaseApiVersion = v1
AddressVersionedFeatures = []Feature{
{Version: v0beta, Item: "address"},
{Version: v0beta, Item: "address_type"},
{Version: v0beta, Item: "subnetwork"},
}
Expand Down Expand Up @@ -60,8 +61,12 @@ func resourceComputeAddress() *schema.Resource {
DiffSuppressFunc: linkDiffSuppress,
},

// address will be computed unless it is specified explicitly.
// address may only be specified for the INTERNAL address_type.
"address": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},

Expand Down Expand Up @@ -107,6 +112,10 @@ func resourceComputeAddressCreate(d *schema.ResourceData, meta interface{}) erro
Subnetwork: d.Get("subnetwork").(string),
}

if desired, ok := d.GetOk("address"); ok {
v0BetaAddress.Address = desired.(string)
}

var op interface{}
switch computeApiVersion {
case v1:
Expand Down
37 changes: 31 additions & 6 deletions google/resource_compute_address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,13 @@ func TestAccComputeAddress_internal(t *testing.T) {
resource.TestStep{
Config: testAccComputeAddress_internal,
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeBetaAddressExists(
"google_compute_address.foobar", &addr),
resource.TestCheckResourceAttr(
"google_compute_address.foobar", "address_type", "INTERNAL"),
testAccCheckComputeBetaAddressExists("google_compute_address.internal", &addr),
testAccCheckComputeBetaAddressExists("google_compute_address.internal_with_subnet", &addr),
testAccCheckComputeBetaAddressExists("google_compute_address.internal_with_subnet_and_address", &addr),
resource.TestCheckResourceAttr("google_compute_address.internal", "address_type", "INTERNAL"),
resource.TestCheckResourceAttr("google_compute_address.internal_with_subnet", "address_type", "INTERNAL"),
resource.TestCheckResourceAttr("google_compute_address.internal_with_subnet_and_address", "address_type", "INTERNAL"),
resource.TestCheckResourceAttr("google_compute_address.internal_with_subnet_and_address", "address", "10.0.42.42"),
),
},
},
Expand Down Expand Up @@ -205,6 +208,12 @@ resource "google_compute_address" "foobar" {
}`, acctest.RandString(10))

var testAccComputeAddress_internal = fmt.Sprintf(`
resource "google_compute_address" "internal" {
name = "address-test-%s"
address_type = "INTERNAL"
region = "us-east1"
}
resource "google_compute_network" "default" {
name = "network-test-%s"
}
Expand All @@ -216,9 +225,25 @@ resource "google_compute_subnetwork" "foo" {
network = "${google_compute_network.default.self_link}"
}
resource "google_compute_address" "foobar" {
resource "google_compute_address" "internal_with_subnet" {
name = "address-test-%s"
subnetwork = "${google_compute_subnetwork.foo.self_link}"
address_type = "INTERNAL"
region = "us-east1"
}`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10))
}
// We can't test the address alone, because we don't know what IP range the
// default subnetwork uses.
resource "google_compute_address" "internal_with_subnet_and_address" {
name = "address-test-%s"
subnetwork = "${google_compute_subnetwork.foo.self_link}"
address_type = "INTERNAL"
address = "10.0.42.42"
region = "us-east1"
}`,
acctest.RandString(10), // google_compute_address.internal name
acctest.RandString(10), // google_compute_network.default name
acctest.RandString(10), // google_compute_subnetwork.foo name
acctest.RandString(10), // google_compute_address.internal_with_subnet_name
acctest.RandString(10), // google_compute_address.internal_with_subnet_and_address name
)
6 changes: 5 additions & 1 deletion website/docs/r/compute_address.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ The following arguments are supported:

* `subnetwork` - (Optional) The self link URI of the subnetwork in which to
create the address. A subnetwork may only be specified for INTERNAL
addresses.
address types.

* `address` - (Optional) The IP address to reserve. An address may only be
specified for INTERNAL address types. The IP address must be inside the
specified subnetwork, if any.

## Attributes Reference

Expand Down

0 comments on commit 5b7a769

Please sign in to comment.