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

     1  package messages
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/gophercloud/gophercloud"
     8  	"github.com/gophercloud/gophercloud/pagination"
     9  )
    10  
    11  // Message contains all the information associated with an OpenStack
    12  // Message.
    13  type Message struct {
    14  	// The message ID
    15  	ID string `json:"id"`
    16  	// The UUID of the project where the message was created
    17  	ProjectID string `json:"project_id"`
    18  	// The ID of the action during which the message was created
    19  	ActionID string `json:"action_id"`
    20  	// The ID of the message detail
    21  	DetailID string `json:"detail_id"`
    22  	// The message level
    23  	MessageLevel string `json:"message_level"`
    24  	// The UUID of the request during which the message was created
    25  	RequestID string `json:"request_id"`
    26  	// The UUID of the resource for which the message was created
    27  	ResourceID string `json:"resource_id"`
    28  	// The type of the resource for which the message was created
    29  	ResourceType string `json:"resource_type"`
    30  	// The message text
    31  	UserMessage string `json:"user_message"`
    32  	// The date and time stamp when the message was created
    33  	CreatedAt time.Time `json:"-"`
    34  	// The date and time stamp when the message will expire
    35  	ExpiresAt time.Time `json:"-"`
    36  }
    37  
    38  func (r *Message) UnmarshalJSON(b []byte) error {
    39  	type tmp Message
    40  	var s struct {
    41  		tmp
    42  		CreatedAt gophercloud.JSONRFC3339MilliNoZ `json:"created_at"`
    43  		ExpiresAt gophercloud.JSONRFC3339MilliNoZ `json:"expires_at"`
    44  	}
    45  	err := json.Unmarshal(b, &s)
    46  	if err != nil {
    47  		return err
    48  	}
    49  	*r = Message(s.tmp)
    50  
    51  	r.CreatedAt = time.Time(s.CreatedAt)
    52  	r.ExpiresAt = time.Time(s.ExpiresAt)
    53  
    54  	return nil
    55  }
    56  
    57  type commonResult struct {
    58  	gophercloud.Result
    59  }
    60  
    61  // MessagePage is a pagination.pager that is returned from a call to the List function.
    62  type MessagePage struct {
    63  	pagination.SinglePageBase
    64  }
    65  
    66  // IsEmpty returns true if a ListResult contains no Messages.
    67  func (r MessagePage) IsEmpty() (bool, error) {
    68  	if r.StatusCode == 204 {
    69  		return true, nil
    70  	}
    71  
    72  	messages, err := ExtractMessages(r)
    73  	return len(messages) == 0, err
    74  }
    75  
    76  // ExtractMessages extracts and returns Messages. It is used while
    77  // iterating over a messages.List call.
    78  func ExtractMessages(r pagination.Page) ([]Message, error) {
    79  	var s struct {
    80  		Messages []Message `json:"messages"`
    81  	}
    82  	err := (r.(MessagePage)).ExtractInto(&s)
    83  	return s.Messages, err
    84  }
    85  
    86  // Extract will get the Message object out of the commonResult object.
    87  func (r commonResult) Extract() (*Message, error) {
    88  	var s struct {
    89  		Message *Message `json:"message"`
    90  	}
    91  	err := r.ExtractInto(&s)
    92  	return s.Message, err
    93  }
    94  
    95  // DeleteResult contains the response body and error from a Delete request.
    96  type DeleteResult struct {
    97  	gophercloud.ErrResult
    98  }
    99  
   100  // GetResult contains the response body and error from a Get request.
   101  type GetResult struct {
   102  	commonResult
   103  }