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

     1  package floatingips
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/gophercloud/gophercloud"
     8  	"github.com/gophercloud/gophercloud/pagination"
     9  )
    10  
    11  // FloatingIP represents a floating IP resource. A floating IP is an external
    12  // IP address that is mapped to an internal port and, optionally, a specific
    13  // IP address on a private network. In other words, it enables access to an
    14  // instance on a private network from an external network. For this reason,
    15  // floating IPs can only be defined on networks where the `router:external'
    16  // attribute (provided by the external network extension) is set to True.
    17  type FloatingIP struct {
    18  	// ID is the unique identifier for the floating IP instance.
    19  	ID string `json:"id"`
    20  
    21  	// Description for the floating IP instance.
    22  	Description string `json:"description"`
    23  
    24  	// FloatingNetworkID is the UUID of the external network where the floating
    25  	// IP is to be created.
    26  	FloatingNetworkID string `json:"floating_network_id"`
    27  
    28  	// FloatingIP is the address of the floating IP on the external network.
    29  	FloatingIP string `json:"floating_ip_address"`
    30  
    31  	// PortID is the UUID of the port on an internal network that is associated
    32  	// with the floating IP.
    33  	PortID string `json:"port_id"`
    34  
    35  	// FixedIP is the specific IP address of the internal port which should be
    36  	// associated with the floating IP.
    37  	FixedIP string `json:"fixed_ip_address"`
    38  
    39  	// TenantID is the project owner of the floating IP. Only admin users can
    40  	// specify a project identifier other than its own.
    41  	TenantID string `json:"tenant_id"`
    42  
    43  	// UpdatedAt and CreatedAt contain ISO-8601 timestamps of when the state of
    44  	// the floating ip last changed, and when it was created.
    45  	UpdatedAt time.Time `json:"-"`
    46  	CreatedAt time.Time `json:"-"`
    47  
    48  	// ProjectID is the project owner of the floating IP.
    49  	ProjectID string `json:"project_id"`
    50  
    51  	// Status is the condition of the API resource.
    52  	Status string `json:"status"`
    53  
    54  	// RouterID is the ID of the router used for this floating IP.
    55  	RouterID string `json:"router_id"`
    56  
    57  	// Tags optionally set via extensions/attributestags
    58  	Tags []string `json:"tags"`
    59  }
    60  
    61  func (r *FloatingIP) UnmarshalJSON(b []byte) error {
    62  	type tmp FloatingIP
    63  
    64  	// Support for older neutron time format
    65  	var s1 struct {
    66  		tmp
    67  		CreatedAt gophercloud.JSONRFC3339NoZ `json:"created_at"`
    68  		UpdatedAt gophercloud.JSONRFC3339NoZ `json:"updated_at"`
    69  	}
    70  
    71  	err := json.Unmarshal(b, &s1)
    72  	if err == nil {
    73  		*r = FloatingIP(s1.tmp)
    74  		r.CreatedAt = time.Time(s1.CreatedAt)
    75  		r.UpdatedAt = time.Time(s1.UpdatedAt)
    76  
    77  		return nil
    78  	}
    79  
    80  	// Support for newer neutron time format
    81  	var s2 struct {
    82  		tmp
    83  		CreatedAt time.Time `json:"created_at"`
    84  		UpdatedAt time.Time `json:"updated_at"`
    85  	}
    86  
    87  	err = json.Unmarshal(b, &s2)
    88  	if err != nil {
    89  		return err
    90  	}
    91  
    92  	*r = FloatingIP(s2.tmp)
    93  	r.CreatedAt = time.Time(s2.CreatedAt)
    94  	r.UpdatedAt = time.Time(s2.UpdatedAt)
    95  
    96  	return nil
    97  }
    98  
    99  type commonResult struct {
   100  	gophercloud.Result
   101  }
   102  
   103  // Extract will extract a FloatingIP resource from a result.
   104  func (r commonResult) Extract() (*FloatingIP, error) {
   105  	var s FloatingIP
   106  	err := r.ExtractInto(&s)
   107  	return &s, err
   108  }
   109  
   110  func (r commonResult) ExtractInto(v interface{}) error {
   111  	return r.Result.ExtractIntoStructPtr(v, "floatingip")
   112  }
   113  
   114  // CreateResult represents the result of a create operation. Call its Extract
   115  // method to interpret it as a FloatingIP.
   116  type CreateResult struct {
   117  	commonResult
   118  }
   119  
   120  // GetResult represents the result of a get operation. Call its Extract
   121  // method to interpret it as a FloatingIP.
   122  type GetResult struct {
   123  	commonResult
   124  }
   125  
   126  // UpdateResult represents the result of an update operation. Call its Extract
   127  // method to interpret it as a FloatingIP.
   128  type UpdateResult struct {
   129  	commonResult
   130  }
   131  
   132  // DeleteResult represents the result of an update operation. Call its
   133  // ExtractErr method to determine if the request succeeded or failed.
   134  type DeleteResult struct {
   135  	gophercloud.ErrResult
   136  }
   137  
   138  // FloatingIPPage is the page returned by a pager when traversing over a
   139  // collection of floating IPs.
   140  type FloatingIPPage struct {
   141  	pagination.LinkedPageBase
   142  }
   143  
   144  // NextPageURL is invoked when a paginated collection of floating IPs has
   145  // reached the end of a page and the pager seeks to traverse over a new one.
   146  // In order to do this, it needs to construct the next page's URL.
   147  func (r FloatingIPPage) NextPageURL() (string, error) {
   148  	var s struct {
   149  		Links []gophercloud.Link `json:"floatingips_links"`
   150  	}
   151  	err := r.ExtractInto(&s)
   152  	if err != nil {
   153  		return "", err
   154  	}
   155  	return gophercloud.ExtractNextURL(s.Links)
   156  }
   157  
   158  // IsEmpty checks whether a FloatingIPPage struct is empty.
   159  func (r FloatingIPPage) IsEmpty() (bool, error) {
   160  	if r.StatusCode == 204 {
   161  		return true, nil
   162  	}
   163  
   164  	is, err := ExtractFloatingIPs(r)
   165  	return len(is) == 0, err
   166  }
   167  
   168  // ExtractFloatingIPs accepts a Page struct, specifically a FloatingIPPage
   169  // struct, and extracts the elements into a slice of FloatingIP structs. In
   170  // other words, a generic collection is mapped into a relevant slice.
   171  func ExtractFloatingIPs(r pagination.Page) ([]FloatingIP, error) {
   172  	var s struct {
   173  		FloatingIPs []FloatingIP `json:"floatingips"`
   174  	}
   175  	err := (r.(FloatingIPPage)).ExtractInto(&s)
   176  	return s.FloatingIPs, err
   177  }
   178  
   179  func ExtractFloatingIPsInto(r pagination.Page, v interface{}) error {
   180  	return r.(FloatingIPPage).Result.ExtractIntoSlicePtr(v, "floatingips")
   181  }