github.com/gophercloud/gophercloud@v1.11.0/openstack/compute/v2/extensions/instanceactions/results.go (about)

     1  package instanceactions
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/gophercloud/gophercloud"
     8  	"github.com/gophercloud/gophercloud/pagination"
     9  )
    10  
    11  // InstanceAction represents an instance action.
    12  type InstanceAction struct {
    13  	// Action is the name of the action.
    14  	Action string `json:"action"`
    15  
    16  	// InstanceUUID is the UUID of the instance.
    17  	InstanceUUID string `json:"instance_uuid"`
    18  
    19  	// Message is the related error message for when an action fails.
    20  	Message string `json:"message"`
    21  
    22  	// Project ID is the ID of the project which initiated the action.
    23  	ProjectID string `json:"project_id"`
    24  
    25  	// RequestID is the ID generated when performing the action.
    26  	RequestID string `json:"request_id"`
    27  
    28  	// StartTime is the time the action started.
    29  	StartTime time.Time `json:"-"`
    30  
    31  	// UserID is the ID of the user which initiated the action.
    32  	UserID string `json:"user_id"`
    33  }
    34  
    35  // UnmarshalJSON converts our JSON API response into our instance action struct
    36  func (i *InstanceAction) UnmarshalJSON(b []byte) error {
    37  	type tmp InstanceAction
    38  	var s struct {
    39  		tmp
    40  		StartTime gophercloud.JSONRFC3339MilliNoZ `json:"start_time"`
    41  	}
    42  	err := json.Unmarshal(b, &s)
    43  	if err != nil {
    44  		return err
    45  	}
    46  	*i = InstanceAction(s.tmp)
    47  
    48  	i.StartTime = time.Time(s.StartTime)
    49  
    50  	return err
    51  }
    52  
    53  // InstanceActionPage abstracts the raw results of making a List() request
    54  // against the API. As OpenStack extensions may freely alter the response bodies
    55  // of structures returned to the client, you may only safely access the data
    56  // provided through the ExtractInstanceActions call.
    57  type InstanceActionPage struct {
    58  	pagination.SinglePageBase
    59  }
    60  
    61  // IsEmpty returns true if an InstanceActionPage contains no instance actions.
    62  func (r InstanceActionPage) IsEmpty() (bool, error) {
    63  	if r.StatusCode == 204 {
    64  		return true, nil
    65  	}
    66  
    67  	instanceactions, err := ExtractInstanceActions(r)
    68  	return len(instanceactions) == 0, err
    69  }
    70  
    71  // ExtractInstanceActions interprets a page of results as a slice
    72  // of InstanceAction.
    73  func ExtractInstanceActions(r pagination.Page) ([]InstanceAction, error) {
    74  	var resp []InstanceAction
    75  	err := ExtractInstanceActionsInto(r, &resp)
    76  	return resp, err
    77  }
    78  
    79  // Event represents an event of instance action.
    80  type Event struct {
    81  	// Event is the name of the event.
    82  	Event string `json:"event"`
    83  
    84  	// Host is the host of the event.
    85  	// This requires microversion 2.62 or later.
    86  	Host *string `json:"host"`
    87  
    88  	// HostID is the host id of the event.
    89  	// This requires microversion 2.62 or later.
    90  	HostID *string `json:"hostId"`
    91  
    92  	// Result is the result of the event.
    93  	Result string `json:"result"`
    94  
    95  	// Traceback is the traceback stack if an error occurred.
    96  	Traceback string `json:"traceback"`
    97  
    98  	// StartTime is the time the action started.
    99  	StartTime time.Time `json:"-"`
   100  
   101  	// FinishTime is the time the event finished.
   102  	FinishTime time.Time `json:"-"`
   103  }
   104  
   105  // UnmarshalJSON converts our JSON API response into our instance action struct.
   106  func (e *Event) UnmarshalJSON(b []byte) error {
   107  	type tmp Event
   108  	var s struct {
   109  		tmp
   110  		StartTime  gophercloud.JSONRFC3339MilliNoZ `json:"start_time"`
   111  		FinishTime gophercloud.JSONRFC3339MilliNoZ `json:"finish_time"`
   112  	}
   113  	err := json.Unmarshal(b, &s)
   114  	if err != nil {
   115  		return err
   116  	}
   117  	*e = Event(s.tmp)
   118  
   119  	e.StartTime = time.Time(s.StartTime)
   120  	e.FinishTime = time.Time(s.FinishTime)
   121  
   122  	return err
   123  }
   124  
   125  // InstanceActionDetail represents the details of an Action.
   126  type InstanceActionDetail struct {
   127  	// Action is the name of the Action.
   128  	Action string `json:"action"`
   129  
   130  	// InstanceUUID is the UUID of the instance.
   131  	InstanceUUID string `json:"instance_uuid"`
   132  
   133  	// Message is the related error message for when an action fails.
   134  	Message string `json:"message"`
   135  
   136  	// Project ID is the ID of the project which initiated the action.
   137  	ProjectID string `json:"project_id"`
   138  
   139  	// RequestID is the ID generated when performing the action.
   140  	RequestID string `json:"request_id"`
   141  
   142  	// UserID is the ID of the user which initiated the action.
   143  	UserID string `json:"user_id"`
   144  
   145  	// Events is the list of events of the action.
   146  	// This requires microversion 2.50 or later.
   147  	Events *[]Event `json:"events"`
   148  
   149  	// UpdatedAt last update date of the action.
   150  	// This requires microversion 2.58 or later.
   151  	UpdatedAt *time.Time `json:"-"`
   152  
   153  	// StartTime is the time the action started.
   154  	StartTime time.Time `json:"-"`
   155  }
   156  
   157  // UnmarshalJSON converts our JSON API response into our instance action struct
   158  func (i *InstanceActionDetail) UnmarshalJSON(b []byte) error {
   159  	type tmp InstanceActionDetail
   160  	var s struct {
   161  		tmp
   162  		UpdatedAt *gophercloud.JSONRFC3339MilliNoZ `json:"updated_at"`
   163  		StartTime gophercloud.JSONRFC3339MilliNoZ  `json:"start_time"`
   164  	}
   165  	err := json.Unmarshal(b, &s)
   166  	if err != nil {
   167  		return err
   168  	}
   169  	*i = InstanceActionDetail(s.tmp)
   170  
   171  	i.UpdatedAt = (*time.Time)(s.UpdatedAt)
   172  	i.StartTime = time.Time(s.StartTime)
   173  	return err
   174  }
   175  
   176  // InstanceActionResult is the result handler of Get.
   177  type InstanceActionResult struct {
   178  	gophercloud.Result
   179  }
   180  
   181  // Extract interprets a result as an InstanceActionDetail.
   182  func (r InstanceActionResult) Extract() (InstanceActionDetail, error) {
   183  	var s InstanceActionDetail
   184  	err := r.ExtractInto(&s)
   185  	return s, err
   186  }
   187  
   188  func (r InstanceActionResult) ExtractInto(v interface{}) error {
   189  	return r.Result.ExtractIntoStructPtr(v, "instanceAction")
   190  }
   191  
   192  func ExtractInstanceActionsInto(r pagination.Page, v interface{}) error {
   193  	return r.(InstanceActionPage).Result.ExtractIntoSlicePtr(v, "instanceActions")
   194  }