forked from denouche/terraform-provider-awx
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from DeLoWaN/add_awx_workflow_job_template_role
feat(datasource): adds awx_workflow_job_template_role data source
- Loading branch information
Showing
8 changed files
with
147 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
v1.2.0 | ||
v1.2.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
examples/data-sources/awx_workflow_job_template_role/data-source.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
2 changes: 2 additions & 0 deletions
2
examples/data-sources/awx_workflow_job_template_role/import.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters