Skip to content
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

added workflow template service #37

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions awx.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ var mandatoryFields = []string{}
type AWX struct {
client *Client

PingService *PingService
InventoriesService *InventoriesService
InventoryUpdatesService *InventoryUpdatesService
JobService *JobService
JobTemplateService *JobTemplateService
ProjectService *ProjectService
ProjectUpdatesService *ProjectUpdatesService
UserService *UserService
GroupService *GroupService
HostService *HostService
PingService *PingService
InventoriesService *InventoriesService
InventoryUpdatesService *InventoryUpdatesService
JobService *JobService
JobTemplateService *JobTemplateService
ProjectService *ProjectService
ProjectUpdatesService *ProjectUpdatesService
UserService *UserService
GroupService *GroupService
HostService *HostService
WorkflowJobTemplateService *WorkflowJobTemplateService
}

// Client implement http client.
Expand Down
145 changes: 145 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,3 +716,148 @@ type Host struct {
InsightsSystemID interface{} `json:"insights_system_id"`
AnsibleFactsModified interface{} `json:"ansible_facts_modified"`
}

// JobTemplate represents the awx api job template.
godiaz89 marked this conversation as resolved.
Show resolved Hide resolved
type WorkflowJobTemplate struct {
ID int `json:"id"`
Type string `json:"type"`
URL string `json:"url"`
Related *Related `json:"related"`
SummaryFields *Summary `json:"summary_fields"`
Created time.Time `json:"created"`
Modified time.Time `json:"modified"`
Name string `json:"name"`
Description string `json:"description"`
Inventory int `json:"inventory"`
Limit string `json:"limit"`
ExtraVars string `json:"extra_vars"`
LastJobRun interface{} `json:"last_job_run"`
LastJobFailed bool `json:"last_job_failed"`
NextJobRun interface{} `json:"next_job_run"`
Status string `json:"status"`
AskVariablesOnLaunch bool `json:"ask_variables_on_launch"`
AskLimitOnLaunch bool `json:"ask_limit_on_launch"`
AskInventoryOnLaunch bool `json:"ask_inventory_on_launch"`
SurveyEnabled bool `json:"survey_enabled"`
AllowSimultaneous bool `json:"allow_simultaneous"`
ScmBranch string `json:"scm_branch"`
AllowScmBranchOnLaunch bool `json:"ask_scm_branch_on_launch"`
}

type WorkflowJobLaunch struct {
Job int `json:"job"`
IgnoredFields map[string]string `json:"ignored_fields"`
ID int `json:"id"`
Type string `json:"type"`
URL string `json:"url"`
Related *Related `json:"related"`
SummaryFields *Summary `json:"summary_fields"`
Created time.Time `json:"created"`
Modified time.Time `json:"modified"`
Name string `json:"name"`
Description string `json:"description"`
JobType string `json:"job_type"`
Inventory int `json:"inventory"`
Project int `json:"project"`
Playbook string `json:"playbook"`
Forks int `json:"forks"`
Limit string `json:"limit"`
Verbosity int `json:"verbosity"`
ExtraVars string `json:"extra_vars"`
JobTags string `json:"job_tags"`
ForceHandlers bool `json:"force_handlers"`
SkipTags string `json:"skip_tags"`
StartAtTask string `json:"start_at_task"`
Timeout int `json:"timeout"`
UseFactCache bool `json:"use_fact_cache"`
UnifiedJobTemplate int `json:"unified_job_template"`
LaunchType string `json:"launch_type"`
Status string `json:"status"`
Failed bool `json:"failed"`
Started interface{} `json:"started"`
Finished interface{} `json:"finished"`
Elapsed int `json:"elapsed"`
JobArgs string `json:"job_args"`
JobCwd string `json:"job_cwd"`
JobEnv map[string]string `json:"job_env"`
JobExplanation string `json:"job_explanation"`
ExecutionNode string `json:"execution_node"`
ResultTraceback string `json:"result_traceback"`
EventProcessingFinished bool `json:"event_processing_finished"`
JobTemplate int `json:"job_template"`
PasswordsNeededToStart []interface{} `json:"passwords_needed_to_start"`
AskDiffModeOnLaunch bool `json:"ask_diff_mode_on_launch"`
AskVariablesOnLaunch bool `json:"ask_variables_on_launch"`
AskLimitOnLaunch bool `json:"ask_limit_on_launch"`
AskTagsOnLaunch bool `json:"ask_tags_on_launch"`
AskSkipTagsOnLaunch bool `json:"ask_skip_tags_on_launch"`
AskJobTypeOnLaunch bool `json:"ask_job_type_on_launch"`
AskVerbosityOnLaunch bool `json:"ask_verbosity_on_launch"`
AskInventoryOnLaunch bool `json:"ask_inventory_on_launch"`
AskCredentialOnLaunch bool `json:"ask_credential_on_launch"`
AllowSimultaneous bool `json:"allow_simultaneous"`
Artifacts map[string]string `json:"artifacts"`
ScmRevision string `json:"scm_revision"`
InstanceGroup interface{} `json:"instance_group"`
DiffMode bool `json:"diff_mode"`
Credential int `json:"credential"`
VaultCredential interface{} `json:"vault_credential"`
}

type WorkflowJob struct {
ID int `json:"id"`
Type string `json:"type"`
URL string `json:"url"`
Related *Related `json:"related"`
SummaryFields *Summary `json:"summary_fields"`
Created time.Time `json:"created"`
Modified time.Time `json:"modified"`
Name string `json:"name"`
Description string `json:"description"`
JobType string `json:"job_type"`
Inventory int `json:"inventory"`
Project int `json:"project"`
Playbook string `json:"playbook"`
Forks int `json:"forks"`
Limit string `json:"limit"`
Verbosity int `json:"verbosity"`
ExtraVars string `json:"extra_vars"`
JobTags string `json:"job_tags"`
ForceHandlers bool `json:"force_handlers"`
SkipTags string `json:"skip_tags"`
StartAtTask string `json:"start_at_task"`
Timeout int `json:"timeout"`
UseFactCache bool `json:"use_fact_cache"`
UnifiedJobTemplate int `json:"unified_job_template"`
LaunchType string `json:"launch_type"`
Status string `json:"status"`
Failed bool `json:"failed"`
Started time.Time `json:"started"`
Finished time.Time `json:"finished"`
Elapsed float64 `json:"elapsed"`
JobArgs string `json:"job_args"`
JobCwd string `json:"job_cwd"`
JobEnv map[string]string `json:"job_env"`
JobExplanation string `json:"job_explanation"`
ExecutionNode string `json:"execution_node"`
ResultTraceback string `json:"result_traceback"`
EventProcessingFinished bool `json:"event_processing_finished"`
JobTemplate int `json:"job_template"`
PasswordsNeededToStart []interface{} `json:"passwords_needed_to_start"`
AskDiffModeOnLaunch bool `json:"ask_diff_mode_on_launch"`
AskVariablesOnLaunch bool `json:"ask_variables_on_launch"`
AskLimitOnLaunch bool `json:"ask_limit_on_launch"`
AskTagsOnLaunch bool `json:"ask_tags_on_launch"`
AskSkipTagsOnLaunch bool `json:"ask_skip_tags_on_launch"`
AskJobTypeOnLaunch bool `json:"ask_job_type_on_launch"`
AskVerbosityOnLaunch bool `json:"ask_verbosity_on_launch"`
AskInventoryOnLaunch bool `json:"ask_inventory_on_launch"`
AskCredentialOnLaunch bool `json:"ask_credential_on_launch"`
AllowSimultaneous bool `json:"allow_simultaneous"`
Artifacts map[string]string `json:"artifacts"`
ScmRevision string `json:"scm_revision"`
InstanceGroup int `json:"instance_group"`
DiffMode bool `json:"diff_mode"`
Credential *Credential `json:"credential"`
VaultCredential interface{} `json:"vault_credential"`
}
122 changes: 122 additions & 0 deletions workflow_template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package awx

import (
"bytes"
"encoding/json"
"errors"
"fmt"
)

// WorkflowJobTemplateService implements awx job template apis.
type WorkflowJobTemplateService struct {
client *Client
}

// ListWorkflowJobTemplatesResponse represents `ListWorkflowJobTemplates` endpoint response.
type ListWorkflowJobTemplatesResponse struct {
Pagination
Results []*WorkflowJobTemplate `json:"results"`
}

// ListWorkflowJobTemplates shows a list of job templates.
func (jt *WorkflowJobTemplateService) ListWorkflowJobTemplates(params map[string]string) ([]*WorkflowJobTemplate, *ListWorkflowJobTemplatesResponse, error) {
result := new(ListWorkflowJobTemplatesResponse)
endpoint := "/api/v2/workflow_job_templates/"
resp, err := jt.client.Requester.GetJSON(endpoint, result, params)
if err != nil {
return nil, result, err
}

if err := CheckResponse(resp); err != nil {
return nil, result, err
}

return result.Results, result, nil
}

// Launch lauchs a job with the job template.
func (jt *WorkflowJobTemplateService) Launch(id int, data map[string]interface{}, params map[string]string) (*JobLaunch, error) {
result := new(JobLaunch)
endpoint := fmt.Sprintf("/api/v2/workflow_job_templates/%d/launch/", id)
payload, err := json.Marshal(data)
if err != nil {
return nil, err
}

resp, err := jt.client.Requester.PostJSON(endpoint, bytes.NewReader(payload), result, params)
if err != nil {
return nil, err
}

if err := CheckResponse(resp); err != nil {
return nil, err
}

// in case invalid job id return
if result.Job == 0 {
return nil, errors.New("invalid job id 0")
}

return result, nil
}

// CreateWorkflowJobTemplate creates a job template
func (jt *WorkflowJobTemplateService) CreateWorkflowJobTemplate(data map[string]interface{}, params map[string]string) (*WorkflowJobTemplate, error) {
result := new(WorkflowJobTemplate)
mandatoryFields = []string{"name", "job_type", "inventory", "project"}
validate, status := ValidateParams(data, mandatoryFields)
if !status {
err := fmt.Errorf("Mandatory input arguments are absent: %s", validate)
return nil, err
}
endpoint := "/api/v2/workflow_job_templates/"
payload, err := json.Marshal(data)
if err != nil {
return nil, err
}

resp, err := jt.client.Requester.PostJSON(endpoint, bytes.NewReader(payload), result, params)
if err != nil {
return nil, err
}
if err := CheckResponse(resp); err != nil {
return nil, err
}
return result, nil
}

// UpdateWorkflowJobTemplate updates a job template
func (jt *WorkflowJobTemplateService) UpdateWorkflowJobTemplate(id int, data map[string]interface{}, params map[string]string) (*WorkflowJobTemplate, error) {
result := new(WorkflowJobTemplate)
endpoint := fmt.Sprintf("/api/v2/workflow_job_templates/%d", id)
payload, err := json.Marshal(data)
if err != nil {
return nil, err
}

resp, err := jt.client.Requester.PatchJSON(endpoint, bytes.NewReader(payload), result, params)
if err != nil {
return nil, err
}
if err := CheckResponse(resp); err != nil {
return nil, err
}
return result, nil
}

// DeleteWorkflowJobTemplate deletes a job template
func (jt *WorkflowJobTemplateService) DeleteWorkflowJobTemplate(id int) (*WorkflowJobTemplate, error) {
result := new(WorkflowJobTemplate)
endpoint := fmt.Sprintf("/api/v2/workflow_job_templates/%d", id)

resp, err := jt.client.Requester.Delete(endpoint, result, nil)
if err != nil {
return nil, err
}

if err := CheckResponse(resp); err != nil {
return nil, err
}

return result, nil
}
Loading