Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement credential association in workflow job template node #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions awx/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func Provider() *schema.Provider {
"awx_workflow_job_template_node_always": resourceWorkflowJobTemplateNodeAlways(),
"awx_workflow_job_template_node_failure": resourceWorkflowJobTemplateNodeFailure(),
"awx_workflow_job_template_node_success": resourceWorkflowJobTemplateNodeSuccess(),
"awx_workflow_job_template_node_credential": resourceWorkflowJobTemplateNodeCredentials(),
"awx_workflow_job_template_node": resourceWorkflowJobTemplateNode(),
"awx_workflow_job_template": resourceWorkflowJobTemplate(),
"awx_workflow_job_template_schedule": resourceWorkflowJobTemplateSchedule(),
Expand Down
95 changes: 95 additions & 0 deletions awx/resource_workflow_job_template_node_credential.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
*TBD*

# Example Usage

```hcl

resource "awx_workflow_job_template_node_credential" "baseconfig" {
workflow_job_template_node_id = awx_workflow_job_template_node.baseconfig.id
credential_id = awx_credential_machine.pi_connection.id
}

```
*/
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/veepee-oss/goawx/client"
)

func resourceWorkflowJobTemplateNodeCredentials() *schema.Resource {
return &schema.Resource{
CreateContext: resourceWorkflowJobTemplateNodeCredentialsCreate,
DeleteContext: resourceWorkflowJobTemplateNodeCredentialsDelete,
ReadContext: resourceWorkflowJobTemplateNodeCredentialsRead,

Schema: map[string]*schema.Schema{

"workflow_job_template_node_id": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
"credential_id": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
},
}
}

func resourceWorkflowJobTemplateNodeCredentialsCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client := m.(*awx.AWX)
awxService := client.WorkflowJobTemplateNodeService
workflowJobTemplateNodeID := d.Get("workflow_job_template_node_id").(int)
_, err := awxService.GetWorkflowJobTemplateNodeByID(workflowJobTemplateNodeID, make(map[string]string))
if err != nil {
return buildDiagNotFoundFail("workflow job template node", workflowJobTemplateNodeID, err)
}

result, err := awxService.AssociateCredentials(workflowJobTemplateNodeID, map[string]interface{}{
"id": d.Get("credential_id").(int),
}, map[string]string{})

if err != nil {
return buildDiagnosticsMessage("Create: WorkflowJobTemplateNode not AssociateCredentials", "Fail to add credentials with Id %v, for Node ID %v, got error: %s", d.Get("credential_id").(int), workflowJobTemplateNodeID, err.Error())
}

d.SetId(strconv.Itoa(result.ID))
return diags
}

func resourceWorkflowJobTemplateNodeCredentialsRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
return diags
}

func resourceWorkflowJobTemplateNodeCredentialsDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client := m.(*awx.AWX)
awxService := client.WorkflowJobTemplateNodeService
workflowJobTemplateNodeID := d.Get("workflow_job_template_node_id").(int)
res, err := awxService.GetWorkflowJobTemplateNodeByID(workflowJobTemplateNodeID, make(map[string]string))
if err != nil {
return buildDiagNotFoundFail("workflow job template node", workflowJobTemplateNodeID, err)
}

_, err = awxService.DisAssociateCredentials(res.ID, map[string]interface{}{
"id": d.Get("credential_id").(int),
}, map[string]string{})
if err != nil {
return buildDiagDeleteFail("WorkflowJobTemplateNode DisAssociateCredentials", fmt.Sprintf("DisAssociateCredentials %v, from Node ID %v got %s ", d.Get("credential_id").(int), d.Get("workflow_job_template_node_id").(int), err.Error()))
}

d.SetId("")
return diags
}