-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
provider/openstack: openstack_networking_port_v2 resource #3731
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
283 changes: 283 additions & 0 deletions
283
builtin/providers/openstack/resource_openstack_networking_port_v2.go
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 |
---|---|---|
@@ -0,0 +1,283 @@ | ||
package openstack | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform/helper/hashcode" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
|
||
"github.com/rackspace/gophercloud" | ||
"github.com/rackspace/gophercloud/openstack/networking/v2/ports" | ||
) | ||
|
||
func resourceNetworkingPortV2() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceNetworkingPortV2Create, | ||
Read: resourceNetworkingPortV2Read, | ||
Update: resourceNetworkingPortV2Update, | ||
Delete: resourceNetworkingPortV2Delete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"region": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
DefaultFunc: envDefaultFuncAllowMissing("OS_REGION_NAME"), | ||
}, | ||
"name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: false, | ||
}, | ||
"network_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"admin_state_up": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: false, | ||
Computed: true, | ||
}, | ||
"mac_address": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
Computed: true, | ||
}, | ||
"tenant_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
Computed: true, | ||
}, | ||
"device_owner": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
Computed: true, | ||
}, | ||
"security_groups": &schema.Schema{ | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
ForceNew: false, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Set: func(v interface{}) int { | ||
return hashcode.String(v.(string)) | ||
}, | ||
}, | ||
"device_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceNetworkingPortV2Create(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
networkingClient, err := config.networkingV2Client(d.Get("region").(string)) | ||
if err != nil { | ||
return fmt.Errorf("Error creating OpenStack networking client: %s", err) | ||
} | ||
|
||
createOpts := ports.CreateOpts{ | ||
Name: d.Get("name").(string), | ||
AdminStateUp: resourcePortAdminStateUpV2(d), | ||
NetworkID: d.Get("network_id").(string), | ||
MACAddress: d.Get("mac_address").(string), | ||
TenantID: d.Get("tenant_id").(string), | ||
DeviceOwner: d.Get("device_owner").(string), | ||
SecurityGroups: resourcePortSecurityGroupsV2(d), | ||
DeviceID: d.Get("device_id").(string), | ||
} | ||
|
||
log.Printf("[DEBUG] Create Options: %#v", createOpts) | ||
p, err := ports.Create(networkingClient, createOpts).Extract() | ||
if err != nil { | ||
return fmt.Errorf("Error creating OpenStack Neutron network: %s", err) | ||
} | ||
log.Printf("[INFO] Network ID: %s", p.ID) | ||
|
||
log.Printf("[DEBUG] Waiting for OpenStack Neutron Port (%s) to become available.", p.ID) | ||
|
||
stateConf := &resource.StateChangeConf{ | ||
Target: "ACTIVE", | ||
Refresh: waitForNetworkPortActive(networkingClient, p.ID), | ||
Timeout: 2 * time.Minute, | ||
Delay: 5 * time.Second, | ||
MinTimeout: 3 * time.Second, | ||
} | ||
|
||
_, err = stateConf.WaitForState() | ||
|
||
d.SetId(p.ID) | ||
|
||
return resourceNetworkingPortV2Read(d, meta) | ||
} | ||
|
||
func resourceNetworkingPortV2Read(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
networkingClient, err := config.networkingV2Client(d.Get("region").(string)) | ||
if err != nil { | ||
return fmt.Errorf("Error creating OpenStack networking client: %s", err) | ||
} | ||
|
||
p, err := ports.Get(networkingClient, d.Id()).Extract() | ||
if err != nil { | ||
return CheckDeleted(d, err, "port") | ||
} | ||
|
||
log.Printf("[DEBUG] Retreived Port %s: %+v", d.Id(), p) | ||
|
||
d.Set("name", p.Name) | ||
d.Set("admin_state_up", strconv.FormatBool(p.AdminStateUp)) | ||
d.Set("network_id", p.NetworkID) | ||
d.Set("mac_address", p.MACAddress) | ||
d.Set("tenant_id", p.TenantID) | ||
d.Set("device_owner", p.DeviceOwner) | ||
d.Set("security_groups", p.SecurityGroups) | ||
d.Set("device_id", p.DeviceID) | ||
|
||
return nil | ||
} | ||
|
||
func resourceNetworkingPortV2Update(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
networkingClient, err := config.networkingV2Client(d.Get("region").(string)) | ||
if err != nil { | ||
return fmt.Errorf("Error creating OpenStack networking client: %s", err) | ||
} | ||
|
||
var updateOpts ports.UpdateOpts | ||
|
||
if d.HasChange("name") { | ||
updateOpts.Name = d.Get("name").(string) | ||
} | ||
|
||
if d.HasChange("admin_state_up") { | ||
updateOpts.AdminStateUp = resourcePortAdminStateUpV2(d) | ||
} | ||
|
||
if d.HasChange("device_owner") { | ||
updateOpts.DeviceOwner = d.Get("device_owner").(string) | ||
} | ||
|
||
if d.HasChange("security_groups") { | ||
updateOpts.SecurityGroups = resourcePortSecurityGroupsV2(d) | ||
} | ||
|
||
if d.HasChange("device_id") { | ||
updateOpts.DeviceID = d.Get("device_id").(string) | ||
} | ||
|
||
log.Printf("[DEBUG] Updating Port %s with options: %+v", d.Id(), updateOpts) | ||
|
||
_, err = ports.Update(networkingClient, d.Id(), updateOpts).Extract() | ||
if err != nil { | ||
return fmt.Errorf("Error updating OpenStack Neutron Network: %s", err) | ||
} | ||
|
||
return resourceNetworkingPortV2Read(d, meta) | ||
} | ||
|
||
func resourceNetworkingPortV2Delete(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
networkingClient, err := config.networkingV2Client(d.Get("region").(string)) | ||
if err != nil { | ||
return fmt.Errorf("Error creating OpenStack networking client: %s", err) | ||
} | ||
|
||
stateConf := &resource.StateChangeConf{ | ||
Pending: []string{"ACTIVE"}, | ||
Target: "DELETED", | ||
Refresh: waitForNetworkPortDelete(networkingClient, d.Id()), | ||
Timeout: 2 * time.Minute, | ||
Delay: 5 * time.Second, | ||
MinTimeout: 3 * time.Second, | ||
} | ||
|
||
_, err = stateConf.WaitForState() | ||
if err != nil { | ||
return fmt.Errorf("Error deleting OpenStack Neutron Network: %s", err) | ||
} | ||
|
||
d.SetId("") | ||
return nil | ||
} | ||
|
||
func resourcePortSecurityGroupsV2(d *schema.ResourceData) []string { | ||
rawSecurityGroups := d.Get("security_groups").(*schema.Set) | ||
groups := make([]string, rawSecurityGroups.Len()) | ||
for i, raw := range rawSecurityGroups.List() { | ||
groups[i] = raw.(string) | ||
} | ||
return groups | ||
} | ||
|
||
func resourcePortAdminStateUpV2(d *schema.ResourceData) *bool { | ||
value := false | ||
|
||
if raw, ok := d.GetOk("admin_state_up"); ok && raw == "true" { | ||
value = true | ||
} | ||
|
||
return &value | ||
} | ||
|
||
func waitForNetworkPortActive(networkingClient *gophercloud.ServiceClient, portId string) resource.StateRefreshFunc { | ||
return func() (interface{}, string, error) { | ||
p, err := ports.Get(networkingClient, portId).Extract() | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
|
||
log.Printf("[DEBUG] OpenStack Neutron Port: %+v", p) | ||
if p.Status == "DOWN" || p.Status == "ACTIVE" { | ||
return p, "ACTIVE", nil | ||
} | ||
|
||
return p, p.Status, nil | ||
} | ||
} | ||
|
||
func waitForNetworkPortDelete(networkingClient *gophercloud.ServiceClient, portId string) resource.StateRefreshFunc { | ||
return func() (interface{}, string, error) { | ||
log.Printf("[DEBUG] Attempting to delete OpenStack Neutron Port %s", portId) | ||
|
||
p, err := ports.Get(networkingClient, portId).Extract() | ||
if err != nil { | ||
errCode, ok := err.(*gophercloud.UnexpectedResponseCodeError) | ||
if !ok { | ||
return p, "ACTIVE", err | ||
} | ||
if errCode.Actual == 404 { | ||
log.Printf("[DEBUG] Successfully deleted OpenStack Port %s", portId) | ||
return p, "DELETED", nil | ||
} | ||
} | ||
|
||
err = ports.Delete(networkingClient, portId).ExtractErr() | ||
if err != nil { | ||
errCode, ok := err.(*gophercloud.UnexpectedResponseCodeError) | ||
if !ok { | ||
return p, "ACTIVE", err | ||
} | ||
if errCode.Actual == 404 { | ||
log.Printf("[DEBUG] Successfully deleted OpenStack Port %s", portId) | ||
return p, "DELETED", nil | ||
} | ||
} | ||
|
||
log.Printf("[DEBUG] OpenStack Port %s still active.\n", portId) | ||
return p, "ACTIVE", nil | ||
} | ||
} |
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.
Would it make sense to also allow none-string
true
? I was surprised that it doesn't work with boolean valueThere 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.
admin_state_up
is defined as a String in the port resource, but boolean in all other network resources. Fixing this.