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

[CloudStack] Adding missing project ID on the get network API call, if present #6069

Closed
wants to merge 1 commit into from
Closed
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
33 changes: 28 additions & 5 deletions builtin/providers/cloudstack/resource_cloudstack_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,18 +185,15 @@ func resourceCloudStackNetworkCreate(d *schema.ResourceData, meta interface{}) e
}

func resourceCloudStackNetworkRead(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
n, count, err := getNetwork(d, meta.(*cloudstack.CloudStackClient))

// Get the virtual machine details
n, count, err := cs.Network.GetNetworkByID(d.Id())
if err != nil {
if count == 0 {
log.Printf(
"[DEBUG] Network %s does no longer exist", d.Get("name").(string))
"[DEBUG] Network %s no longer exists", d.Get("name").(string))
d.SetId("")
return nil
}

return err
}

Expand All @@ -219,6 +216,32 @@ func resourceCloudStackNetworkRead(d *schema.ResourceData, meta interface{}) err
return nil
}

func getNetwork(d *schema.ResourceData, cs *cloudstack.CloudStackClient) (*cloudstack.Network, int, error) {
// If there is a project supplied, we retrieve and set the project id
if project, ok := d.GetOk("project"); ok {
// Retrieve the project ID
projectid, e := retrieveID(cs, "project", project.(string))
if e != nil {
return nil, 0, e.Error()
}

params := cs.Network.NewListNetworksParams()
params.SetId(d.Id())
params.SetProjectid(projectid)

response, err := cs.Network.ListNetworks(params)
if err != nil {
return nil, 0, fmt.Errorf("Error listing network %s: %s", d.Id(), err)
}
if response.Count != 1 {
return nil, response.Count, fmt.Errorf("Error listing network %s: Got %d networks", d.Id(), response.Count)
}
return response.Networks[0], response.Count, nil
} else {
return cs.Network.GetNetworkByID(d.Id())
}
}

func resourceCloudStackNetworkUpdate(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
name := d.Get("name").(string)
Expand Down