github.com/gophercloud/gophercloud@v1.11.0/openstack/clustering/v1/receivers/results.go (about)

     1  package receivers
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/gophercloud/gophercloud"
     8  	"github.com/gophercloud/gophercloud/pagination"
     9  )
    10  
    11  // Receiver represent a detailed receiver
    12  type Receiver struct {
    13  	Action    string                 `json:"action"`
    14  	Actor     map[string]interface{} `json:"actor"`
    15  	Channel   map[string]interface{} `json:"channel"`
    16  	ClusterID string                 `json:"cluster_id"`
    17  	CreatedAt time.Time              `json:"-"`
    18  	Domain    string                 `json:"domain"`
    19  	ID        string                 `json:"id"`
    20  	Name      string                 `json:"name"`
    21  	Params    map[string]interface{} `json:"params"`
    22  	Project   string                 `json:"project"`
    23  	Type      string                 `json:"type"`
    24  	UpdatedAt time.Time              `json:"-"`
    25  	User      string                 `json:"user"`
    26  }
    27  
    28  func (r *Receiver) UnmarshalJSON(b []byte) error {
    29  	type tmp Receiver
    30  	var s struct {
    31  		tmp
    32  		CreatedAt string `json:"created_at"`
    33  		UpdatedAt string `json:"updated_at"`
    34  	}
    35  	err := json.Unmarshal(b, &s)
    36  	if err != nil {
    37  		return err
    38  	}
    39  	*r = Receiver(s.tmp)
    40  
    41  	if s.CreatedAt != "" {
    42  		r.CreatedAt, err = time.Parse(time.RFC3339, s.CreatedAt)
    43  		if err != nil {
    44  			return err
    45  		}
    46  	}
    47  
    48  	if s.UpdatedAt != "" {
    49  		r.UpdatedAt, err = time.Parse(time.RFC3339, s.UpdatedAt)
    50  		if err != nil {
    51  			return err
    52  		}
    53  	}
    54  
    55  	return nil
    56  }
    57  
    58  // commonResult is the response of a base result.
    59  type commonResult struct {
    60  	gophercloud.Result
    61  }
    62  
    63  // Extract interprets any commonResult-based result as a Receiver.
    64  func (r commonResult) Extract() (*Receiver, error) {
    65  	var s struct {
    66  		Receiver *Receiver `json:"receiver"`
    67  	}
    68  	err := r.ExtractInto(&s)
    69  	return s.Receiver, err
    70  }
    71  
    72  // CreateResult is the result of a Create operation. Call its Extract method
    73  // to interpret it as a Receiver.
    74  type CreateResult struct {
    75  	commonResult
    76  }
    77  
    78  // GetResult is the result for of a Get operation. Call its Extract method
    79  // to interpret it as a Receiver.
    80  type GetResult struct {
    81  	commonResult
    82  }
    83  
    84  // UpdateResult is the result of a Update operation. Call its Extract method
    85  // to interpret it as a Receiver.
    86  type UpdateResult struct {
    87  	commonResult
    88  }
    89  
    90  // DeleteResult is the result from a Delete operation. Call its ExtractErr
    91  // method to determine if the call succeeded or failed.
    92  type DeleteResult struct {
    93  	gophercloud.ErrResult
    94  }
    95  
    96  // NotifyResult is the result from a Notify operation. Call its Extract
    97  // method to determine if the call succeeded or failed.
    98  type NotifyResult struct {
    99  	commonResult
   100  }
   101  
   102  // ReceiverPage contains a single page of all nodes from a List operation.
   103  type ReceiverPage struct {
   104  	pagination.LinkedPageBase
   105  }
   106  
   107  // IsEmpty determines if a ReceiverPage contains any results.
   108  func (page ReceiverPage) IsEmpty() (bool, error) {
   109  	if page.StatusCode == 204 {
   110  		return true, nil
   111  	}
   112  
   113  	receivers, err := ExtractReceivers(page)
   114  	return len(receivers) == 0, err
   115  }
   116  
   117  // ExtractReceivers returns a slice of Receivers from the List operation.
   118  func ExtractReceivers(r pagination.Page) ([]Receiver, error) {
   119  	var s struct {
   120  		Receivers []Receiver `json:"receivers"`
   121  	}
   122  	err := (r.(ReceiverPage)).ExtractInto(&s)
   123  	return s.Receivers, err
   124  }
   125  
   126  // Extract returns action for notify receivers
   127  func (r NotifyResult) Extract() (string, error) {
   128  	requestID := r.Header.Get("X-Openstack-Request-Id")
   129  	return requestID, r.Err
   130  }