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

Add ipVersion field to support IPv6 in global address and release to Terraform #110

Merged
merged 7 commits into from
Apr 27, 2018
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
2 changes: 1 addition & 1 deletion build/terraform
19 changes: 17 additions & 2 deletions products/compute/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -901,9 +901,17 @@ objects:
name: 'GlobalAddress'
kind: 'compute#address'
base_url: projects/{{project}}/global/addresses
exports:
- !ruby/object:Api::Type::SelfLink
name: 'selfLink'
description: |
Represents a Global Address resource. Global addresses are used for
HTTP(S) load balancing.
references: !ruby/object:Api::Resource::ReferenceLinks
guides:
'Reserving a Static External IP Address':
'https://cloud.google.com/compute/docs/ip-addresses/reserve-static-external-ip-address'
api: 'https://cloud.google.com/compute/docs/reference/latest/globalAddresses'
input: true
<%= indent(compile_file({}, 'templates/global_async.yaml.erb'), 4) %>
properties:
Expand Down Expand Up @@ -938,13 +946,20 @@ objects:
characters must be a dash, lowercase letter, or digit, except the last
character, which cannot be a dash.
required: true
- !ruby/object:Api::Type::Enum
name: 'ipVersion'
description: |
The IP Version that will be used by this address. Valid options are
IPV4 or IPV6. The default value is IPV4.
values:
- :IPV4
- :IPV6
- !ruby/object:Api::Type::ResourceRef
name: 'region'
resource: 'Region'
imports: 'selfLink'
description: |
A reference to the region where the regional address resides. This
field is not applicable to global addresses.
A reference to the region where the regional address resides.
output: true
# status is not useful for state convergence
# users[] is not useful for state convergence
Expand Down
11 changes: 10 additions & 1 deletion products/compute/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,17 @@ overrides: !ruby/object:Provider::ResourceOverrides
portRange: !ruby/object:Provider::Terraform::PropertyOverride
diff_suppress_func: 'portRangeDiffSuppress'
GlobalAddress: !ruby/object:Provider::Terraform::ResourceOverride
examples: |
```hcl
resource "google_compute_global_address" "default" {
name = "global-appserver-ip"
}
```
properties:
id: !ruby/object:Provider::Terraform::PropertyOverride
exclude: true
region: !ruby/object:Provider::Terraform::PropertyOverride
exclude: true
exclude: true
GlobalForwardingRule: !ruby/object:Provider::Terraform::ResourceOverride
exclude: true
HttpHealthCheck: !ruby/object:Provider::Terraform::ResourceOverride
Expand Down Expand Up @@ -389,6 +394,10 @@ files: !ruby/object:Provider::Config::Files
'google/transport_test.go': 'templates/terraform/transport_test.go'
'google/import.go': 'templates/terraform/import.go'
'google/import_test.go': 'templates/terraform/import_test.go'
# Handwritten acceptance tests for autogenerated resources.
# Adding them here allows updating the tests as part of a MM pull request.
'google/resource_compute_global_address_test.go':
'templates/terraform/tests/resource_compute_global_address_test.go'
# These files have templating (ERB) code that will be run.
# This is usually to add licensing info, autogeneration notices, etc.
compile:
Expand Down
7 changes: 7 additions & 0 deletions templates/terraform/schema_property.erb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
ValidateFunc: <%= property.validation.function -%>,
<% end # property.validation.nil? -%>
<% end # property.validation.nil? -%>
<% if property.is_a?(Api::Type::Enum) && property.validation.nil? -%>
<%
enum_values = property.values
enum_values.push "" unless property.required
-%>
ValidateFunc: validation.StringInSlice([]string{"<%= enum_values.join '","' -%>"}, false),
<% end -%>
<% if !property.diff_suppress_func.nil? -%>
DiffSuppressFunc: <%= property.diff_suppress_func %>,
<% elsif property.is_a?(Api::Type::ResourceRef) -%>
Expand Down
158 changes: 158 additions & 0 deletions templates/terraform/tests/resource_compute_global_address_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package google

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"

"google.golang.org/api/compute/v1"
)

func TestAccComputeGlobalAddress_basic(t *testing.T) {
t.Parallel()

var addr compute.Address

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeGlobalAddressDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeGlobalAddress_basic(),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeGlobalAddressExists(
"google_compute_global_address.foobar", &addr),

// implicitly IPV4 - if we don't send an ip_version, we don't get one back.
testAccCheckComputeGlobalAddressIpVersion("google_compute_global_address.foobar", ""),
),
},
resource.TestStep{
ResourceName: "google_compute_global_address.foobar",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccComputeGlobalAddress_ipv6(t *testing.T) {
t.Parallel()

var addr compute.Address

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeGlobalAddressDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeGlobalAddress_ipv6(),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeGlobalAddressExists(
"google_compute_global_address.foobar", &addr),
testAccCheckComputeGlobalAddressIpVersion("google_compute_global_address.foobar", "IPV6"),
),
},
resource.TestStep{
ResourceName: "google_compute_global_address.foobar",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckComputeGlobalAddressDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)

for _, rs := range s.RootModule().Resources {
if rs.Type != "google_compute_global_address" {
continue
}

_, err := config.clientCompute.GlobalAddresses.Get(
config.Project, rs.Primary.ID).Do()
if err == nil {
return fmt.Errorf("Address still exists")
}
}

return nil
}

func testAccCheckComputeGlobalAddressExists(n string, addr *compute.Address) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No ID is set")
}

config := testAccProvider.Meta().(*Config)

found, err := config.clientCompute.GlobalAddresses.Get(
config.Project, rs.Primary.ID).Do()
if err != nil {
return err
}

if found.Name != rs.Primary.ID {
return fmt.Errorf("Addr not found")
}

*addr = *found

return nil
}
}

func testAccCheckComputeGlobalAddressIpVersion(n, version string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No ID is set")
}

config := testAccProvider.Meta().(*Config)

addr, err := config.clientCompute.GlobalAddresses.Get(config.Project, rs.Primary.ID).Do()
if err != nil {
return err
}

if addr.IpVersion != version {
return fmt.Errorf("Expected IP version to be %s, got %s", version, addr.IpVersion)
}

return nil
}
}

func testAccComputeGlobalAddress_basic() string {
return fmt.Sprintf(`
resource "google_compute_global_address" "foobar" {
name = "address-test-%s"
description = "Created for Terraform acceptance testing"
}`, acctest.RandString(10))
}

func testAccComputeGlobalAddress_ipv6() string {
return fmt.Sprintf(`
resource "google_compute_global_address" "foobar" {
name = "address-test-%s"
description = "Created for Terraform acceptance testing"
ip_version = "IPV6"
}`, acctest.RandString(10))
}