Skip to content

Commit

Permalink
Use pointers for return data types of nova and glance apis so that ni…
Browse files Browse the repository at this point in the history
…l can be returned if there is an error
  • Loading branch information
wallyworld committed Dec 2, 2012
1 parent c921999 commit d2a27b4
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 53 deletions.
11 changes: 7 additions & 4 deletions glance/glance.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,27 @@ type ImageDetail struct {
}

// ListImageDetails lists all details for available images.
func (c *Client) ListImagesDetail() ([]ImageDetail, error) {
func (c *Client) ListImagesDetail() (*[]ImageDetail, error) {
var resp struct {
Images []ImageDetail
}
requestData := goosehttp.RequestData{RespValue: &resp}
err := c.client.SendRequest(client.GET, "compute", apiImagesDetail, &requestData,
"failed to get list of images details")
return resp.Images, err
if err != nil {
return nil, err
}
return &resp.Images, nil
}

// GetImageDetail lists details of the specified image.
func (c *Client) GetImageDetail(imageId string) (ImageDetail, error) {
func (c *Client) GetImageDetail(imageId string) (*ImageDetail, error) {
var resp struct {
Image ImageDetail
}
url := fmt.Sprintf("%s/%s", apiImages, imageId)
requestData := goosehttp.RequestData{RespValue: &resp}
err := c.client.SendRequest(client.GET, "compute", url, &requestData,
"failed to get details for imageId=%s", imageId)
return resp.Image, err
return &resp.Image, err
}
6 changes: 3 additions & 3 deletions glance/glance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func (s *GlanceSuite) TestListImages(c *C) {
func (s *GlanceSuite) TestListImagesDetail(c *C) {
images, err := s.glance.ListImagesDetail()
c.Assert(err, IsNil)
c.Assert(images, Not(HasLen), 0)
for _, ir := range images {
c.Assert(*images, Not(HasLen), 0)
for _, ir := range *images {
c.Assert(ir.Created, Matches, `\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.*`)
c.Assert(ir.Updated, Matches, `\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.*`)
c.Assert(ir.Id, Not(Equals), "")
Expand All @@ -68,7 +68,7 @@ func (s *GlanceSuite) TestListImagesDetail(c *C) {
func (s *GlanceSuite) TestGetImageDetail(c *C) {
images, err := s.glance.ListImagesDetail()
c.Assert(err, IsNil)
firstImage := images[0]
firstImage := (*images)[0]
ir, err := s.glance.GetImageDetail(firstImage.Id)
c.Assert(err, IsNil)
c.Assert(ir.Created, Matches, firstImage.Created)
Expand Down
77 changes: 55 additions & 22 deletions nova/nova.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,31 @@ type FlavorDetail struct {
}

// ListFlavorsDetail lists all details for available flavors.
func (c *Client) ListFlavorsDetail() ([]FlavorDetail, error) {
func (c *Client) ListFlavorsDetail() (*[]FlavorDetail, error) {
var resp struct {
Flavors []FlavorDetail
}
requestData := goosehttp.RequestData{RespValue: &resp}
err := c.client.SendRequest(client.GET, "compute", apiFlavorsDetail, &requestData,
"failed to get list of flavors details")
return resp.Flavors, err
if err != nil {
return nil, err
}
return &resp.Flavors, nil
}

// ListServers lists IDs, names, and links for all servers.
func (c *Client) ListServers() ([]Entity, error) {
func (c *Client) ListServers() (*[]Entity, error) {
var resp struct {
Servers []Entity
}
requestData := goosehttp.RequestData{RespValue: &resp, ExpectedStatus: []int{http.StatusOK}}
err := c.client.SendRequest(client.GET, "compute", apiServers, &requestData,
"failed to get list of servers")
return resp.Servers, err
if err != nil {
return nil, err
}
return &resp.Servers, nil
}

type ServerDetail struct {
Expand All @@ -102,26 +108,32 @@ type ServerDetail struct {
}

// ListServersDetail lists all details for available servers.
func (c *Client) ListServersDetail() ([]ServerDetail, error) {
func (c *Client) ListServersDetail() (*[]ServerDetail, error) {
var resp struct {
Servers []ServerDetail
}
requestData := goosehttp.RequestData{RespValue: &resp}
err := c.client.SendRequest(client.GET, "compute", apiServersDetail, &requestData,
"failed to get list of servers details")
return resp.Servers, err
if err != nil {
return nil, err
}
return &resp.Servers, nil
}

// GetServer lists details for the specified server.
func (c *Client) GetServer(serverId string) (ServerDetail, error) {
func (c *Client) GetServer(serverId string) (*ServerDetail, error) {
var resp struct {
Server ServerDetail
}
url := fmt.Sprintf("%s/%s", apiServers, serverId)
requestData := goosehttp.RequestData{RespValue: &resp}
err := c.client.SendRequest(client.GET, "compute", url, &requestData,
"failed to get details for serverId=%s", serverId)
return resp.Server, err
if err != nil {
return nil, err
}
return &resp.Server, nil
}

// DeleteServer terminates the specified server.
Expand Down Expand Up @@ -182,18 +194,21 @@ type SecurityGroup struct {
}

// ListSecurityGroups lists IDs, names, and other details for all security groups.
func (c *Client) ListSecurityGroups() ([]SecurityGroup, error) {
func (c *Client) ListSecurityGroups() (*[]SecurityGroup, error) {
var resp struct {
Groups []SecurityGroup `json:"security_groups"`
}
requestData := goosehttp.RequestData{RespValue: &resp}
err := c.client.SendRequest(client.GET, "compute", apiSecurityGroups, &requestData,
"failed to list security groups")
return resp.Groups, err
if err != nil {
return nil, err
}
return &resp.Groups, nil
}

// GetServerSecurityGroups list security groups for a specific server.
func (c *Client) GetServerSecurityGroups(serverId string) ([]SecurityGroup, error) {
func (c *Client) GetServerSecurityGroups(serverId string) (*[]SecurityGroup, error) {

var resp struct {
Groups []SecurityGroup `json:"security_groups"`
Expand All @@ -202,11 +217,14 @@ func (c *Client) GetServerSecurityGroups(serverId string) ([]SecurityGroup, erro
requestData := goosehttp.RequestData{RespValue: &resp}
err := c.client.SendRequest(client.GET, "compute", url, &requestData,
"failed to list server (%s) security groups", serverId)
return resp.Groups, err
if err != nil {
return nil, err
}
return &resp.Groups, nil
}

// CreateSecurityGroup creates a new security group.
func (c *Client) CreateSecurityGroup(name, description string) (SecurityGroup, error) {
func (c *Client) CreateSecurityGroup(name, description string) (*SecurityGroup, error) {
var req struct {
SecurityGroup struct {
Name string `json:"name"`
Expand All @@ -222,7 +240,10 @@ func (c *Client) CreateSecurityGroup(name, description string) (SecurityGroup, e
requestData := goosehttp.RequestData{ReqValue: req, RespValue: &resp, ExpectedStatus: []int{http.StatusOK}}
err := c.client.SendRequest(client.POST, "compute", apiSecurityGroups, &requestData,
"failed to create a security group with name=%s", name)
return resp.SecurityGroup, err
if err != nil {
return nil, err
}
return &resp.SecurityGroup, nil
}

// DeleteSecurityGroup deletes the specified security group.
Expand All @@ -244,7 +265,7 @@ type RuleInfo struct {
}

// CreateSecurityGroupRule creates a security group rule.
func (c *Client) CreateSecurityGroupRule(ruleInfo RuleInfo) (SecurityGroupRule, error) {
func (c *Client) CreateSecurityGroupRule(ruleInfo RuleInfo) (*SecurityGroupRule, error) {
var req struct {
SecurityGroupRule RuleInfo `json:"security_group_rule"`
}
Expand All @@ -257,7 +278,10 @@ func (c *Client) CreateSecurityGroupRule(ruleInfo RuleInfo) (SecurityGroupRule,
requestData := goosehttp.RequestData{ReqValue: req, RespValue: &resp}
err := c.client.SendRequest(client.POST, "compute", apiSecurityGroupRules, &requestData,
"failed to create a rule for the security group with id=%s", ruleInfo.GroupId)
return resp.SecurityGroupRule, err
if err != nil {
return nil, err
}
return &resp.SecurityGroupRule, nil
}

// DeleteSecurityGroupRule deletes the specified security group rule.
Expand Down Expand Up @@ -310,19 +334,22 @@ type FloatingIP struct {
}

// ListFloatingIPs lists floating IP addresses associated with the tenant or account.
func (c *Client) ListFloatingIPs() ([]FloatingIP, error) {
func (c *Client) ListFloatingIPs() (*[]FloatingIP, error) {
var resp struct {
FloatingIPs []FloatingIP `json:"floating_ips"`
}

requestData := goosehttp.RequestData{RespValue: &resp}
err := c.client.SendRequest(client.GET, "compute", apiFloatingIPs, &requestData,
"failed to list floating ips")
return resp.FloatingIPs, err
if err != nil {
return nil, err
}
return &resp.FloatingIPs, nil
}

// GetFloatingIP lists details of the floating IP address associated with specified id.
func (c *Client) GetFloatingIP(ipId int) (FloatingIP, error) {
func (c *Client) GetFloatingIP(ipId int) (*FloatingIP, error) {
var resp struct {
FloatingIP FloatingIP `json:"floating_ip"`
}
Expand All @@ -331,19 +358,25 @@ func (c *Client) GetFloatingIP(ipId int) (FloatingIP, error) {
requestData := goosehttp.RequestData{RespValue: &resp}
err := c.client.SendRequest(client.GET, "compute", url, &requestData,
"failed to get floating ip %d details", ipId)
return resp.FloatingIP, err
if err != nil {
return nil, err
}
return &resp.FloatingIP, nil
}

// AllocateFloatingIP allocates a new floating IP address to a tenant or account.
func (c *Client) AllocateFloatingIP() (FloatingIP, error) {
func (c *Client) AllocateFloatingIP() (*FloatingIP, error) {
var resp struct {
FloatingIP FloatingIP `json:"floating_ip"`
}

requestData := goosehttp.RequestData{RespValue: &resp}
err := c.client.SendRequest(client.POST, "compute", apiFloatingIPs, &requestData,
"failed to allocate a floating ip")
return resp.FloatingIP, err
if err != nil {
return nil, err
}
return &resp.FloatingIP, nil
}

// DeleteFloatingIP deallocates the floating IP address associated with the specified id.
Expand Down
Loading

0 comments on commit d2a27b4

Please sign in to comment.