Skip to content

Commit fb9f104

Browse files
vaxvmsrtagliabue
andauthored
feat: Merge various fix from community (#32)
* feat: Add api to create inventory source schedule * Add missing fields for job template API * Add support for organization association to instance groups * Unmarshall Summary field of Credential * Fix typo for organization galaxy credentials API endpoint --------- Co-authored-by: robyt96 <r.tagliabue@reply.it>
1 parent 7102639 commit fb9f104

5 files changed

Lines changed: 155 additions & 58 deletions

File tree

client/awx.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type AWX struct {
2929
CredentialTypeService *CredentialTypeService
3030
CredentialInputSourceService *CredentialInputSourceService
3131
InventorySourcesService *InventorySourcesService
32+
InventorySourcesSchedulesService *InventorySourcesSchedulesService
3233
InventoryGroupService *InventoryGroupService
3334
InstanceGroupsService *InstanceGroupsService
3435
NotificationTemplatesService *NotificationTemplatesService
@@ -173,6 +174,9 @@ func newAWX(c *Client) *AWX {
173174
InventorySourcesService: &InventorySourcesService{
174175
client: c,
175176
},
177+
InventorySourcesSchedulesService: &InventorySourcesSchedulesService{
178+
client: c,
179+
},
176180
InventoryGroupService: &InventoryGroupService{
177181
client: c,
178182
},
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package awx
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"log"
8+
)
9+
10+
const inventorySourcesSchedulesAPIEndpoint = "/api/v2/inventory_sources/%d/schedules/"
11+
12+
// InventorySourcesSchedulesService implements awx inventory sources schedules apis.
13+
type InventorySourcesSchedulesService struct {
14+
client *Client
15+
}
16+
17+
// ListInventorySourcesSchedules shows a list of schedules for a given inventory_source
18+
func (is *InventorySourcesSchedulesService) ListInventorySourcesSchedules(id int, params map[string]string) ([]*Schedule, *ListSchedulesResponse, error) {
19+
result := new(ListSchedulesResponse)
20+
resp, err := is.client.Requester.GetJSON(
21+
fmt.Sprintf(inventorySourcesSchedulesAPIEndpoint, id),
22+
result, params)
23+
if err != nil {
24+
return nil, result, err
25+
}
26+
27+
if err := CheckResponse(resp); err != nil {
28+
return nil, result, err
29+
}
30+
31+
return result.Results, result, nil
32+
}
33+
34+
// CreateInventorySourcesSchedule will create a schedule for an existing inventory_source
35+
func (is *InventorySourcesSchedulesService) CreateInventorySourcesSchedule(id int, data map[string]interface{}, params map[string]string) (*Schedule, error) {
36+
mandatoryFields = []string{"name", "rrule"}
37+
validate, status := ValidateParams(data, mandatoryFields)
38+
if !status {
39+
err := fmt.Errorf("mandatory input arguments are absent: %s", validate)
40+
return nil, err
41+
}
42+
43+
result := new(Schedule)
44+
payload, err := json.Marshal(data)
45+
if err != nil {
46+
return nil, err
47+
}
48+
49+
resp, err := is.client.Requester.PostJSON(
50+
fmt.Sprintf(inventorySourcesSchedulesAPIEndpoint, id),
51+
bytes.NewReader(payload), result, params,
52+
)
53+
54+
log.Println("OK")
55+
56+
if err != nil {
57+
return nil, err
58+
}
59+
60+
if err := CheckResponse(resp); err != nil {
61+
return nil, err
62+
}
63+
64+
return result, nil
65+
}

client/organizations.go

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,33 @@ func (p *OrganizationsService) DeleteOrganization(id int) (*Organization, error)
109109
return result, nil
110110
}
111111

112-
// DisAssociateGalaxyCredentials remove Credentials form an awx job template
112+
// DisAssociateGalaxyCredentials remove Credentials from an organization
113113
func (p *OrganizationsService) DisAssociateGalaxyCredentials(id int, data map[string]interface{}, params map[string]string) (*Organization, error) {
114+
return p.disAssociate(id, "galaxy_credentials", data, params)
115+
}
116+
117+
// AssociateGalaxyCredentials adding credentials to Organization.
118+
func (p *OrganizationsService) AssociateGalaxyCredentials(id int, data map[string]interface{}, params map[string]string) (*Organization, error) {
119+
return p.associate(id, "galaxy_credentials", data, params)
120+
}
121+
122+
// DisAssociateInstanceGroups remove instance_groups from an organization
123+
func (p *OrganizationsService) DisAssociateInstanceGroups(id int, data map[string]interface{}, params map[string]string) (*Organization, error) {
124+
return p.disAssociate(id, "instance_groups", data, params)
125+
}
126+
127+
// AssociateInstanceGroups adding instance_groups to Organization.
128+
func (p *OrganizationsService) AssociateInstanceGroups(id int, data map[string]interface{}, params map[string]string) (*Organization, error) {
129+
return p.associate(id, "instance_groups", data, params)
130+
}
131+
132+
// Associate associate an element to Organization.
133+
func (p *OrganizationsService) associate(id int, typ string, data map[string]interface{}, params map[string]string) (*Organization, error) {
114134
result := new(Organization)
115-
endpoint := fmt.Sprintf("%s%d/galaxy_credentials/", organizationsAPIEndpoint, id)
116-
data["disassociate"] = true
117-
mandatoryFields = []string{"id", "disassociate"}
135+
136+
endpoint := fmt.Sprintf("%s%d/%s/", organizationsAPIEndpoint, id, typ)
137+
data["associate"] = true
138+
mandatoryFields = []string{"id"}
118139
validate, status := ValidateParams(data, mandatoryFields)
119140
if !status {
120141
err := fmt.Errorf("mandatory input arguments are absent: %s", validate)
@@ -136,13 +157,12 @@ func (p *OrganizationsService) DisAssociateGalaxyCredentials(id int, data map[st
136157
return result, nil
137158
}
138159

139-
// AssociateGalaxyCredentials adding credentials to Organization.
140-
func (p *OrganizationsService) AssociateGalaxyCredentials(id int, data map[string]interface{}, params map[string]string) (*Organization, error) {
160+
// DisAssociate remove element from an organization
161+
func (p *OrganizationsService) disAssociate(id int, typ string, data map[string]interface{}, params map[string]string) (*Organization, error) {
141162
result := new(Organization)
142-
143-
endpoint := fmt.Sprintf("%s%d/galaxy_credentials/", organizationsAPIEndpoint, id)
144-
data["associate"] = true
145-
mandatoryFields = []string{"id"}
163+
endpoint := fmt.Sprintf("%s%d/%s/", organizationsAPIEndpoint, id, typ)
164+
data["disassociate"] = true
165+
mandatoryFields = []string{"id", "disassociate"}
146166
validate, status := ValidateParams(data, mandatoryFields)
147167
if !status {
148168
err := fmt.Errorf("mandatory input arguments are absent: %s", validate)

client/types.go

Lines changed: 55 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ type Credential struct {
289289
OrganizationID int `json:"organization"`
290290
CredentialTypeID int `json:"credential_type"`
291291
Inputs map[string]interface{} `json:"inputs"`
292-
SummaryFields map[string]interface{} `json:"summary_fields"`
292+
SummaryFields *Summary `json:"summary_fields"`
293293
}
294294

295295
type CredentialType struct {
@@ -361,52 +361,60 @@ type Ping struct {
361361

362362
// JobTemplate represents the awx api job template.
363363
type JobTemplate struct {
364-
ID int `json:"id"`
365-
Type string `json:"type"`
366-
URL string `json:"url"`
367-
Related *Related `json:"related"`
368-
SummaryFields *Summary `json:"summary_fields"`
369-
Created time.Time `json:"created"`
370-
Modified time.Time `json:"modified"`
371-
Name string `json:"name"`
372-
Description string `json:"description"`
373-
JobType string `json:"job_type"`
374-
Inventory int `json:"inventory"`
375-
Project int `json:"project"`
376-
Playbook string `json:"playbook"`
377-
Forks int `json:"forks"`
378-
JobSliceCount int `json:"job_slice_count"`
379-
Limit string `json:"limit"`
380-
Verbosity int `json:"verbosity"`
381-
ExtraVars string `json:"extra_vars"`
382-
JobTags string `json:"job_tags"`
383-
ForceHandlers bool `json:"force_handlers"`
384-
SkipTags string `json:"skip_tags"`
385-
StartAtTask string `json:"start_at_task"`
386-
Timeout int `json:"timeout"`
387-
UseFactCache bool `json:"use_fact_cache"`
388-
LastJobRun interface{} `json:"last_job_run"`
389-
LastJobFailed bool `json:"last_job_failed"`
390-
NextJobRun interface{} `json:"next_job_run"`
391-
Status string `json:"status"`
392-
HostConfigKey string `json:"host_config_key"`
393-
AskDiffModeOnLaunch bool `json:"ask_diff_mode_on_launch"`
394-
AskVariablesOnLaunch bool `json:"ask_variables_on_launch"`
395-
AskLimitOnLaunch bool `json:"ask_limit_on_launch"`
396-
AskTagsOnLaunch bool `json:"ask_tags_on_launch"`
397-
AskSkipTagsOnLaunch bool `json:"ask_skip_tags_on_launch"`
398-
AskJobTypeOnLaunch bool `json:"ask_job_type_on_launch"`
399-
AskVerbosityOnLaunch bool `json:"ask_verbosity_on_launch"`
400-
AskInventoryOnLaunch bool `json:"ask_inventory_on_launch"`
401-
AskCredentialOnLaunch bool `json:"ask_credential_on_launch"`
402-
SurveyEnabled bool `json:"survey_enabled"`
403-
BecomeEnabled bool `json:"become_enabled"`
404-
DiffMode bool `json:"diff_mode"`
405-
AllowSimultaneous bool `json:"allow_simultaneous"`
406-
CustomVirtualenv interface{} `json:"custom_virtualenv"`
407-
Credential int `json:"credential"`
408-
VaultCredential interface{} `json:"vault_credential"`
409-
ExecutionEnvironment int `json:"execution_environment"`
364+
ID int `json:"id"`
365+
Type string `json:"type"`
366+
URL string `json:"url"`
367+
Related *Related `json:"related"`
368+
SummaryFields *Summary `json:"summary_fields"`
369+
Created time.Time `json:"created"`
370+
Modified time.Time `json:"modified"`
371+
Name string `json:"name"`
372+
Description string `json:"description"`
373+
JobType string `json:"job_type"`
374+
Inventory int `json:"inventory"`
375+
Project int `json:"project"`
376+
Playbook string `json:"playbook"`
377+
ScmBranch string `json:"scm_branch"`
378+
Forks int `json:"forks"`
379+
Limit string `json:"limit"`
380+
Verbosity int `json:"verbosity"`
381+
ExtraVars string `json:"extra_vars"`
382+
JobTags string `json:"job_tags"`
383+
ForceHandlers bool `json:"force_handlers"`
384+
SkipTags string `json:"skip_tags"`
385+
StartAtTask string `json:"start_at_task"`
386+
Timeout int `json:"timeout"`
387+
UseFactCache bool `json:"use_fact_cache"`
388+
LastJobRun interface{} `json:"last_job_run"`
389+
LastJobFailed bool `json:"last_job_failed"`
390+
NextJobRun interface{} `json:"next_job_run"`
391+
Status string `json:"status"`
392+
ExecutionEnvironment string `json:"execution_environment"`
393+
HostConfigKey string `json:"host_config_key"`
394+
AskScmBranchOnLaunch bool `json:"ask_scm_branch_on_launch"`
395+
AskDiffModeOnLaunch bool `json:"ask_diff_mode_on_launch"`
396+
AskVariablesOnLaunch bool `json:"ask_variables_on_launch"`
397+
AskLimitOnLaunch bool `json:"ask_limit_on_launch"`
398+
AskTagsOnLaunch bool `json:"ask_tags_on_launch"`
399+
AskSkipTagsOnLaunch bool `json:"ask_skip_tags_on_launch"`
400+
AskJobTypeOnLaunch bool `json:"ask_job_type_on_launch"`
401+
AskVerbosityOnLaunch bool `json:"ask_verbosity_on_launch"`
402+
AskInventoryOnLaunch bool `json:"ask_inventory_on_launch"`
403+
AskCredentialOnLaunch bool `json:"ask_credential_on_launch"`
404+
AskExecutionEnvironmentOnLaunch bool `json:"ask_execution_environment_on_launch"`
405+
AskLabelsOnLaunch bool `json:"ask_labels_on_launch"`
406+
AskForksOnLaunch bool `json:"ask_forks_on_launch"`
407+
AskJobSliceCountOnLaunch bool `json:"ask_job_slice_count_on_launch"`
408+
AskTimeoutOnLaunch bool `json:"ask_timeout_on_launch"`
409+
AskInstanceGroupsOnLaunch bool `json:"ask_instance_groups_on_launch"`
410+
SurveyEnabled bool `json:"survey_enabled"`
411+
BecomeEnabled bool `json:"become_enabled"`
412+
DiffMode bool `json:"diff_mode"`
413+
AllowSimultaneous bool `json:"allow_simultaneous"`
414+
CustomVirtualenv interface{} `json:"custom_virtualenv"`
415+
JobSliceCount int `json:"job_slice_count"`
416+
Credential int `json:"credential"`
417+
VaultCredential interface{} `json:"vault_credential"`
410418
}
411419

412420
// JobLaunch represents the awx api job launch.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/denouche/goawx
22

3-
go 1.14
3+
go 1.20

0 commit comments

Comments
 (0)