github.com/gophercloud/gophercloud@v1.11.0/openstack/compute/v2/extensions/floatingips/results.go (about)

     1  package floatingips
     2  
     3  import (
     4  	"encoding/json"
     5  	"strconv"
     6  
     7  	"github.com/gophercloud/gophercloud"
     8  	"github.com/gophercloud/gophercloud/pagination"
     9  )
    10  
    11  // A FloatingIP is an IP that can be associated with a server.
    12  type FloatingIP struct {
    13  	// ID is a unique ID of the Floating IP
    14  	ID string `json:"-"`
    15  
    16  	// FixedIP is a specific IP on the server to pair the Floating IP with.
    17  	FixedIP string `json:"fixed_ip,omitempty"`
    18  
    19  	// InstanceID is the ID of the server that is using the Floating IP.
    20  	InstanceID string `json:"instance_id"`
    21  
    22  	// IP is the actual Floating IP.
    23  	IP string `json:"ip"`
    24  
    25  	// Pool is the pool of Floating IPs that this Floating IP belongs to.
    26  	Pool string `json:"pool"`
    27  }
    28  
    29  func (r *FloatingIP) UnmarshalJSON(b []byte) error {
    30  	type tmp FloatingIP
    31  	var s struct {
    32  		tmp
    33  		ID interface{} `json:"id"`
    34  	}
    35  	err := json.Unmarshal(b, &s)
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	*r = FloatingIP(s.tmp)
    41  
    42  	switch t := s.ID.(type) {
    43  	case float64:
    44  		r.ID = strconv.FormatFloat(t, 'f', -1, 64)
    45  	case string:
    46  		r.ID = t
    47  	}
    48  
    49  	return err
    50  }
    51  
    52  // FloatingIPPage stores a single page of FloatingIPs from a List call.
    53  type FloatingIPPage struct {
    54  	pagination.SinglePageBase
    55  }
    56  
    57  // IsEmpty determines whether or not a FloatingIPsPage is empty.
    58  func (page FloatingIPPage) IsEmpty() (bool, error) {
    59  	if page.StatusCode == 204 {
    60  		return true, nil
    61  	}
    62  
    63  	va, err := ExtractFloatingIPs(page)
    64  	return len(va) == 0, err
    65  }
    66  
    67  // ExtractFloatingIPs interprets a page of results as a slice of FloatingIPs.
    68  func ExtractFloatingIPs(r pagination.Page) ([]FloatingIP, error) {
    69  	var s struct {
    70  		FloatingIPs []FloatingIP `json:"floating_ips"`
    71  	}
    72  	err := (r.(FloatingIPPage)).ExtractInto(&s)
    73  	return s.FloatingIPs, err
    74  }
    75  
    76  // FloatingIPResult is the raw result from a FloatingIP request.
    77  type FloatingIPResult struct {
    78  	gophercloud.Result
    79  }
    80  
    81  // Extract is a method that attempts to interpret any FloatingIP resource
    82  // response as a FloatingIP struct.
    83  func (r FloatingIPResult) Extract() (*FloatingIP, error) {
    84  	var s struct {
    85  		FloatingIP *FloatingIP `json:"floating_ip"`
    86  	}
    87  	err := r.ExtractInto(&s)
    88  	return s.FloatingIP, err
    89  }
    90  
    91  // CreateResult is the response from a Create operation. Call its Extract method
    92  // to interpret it as a FloatingIP.
    93  type CreateResult struct {
    94  	FloatingIPResult
    95  }
    96  
    97  // GetResult is the response from a Get operation. Call its Extract method to
    98  // interpret it as a FloatingIP.
    99  type GetResult struct {
   100  	FloatingIPResult
   101  }
   102  
   103  // DeleteResult is the response from a Delete operation. Call its ExtractErr
   104  // method to determine if the call succeeded or failed.
   105  type DeleteResult struct {
   106  	gophercloud.ErrResult
   107  }
   108  
   109  // AssociateResult is the response from a Delete operation. Call its ExtractErr
   110  // method to determine if the call succeeded or failed.
   111  type AssociateResult struct {
   112  	gophercloud.ErrResult
   113  }
   114  
   115  // DisassociateResult is the response from a Delete operation. Call its
   116  // ExtractErr method to determine if the call succeeded or failed.
   117  type DisassociateResult struct {
   118  	gophercloud.ErrResult
   119  }