Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions params.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import (
"time"
)

type IPVersion int

const (
IPv4 IPVersion = 4
IPv6 IPVersion = 6
)

// EnabledState is a convenience type, mostly used in Create and Update
// operations. Because the zero value of a bool is FALSE, we need to use a
// pointer instead to indicate zero-ness.
Expand Down
33 changes: 1 addition & 32 deletions rackspace/compute/v2/servers/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,11 @@ import (
"github.com/rackspace/gophercloud/pagination"
)

// List makes a request against the API to list servers accessible to you.
func List(client *gophercloud.ServiceClient, opts os.ListOptsBuilder) pagination.Pager {
return os.List(client, opts)
}

// Create requests a server to be provisioned to the user in the current tenant.
func Create(client *gophercloud.ServiceClient, opts os.CreateOptsBuilder) os.CreateResult {
return os.Create(client, opts)
}

// Update requests an existing server to be updated with the supplied options.
func Update(client *gophercloud.ServiceClient, id string, opts os.UpdateOptsBuilder) os.UpdateResult {
return os.Update(client, id, opts)
}

// Delete requests that a server previously provisioned be removed from your account.
// Delete deletes an existing server instance.
func Delete(client *gophercloud.ServiceClient, id string) os.DeleteResult {
return os.Delete(client, id)
}

// Get requests details on a single server, by ID.
func Get(client *gophercloud.ServiceClient, id string) os.GetResult {
return os.Get(client, id)
}

// ChangeAdminPassword alters the administrator or root password for a specified server.
func ChangeAdminPassword(client *gophercloud.ServiceClient, id, newPassword string) os.ActionResult {
return os.ChangeAdminPassword(client, id, newPassword)
Expand All @@ -48,12 +28,6 @@ func Reboot(client *gophercloud.ServiceClient, id string, how os.RebootMethod) o
return os.Reboot(client, id, how)
}

// Rebuild will reprovision the server according to the configuration options provided in the
// RebuildOpts struct.
func Rebuild(client *gophercloud.ServiceClient, id string, opts os.RebuildOptsBuilder) os.RebuildResult {
return os.Rebuild(client, id, opts)
}

// Resize instructs the provider to change the flavor of the server.
// Note that this implies rebuilding it.
// Unfortunately, one cannot pass rebuild parameters to the resize function.
Expand All @@ -77,11 +51,6 @@ func WaitForStatus(c *gophercloud.ServiceClient, id, status string, secs int) er
return os.WaitForStatus(c, id, status, secs)
}

// ExtractServers interprets the results of a single page from a List() call, producing a slice of Server entities.
func ExtractServers(page pagination.Page) ([]os.Server, error) {
return os.ExtractServers(page)
}

// ListAddresses makes a request against the API to list the servers IP addresses.
func ListAddresses(client *gophercloud.ServiceClient, id string) pagination.Pager {
return os.ListAddresses(client, id)
Expand Down
16 changes: 6 additions & 10 deletions rackspace/compute/v2/servers/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

package servers

import (
os "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
)

// ListOutput is the recorded output of a Rackspace servers.List request.
const ListOutput = `
{
Expand Down Expand Up @@ -308,7 +304,7 @@ const CreateOutput = `
`

// DevstackServer is the expected first result from parsing ListOutput.
var DevstackServer = os.Server{
var DevstackServer = Server{
ID: "59818cee-bc8c-44eb-8073-673ee65105f7",
Name: "devstack",
TenantID: "111111",
Expand Down Expand Up @@ -372,7 +368,7 @@ var DevstackServer = os.Server{
}

// PerilServer is the expected second result from parsing ListOutput.
var PerilServer = os.Server{
var PerilServer = Server{
ID: "25f1c7f5-e00a-4715-b354-16e24b2f4630",
Name: "peril-dfw",
TenantID: "111111",
Expand Down Expand Up @@ -436,7 +432,7 @@ var PerilServer = os.Server{
}

// GophercloudServer is the expected result from parsing GetOutput.
var GophercloudServer = os.Server{
var GophercloudServer = Server{
ID: "8c65cb68-0681-4c30-bc88-6b83a8a26aee",
Name: "Gophercloud-pxpGGuey",
TenantID: "111111",
Expand Down Expand Up @@ -500,7 +496,7 @@ var GophercloudServer = os.Server{
}

// GophercloudUpdatedServer is the expected result from parsing UpdateOutput.
var GophercloudUpdatedServer = os.Server{
var GophercloudUpdatedServer = Server{
ID: "8c65cb68-0681-4c30-bc88-6b83a8a26aee",
Name: "test-server-updated",
TenantID: "111111",
Expand Down Expand Up @@ -564,11 +560,11 @@ var GophercloudUpdatedServer = os.Server{
}

// CreatedServer is the partial Server struct that can be parsed from CreateOutput.
var CreatedServer = os.Server{
var CreatedServer = Server{
ID: "bb63327b-6a2f-34bc-b0ef-4b6d97ea637e",
AdminPass: "v7tADqbE5pr9",
Links: []interface{}{},
}

// ExpectedServerSlice is the collection of servers, in order, that should be parsed from ListOutput.
var ExpectedServerSlice = []os.Server{DevstackServer, PerilServer}
var ExpectedServerSlice = []Server{DevstackServer, PerilServer}
111 changes: 111 additions & 0 deletions rackspace/compute/v2/servers/requests.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package servers

import (
"errors"
"fmt"

"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/bootfromvolume"
"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/diskconfig"
"github.com/rackspace/gophercloud/openstack/compute/v2/flavors"
"github.com/rackspace/gophercloud/openstack/compute/v2/images"
os "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
"github.com/rackspace/gophercloud/pagination"
)

// CreateOpts specifies all of the options that Rackspace accepts in its Create request, including
Expand Down Expand Up @@ -124,6 +131,90 @@ func (opts CreateOpts) ToServerCreateMap() (map[string]interface{}, error) {
return res, nil
}

// Create requests a server to be provisioned to the user in the current tenant.
func Create(client *gophercloud.ServiceClient, opts os.CreateOptsBuilder) CreateResult {
var res CreateResult

reqBody, err := opts.ToServerCreateMap()
if err != nil {
res.Err = err
return res
}

// If ImageRef isn't provided, use ImageName to ascertain the image ID.
if reqBody["server"].(map[string]interface{})["imageRef"].(string) == "" {
imageName := reqBody["server"].(map[string]interface{})["imageName"].(string)
if imageName == "" {
res.Err = errors.New("One and only one of ImageRef and ImageName must be provided.")
return res
}
imageID, err := images.IDFromName(client, imageName)
if err != nil {
res.Err = err
return res
}
reqBody["server"].(map[string]interface{})["imageRef"] = imageID
}
delete(reqBody["server"].(map[string]interface{}), "imageName")

// If FlavorRef isn't provided, use FlavorName to ascertain the flavor ID.
if reqBody["server"].(map[string]interface{})["flavorRef"].(string) == "" {
flavorName := reqBody["server"].(map[string]interface{})["flavorName"].(string)
if flavorName == "" {
res.Err = errors.New("One and only one of FlavorRef and FlavorName must be provided.")
return res
}
flavorID, err := flavors.IDFromName(client, flavorName)
if err != nil {
res.Err = err
return res
}
reqBody["server"].(map[string]interface{})["flavorRef"] = flavorID
}
delete(reqBody["server"].(map[string]interface{}), "flavorName")

_, res.Err = client.Post(createURL(client), reqBody, &res.Body, nil)
return res
}

// List makes a request against the API to list servers accessible to you.
func List(client *gophercloud.ServiceClient, opts os.ListOptsBuilder) pagination.Pager {
url := listDetailURL(client)

if opts != nil {
query, err := opts.ToServerListQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}

createPageFn := func(r pagination.PageResult) pagination.Page {
return ServerPage{pagination.LinkedPageBase{PageResult: r}}
}

return pagination.NewPager(client, url, createPageFn)
}

// Update requests an existing server to be updated with the supplied options.
func Update(client *gophercloud.ServiceClient, id string, opts os.UpdateOptsBuilder) UpdateResult {
var result UpdateResult
reqBody := opts.ToServerUpdateMap()
_, result.Err = client.Put(updateURL(client, id), reqBody, &result.Body, &gophercloud.RequestOpts{
OkCodes: []int{200},
})
return result
}

// Get requests details on a single server, by ID.
func Get(client *gophercloud.ServiceClient, id string) GetResult {
var result GetResult
_, result.Err = client.Get(getURL(client, id), &result.Body, &gophercloud.RequestOpts{
OkCodes: []int{200, 203},
})
return result
}

// RebuildOpts represents all of the configuration options used in a server rebuild operation that
// are supported by Rackspace.
type RebuildOpts struct {
Expand Down Expand Up @@ -176,3 +267,23 @@ func (opts RebuildOpts) ToServerRebuildMap() (map[string]interface{}, error) {

return drive.ToServerRebuildMap()
}

// Rebuild will reprovision the server according to the configuration options provided in the
// RebuildOpts struct.
func Rebuild(client *gophercloud.ServiceClient, id string, opts os.RebuildOptsBuilder) RebuildResult {
var result RebuildResult

if id == "" {
result.Err = fmt.Errorf("ID is required")
return result
}

reqBody, err := opts.ToServerRebuildMap()
if err != nil {
result.Err = err
return result
}

_, result.Err = client.Post(actionURL(client, id), reqBody, &result.Body, nil)
return result
}
Loading