-
Notifications
You must be signed in to change notification settings - Fork 102
/
internal_run_task.go
103 lines (89 loc) · 3.1 KB
/
internal_run_task.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfe
// A private struct we need for unmarshalling
type internalRunTask struct {
ID string `jsonapi:"primary,tasks"`
Name string `jsonapi:"attr,name"`
URL string `jsonapi:"attr,url"`
Description string `jsonapi:"attr,description"`
Category string `jsonapi:"attr,category"`
HMACKey *string `jsonapi:"attr,hmac-key,omitempty"`
Enabled bool `jsonapi:"attr,enabled"`
RawGlobal map[string]interface{} `jsonapi:"attr,global-configuration,omitempty"`
Organization *Organization `jsonapi:"relation,organization"`
WorkspaceRunTasks []*internalWorkspaceRunTask `jsonapi:"relation,workspace-tasks"`
}
// Due to https://github.com/google/jsonapi/issues/74 we must first unmarshall using map[string]interface{}
// and then perform our own conversion from the map into a GlobalRunTask struct
func (irt internalRunTask) ToRunTask() *RunTask {
obj := RunTask{
ID: irt.ID,
Name: irt.Name,
URL: irt.URL,
Description: irt.Description,
Category: irt.Category,
HMACKey: irt.HMACKey,
Enabled: irt.Enabled,
Organization: irt.Organization,
}
// Convert the WorkspaceRunTasks
workspaceTasks := make([]*WorkspaceRunTask, len(irt.WorkspaceRunTasks))
for idx, rawTask := range irt.WorkspaceRunTasks {
if rawTask != nil {
workspaceTasks[idx] = rawTask.ToWorkspaceRunTask()
}
}
obj.WorkspaceRunTasks = workspaceTasks
// Check if the global configuration exists
if val, ok := irt.RawGlobal["enabled"]; !ok {
// The enabled property is required so we can assume now that the
// global configuration was not supplied
return &obj
} else if boolVal, ok := val.(bool); !ok {
// The enabled property exists but it is invalid (Couldn't cast to boolean)
// so assume the global configuration was not supplied
return &obj
} else {
obj.Global = &GlobalRunTask{
Enabled: boolVal,
}
}
// Global Enforcement Level
if val, ok := irt.RawGlobal["enforcement-level"]; ok {
if stringVal, ok := val.(string); ok {
obj.Global.EnforcementLevel = TaskEnforcementLevel(stringVal)
}
}
// Global Stages
if val, ok := irt.RawGlobal["stages"]; ok {
if stringsVal, ok := val.([]interface{}); ok {
obj.Global.Stages = make([]Stage, len(stringsVal))
for idx, stageName := range stringsVal {
if stringVal, ok := stageName.(string); ok {
obj.Global.Stages[idx] = Stage(stringVal)
}
}
}
}
return &obj
}
// A private struct we need for unmarshalling
type internalRunTaskList struct {
*Pagination
Items []*internalRunTask
}
// Due to https://github.com/google/jsonapi/issues/74 we must first unmarshall using
// the internal RunTask struct and convert that a RunTask
func (irt internalRunTaskList) ToRunTaskList() *RunTaskList {
obj := RunTaskList{
Pagination: irt.Pagination,
Items: make([]*RunTask, len(irt.Items)),
}
for idx, src := range irt.Items {
if src != nil {
obj.Items[idx] = src.ToRunTask()
}
}
return &obj
}