Skip to content

Commit

Permalink
Merge pull request #20 from DeLoWaN/add_awx_workflow_job_template_role
Browse files Browse the repository at this point in the history
feat(datasource): adds awx_workflow_job_template_role data source
  • Loading branch information
josh-silvas authored Nov 25, 2024
2 parents 74da97d + b5106b6 commit d441b0b
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 3 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.2.0
v1.2.1
2 changes: 1 addition & 1 deletion docs/data-sources/job_template_role.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```

Expand Down
38 changes: 38 additions & 0 deletions docs/data-sources/workflow_job_template_role.md
Original file line number Diff line number Diff line change
@@ -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 generated by tfplugindocs -->
## 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
2 changes: 1 addition & 1 deletion examples/data-sources/awx_job_template_role/data-source.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Order can be imported by specifying the numeric identifier.
terraform import awx_workflow_job_template_role.example 55
93 changes: 93 additions & 0 deletions internal/awx/data_source_workflow_job_template_role.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions internal/awx/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit d441b0b

Please sign in to comment.