github.com/gophercloud/gophercloud@v1.11.0/openstack/sharedfilesystems/v2/sharenetworks/results.go (about)

     1  package sharenetworks
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/url"
     6  	"strconv"
     7  	"time"
     8  
     9  	"github.com/gophercloud/gophercloud"
    10  	"github.com/gophercloud/gophercloud/pagination"
    11  )
    12  
    13  // ShareNetwork contains all the information associated with an OpenStack
    14  // ShareNetwork.
    15  type ShareNetwork struct {
    16  	// The Share Network ID
    17  	ID string `json:"id"`
    18  	// The UUID of the project where the share network was created
    19  	ProjectID string `json:"project_id"`
    20  	// The neutron network ID
    21  	NeutronNetID string `json:"neutron_net_id"`
    22  	// The neutron subnet ID
    23  	NeutronSubnetID string `json:"neutron_subnet_id"`
    24  	// The nova network ID
    25  	NovaNetID string `json:"nova_net_id"`
    26  	// The network type. A valid value is VLAN, VXLAN, GRE or flat
    27  	NetworkType string `json:"network_type"`
    28  	// The segmentation ID
    29  	SegmentationID int `json:"segmentation_id"`
    30  	// The IP block from which to allocate the network, in CIDR notation
    31  	CIDR string `json:"cidr"`
    32  	// The IP version of the network. A valid value is 4 or 6
    33  	IPVersion int `json:"ip_version"`
    34  	// The Share Network name
    35  	Name string `json:"name"`
    36  	// The Share Network description
    37  	Description string `json:"description"`
    38  	// The date and time stamp when the Share Network was created
    39  	CreatedAt time.Time `json:"-"`
    40  	// The date and time stamp when the Share Network was updated
    41  	UpdatedAt time.Time `json:"-"`
    42  }
    43  
    44  func (r *ShareNetwork) UnmarshalJSON(b []byte) error {
    45  	type tmp ShareNetwork
    46  	var s struct {
    47  		tmp
    48  		CreatedAt gophercloud.JSONRFC3339MilliNoZ `json:"created_at"`
    49  		UpdatedAt gophercloud.JSONRFC3339MilliNoZ `json:"updated_at"`
    50  	}
    51  	err := json.Unmarshal(b, &s)
    52  	if err != nil {
    53  		return err
    54  	}
    55  	*r = ShareNetwork(s.tmp)
    56  
    57  	r.CreatedAt = time.Time(s.CreatedAt)
    58  	r.UpdatedAt = time.Time(s.UpdatedAt)
    59  
    60  	return nil
    61  }
    62  
    63  type commonResult struct {
    64  	gophercloud.Result
    65  }
    66  
    67  // ShareNetworkPage is a pagination.pager that is returned from a call to the List function.
    68  type ShareNetworkPage struct {
    69  	pagination.MarkerPageBase
    70  }
    71  
    72  // NextPageURL generates the URL for the page of results after this one.
    73  func (r ShareNetworkPage) NextPageURL() (string, error) {
    74  	currentURL := r.URL
    75  	mark, err := r.Owner.LastMarker()
    76  	if err != nil {
    77  		return "", err
    78  	}
    79  
    80  	q := currentURL.Query()
    81  	q.Set("offset", mark)
    82  	currentURL.RawQuery = q.Encode()
    83  	return currentURL.String(), nil
    84  }
    85  
    86  // LastMarker returns the last offset in a ListResult.
    87  func (r ShareNetworkPage) LastMarker() (string, error) {
    88  	maxInt := strconv.Itoa(int(^uint(0) >> 1))
    89  	shareNetworks, err := ExtractShareNetworks(r)
    90  	if err != nil {
    91  		return maxInt, err
    92  	}
    93  	if len(shareNetworks) == 0 {
    94  		return maxInt, nil
    95  	}
    96  
    97  	u, err := url.Parse(r.URL.String())
    98  	if err != nil {
    99  		return maxInt, err
   100  	}
   101  	queryParams := u.Query()
   102  	offset := queryParams.Get("offset")
   103  	limit := queryParams.Get("limit")
   104  
   105  	// Limit is not present, only one page required
   106  	if limit == "" {
   107  		return maxInt, nil
   108  	}
   109  
   110  	iOffset := 0
   111  	if offset != "" {
   112  		iOffset, err = strconv.Atoi(offset)
   113  		if err != nil {
   114  			return maxInt, err
   115  		}
   116  	}
   117  	iLimit, err := strconv.Atoi(limit)
   118  	if err != nil {
   119  		return maxInt, err
   120  	}
   121  	iOffset = iOffset + iLimit
   122  	offset = strconv.Itoa(iOffset)
   123  
   124  	return offset, nil
   125  }
   126  
   127  // IsEmpty satisifies the IsEmpty method of the Page interface
   128  func (r ShareNetworkPage) IsEmpty() (bool, error) {
   129  	if r.StatusCode == 204 {
   130  		return true, nil
   131  	}
   132  
   133  	shareNetworks, err := ExtractShareNetworks(r)
   134  	return len(shareNetworks) == 0, err
   135  }
   136  
   137  // ExtractShareNetworks extracts and returns ShareNetworks. It is used while
   138  // iterating over a sharenetworks.List call.
   139  func ExtractShareNetworks(r pagination.Page) ([]ShareNetwork, error) {
   140  	var s struct {
   141  		ShareNetworks []ShareNetwork `json:"share_networks"`
   142  	}
   143  	err := (r.(ShareNetworkPage)).ExtractInto(&s)
   144  	return s.ShareNetworks, err
   145  }
   146  
   147  // Extract will get the ShareNetwork object out of the commonResult object.
   148  func (r commonResult) Extract() (*ShareNetwork, error) {
   149  	var s struct {
   150  		ShareNetwork *ShareNetwork `json:"share_network"`
   151  	}
   152  	err := r.ExtractInto(&s)
   153  	return s.ShareNetwork, err
   154  }
   155  
   156  // CreateResult contains the response body and error from a Create request.
   157  type CreateResult struct {
   158  	commonResult
   159  }
   160  
   161  // DeleteResult contains the response body and error from a Delete request.
   162  type DeleteResult struct {
   163  	gophercloud.ErrResult
   164  }
   165  
   166  // GetResult contains the response body and error from a Get request.
   167  type GetResult struct {
   168  	commonResult
   169  }
   170  
   171  // UpdateResult contains the response body and error from an Update request.
   172  type UpdateResult struct {
   173  	commonResult
   174  }
   175  
   176  // AddSecurityServiceResult contains the response body and error from a security
   177  // service addition request.
   178  type AddSecurityServiceResult struct {
   179  	commonResult
   180  }
   181  
   182  // RemoveSecurityServiceResult contains the response body and error from a security
   183  // service removal request.
   184  type RemoveSecurityServiceResult struct {
   185  	commonResult
   186  }