forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Fix for Bugs #8227, #11226 - subnet info lost on vNet update #2
Closed
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
85ed0f6
Merge branch 'master' of https://github.com/hashicorp/terraform
echuvyrov be80f8a
Merge branch 'master' of https://github.com/harijayms/terraform
echuvyrov 772fac7
Changes to vNet and subnet files to allow subnet updates without losi…
echuvyrov 0810627
Minor changes to vNet properties retrieval
echuvyrov 0787abd
Merge branch 'master' into bug8227
echuvyrov 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,6 +61,10 @@ func resourceArmVirtualNetwork() *schema.Resource { | |
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"route_table": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
}, | ||
}, | ||
Set: resourceAzureSubnetHash, | ||
|
@@ -93,7 +97,7 @@ func resourceArmVirtualNetworkCreate(d *schema.ResourceData, meta interface{}) e | |
vnet := network.VirtualNetwork{ | ||
Name: &name, | ||
Location: &location, | ||
VirtualNetworkPropertiesFormat: getVirtualNetworkProperties(d), | ||
VirtualNetworkPropertiesFormat: getVirtualNetworkProperties(d, meta), | ||
Tags: expandTags(tags), | ||
} | ||
|
||
|
@@ -187,7 +191,7 @@ func resourceArmVirtualNetworkDelete(d *schema.ResourceData, meta interface{}) e | |
return err | ||
} | ||
|
||
func getVirtualNetworkProperties(d *schema.ResourceData) *network.VirtualNetworkPropertiesFormat { | ||
func getVirtualNetworkProperties(d *schema.ResourceData, meta interface{}) *network.VirtualNetworkPropertiesFormat { | ||
// first; get address space prefixes: | ||
prefixes := []string{} | ||
for _, prefix := range d.Get("address_space").([]interface{}) { | ||
|
@@ -207,20 +211,41 @@ func getVirtualNetworkProperties(d *schema.ResourceData) *network.VirtualNetwork | |
subnet := subnet.(map[string]interface{}) | ||
|
||
name := subnet["name"].(string) | ||
log.Printf("[INFO] setting subnets inside vNet, processing %q", name) | ||
//since subnets can also be created outside of vNet definition (as root objects) | ||
// do a GET on subnet properties from the server before setting them | ||
resGroup := d.Get("resource_group_name").(string) | ||
vnetName := d.Get("name").(string) | ||
subnetObj := getExistingSubnet(resGroup, vnetName, name, meta) | ||
log.Printf("[INFO] Completer GET of Subnet props ") | ||
|
||
prefix := subnet["address_prefix"].(string) | ||
secGroup := subnet["security_group"].(string) | ||
routeTable := subnet["route_table"].(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. route_table and security_group are optional, you need error checking. |
||
|
||
var subnetObj network.Subnet | ||
subnetObj.Name = &name | ||
subnetObj.SubnetPropertiesFormat = &network.SubnetPropertiesFormat{} | ||
subnetObj.SubnetPropertiesFormat.AddressPrefix = &prefix | ||
if name != "" { | ||
log.Printf("[INFO] setting SUBNETNAME TO %q", name) | ||
subnetObj.Name = &name | ||
} | ||
if subnetObj.SubnetPropertiesFormat == nil { | ||
subnetObj.SubnetPropertiesFormat = &network.SubnetPropertiesFormat{} | ||
} | ||
if subnetObj.SubnetPropertiesFormat.AddressPrefix == nil { | ||
subnetObj.SubnetPropertiesFormat.AddressPrefix = &prefix | ||
} | ||
|
||
if secGroup != "" { | ||
subnetObj.SubnetPropertiesFormat.NetworkSecurityGroup = &network.SecurityGroup{ | ||
ID: &secGroup, | ||
} | ||
} | ||
|
||
if routeTable != "" { | ||
subnetObj.SubnetPropertiesFormat.RouteTable = &network.RouteTable{ | ||
ID: &routeTable, | ||
} | ||
} | ||
|
||
subnets = append(subnets, subnetObj) | ||
} | ||
} | ||
|
@@ -245,3 +270,38 @@ func resourceAzureSubnetHash(v interface{}) int { | |
} | ||
return hashcode.String(subnet) | ||
} | ||
|
||
func getExistingSubnet(resGroup string, vnetName string, subnetName string, meta interface{}) network.Subnet { | ||
//attempt to retrieve existing subnet from the server | ||
existingSubnet := network.Subnet{} | ||
subnetClient := meta.(*ArmClient).subnetClient | ||
resp, err := subnetClient.Get(resGroup, vnetName, subnetName, "") | ||
|
||
if err != nil { | ||
if resp.StatusCode == http.StatusNotFound { | ||
return existingSubnet | ||
} | ||
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. Should we be doing something with other errors? |
||
} | ||
|
||
existingSubnet.SubnetPropertiesFormat = &network.SubnetPropertiesFormat{} | ||
existingSubnet.SubnetPropertiesFormat.AddressPrefix = resp.SubnetPropertiesFormat.AddressPrefix | ||
|
||
if resp.SubnetPropertiesFormat.NetworkSecurityGroup != nil { | ||
existingSubnet.SubnetPropertiesFormat.NetworkSecurityGroup = resp.SubnetPropertiesFormat.NetworkSecurityGroup | ||
} | ||
|
||
if resp.SubnetPropertiesFormat.RouteTable != nil { | ||
existingSubnet.SubnetPropertiesFormat.RouteTable = resp.SubnetPropertiesFormat.RouteTable | ||
} | ||
|
||
if resp.SubnetPropertiesFormat.IPConfigurations != nil { | ||
ips := make([]string, 0, len(*resp.SubnetPropertiesFormat.IPConfigurations)) | ||
for _, ip := range *resp.SubnetPropertiesFormat.IPConfigurations { | ||
ips = append(ips, *ip.ID) | ||
} | ||
|
||
existingSubnet.SubnetPropertiesFormat.IPConfigurations = resp.SubnetPropertiesFormat.IPConfigurations | ||
} | ||
|
||
return existingSubnet | ||
} |
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.
Completed? :-)