-
Notifications
You must be signed in to change notification settings - Fork 102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update Run Tasks for global config and stages #865
Changes from all commits
7b6d0ad
44b44d5
bf80303
3cbccb0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TIL that jsonapi doesn't automatically decode json annotated attribute object types. It's certainly in spec to have an object within an attribute. I will take a closer look after I finish this review. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
DERP ME ... we are using a fork |
||
|
||
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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package tfe | ||
|
||
// A private struct we need for unmarshalling | ||
type internalWorkspaceRunTask struct { | ||
ID string `jsonapi:"primary,workspace-tasks"` | ||
EnforcementLevel TaskEnforcementLevel `jsonapi:"attr,enforcement-level"` | ||
Stage Stage `jsonapi:"attr,stage"` | ||
Stages []string `jsonapi:"attr,stages"` | ||
|
||
RunTask *RunTask `jsonapi:"relation,task"` | ||
Workspace *Workspace `jsonapi:"relation,workspace"` | ||
} | ||
|
||
// Due to https://github.com/google/jsonapi/issues/74 we must first unmarshall using map[string]interface{} | ||
// and then perform our own conversion for the Stages | ||
func (irt internalWorkspaceRunTask) ToWorkspaceRunTask() *WorkspaceRunTask { | ||
obj := WorkspaceRunTask{ | ||
ID: irt.ID, | ||
EnforcementLevel: irt.EnforcementLevel, | ||
Stage: irt.Stage, | ||
Stages: make([]Stage, len(irt.Stages)), | ||
RunTask: irt.RunTask, | ||
Workspace: irt.Workspace, | ||
} | ||
|
||
for idx, val := range irt.Stages { | ||
obj.Stages[idx] = Stage(val) | ||
} | ||
|
||
return &obj | ||
} | ||
|
||
// A private struct we need for unmarshalling | ||
type internalWorkspaceRunTaskList struct { | ||
*Pagination | ||
Items []*internalWorkspaceRunTask | ||
} | ||
|
||
// Due to https://github.com/google/jsonapi/issues/74 we must first unmarshall using | ||
// the internal WorkspaceRunTask struct and convert that a WorkspaceRunTask | ||
func (irt internalWorkspaceRunTaskList) ToWorkspaceRunTaskList() *WorkspaceRunTaskList { | ||
obj := WorkspaceRunTaskList{ | ||
Pagination: irt.Pagination, | ||
Items: make([]*WorkspaceRunTask, len(irt.Items)), | ||
} | ||
|
||
for idx, src := range irt.Items { | ||
if src != nil { | ||
obj.Items[idx] = src.ToWorkspaceRunTask() | ||
} | ||
} | ||
|
||
return &obj | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice!! 👍