From b5106b614d1187d8d43fb0845dc22fa22eaa6178 Mon Sep 17 00:00:00 2001 From: Damien Gustave Date: Wed, 20 Nov 2024 17:35:48 +0100 Subject: [PATCH] feat(datasource): adds awx_workflow_job_template_role data source --- VERSION | 2 +- docs/data-sources/job_template_role.md | 2 +- .../workflow_job_template_role.md | 38 ++++++++ .../awx_job_template_role/data-source.tf | 2 +- .../data-source.tf | 10 ++ .../awx_workflow_job_template_role/import.sh | 2 + .../data_source_workflow_job_template_role.go | 93 +++++++++++++++++++ internal/awx/provider.go | 1 + 8 files changed, 147 insertions(+), 3 deletions(-) create mode 100644 docs/data-sources/workflow_job_template_role.md create mode 100644 examples/data-sources/awx_workflow_job_template_role/data-source.tf create mode 100644 examples/data-sources/awx_workflow_job_template_role/import.sh create mode 100644 internal/awx/data_source_workflow_job_template_role.go diff --git a/VERSION b/VERSION index 79127d85..6a5e98a7 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -v1.2.0 +v1.2.1 diff --git a/docs/data-sources/job_template_role.md b/docs/data-sources/job_template_role.md index 9ea992e3..ccf8e737 100644 --- a/docs/data-sources/job_template_role.md +++ b/docs/data-sources/job_template_role.md @@ -24,7 +24,7 @@ resource "awx_job_template" "example" { data "awx_job_template_role" "example" { name = "Admin" - job_template_id = data.awx_job_template.example.id + job_template_id = awx_job_template.example.id } ``` diff --git a/docs/data-sources/workflow_job_template_role.md b/docs/data-sources/workflow_job_template_role.md new file mode 100644 index 00000000..fcbc073a --- /dev/null +++ b/docs/data-sources/workflow_job_template_role.md @@ -0,0 +1,38 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "awx_workflow_job_template_role Data Source - terraform-provider-awx" +subcategory: "" +description: |- + Data source for AWX Workflow Job Template Role +--- + +# awx_workflow_job_template_role (Data Source) + +Data source for AWX Workflow Job Template Role + +## Example Usage + +```terraform +resource "awx_workflow_job_template" "example" { + name = "workflow-job" + organization_id = data.awx_organization.example.id + inventory_id = data.awx_inventory.default.id +} + +data "awx_workflow_job_template_role" "example" { + name = "Admin" + workflow_job_template_id = awx_workflow_job_template.example.id +} +``` + + +## Schema + +### Required + +- `workflow_job_template_id` (Number) The ID of the workflow job template + +### Optional + +- `id` (Number) The ID of the role +- `name` (String) The name of the role diff --git a/examples/data-sources/awx_job_template_role/data-source.tf b/examples/data-sources/awx_job_template_role/data-source.tf index 7181d2b4..2687c03d 100644 --- a/examples/data-sources/awx_job_template_role/data-source.tf +++ b/examples/data-sources/awx_job_template_role/data-source.tf @@ -9,5 +9,5 @@ resource "awx_job_template" "example" { data "awx_job_template_role" "example" { name = "Admin" - job_template_id = data.awx_job_template.example.id + job_template_id = awx_job_template.example.id } diff --git a/examples/data-sources/awx_workflow_job_template_role/data-source.tf b/examples/data-sources/awx_workflow_job_template_role/data-source.tf new file mode 100644 index 00000000..bc02ea68 --- /dev/null +++ b/examples/data-sources/awx_workflow_job_template_role/data-source.tf @@ -0,0 +1,10 @@ +resource "awx_workflow_job_template" "example" { + name = "workflow-job" + organization_id = data.awx_organization.example.id + inventory_id = data.awx_inventory.default.id +} + +data "awx_workflow_job_template_role" "example" { + name = "Admin" + workflow_job_template_id = awx_workflow_job_template.example.id +} diff --git a/examples/data-sources/awx_workflow_job_template_role/import.sh b/examples/data-sources/awx_workflow_job_template_role/import.sh new file mode 100644 index 00000000..7f881b54 --- /dev/null +++ b/examples/data-sources/awx_workflow_job_template_role/import.sh @@ -0,0 +1,2 @@ +# Order can be imported by specifying the numeric identifier. +terraform import awx_workflow_job_template_role.example 55 diff --git a/internal/awx/data_source_workflow_job_template_role.go b/internal/awx/data_source_workflow_job_template_role.go new file mode 100644 index 00000000..6eb9b162 --- /dev/null +++ b/internal/awx/data_source_workflow_job_template_role.go @@ -0,0 +1,93 @@ +package awx + +import ( + "context" + "fmt" + "strconv" + + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + awx "github.com/josh-silvas/terraform-provider-awx/tools/goawx" + "github.com/josh-silvas/terraform-provider-awx/tools/utils" +) + +const diagWorkflowJobTemplateRole = "Workflow Job Template Role" + +func dataSourceWorkflowJobTemplateRole() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceWorkflowJobTemplateRoleRead, + Description: "Data source for AWX Workflow Job Template Role", + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeInt, + Optional: true, + Computed: true, + Description: "The ID of the role", + }, + "name": { + Type: schema.TypeString, + Optional: true, + Computed: true, + Description: "The name of the role", + ExactlyOneOf: []string{"id", "name"}, + }, + "workflow_job_template_id": { + Type: schema.TypeInt, + Required: true, + Description: "The ID of the workflow job template", + }, + }, + } +} + +func dataSourceWorkflowJobTemplateRoleRead(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + var diags diag.Diagnostics + client := m.(*awx.AWX) + params := make(map[string]string) + + templateID := d.Get("workflow_job_template_id").(int) + workflowJobTemplate, err := client.WorkflowJobTemplateService.GetWorkflowJobTemplateByID(templateID, params) + if err != nil { + return utils.DiagFetch(diagWorkflowJobTemplateRole, templateID, err) + } + + rolesList := []*awx.ApplyRole{ + workflowJobTemplate.SummaryFields.ObjectRoles.UseRole, + workflowJobTemplate.SummaryFields.ObjectRoles.AdminRole, + workflowJobTemplate.SummaryFields.ObjectRoles.AdhocRole, + workflowJobTemplate.SummaryFields.ObjectRoles.UpdateRole, + workflowJobTemplate.SummaryFields.ObjectRoles.ReadRole, + workflowJobTemplate.SummaryFields.ObjectRoles.ExecuteRole, + } + + if roleID, okID := d.GetOk("id"); okID { + id := roleID.(int) + for _, v := range rolesList { + if v != nil && id == v.ID { + d = setWorkflowJobTemplateRoleData(d, v) + return diags + } + } + } + + if roleName, okName := d.GetOk("name"); okName { + name := roleName.(string) + + for _, v := range rolesList { + if v != nil && name == v.Name { + d = setWorkflowJobTemplateRoleData(d, v) + return diags + } + } + } + + return utils.DiagNotFound(diagWorkflowJobTemplateRole, templateID, nil) +} + +func setWorkflowJobTemplateRoleData(d *schema.ResourceData, r *awx.ApplyRole) *schema.ResourceData { + if err := d.Set("name", r.Name); err != nil { + fmt.Println("Error setting name", err) + } + d.SetId(strconv.Itoa(r.ID)) + return d +} diff --git a/internal/awx/provider.go b/internal/awx/provider.go index 389d59e1..95b30e1d 100644 --- a/internal/awx/provider.go +++ b/internal/awx/provider.go @@ -117,6 +117,7 @@ func Provider() *schema.Provider { //nolint:funlen "awx_project_role": dataSourceProjectRole(), "awx_schedule": dataSourceSchedule(), "awx_workflow_job_template": dataSourceWorkflowJobTemplate(), + "awx_workflow_job_template_role": dataSourceWorkflowJobTemplateRole(), "awx_team": dataSourceTeam(), }, ConfigureContextFunc: providerConfigure,