github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/sharedfilesystems/v2/shares/results.go (about)

     1  package shares
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/url"
     6  	"strconv"
     7  	"time"
     8  
     9  	"github.com/vnpaycloud-console/gophercloud/v2"
    10  	"github.com/vnpaycloud-console/gophercloud/v2/pagination"
    11  )
    12  
    13  const (
    14  	invalidMarker = "-1"
    15  )
    16  
    17  // Share contains all information associated with an OpenStack Share
    18  type Share struct {
    19  	// The availability zone of the share
    20  	AvailabilityZone string `json:"availability_zone"`
    21  	// A description of the share
    22  	Description string `json:"description,omitempty"`
    23  	// DisplayDescription is inherited from BlockStorage API.
    24  	// Both Description and DisplayDescription can be used
    25  	DisplayDescription string `json:"display_description,omitempty"`
    26  	// DisplayName is inherited from BlockStorage API
    27  	// Both DisplayName and Name can be used
    28  	DisplayName string `json:"display_name,omitempty"`
    29  	// Indicates whether a share has replicas or not.
    30  	HasReplicas bool `json:"has_replicas"`
    31  	// The host name of the share
    32  	Host string `json:"host"`
    33  	// The UUID of the share
    34  	ID string `json:"id"`
    35  	// Indicates the visibility of the share
    36  	IsPublic bool `json:"is_public,omitempty"`
    37  	// Share links for pagination
    38  	Links []map[string]string `json:"links"`
    39  	// Key, value -pairs of custom metadata
    40  	Metadata map[string]string `json:"metadata,omitempty"`
    41  	// The name of the share
    42  	Name string `json:"name,omitempty"`
    43  	// The UUID of the project to which this share belongs to
    44  	ProjectID string `json:"project_id"`
    45  	// The share replication type
    46  	ReplicationType string `json:"replication_type,omitempty"`
    47  	// The UUID of the share network
    48  	ShareNetworkID string `json:"share_network_id"`
    49  	// The shared file system protocol
    50  	ShareProto string `json:"share_proto"`
    51  	// The UUID of the share server
    52  	ShareServerID string `json:"share_server_id"`
    53  	// The UUID of the share type.
    54  	ShareType string `json:"share_type"`
    55  	// The name of the share type.
    56  	ShareTypeName string `json:"share_type_name"`
    57  	// The UUID of the share group. Available starting from the microversion 2.31
    58  	ShareGroupID string `json:"share_group_id"`
    59  	// Size of the share in GB
    60  	Size int `json:"size"`
    61  	// UUID of the snapshot from which to create the share
    62  	SnapshotID string `json:"snapshot_id"`
    63  	// The share status
    64  	Status string `json:"status"`
    65  	// The task state, used for share migration
    66  	TaskState string `json:"task_state"`
    67  	// The type of the volume
    68  	VolumeType string `json:"volume_type,omitempty"`
    69  	// The UUID of the consistency group this share belongs to
    70  	ConsistencyGroupID string `json:"consistency_group_id"`
    71  	// Used for filtering backends which either support or do not support share snapshots
    72  	SnapshotSupport          bool   `json:"snapshot_support"`
    73  	SourceCgsnapshotMemberID string `json:"source_cgsnapshot_member_id"`
    74  	// Used for filtering backends which either support or do not support creating shares from snapshots
    75  	CreateShareFromSnapshotSupport bool `json:"create_share_from_snapshot_support"`
    76  	// Timestamp when the share was created
    77  	CreatedAt time.Time `json:"-"`
    78  	// Timestamp when the share was updated
    79  	UpdatedAt time.Time `json:"-"`
    80  }
    81  
    82  func (r *Share) UnmarshalJSON(b []byte) error {
    83  	type tmp Share
    84  	var s struct {
    85  		tmp
    86  		CreatedAt gophercloud.JSONRFC3339MilliNoZ `json:"created_at"`
    87  		UpdatedAt gophercloud.JSONRFC3339MilliNoZ `json:"updated_at"`
    88  	}
    89  	err := json.Unmarshal(b, &s)
    90  	if err != nil {
    91  		return err
    92  	}
    93  	*r = Share(s.tmp)
    94  
    95  	r.CreatedAt = time.Time(s.CreatedAt)
    96  	r.UpdatedAt = time.Time(s.UpdatedAt)
    97  
    98  	return nil
    99  }
   100  
   101  type commonResult struct {
   102  	gophercloud.Result
   103  }
   104  
   105  // Extract will get the Share object from the commonResult
   106  func (r commonResult) Extract() (*Share, error) {
   107  	var s struct {
   108  		Share *Share `json:"share"`
   109  	}
   110  	err := r.ExtractInto(&s)
   111  	return s.Share, err
   112  }
   113  
   114  // CreateResult contains the response body and error from a Create request.
   115  type CreateResult struct {
   116  	commonResult
   117  }
   118  
   119  // SharePage is a pagination.pager that is returned from a call to the List function.
   120  type SharePage struct {
   121  	pagination.MarkerPageBase
   122  }
   123  
   124  // NextPageURL generates the URL for the page of results after this one.
   125  func (r SharePage) NextPageURL() (string, error) {
   126  	currentURL := r.URL
   127  	mark, err := r.Owner.LastMarker()
   128  	if err != nil {
   129  		return "", err
   130  	}
   131  	if mark == invalidMarker {
   132  		return "", nil
   133  	}
   134  
   135  	q := currentURL.Query()
   136  	q.Set("offset", mark)
   137  	currentURL.RawQuery = q.Encode()
   138  	return currentURL.String(), nil
   139  }
   140  
   141  // LastMarker returns the last offset in a ListResult.
   142  func (r SharePage) LastMarker() (string, error) {
   143  	shares, err := ExtractShares(r)
   144  	if err != nil {
   145  		return invalidMarker, err
   146  	}
   147  	if len(shares) == 0 {
   148  		return invalidMarker, nil
   149  	}
   150  
   151  	u, err := url.Parse(r.URL.String())
   152  	if err != nil {
   153  		return invalidMarker, err
   154  	}
   155  	queryParams := u.Query()
   156  	offset := queryParams.Get("offset")
   157  	limit := queryParams.Get("limit")
   158  
   159  	// Limit is not present, only one page required
   160  	if limit == "" {
   161  		return invalidMarker, nil
   162  	}
   163  
   164  	iOffset := 0
   165  	if offset != "" {
   166  		iOffset, err = strconv.Atoi(offset)
   167  		if err != nil {
   168  			return invalidMarker, err
   169  		}
   170  	}
   171  	iLimit, err := strconv.Atoi(limit)
   172  	if err != nil {
   173  		return invalidMarker, err
   174  	}
   175  	iOffset = iOffset + iLimit
   176  	offset = strconv.Itoa(iOffset)
   177  
   178  	return offset, nil
   179  }
   180  
   181  // IsEmpty satisifies the IsEmpty method of the Page interface
   182  func (r SharePage) IsEmpty() (bool, error) {
   183  	if r.StatusCode == 204 {
   184  		return true, nil
   185  	}
   186  
   187  	shares, err := ExtractShares(r)
   188  	return len(shares) == 0, err
   189  }
   190  
   191  // ExtractShares extracts and returns a Share slice. It is used while
   192  // iterating over a shares.List call.
   193  func ExtractShares(r pagination.Page) ([]Share, error) {
   194  	var s struct {
   195  		Shares []Share `json:"shares"`
   196  	}
   197  
   198  	err := (r.(SharePage)).ExtractInto(&s)
   199  
   200  	return s.Shares, err
   201  }
   202  
   203  // DeleteResult contains the response body and error from a Delete request.
   204  type DeleteResult struct {
   205  	gophercloud.ErrResult
   206  }
   207  
   208  // GetResult contains the response body and error from a Get request.
   209  type GetResult struct {
   210  	commonResult
   211  }
   212  
   213  // UpdateResult contains the response body and error from an Update request.
   214  type UpdateResult struct {
   215  	commonResult
   216  }
   217  
   218  // ListExportLocationsResult contains the result body and error from a
   219  // ListExportLocations request.
   220  type ListExportLocationsResult struct {
   221  	gophercloud.Result
   222  }
   223  
   224  // GetExportLocationResult contains the result body and error from a
   225  // GetExportLocation request.
   226  type GetExportLocationResult struct {
   227  	gophercloud.Result
   228  }
   229  
   230  // ExportLocation contains all information associated with a share export location
   231  type ExportLocation struct {
   232  	// The export location path that should be used for mount operation.
   233  	Path string `json:"path"`
   234  	// The UUID of the share instance that this export location belongs to.
   235  	ShareInstanceID string `json:"share_instance_id"`
   236  	// Defines purpose of an export location.
   237  	// If set to true, then it is expected to be used for service needs
   238  	// and by administrators only.
   239  	// If it is set to false, then this export location can be used by end users.
   240  	IsAdminOnly bool `json:"is_admin_only"`
   241  	// The share export location UUID.
   242  	ID string `json:"id"`
   243  	// Drivers may use this field to identify which export locations are
   244  	// most efficient and should be used preferentially by clients.
   245  	// By default it is set to false value. New in version 2.14
   246  	Preferred bool `json:"preferred"`
   247  }
   248  
   249  // Extract will get the Export Locations from the ListExportLocationsResult
   250  func (r ListExportLocationsResult) Extract() ([]ExportLocation, error) {
   251  	var s struct {
   252  		ExportLocations []ExportLocation `json:"export_locations"`
   253  	}
   254  	err := r.ExtractInto(&s)
   255  	return s.ExportLocations, err
   256  }
   257  
   258  // Extract will get the Export Location from the GetExportLocationResult
   259  func (r GetExportLocationResult) Extract() (*ExportLocation, error) {
   260  	var s struct {
   261  		ExportLocation *ExportLocation `json:"export_location"`
   262  	}
   263  	err := r.ExtractInto(&s)
   264  	return s.ExportLocation, err
   265  }
   266  
   267  // AccessRight contains all information associated with an OpenStack share
   268  // Grant Access Response
   269  type AccessRight struct {
   270  	// The UUID of the share to which you are granted or denied access.
   271  	ShareID string `json:"share_id"`
   272  	// The access rule type that can be "ip", "cert" or "user".
   273  	AccessType string `json:"access_type,omitempty"`
   274  	// The value that defines the access that can be a valid format of IP, cert or user.
   275  	AccessTo string `json:"access_to,omitempty"`
   276  	// The access credential of the entity granted share access.
   277  	AccessKey string `json:"access_key,omitempty"`
   278  	// The access level to the share is either "rw" or "ro".
   279  	AccessLevel string `json:"access_level,omitempty"`
   280  	// The state of the access rule
   281  	State string `json:"state,omitempty"`
   282  	// The access rule ID.
   283  	ID string `json:"id"`
   284  }
   285  
   286  // Extract will get the GrantAccess object from the commonResult
   287  func (r GrantAccessResult) Extract() (*AccessRight, error) {
   288  	var s struct {
   289  		AccessRight *AccessRight `json:"access"`
   290  	}
   291  	err := r.ExtractInto(&s)
   292  	return s.AccessRight, err
   293  }
   294  
   295  // GrantAccessResult contains the result body and error from an GrantAccess request.
   296  type GrantAccessResult struct {
   297  	gophercloud.Result
   298  }
   299  
   300  // RevokeAccessResult contains the response body and error from a Revoke access request.
   301  type RevokeAccessResult struct {
   302  	gophercloud.ErrResult
   303  }
   304  
   305  // Extract will get a slice of AccessRight objects from the commonResult
   306  func (r ListAccessRightsResult) Extract() ([]AccessRight, error) {
   307  	var s struct {
   308  		AccessRights []AccessRight `json:"access_list"`
   309  	}
   310  	err := r.ExtractInto(&s)
   311  	return s.AccessRights, err
   312  }
   313  
   314  // ListAccessRightsResult contains the result body and error from a ListAccessRights request.
   315  type ListAccessRightsResult struct {
   316  	gophercloud.Result
   317  }
   318  
   319  // ExtendResult contains the response body and error from an Extend request.
   320  type ExtendResult struct {
   321  	gophercloud.ErrResult
   322  }
   323  
   324  // ShrinkResult contains the response body and error from a Shrink request.
   325  type ShrinkResult struct {
   326  	gophercloud.ErrResult
   327  }
   328  
   329  // GetMetadatumResult contains the response body and error from a GetMetadatum request.
   330  type GetMetadatumResult struct {
   331  	gophercloud.Result
   332  }
   333  
   334  // Extract will get the string-string map from GetMetadatumResult
   335  func (r GetMetadatumResult) Extract() (map[string]string, error) {
   336  	var s struct {
   337  		Meta map[string]string `json:"meta"`
   338  	}
   339  	err := r.ExtractInto(&s)
   340  	return s.Meta, err
   341  }
   342  
   343  // MetadataResult contains the response body and error from GetMetadata, SetMetadata or UpdateMetadata requests.
   344  type MetadataResult struct {
   345  	gophercloud.Result
   346  }
   347  
   348  // Extract will get the string-string map from MetadataResult
   349  func (r MetadataResult) Extract() (map[string]string, error) {
   350  	var s struct {
   351  		Metadata map[string]string `json:"metadata"`
   352  	}
   353  	err := r.ExtractInto(&s)
   354  	return s.Metadata, err
   355  }
   356  
   357  // DeleteMetadatumResult contains the response body and error from a DeleteMetadatum request.
   358  type DeleteMetadatumResult struct {
   359  	gophercloud.ErrResult
   360  }
   361  
   362  // RevertResult contains the response error from an Revert request.
   363  type RevertResult struct {
   364  	gophercloud.ErrResult
   365  }
   366  
   367  // ResetStatusResult contains the response error from an ResetStatus request.
   368  type ResetStatusResult struct {
   369  	gophercloud.ErrResult
   370  }
   371  
   372  // ForceDeleteResult contains the response error from an ForceDelete request.
   373  type ForceDeleteResult struct {
   374  	gophercloud.ErrResult
   375  }
   376  
   377  // UnmanageResult contains the response error from an Unmanage request.
   378  type UnmanageResult struct {
   379  	gophercloud.ErrResult
   380  }