File: results.go

package info (click to toggle)
golang-github-gophercloud-gophercloud 0.0~git20180917.45f1c769-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,768 kB
  • sloc: sh: 98; makefile: 14
file content (109 lines) | stat: -rw-r--r-- 3,077 bytes parent folder | download
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
106
107
108
109
package crontriggers

import (
	"encoding/json"
	"time"

	"github.com/gophercloud/gophercloud"
)

type commonResult struct {
	gophercloud.Result
}

// CreateResult is the response of a Post operations. Call its Extract method to interpret it as a CronTrigger.
type CreateResult struct {
	commonResult
}

// Extract helps to get a CronTrigger struct from a Get or a Create function.
func (r commonResult) Extract() (*CronTrigger, error) {
	var s CronTrigger
	err := r.ExtractInto(&s)
	return &s, err
}

// CronTrigger represents a workflow cron trigger on OpenStack mistral API.
type CronTrigger struct {
	// ID is the cron trigger's unique ID.
	ID string `json:"id"`

	// Name is the name of the cron trigger.
	Name string `json:"name"`

	// Pattern is the cron-like style pattern to execute the workflow.
	// Example of value: "* * * * *"
	Pattern string `json:"pattern"`

	// ProjectID is the project id owner of the cron trigger.
	ProjectID string `json:"project_id"`

	// RemainingExecutions is the number of remaining executions of this trigger.
	RemainingExecutions int `json:"remaining_executions"`

	// Scope is the scope of the trigger.
	// Values can be "private" or "public".
	Scope string `json:"scope"`

	// WorkflowID is the ID of the workflow linked to the trigger.
	WorkflowID string `json:"workflow_id"`

	// WorkflowName is the name of the workflow linked to the trigger.
	WorkflowName string `json:"workflow_name"`

	// WorkflowInput contains the workflow input values.
	WorkflowInput map[string]interface{} `json:"-"`

	// WorkflowParams contains workflow type specific parameters.
	WorkflowParams map[string]interface{} `json:"-"`

	// CreatedAt contains the cron trigger creation date.
	CreatedAt time.Time `json:"-"`

	// FirstExecutionTime is the date of the first execution of the trigger.
	FirstExecutionTime *time.Time `json:"-"`

	// NextExecutionTime is the date of the next execution of the trigger.
	NextExecutionTime *time.Time `json:"-"`
}

// UnmarshalJSON implements unmarshalling custom types
func (r *CronTrigger) UnmarshalJSON(b []byte) error {
	type tmp CronTrigger
	var s struct {
		tmp
		CreatedAt          gophercloud.JSONRFC3339ZNoTNoZ  `json:"created_at"`
		FirstExecutionTime *gophercloud.JSONRFC3339ZNoTNoZ `json:"first_execution_time"`
		NextExecutionTime  *gophercloud.JSONRFC3339ZNoTNoZ `json:"next_execution_time"`
		WorkflowInput      string                          `json:"workflow_input"`
		WorkflowParams     string                          `json:"workflow_params"`
	}

	err := json.Unmarshal(b, &s)
	if err != nil {
		return err
	}

	*r = CronTrigger(s.tmp)

	r.CreatedAt = time.Time(s.CreatedAt)
	if s.FirstExecutionTime != nil {
		t := time.Time(*s.FirstExecutionTime)
		r.FirstExecutionTime = &t
	}

	if s.NextExecutionTime != nil {
		t := time.Time(*s.NextExecutionTime)
		r.NextExecutionTime = &t
	}

	if err := json.Unmarshal([]byte(s.WorkflowInput), &r.WorkflowInput); err != nil {
		return err
	}

	if err := json.Unmarshal([]byte(s.WorkflowParams), &r.WorkflowParams); err != nil {
		return err
	}

	return nil
}