diff --git a/client/awx.go b/client/awx.go index 0595e61..5ae040b 100644 --- a/client/awx.go +++ b/client/awx.go @@ -29,6 +29,7 @@ type AWX struct { CredentialTypeService *CredentialTypeService CredentialInputSourceService *CredentialInputSourceService InventorySourcesService *InventorySourcesService + InventorySourcesSchedulesService *InventorySourcesSchedulesService InventoryGroupService *InventoryGroupService InstanceGroupsService *InstanceGroupsService NotificationTemplatesService *NotificationTemplatesService @@ -173,6 +174,9 @@ func newAWX(c *Client) *AWX { InventorySourcesService: &InventorySourcesService{ client: c, }, + InventorySourcesSchedulesService: &InventorySourcesSchedulesService{ + client: c, + }, InventoryGroupService: &InventoryGroupService{ client: c, }, diff --git a/client/inventory_sources_schedule.go b/client/inventory_sources_schedule.go new file mode 100644 index 0000000..b504055 --- /dev/null +++ b/client/inventory_sources_schedule.go @@ -0,0 +1,65 @@ +package awx + +import ( + "bytes" + "encoding/json" + "fmt" + "log" +) + +const inventorySourcesSchedulesAPIEndpoint = "/api/v2/inventory_sources/%d/schedules/" + +// InventorySourcesSchedulesService implements awx inventory sources schedules apis. +type InventorySourcesSchedulesService struct { + client *Client +} + +// ListInventorySourcesSchedules shows a list of schedules for a given inventory_source +func (is *InventorySourcesSchedulesService) ListInventorySourcesSchedules(id int, params map[string]string) ([]*Schedule, *ListSchedulesResponse, error) { + result := new(ListSchedulesResponse) + resp, err := is.client.Requester.GetJSON( + fmt.Sprintf(inventorySourcesSchedulesAPIEndpoint, id), + result, params) + if err != nil { + return nil, result, err + } + + if err := CheckResponse(resp); err != nil { + return nil, result, err + } + + return result.Results, result, nil +} + +// CreateInventorySourcesSchedule will create a schedule for an existing inventory_source +func (is *InventorySourcesSchedulesService) CreateInventorySourcesSchedule(id int, data map[string]interface{}, params map[string]string) (*Schedule, error) { + mandatoryFields = []string{"name", "rrule"} + validate, status := ValidateParams(data, mandatoryFields) + if !status { + err := fmt.Errorf("mandatory input arguments are absent: %s", validate) + return nil, err + } + + result := new(Schedule) + payload, err := json.Marshal(data) + if err != nil { + return nil, err + } + + resp, err := is.client.Requester.PostJSON( + fmt.Sprintf(inventorySourcesSchedulesAPIEndpoint, id), + bytes.NewReader(payload), result, params, + ) + + log.Println("OK") + + if err != nil { + return nil, err + } + + if err := CheckResponse(resp); err != nil { + return nil, err + } + + return result, nil +} diff --git a/client/organizations.go b/client/organizations.go index 2559593..a619970 100644 --- a/client/organizations.go +++ b/client/organizations.go @@ -109,12 +109,33 @@ func (p *OrganizationsService) DeleteOrganization(id int) (*Organization, error) return result, nil } -// DisAssociateGalaxyCredentials remove Credentials form an awx job template +// DisAssociateGalaxyCredentials remove Credentials from an organization func (p *OrganizationsService) DisAssociateGalaxyCredentials(id int, data map[string]interface{}, params map[string]string) (*Organization, error) { + return p.disAssociate(id, "galaxy_credentials", data, params) +} + +// AssociateGalaxyCredentials adding credentials to Organization. +func (p *OrganizationsService) AssociateGalaxyCredentials(id int, data map[string]interface{}, params map[string]string) (*Organization, error) { + return p.associate(id, "galaxy_credentials", data, params) +} + +// DisAssociateInstanceGroups remove instance_groups from an organization +func (p *OrganizationsService) DisAssociateInstanceGroups(id int, data map[string]interface{}, params map[string]string) (*Organization, error) { + return p.disAssociate(id, "instance_groups", data, params) +} + +// AssociateInstanceGroups adding instance_groups to Organization. +func (p *OrganizationsService) AssociateInstanceGroups(id int, data map[string]interface{}, params map[string]string) (*Organization, error) { + return p.associate(id, "instance_groups", data, params) +} + +// Associate associate an element to Organization. +func (p *OrganizationsService) associate(id int, typ string, data map[string]interface{}, params map[string]string) (*Organization, error) { result := new(Organization) - endpoint := fmt.Sprintf("%s%d/galaxy_credentials/", organizationsAPIEndpoint, id) - data["disassociate"] = true - mandatoryFields = []string{"id", "disassociate"} + + endpoint := fmt.Sprintf("%s%d/%s/", organizationsAPIEndpoint, id, typ) + data["associate"] = true + mandatoryFields = []string{"id"} validate, status := ValidateParams(data, mandatoryFields) if !status { err := fmt.Errorf("mandatory input arguments are absent: %s", validate) @@ -136,13 +157,12 @@ func (p *OrganizationsService) DisAssociateGalaxyCredentials(id int, data map[st return result, nil } -// AssociateGalaxyCredentials adding credentials to Organization. -func (p *OrganizationsService) AssociateGalaxyCredentials(id int, data map[string]interface{}, params map[string]string) (*Organization, error) { +// DisAssociate remove element from an organization +func (p *OrganizationsService) disAssociate(id int, typ string, data map[string]interface{}, params map[string]string) (*Organization, error) { result := new(Organization) - - endpoint := fmt.Sprintf("%s%d/galaxy_credentials/", organizationsAPIEndpoint, id) - data["associate"] = true - mandatoryFields = []string{"id"} + endpoint := fmt.Sprintf("%s%d/%s/", organizationsAPIEndpoint, id, typ) + data["disassociate"] = true + mandatoryFields = []string{"id", "disassociate"} validate, status := ValidateParams(data, mandatoryFields) if !status { err := fmt.Errorf("mandatory input arguments are absent: %s", validate) diff --git a/client/types.go b/client/types.go index 2c4941b..70ffac8 100644 --- a/client/types.go +++ b/client/types.go @@ -289,7 +289,7 @@ type Credential struct { OrganizationID int `json:"organization"` CredentialTypeID int `json:"credential_type"` Inputs map[string]interface{} `json:"inputs"` - SummaryFields map[string]interface{} `json:"summary_fields"` + SummaryFields *Summary `json:"summary_fields"` } type CredentialType struct { @@ -361,52 +361,60 @@ type Ping struct { // JobTemplate represents the awx api job template. type JobTemplate 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"` - JobSliceCount int `json:"job_slice_count"` - 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"` - LastJobRun interface{} `json:"last_job_run"` - LastJobFailed bool `json:"last_job_failed"` - NextJobRun interface{} `json:"next_job_run"` - Status string `json:"status"` - HostConfigKey string `json:"host_config_key"` - 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"` - SurveyEnabled bool `json:"survey_enabled"` - BecomeEnabled bool `json:"become_enabled"` - DiffMode bool `json:"diff_mode"` - AllowSimultaneous bool `json:"allow_simultaneous"` - CustomVirtualenv interface{} `json:"custom_virtualenv"` - Credential int `json:"credential"` - VaultCredential interface{} `json:"vault_credential"` - ExecutionEnvironment int `json:"execution_environment"` + 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"` + ScmBranch string `json:"scm_branch"` + 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"` + LastJobRun interface{} `json:"last_job_run"` + LastJobFailed bool `json:"last_job_failed"` + NextJobRun interface{} `json:"next_job_run"` + Status string `json:"status"` + ExecutionEnvironment string `json:"execution_environment"` + HostConfigKey string `json:"host_config_key"` + AskScmBranchOnLaunch bool `json:"ask_scm_branch_on_launch"` + 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"` + AskExecutionEnvironmentOnLaunch bool `json:"ask_execution_environment_on_launch"` + AskLabelsOnLaunch bool `json:"ask_labels_on_launch"` + AskForksOnLaunch bool `json:"ask_forks_on_launch"` + AskJobSliceCountOnLaunch bool `json:"ask_job_slice_count_on_launch"` + AskTimeoutOnLaunch bool `json:"ask_timeout_on_launch"` + AskInstanceGroupsOnLaunch bool `json:"ask_instance_groups_on_launch"` + SurveyEnabled bool `json:"survey_enabled"` + BecomeEnabled bool `json:"become_enabled"` + DiffMode bool `json:"diff_mode"` + AllowSimultaneous bool `json:"allow_simultaneous"` + CustomVirtualenv interface{} `json:"custom_virtualenv"` + JobSliceCount int `json:"job_slice_count"` + Credential int `json:"credential"` + VaultCredential interface{} `json:"vault_credential"` } // JobLaunch represents the awx api job launch. diff --git a/go.mod b/go.mod index 7ca1a8e..8d5b2e7 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/denouche/goawx -go 1.14 +go 1.20