forked from stmcginnis/gofish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaskervice.go
105 lines (87 loc) · 3.3 KB
/
taskervice.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//
// SPDX-License-Identifier: BSD-3-Clause
//
package redfish
import (
"encoding/json"
"reflect"
"time"
"github.com/stmcginnis/gofish/common"
)
type TaskOverWritePolicy string
const (
// ManualTaskOverWritePolicy Completed tasks are not automatically overwritten.
ManualTaskOverWritePolicy TaskOverWritePolicy = "Manual"
// OldestTaskOverWritePolicy Oldest completed tasks are overwritten.
OldestTaskOverWritePolicy TaskOverWritePolicy = "Oldest"
)
// TaskService is used to represent the task service offered by the redfish API
type TaskService struct {
common.Entity
// ODataContext is the odata context.
ODataContext string `json:"@odata.context"`
// ODataType is the odata type.
ODataType string `json:"@odata.type"`
// Description provides a description of this resource.
Description string
// CompletedTaskOverWritePolicy how to handle the completed tasks.
CompletedTaskOverWritePolicy TaskOverWritePolicy
// DateTime system time.
DateTime time.Time
// LifeCycleEventOnTaskStateChange whether an event is reported when the task status is changed.
LifeCycleEventOnTaskStateChange bool
// Oem shall contain the OEM extensions. All values for properties that this object contains shall conform to the
// Redfish Specification-described requirements.
OEM json.RawMessage `json:"Oem"`
// ServiceEnabled indicates whether this service isenabled.
ServiceEnabled bool
// Status describes the status and health of a resource and its children.
Status common.Status
// TaskAutoDeleteTimeoutMinutes shall contain the number of minutes after which a completed task, where TaskState
// contains the value 'Completed', 'Killed', 'Cancelled', or 'Exception', is deleted by the service.
TaskAutoDeleteTimeoutMinutes int
// tasks points towards the tasks store endpoint
tasks string
// rawData holds the original serialized JSON so we can compare updates.
rawData []byte
}
// UnmarshalJSON unmarshals a TaskService object from the raw JSON.
func (taskService *TaskService) UnmarshalJSON(b []byte) error {
type temp TaskService
var t struct {
temp
Tasks common.Link
}
err := json.Unmarshal(b, &t)
if err != nil {
return err
}
// Extract the links to other entities for later
*taskService = TaskService(t.temp)
taskService.tasks = t.Tasks.String()
// This is a read/write object, so we need to save the raw object data for later
taskService.rawData = b
return nil
}
// Tasks gets the collection of tasks of this task service
func (taskService *TaskService) Tasks() ([]*Task, error) {
return ListReferencedTasks(taskService.GetClient(), taskService.tasks)
}
// Update commits updates to this object's properties to the running system.
func (taskService *TaskService) Update() error {
// Get a representation of the object's original state so we can find what
// to update.
original := new(TaskService)
original.UnmarshalJSON(taskService.rawData)
readWriteFields := []string{
"ServiceEnabled",
"TaskAutoDeleteTimeoutMinutes",
}
originalElement := reflect.ValueOf(original).Elem()
currentElement := reflect.ValueOf(taskService).Elem()
return taskService.Entity.Update(originalElement, currentElement, readWriteFields)
}
// GetTaskService will get a TaskService instance from the service.
func GetTaskService(c common.Client, uri string) (*TaskService, error) {
return common.GetObject[TaskService](c, uri)
}