Skip to content

Commit

Permalink
Remove alias ip range from beta feature for compute subnetwork
Browse files Browse the repository at this point in the history
  • Loading branch information
rosbo committed Sep 28, 2017
1 parent b5e1bf4 commit 39a8588
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 118 deletions.
98 changes: 15 additions & 83 deletions google/resource_compute_subnetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,9 @@ import (
"strings"

"github.com/hashicorp/terraform/helper/schema"
computeBeta "google.golang.org/api/compute/v0.beta"
"google.golang.org/api/compute/v1"
)

var SubnetworkBaseApiVersion = v1
var SubnetworkVersionedFeatures = []Feature{
{
Version: v0beta,
Item: "secondary_ip_range",
},
}

func resourceComputeSubnetwork() *schema.Resource {
return &schema.Resource{
Create: resourceComputeSubnetworkCreate,
Expand Down Expand Up @@ -107,7 +98,6 @@ func resourceComputeSubnetwork() *schema.Resource {
}

func resourceComputeSubnetworkCreate(d *schema.ResourceData, meta interface{}) error {
computeApiVersion := getComputeApiVersion(d, SubnetworkBaseApiVersion, SubnetworkVersionedFeatures)
config := meta.(*Config)

region, err := getRegion(d, config)
Expand All @@ -126,7 +116,7 @@ func resourceComputeSubnetworkCreate(d *schema.ResourceData, meta interface{}) e
}

// Build the subnetwork parameters
subnetwork := &computeBeta.Subnetwork{
subnetwork := &compute.Subnetwork{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
IpCidrRange: d.Get("ip_cidr_range").(string),
Expand All @@ -137,21 +127,7 @@ func resourceComputeSubnetworkCreate(d *schema.ResourceData, meta interface{}) e

log.Printf("[DEBUG] Subnetwork insert request: %#v", subnetwork)

var op interface{}
switch computeApiVersion {
case v1:
subnetworkV1 := &compute.Subnetwork{}
err = Convert(subnetwork, subnetworkV1)
if err != nil {
return err
}

op, err = config.clientCompute.Subnetworks.Insert(
project, region, subnetworkV1).Do()
case v0beta:
op, err = config.clientComputeBeta.Subnetworks.Insert(
project, region, subnetwork).Do()
}
op, err := config.clientCompute.Subnetworks.Insert(project, region, subnetwork).Do()

if err != nil {
return fmt.Errorf("Error creating subnetwork: %s", err)
Expand All @@ -163,7 +139,7 @@ func resourceComputeSubnetworkCreate(d *schema.ResourceData, meta interface{}) e
// The same name can appear twice in a project, as long as each one is in a different region."
// https://cloud.google.com/compute/docs/subnetworks
subnetwork.Region = region
d.SetId(createBetaSubnetID(subnetwork))
d.SetId(createSubnetID(subnetwork))

err = computeSharedOperationWait(config, op, project, "Creating Subnetwork")
if err != nil {
Expand All @@ -174,7 +150,6 @@ func resourceComputeSubnetworkCreate(d *schema.ResourceData, meta interface{}) e
}

func resourceComputeSubnetworkRead(d *schema.ResourceData, meta interface{}) error {
computeApiVersion := getComputeApiVersion(d, SubnetworkBaseApiVersion, SubnetworkVersionedFeatures)
config := meta.(*Config)

region, err := getRegion(d, config)
Expand All @@ -189,25 +164,9 @@ func resourceComputeSubnetworkRead(d *schema.ResourceData, meta interface{}) err

name := d.Get("name").(string)

subnetwork := &computeBeta.Subnetwork{}
switch computeApiVersion {
case v1:
subnetworkV1, err := config.clientCompute.Subnetworks.Get(
project, region, name).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Subnetwork %q", name))
}

err = Convert(subnetworkV1, subnetwork)
if err != nil {
return err
}
case v0beta:
var err error
subnetwork, err = config.clientComputeBeta.Subnetworks.Get(project, region, name).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Subnetwork %q", name))
}
subnetwork, err := config.clientCompute.Subnetworks.Get(project, region, name).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Subnetwork %q", name))
}

d.Set("name", subnetwork.Name)
Expand All @@ -223,7 +182,6 @@ func resourceComputeSubnetworkRead(d *schema.ResourceData, meta interface{}) err
}

func resourceComputeSubnetworkUpdate(d *schema.ResourceData, meta interface{}) error {
computeApiVersion := getComputeApiVersionUpdate(d, SubnetworkBaseApiVersion, SubnetworkVersionedFeatures, []Feature{})
config := meta.(*Config)

region, err := getRegion(d, config)
Expand All @@ -239,28 +197,14 @@ func resourceComputeSubnetworkUpdate(d *schema.ResourceData, meta interface{}) e
d.Partial(true)

if d.HasChange("private_ip_google_access") {
subnetworksSetPrivateIpGoogleAccessRequest := &computeBeta.SubnetworksSetPrivateIpGoogleAccessRequest{
subnetworksSetPrivateIpGoogleAccessRequest := &compute.SubnetworksSetPrivateIpGoogleAccessRequest{
PrivateIpGoogleAccess: d.Get("private_ip_google_access").(bool),
}

log.Printf("[DEBUG] Updating Subnetwork PrivateIpGoogleAccess %q: %#v", d.Id(), subnetworksSetPrivateIpGoogleAccessRequest)

var op interface{}
switch computeApiVersion {
case v1:
subnetworksSetPrivateIpGoogleAccessRequestV1 := &compute.SubnetworksSetPrivateIpGoogleAccessRequest{}
err = Convert(subnetworksSetPrivateIpGoogleAccessRequest, subnetworksSetPrivateIpGoogleAccessRequestV1)
if err != nil {
return err
}

op, err = config.clientCompute.Subnetworks.SetPrivateIpGoogleAccess(
project, region, d.Get("name").(string), subnetworksSetPrivateIpGoogleAccessRequestV1).Do()
case v0beta:
op, err = config.clientComputeBeta.Subnetworks.SetPrivateIpGoogleAccess(
project, region, d.Get("name").(string), subnetworksSetPrivateIpGoogleAccessRequest).Do()

}
op, err := config.clientCompute.Subnetworks.SetPrivateIpGoogleAccess(
project, region, d.Get("name").(string), subnetworksSetPrivateIpGoogleAccessRequest).Do()

if err != nil {
return fmt.Errorf("Error updating subnetwork PrivateIpGoogleAccess: %s", err)
Expand All @@ -280,7 +224,6 @@ func resourceComputeSubnetworkUpdate(d *schema.ResourceData, meta interface{}) e
}

func resourceComputeSubnetworkDelete(d *schema.ResourceData, meta interface{}) error {
computeApiVersion := getComputeApiVersion(d, SubnetworkBaseApiVersion, SubnetworkVersionedFeatures)
config := meta.(*Config)

region, err := getRegion(d, config)
Expand All @@ -294,15 +237,8 @@ func resourceComputeSubnetworkDelete(d *schema.ResourceData, meta interface{}) e
}

// Delete the subnetwork
var op interface{}
switch computeApiVersion {
case v1:
op, err = config.clientCompute.Subnetworks.Delete(
project, region, d.Get("name").(string)).Do()
case v0beta:
op, err = config.clientComputeBeta.Subnetworks.Delete(
project, region, d.Get("name").(string)).Do()
}
op, err := config.clientCompute.Subnetworks.Delete(
project, region, d.Get("name").(string)).Do()
if err != nil {
return fmt.Errorf("Error deleting subnetwork: %s", err)
}
Expand Down Expand Up @@ -334,10 +270,6 @@ func resourceComputeSubnetworkImportState(d *schema.ResourceData, meta interface
return []*schema.ResourceData{d}, nil
}

func createBetaSubnetID(s *computeBeta.Subnetwork) string {
return fmt.Sprintf("%s/%s", s.Region, s.Name)
}

func createSubnetID(s *compute.Subnetwork) string {
return fmt.Sprintf("%s/%s", s.Region, s.Name)
}
Expand All @@ -349,11 +281,11 @@ func splitSubnetID(id string) (region string, name string) {
return
}

func expandSecondaryRanges(configured []interface{}) []*computeBeta.SubnetworkSecondaryRange {
secondaryRanges := make([]*computeBeta.SubnetworkSecondaryRange, 0, len(configured))
func expandSecondaryRanges(configured []interface{}) []*compute.SubnetworkSecondaryRange {
secondaryRanges := make([]*compute.SubnetworkSecondaryRange, 0, len(configured))
for _, raw := range configured {
data := raw.(map[string]interface{})
secondaryRange := computeBeta.SubnetworkSecondaryRange{
secondaryRange := compute.SubnetworkSecondaryRange{
RangeName: data["range_name"].(string),
IpCidrRange: data["ip_cidr_range"].(string),
}
Expand All @@ -363,7 +295,7 @@ func expandSecondaryRanges(configured []interface{}) []*computeBeta.SubnetworkSe
return secondaryRanges
}

func flattenSecondaryRanges(secondaryRanges []*computeBeta.SubnetworkSecondaryRange) []map[string]interface{} {
func flattenSecondaryRanges(secondaryRanges []*compute.SubnetworkSecondaryRange) []map[string]interface{} {
secondaryRangesSchema := make([]map[string]interface{}, 0, len(secondaryRanges))
for _, secondaryRange := range secondaryRanges {
data := map[string]interface{}{
Expand Down
38 changes: 3 additions & 35 deletions google/resource_compute_subnetwork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"google.golang.org/api/compute/v1"

computeBeta "google.golang.org/api/compute/v0.beta"
)

func TestAccComputeSubnetwork_basic(t *testing.T) {
Expand Down Expand Up @@ -73,7 +71,7 @@ func TestAccComputeSubnetwork_update(t *testing.T) {
}

func TestAccComputeSubnetwork_secondaryIpRanges(t *testing.T) {
var subnetwork computeBeta.Subnetwork
var subnetwork compute.Subnetwork

cnName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
subnetworkName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
Expand All @@ -86,7 +84,7 @@ func TestAccComputeSubnetwork_secondaryIpRanges(t *testing.T) {
resource.TestStep{
Config: testAccComputeSubnetwork_secondaryIpRanges(cnName, subnetworkName),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeBetaSubnetworkExists("google_compute_subnetwork.network-with-private-secondary-ip-range", &subnetwork),
testAccCheckComputeSubnetworkExists("google_compute_subnetwork.network-with-private-secondary-ip-range", &subnetwork),
testAccCheckComputeSubnetworkHasSecondaryIpRange(&subnetwork, "tf-test-secondary-range", "192.168.1.0/24"),
),
},
Expand Down Expand Up @@ -143,37 +141,7 @@ func testAccCheckComputeSubnetworkExists(n string, subnetwork *compute.Subnetwor
}
}

func testAccCheckComputeBetaSubnetworkExists(n string, subnetwork *computeBeta.Subnetwork) 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)

region, subnet_name := splitSubnetID(rs.Primary.ID)
found, err := config.clientComputeBeta.Subnetworks.Get(
config.Project, region, subnet_name).Do()
if err != nil {
return err
}

if found.Name != subnet_name {
return fmt.Errorf("Subnetwork not found")
}

*subnetwork = *found

return nil
}
}

func testAccCheckComputeSubnetworkHasSecondaryIpRange(subnetwork *computeBeta.Subnetwork, rangeName, ipCidrRange string) resource.TestCheckFunc {
func testAccCheckComputeSubnetworkHasSecondaryIpRange(subnetwork *compute.Subnetwork, rangeName, ipCidrRange string) resource.TestCheckFunc {
return func(s *terraform.State) error {
for _, secondaryRange := range subnetwork.SecondaryIpRanges {
if secondaryRange.RangeName == rangeName {
Expand Down

0 comments on commit 39a8588

Please sign in to comment.