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

Fix inventoryID issue with Job resource #32

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
bugfixes:
- resource/job - fix issue when attempting to modify ``inventory_id`` attribute using job template which does not allow (https://github.com/ansible/terraform-provider-aap/issues/31).
9 changes: 4 additions & 5 deletions internal/provider/job_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,10 @@ func (r *JobResourceModel) ParseHttpResponse(body []byte) diag.Diagnostics {
r.URL = types.StringValue(resultApiJob.URL)
r.Status = types.StringValue(resultApiJob.Status)
r.TemplateID = types.Int64Value(resultApiJob.TemplateID)
r.InventoryID = types.Int64Value(resultApiJob.Inventory)
if r.InventoryID.ValueInt64() == 0 {
r.InventoryID = types.Int64Value(resultApiJob.Inventory)
}
Comment on lines +270 to +272
Copy link
Member

@gravesm gravesm Jul 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like the bug this is trying to fix was introduced by https://github.com/ansible/terraform-provider-aap/pull/10/files#diff-afd9b829236927b85ef1814941bb6f012eed9cca71547f9f9ce15dcceb29a6f4R216-R219. I don't know why that change was made, but, among other things, it makes it impossible to use a job template that has a default inventory and is configured to prompt on launch. I think fixing that bug makes this bug go away, because it shouldn't be possible to launch a job with prompt on launch and no default inventory set.


diags = r.ParseIgnoredFields(resultApiJob.IgnoredFields)
return diags
}
Expand Down Expand Up @@ -312,10 +315,6 @@ func (r *JobResource) LaunchJob(data *JobResourceModel) diag.Diagnostics {

// Save new job data into job resource model
diags.Append(data.ParseHttpResponse(body)...)
if diags.HasError() {
return diags
}

return diags
}

Expand Down
140 changes: 92 additions & 48 deletions internal/provider/job_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"reflect"
"regexp"
"slices"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -196,21 +197,21 @@ func TestJobResourceParseHttpResponse(t *testing.T) {
}

// Acceptance tests
const AAPJobResource = "aap_job"

func getJobResourceFromStateFile(s *terraform.State) (map[string]interface{}, error) {
for _, rs := range s.RootModule().Resources {
if rs.Type != "aap_job" {
continue
}
jobURL := rs.Primary.Attributes["url"]
body, err := testGetResource(jobURL)
if err != nil {
return nil, err
}
if rs.Type == AAPJobResource {
jobURL := rs.Primary.Attributes["url"]
body, err := testGetResource(jobURL)
if err != nil {
return nil, err
}

var result map[string]interface{}
err = json.Unmarshal(body, &result)
return result, err
var result map[string]interface{}
err = json.Unmarshal(body, &result)
return result, err
}
}
return nil, fmt.Errorf("Job resource not found from state file")
}
Expand All @@ -220,11 +221,11 @@ func testAccCheckJobExists(s *terraform.State) error {
return err
}

func testAccCheckJobUpdate(urlBefore *string, shouldDiffer bool) func(s *terraform.State) error {
func testAccCheckJobUpdateURL(urlBefore *string, shouldDiffer bool) func(s *terraform.State) error {
return func(s *terraform.State) error {
var jobURL string
for _, rs := range s.RootModule().Resources {
if rs.Type != "aap_job" {
if rs.Type != AAPJobResource {
continue
}
jobURL = rs.Primary.Attributes["url"]
Expand All @@ -245,6 +246,40 @@ func testAccCheckJobUpdate(urlBefore *string, shouldDiffer bool) func(s *terrafo
}
}

const defaultInventoryID = 1

func testAccCheckJobUpdateInventoryID() func(s *terraform.State) error {
return func(s *terraform.State) error {
var inventoryID int64 = -1
var expectedInventoryID int64 = defaultInventoryID
for _, rs := range s.RootModule().Resources {
if rs.Type == AAPJobResource {
inventory_id_s := rs.Primary.Attributes["inventory_id"]
var err error
inventoryID, err = strconv.ParseInt(inventory_id_s, 10, 64)
if err != nil {
return fmt.Errorf("Could not convert attribute 'inventory_id' (%s) from '%s' resource into int64", inventory_id_s, AAPJobResource)
}
}
if rs.Type == "aap_inventory" {
resource_id_s := rs.Primary.Attributes["id"]
var err error
expectedInventoryID, err = strconv.ParseInt(resource_id_s, 10, 64)
if err != nil {
return fmt.Errorf("Could not convert attribute 'id' (%s) from 'aap_inventory' resource into int64", resource_id_s)
}
}
}
if inventoryID == -1 {
return fmt.Errorf("Job resource not found from state file")
}
if expectedInventoryID != inventoryID {
return fmt.Errorf("Inventory ID does not match on Job resource. Expected (%d), Found (%d)", expectedInventoryID, inventoryID)
}
return nil
}
}

func testAccJobResourcePreCheck(t *testing.T) {
// ensure provider requirements
testAccPreCheck(t)
Expand All @@ -260,7 +295,9 @@ func testAccJobResourcePreCheck(t *testing.T) {
}
}

const resourceName = "aap_job.test"
func ResourceName() string {
return fmt.Sprintf("%s.test", AAPJobResource)
}

func TestAccAAPJob_basic(t *testing.T) {
jobTemplateID := os.Getenv("AAP_TEST_JOB_TEMPLATE_ID")
Expand All @@ -273,10 +310,11 @@ func TestAccAAPJob_basic(t *testing.T) {
{
Config: testAccBasicJob(jobTemplateID),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestMatchResourceAttr(resourceName, "status", regexp.MustCompile("^(failed|pending|running|complete|successful|waiting)$")),
resource.TestMatchResourceAttr(resourceName, "job_type", regexp.MustCompile("^(run|check)$")),
resource.TestMatchResourceAttr(resourceName, "url", regexp.MustCompile("^/api/v2/jobs/[0-9]*/$")),
resource.TestMatchResourceAttr(ResourceName(), "status", regexp.MustCompile("^(failed|pending|running|complete|successful|waiting)$")),
resource.TestMatchResourceAttr(ResourceName(), "job_type", regexp.MustCompile("^(run|check)$")),
resource.TestMatchResourceAttr(ResourceName(), "url", regexp.MustCompile("^/api/v2/jobs/[0-9]*/$")),
testAccCheckJobExists,
testAccCheckJobUpdateInventoryID(),
),
},
},
Expand All @@ -297,19 +335,21 @@ func TestAccAAPJob_UpdateWithSameParameters(t *testing.T) {
{
Config: testAccBasicJob(jobTemplateID),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestMatchResourceAttr(resourceName, "status", regexp.MustCompile("^(failed|pending|running|complete|successful|waiting)$")),
resource.TestMatchResourceAttr(resourceName, "job_type", regexp.MustCompile("^(run|check)$")),
resource.TestMatchResourceAttr(resourceName, "url", regexp.MustCompile("^/api/v2/jobs/[0-9]*/$")),
testAccCheckJobUpdate(&jobURLBefore, false),
resource.TestMatchResourceAttr(ResourceName(), "status", regexp.MustCompile("^(failed|pending|running|complete|successful|waiting)$")),
resource.TestMatchResourceAttr(ResourceName(), "job_type", regexp.MustCompile("^(run|check)$")),
resource.TestMatchResourceAttr(ResourceName(), "url", regexp.MustCompile("^/api/v2/jobs/[0-9]*/$")),
testAccCheckJobUpdateURL(&jobURLBefore, false),
testAccCheckJobUpdateInventoryID(),
),
},
{
Config: testAccBasicJob(jobTemplateID),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestMatchResourceAttr(resourceName, "status", regexp.MustCompile("^(failed|pending|running|complete|successful|waiting)$")),
resource.TestMatchResourceAttr(resourceName, "job_type", regexp.MustCompile("^(run|check)$")),
resource.TestMatchResourceAttr(resourceName, "url", regexp.MustCompile("^/api/v2/jobs/[0-9]*/$")),
testAccCheckJobUpdate(&jobURLBefore, false),
resource.TestMatchResourceAttr(ResourceName(), "status", regexp.MustCompile("^(failed|pending|running|complete|successful|waiting)$")),
resource.TestMatchResourceAttr(ResourceName(), "job_type", regexp.MustCompile("^(run|check)$")),
resource.TestMatchResourceAttr(ResourceName(), "url", regexp.MustCompile("^/api/v2/jobs/[0-9]*/$")),
testAccCheckJobUpdateURL(&jobURLBefore, false),
testAccCheckJobUpdateInventoryID(),
),
},
},
Expand All @@ -330,21 +370,23 @@ func TestAccAAPJob_UpdateWithNewInventoryId(t *testing.T) {
{
Config: testAccBasicJob(jobTemplateID),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestMatchResourceAttr(resourceName, "status", regexp.MustCompile("^(failed|pending|running|complete|successful|waiting)$")),
resource.TestMatchResourceAttr(resourceName, "job_type", regexp.MustCompile("^(run|check)$")),
resource.TestMatchResourceAttr(resourceName, "url", regexp.MustCompile("^/api/v2/jobs/[0-9]*/$")),
testAccCheckJobUpdate(&jobURLBefore, false),
resource.TestMatchResourceAttr(ResourceName(), "status", regexp.MustCompile("^(failed|pending|running|complete|successful|waiting)$")),
resource.TestMatchResourceAttr(ResourceName(), "job_type", regexp.MustCompile("^(run|check)$")),
resource.TestMatchResourceAttr(ResourceName(), "url", regexp.MustCompile("^/api/v2/jobs/[0-9]*/$")),
testAccCheckJobUpdateURL(&jobURLBefore, false),
testAccCheckJobUpdateInventoryID(),
),
},
{
Config: testAccUpdateJobWithInventoryID(inventoryName, jobTemplateID),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestMatchResourceAttr(resourceName, "status", regexp.MustCompile("^(failed|pending|running|complete|successful|waiting)$")),
resource.TestMatchResourceAttr(resourceName, "job_type", regexp.MustCompile("^(run|check)$")),
resource.TestMatchResourceAttr(resourceName, "url", regexp.MustCompile("^/api/v2/jobs/[0-9]*/$")),
testAccCheckJobUpdate(&jobURLBefore, true),
resource.TestMatchResourceAttr(ResourceName(), "status", regexp.MustCompile("^(failed|pending|running|complete|successful|waiting)$")),
resource.TestMatchResourceAttr(ResourceName(), "job_type", regexp.MustCompile("^(run|check)$")),
resource.TestMatchResourceAttr(ResourceName(), "url", regexp.MustCompile("^/api/v2/jobs/[0-9]*/$")),
testAccCheckJobUpdateURL(&jobURLBefore, true),
testAccCheckJobUpdateInventoryID(),
// Wait for the job to finish so the inventory can be deleted
testAccCheckJobPause("aap_job.test"),
testAccCheckJobPause(fmt.Sprintf("%s.test", AAPJobResource)),
),
},
},
Expand All @@ -365,19 +407,21 @@ func TestAccAAPJob_UpdateWithTrigger(t *testing.T) {
{
Config: testAccBasicJob(jobTemplateID),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestMatchResourceAttr(resourceName, "status", regexp.MustCompile("^(failed|pending|running|complete|successful|waiting)$")),
resource.TestMatchResourceAttr(resourceName, "job_type", regexp.MustCompile("^(run|check)$")),
resource.TestMatchResourceAttr(resourceName, "url", regexp.MustCompile("^/api/v2/jobs/[0-9]*/$")),
testAccCheckJobUpdate(&jobURLBefore, false),
resource.TestMatchResourceAttr(ResourceName(), "status", regexp.MustCompile("^(failed|pending|running|complete|successful|waiting)$")),
resource.TestMatchResourceAttr(ResourceName(), "job_type", regexp.MustCompile("^(run|check)$")),
resource.TestMatchResourceAttr(ResourceName(), "url", regexp.MustCompile("^/api/v2/jobs/[0-9]*/$")),
testAccCheckJobUpdateURL(&jobURLBefore, false),
testAccCheckJobUpdateInventoryID(),
),
},
{
Config: testAccUpdateJobWithTrigger(jobTemplateID),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestMatchResourceAttr(resourceName, "status", regexp.MustCompile("^(failed|pending|running|complete|successful|waiting)$")),
resource.TestMatchResourceAttr(resourceName, "job_type", regexp.MustCompile("^(run|check)$")),
resource.TestMatchResourceAttr(resourceName, "url", regexp.MustCompile("^/api/v2/jobs/[0-9]*/$")),
testAccCheckJobUpdate(&jobURLBefore, true),
resource.TestMatchResourceAttr(ResourceName(), "status", regexp.MustCompile("^(failed|pending|running|complete|successful|waiting)$")),
resource.TestMatchResourceAttr(ResourceName(), "job_type", regexp.MustCompile("^(run|check)$")),
resource.TestMatchResourceAttr(ResourceName(), "url", regexp.MustCompile("^/api/v2/jobs/[0-9]*/$")),
testAccCheckJobUpdateURL(&jobURLBefore, true),
testAccCheckJobUpdateInventoryID(),
),
},
},
Expand Down Expand Up @@ -418,10 +462,10 @@ func testAccCheckJobPause(name string) resource.TestCheckFunc {

func testAccBasicJob(jobTemplateID string) string {
return fmt.Sprintf(`
resource "aap_job" "test" {
resource "%s" "test" {
job_template_id = %s
}
`, jobTemplateID)
`, AAPJobResource, jobTemplateID)
}

func testAccUpdateJobWithInventoryID(inventoryName, jobTemplateID string) string {
Expand All @@ -430,21 +474,21 @@ resource "aap_inventory" "test" {
name = "%s"
}

resource "aap_job" "test" {
resource "%s" "test" {
job_template_id = %s
inventory_id = aap_inventory.test.id
}
`, inventoryName, jobTemplateID)
`, inventoryName, AAPJobResource, jobTemplateID)
}

func testAccUpdateJobWithTrigger(jobTemplateID string) string {
return fmt.Sprintf(`
resource "aap_job" "test" {
resource "%s" "test" {
job_template_id = %s
triggers = {
"key1" = "value1"
"key2" = "value2"
}
}
`, jobTemplateID)
`, AAPJobResource, jobTemplateID)
}
Loading