-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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 subnetwork secondary ip ranges beta feature #310
Merged
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d6a1aec
Change subnetwork to a versioned resource
rosbo bcb3c20
Fix beta operation waiting
rosbo 1d9ff17
Add method to compare self link resource names
rosbo 5bb2ed0
Suppress diff for network name in subnetwork
rosbo 4b33f9a
Add documentation for beta secondary ip ranges for subnetwork
rosbo ece8cde
Add secondary ip range beta support to subnetwork resource
rosbo a095412
Remove duplication of error checking
rosbo da4f828
make fmt
rosbo b27758f
Add ForceNew to secondary_ip_range field
rosbo 0230844
Addressed Dana's comments
rosbo 6aad76d
Rename self link or name method comparison method
rosbo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,18 @@ 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, | ||
|
@@ -37,7 +46,7 @@ func resourceComputeSubnetwork() *schema.Resource { | |
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
DiffSuppressFunc: linkDiffSuppress, | ||
DiffSuppressFunc: compareSelfLinkResourceNames, | ||
}, | ||
|
||
"description": &schema.Schema{ | ||
|
@@ -68,6 +77,26 @@ func resourceComputeSubnetwork() *schema.Resource { | |
Optional: true, | ||
}, | ||
|
||
"secondary_ip_range": &schema.Schema{ | ||
Type: schema.TypeList, | ||
Optional: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"range_name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validateGCPName, | ||
}, | ||
"ip_cidr_range": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
"self_link": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
|
@@ -76,18 +105,8 @@ func resourceComputeSubnetwork() *schema.Resource { | |
} | ||
} | ||
|
||
func createSubnetID(s *compute.Subnetwork) string { | ||
return fmt.Sprintf("%s/%s", s.Region, s.Name) | ||
} | ||
|
||
func splitSubnetID(id string) (region string, name string) { | ||
parts := strings.Split(id, "/") | ||
region = parts[0] | ||
name = parts[1] | ||
return | ||
} | ||
|
||
func resourceComputeSubnetworkCreate(d *schema.ResourceData, meta interface{}) error { | ||
computeApiVersion := getComputeApiVersion(d, SubnetworkBaseApiVersion, SubnetworkVersionedFeatures) | ||
config := meta.(*Config) | ||
|
||
region, err := getRegion(d, config) | ||
|
@@ -106,17 +125,32 @@ func resourceComputeSubnetworkCreate(d *schema.ResourceData, meta interface{}) e | |
} | ||
|
||
// Build the subnetwork parameters | ||
subnetwork := &compute.Subnetwork{ | ||
subnetwork := &computeBeta.Subnetwork{ | ||
Name: d.Get("name").(string), | ||
Description: d.Get("description").(string), | ||
IpCidrRange: d.Get("ip_cidr_range").(string), | ||
PrivateIpGoogleAccess: d.Get("private_ip_google_access").(bool), | ||
SecondaryIpRanges: expandSecondaryRanges(d.Get("secondary_ip_range").([]interface{})), | ||
Network: network, | ||
} | ||
|
||
log.Printf("[DEBUG] Subnetwork insert request: %#v", subnetwork) | ||
op, err := config.clientCompute.Subnetworks.Insert( | ||
project, region, subnetwork).Do() | ||
|
||
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() | ||
} | ||
|
||
if err != nil { | ||
return fmt.Errorf("Error creating subnetwork: %s", err) | ||
|
@@ -128,9 +162,9 @@ 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(createSubnetID(subnetwork)) | ||
d.SetId(createBetaSubnetID(subnetwork)) | ||
|
||
err = computeOperationWait(config, op, project, "Creating Subnetwork") | ||
err = computeSharedOperationWait(config, op, project, "Creating Subnetwork") | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -139,6 +173,7 @@ 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) | ||
|
@@ -153,10 +188,25 @@ func resourceComputeSubnetworkRead(d *schema.ResourceData, meta interface{}) err | |
|
||
name := d.Get("name").(string) | ||
|
||
subnetwork, err := config.clientCompute.Subnetworks.Get( | ||
project, region, name).Do() | ||
if err != nil { | ||
return handleNotFoundError(err, d, fmt.Sprintf("Subnetwork %q", name)) | ||
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)) | ||
} | ||
} | ||
|
||
d.Set("name", subnetwork.Name) | ||
|
@@ -165,12 +215,14 @@ func resourceComputeSubnetworkRead(d *schema.ResourceData, meta interface{}) err | |
d.Set("description", subnetwork.Description) | ||
d.Set("private_ip_google_access", subnetwork.PrivateIpGoogleAccess) | ||
d.Set("gateway_address", subnetwork.GatewayAddress) | ||
d.Set("self_link", subnetwork.SelfLink) | ||
d.Set("secondary_ip_range", flattenSecondaryRanges(subnetwork.SecondaryIpRanges)) | ||
d.Set("self_link", ConvertSelfLinkToV1(subnetwork.SelfLink)) | ||
|
||
return nil | ||
} | ||
|
||
func resourceComputeSubnetworkUpdate(d *schema.ResourceData, meta interface{}) error { | ||
computeApiVersion := getComputeApiVersionUpdate(d, SubnetworkBaseApiVersion, SubnetworkVersionedFeatures, []Feature{}) | ||
config := meta.(*Config) | ||
|
||
region, err := getRegion(d, config) | ||
|
@@ -186,18 +238,34 @@ func resourceComputeSubnetworkUpdate(d *schema.ResourceData, meta interface{}) e | |
d.Partial(true) | ||
|
||
if d.HasChange("private_ip_google_access") { | ||
subnetworksSetPrivateIpGoogleAccessRequest := &compute.SubnetworksSetPrivateIpGoogleAccessRequest{ | ||
subnetworksSetPrivateIpGoogleAccessRequest := &computeBeta.SubnetworksSetPrivateIpGoogleAccessRequest{ | ||
PrivateIpGoogleAccess: d.Get("private_ip_google_access").(bool), | ||
} | ||
|
||
log.Printf("[DEBUG] Updating Subnetwork PrivateIpGoogleAccess %q: %#v", d.Id(), subnetworksSetPrivateIpGoogleAccessRequest) | ||
op, err := config.clientCompute.Subnetworks.SetPrivateIpGoogleAccess( | ||
project, region, d.Get("name").(string), subnetworksSetPrivateIpGoogleAccessRequest).Do() | ||
|
||
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() | ||
|
||
} | ||
|
||
if err != nil { | ||
return fmt.Errorf("Error updating subnetwork PrivateIpGoogleAccess: %s", err) | ||
} | ||
|
||
err = computeOperationWait(config, op, project, "Updating Subnetwork PrivateIpGoogleAccess") | ||
err = computeSharedOperationWait(config, op, project, "Updating Subnetwork PrivateIpGoogleAccess") | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -211,6 +279,7 @@ 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) | ||
|
@@ -224,13 +293,20 @@ func resourceComputeSubnetworkDelete(d *schema.ResourceData, meta interface{}) e | |
} | ||
|
||
// Delete the subnetwork | ||
op, err := config.clientCompute.Subnetworks.Delete( | ||
project, region, d.Get("name").(string)).Do() | ||
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() | ||
} | ||
if err != nil { | ||
return fmt.Errorf("Error deleting subnetwork: %s", err) | ||
} | ||
|
||
err = computeOperationWait(config, op, project, "Deleting Subnetwork") | ||
err = computeSharedOperationWait(config, op, project, "Deleting Subnetwork") | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -256,3 +332,41 @@ func resourceComputeSubnetworkImportState(d *schema.ResourceData, meta interface | |
|
||
return []*schema.ResourceData{d}, nil | ||
} | ||
|
||
func createBetaSubnetID(s *computeBeta.Subnetwork) string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This and There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved |
||
return fmt.Sprintf("%s/%s", s.Region, s.Name) | ||
} | ||
|
||
func splitSubnetID(id string) (region string, name string) { | ||
parts := strings.Split(id, "/") | ||
region = parts[0] | ||
name = parts[1] | ||
return | ||
} | ||
|
||
func expandSecondaryRanges(configured []interface{}) []*computeBeta.SubnetworkSecondaryRange { | ||
secondaryRanges := make([]*computeBeta.SubnetworkSecondaryRange, 0, len(configured)) | ||
for _, raw := range configured { | ||
data := raw.(map[string]interface{}) | ||
secondaryRange := computeBeta.SubnetworkSecondaryRange{ | ||
RangeName: data["range_name"].(string), | ||
IpCidrRange: data["ip_cidr_range"].(string), | ||
} | ||
|
||
secondaryRanges = append(secondaryRanges, &secondaryRange) | ||
} | ||
return secondaryRanges | ||
} | ||
|
||
func flattenSecondaryRanges(secondaryRanges []*computeBeta.SubnetworkSecondaryRange) []map[string]interface{} { | ||
secondaryRangesSchema := make([]map[string]interface{}, 0, len(secondaryRanges)) | ||
for _, secondaryRange := range secondaryRanges { | ||
data := map[string]interface{}{ | ||
"range_name": secondaryRange.RangeName, | ||
"ip_cidr_range": secondaryRange.IpCidrRange, | ||
} | ||
|
||
secondaryRangesSchema = append(secondaryRangesSchema, data) | ||
} | ||
return secondaryRangesSchema | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if it will ever make a difference, but this should probably be marked
ForceNew
as well as it's children.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done