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

Add resource azurerm_automation_job_schedule #3386

Merged
merged 23 commits into from
Oct 28, 2019

Conversation

draggeta
Copy link
Contributor

@draggeta draggeta commented May 6, 2019

For a personal project I was looking for this resource and while there are issues requesting this, there were no PRs. This PR adds the job schedule as a resource.

Closes #1955

Remarks

This resource only supports create, delete and import. No updates are possible. Any change recreates the resource.

I'm also not sure what to do about the requiresImport test, so if it is wrong, please let me know.

Example

resource "azurerm_resource_group" "example" {
  name     = "tfex-automation-account"
  location = "West Europe"
}

resource "azurerm_automation_account" "example" {
  name                = "tfex-automation-account"
  location            = "${azurerm_resource_group.example.location}"
  resource_group_name = "${azurerm_resource_group.example.name}"

  sku {
    name = "Basic"
  }
}

resource "azurerm_automation_runbook" "example" {
  name                = "Get-AzureVMTutorial"
  location            = "${azurerm_resource_group.example.location}"
  resource_group_name = "${azurerm_resource_group.example.name}"
  account_name        = "${azurerm_automation_account.example.name}"
  log_verbose         = "true"
  log_progress        = "true"
  description         = "This is an example runbook"
  runbook_type        = "PowerShellWorkflow"

  publish_content_link {
    uri = "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-automation-runbook-getvms/Runbooks/Get-AzureVMTutorial.ps1"
  }
}

resource "azurerm_automation_schedule" "example" {
  name                    = "tfex-automation-schedule"
  resource_group_name     = "${azurerm_resource_group.example.name}"
  automation_account_name = "${azurerm_automation_account.example.name}"
  frequency               = "Week"
  interval                = 1
  timezone                = "Central Europe Standard Time"
  start_time              = "2014-04-15T18:00:15+02:00"
  description             = "This is an example schedule"

  advanced_schedule {
    week_days = ["Friday"]
  }
}

resource "azurerm_automation_job_schedule" "example" {
  resource_group_name     = "${azurerm_resource_group.example.name}"
  automation_account_name = "${azurerm_automation_account.example.name}"
  schedule_name           = "${azurerm_automation_schedule.example.name}"
  runbook_name            = "${azurerm_automation_runbook.example.name}"

  parameters = {
    Connection         = "AzureRunAsConnection"
    VMCount            = 10
  }
}

Exports

The following items are also exported

  • id: The full ID of the resource. This is generated by Azure itself, based on the job_schedule_id.
  • job_schedule_id: the UUID identifying the runbook. This is generated at creation time by the resource and doesn't need to be provided by the user.

Acceptance tests

PS > go test -timeout 180m -v -ldflags="-X=github.com/terraform-providers/terraform-provider-azurerm/version.ProviderVersion=acc" `
>> github.com\terraform-providers\terraform-provider-azurerm\azurerm `
>> -run "^TestAccAzureRMAutomationJobSchedule" -count=1
=== RUN   TestAccAzureRMAutomationJobSchedule_basic
=== PAUSE TestAccAzureRMAutomationJobSchedule_basic
=== RUN   TestAccAzureRMAutomationJobSchedule_complete
=== PAUSE TestAccAzureRMAutomationJobSchedule_complete
=== RUN   TestAccAzureRMAutomationJobSchedule_update
=== PAUSE TestAccAzureRMAutomationJobSchedule_update
=== RUN   TestAccAzureRMAutomationJobSchedule_requiresImport
--- SKIP: TestAccAzureRMAutomationJobSchedule_requiresImport (0.00s)
    resource_arm_automation_job_schedule_test.go:91: Skipping since resources aren't required to be imported
=== CONT  TestAccAzureRMAutomationJobSchedule_basic
=== CONT  TestAccAzureRMAutomationJobSchedule_complete
=== CONT  TestAccAzureRMAutomationJobSchedule_update
--- PASS: TestAccAzureRMAutomationJobSchedule_basic (67.48s)
--- PASS: TestAccAzureRMAutomationJobSchedule_complete (67.51s)
--- PASS: TestAccAzureRMAutomationJobSchedule_update (97.84s)
PASS
ok      github.com/terraform-providers/terraform-provider-azurerm/azurerm       103.420s

- changed name/nameUUID to jobScheduleID and JobScheduleUUID
- Fix some errors referring to Automation Schedule instead
of Automation Job Schedule
Copy link
Collaborator

@katbyte katbyte left a comment

Choose a reason for hiding this comment

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

Thanks for the new resource @draggeta,

I've left some comments inline but this is off to a good start, once we have the tests sorted and some additional validation this should be good to merge.

website/docs/r/automation_job_schedule.html.markdown Outdated Show resolved Hide resolved
azurerm/resource_arm_automation_job_schedule_test.go Outdated Show resolved Hide resolved
@katbyte katbyte self-assigned this May 7, 2019
@draggeta
Copy link
Contributor Author

draggeta commented May 9, 2019

I needed to use rand to generate the random string as there were issues with the datetime random string. If the tests were run separately, there would be no issues, but if you run them in parallel, they would generate the same random string and conflict.

@ghost ghost removed the waiting-response label May 9, 2019
@draggeta
Copy link
Contributor Author

draggeta commented May 9, 2019

There is also an issue where the parameters for the runbook have different capitalization randomly. This happens in the API. I've tested it with my own runbooks without terraform and it happens there as well. Sometimes a parameter named "Output" in my script, displays as "output" in the API while other times a parameter named "ResourceGroup" appears as "ResourceGroup" in the API.

Is it an idea to make everything lower case when comparing/writing to state or should it be left as is?

@katbyte
Copy link
Collaborator

katbyte commented May 10, 2019

Is that a random occurrence that happens on the API side?

If that is the case they what we should do is open a bug on the azure-sdk-for-go detailing what the issue is, link to it here. Thinks like resource group names shouldn't change case randomly.

Once the issue is opened we can mark the property as case insensitive in the schema with a comment linking to the azure-sdk-for-go issue.

Copy link
Collaborator

@katbyte katbyte left a comment

Choose a reason for hiding this comment

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

Thanks for the updates,

the tests are failing for us currently:

------- Stdout: -------
=== RUN   TestAccAzureRMAutomationJobSchedule_basic
=== PAUSE TestAccAzureRMAutomationJobSchedule_basic
=== CONT  TestAccAzureRMAutomationJobSchedule_basic
--- FAIL: TestAccAzureRMAutomationJobSchedule_basic (105.87s)
    testing.go:568: Step 0 error: errors during apply:
        
        Error: Error creating/updating Automation Runbook "Output-HelloWorld" (Account "acctestAA-5577006791947779410" / Resource Group "acctestRG-5577006791947779410"): automation.RunbookClient#CreateOrUpdate: Failure responding to request: StatusCode=404 -- Original Error: autorest/azure: Service returned an error. Status=404 Code="NotFound" Message="{\"Message\":\"Could not find the account. SubscriptionId: XXXXXXX AccountName: acctestAA-5577006791947779410\"}"
        
          on /opt/teamcity-agent/temp/buildTmp/tf-test668228083/main.tf line 18:
          (source code not available)
        
        
FAIL

and

------- Stdout: -------
=== RUN   TestAccAzureRMAutomationJobSchedule_complete
=== PAUSE TestAccAzureRMAutomationJobSchedule_complete
=== CONT  TestAccAzureRMAutomationJobSchedule_complete
--- FAIL: TestAccAzureRMAutomationJobSchedule_complete (107.69s)
    testing.go:568: Step 0 error: Check failed: 1 error occurred:
        	* Check 1/8 error: Automation Job Schedule '32f72eef-b997-4d32-a890-123b9f6c5a72' (Account "acctestAA-5577006791947779410" / Resource Group "acctestRG-5577006791947779410") does not exist
        
        
FAIL

azurerm/resource_arm_automation_job_schedule_test.go Outdated Show resolved Hide resolved
azurerm/resource_arm_automation_job_schedule_test.go Outdated Show resolved Hide resolved
azurerm/resource_arm_automation_job_schedule_test.go Outdated Show resolved Hide resolved
@draggeta
Copy link
Contributor Author

Hi @katbyte , I've just run the tests and they run successfully here. I don't know why they failed on your end.
When I switch to tf.AccRandTimeInt(), the tests fail as mentioned before, due to seemingly the tests generating the same random integer:

--- FAIL: TestAccAzureRMAutomationJobSchedule_complete (77.17s)
    testing.go:568: Step 0 error: errors during apply:

        Error: automation.JobScheduleClient#Create: Failure responding to request: StatusCode=400 -- Original Error: autorest/azure: Service returned an error. Status=400 Code="BadRequest" Message="A job schedule for the specified runbook and schedule already exists."

          on ~\AppData\Local\Temp\tf-test676055953\main.tf line 49:
          (source code not available)

It could be a Windows issue where it somehow grabs the same 4 random numbers every time. For now I'll change it so hopefully it succeeds on your end.

@ghost ghost removed the waiting-response label May 10, 2019
@draggeta
Copy link
Contributor Author

draggeta commented Oct 14, 2019

@katbyte there seems to be a bug in the runbook deployment. In don't know exactly what, but it seems like it's not creating and/or updating the resources. Reading works fine. This doesn't only happen in my branch, but also with v 1.35 of the AzureRM module. I'll try and look into it tomorrow.

@ghost ghost removed the waiting-response label Oct 14, 2019
Tony Fortes Ramos added 2 commits October 14, 2019 16:54
@katbyte
Copy link
Collaborator

katbyte commented Oct 15, 2019

Sounds good @draggeta, thank you for the update!

@draggeta
Copy link
Contributor Author

After some further testing it seems that only the createorupdate function is broken. Reading, listing, and deleting all work fine. I'll go and test it with the SDK directly

@ghost ghost removed the waiting-response label Oct 15, 2019
@draggeta
Copy link
Contributor Author

draggeta commented Oct 16, 2019

Hi @katbyte,

After too many hours spent trying to figure out what wasn't working I figured out it was the script itself in the publish link parameter:
https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-automation-runbook-getvms/Runbooks/Get-AzureVMTutorial.ps1

The above link now returns a 404. Is it possible to put a script somewhere in this repo or somewhere else that won't change? The script also must remain static as one of the tests verifies the hash.

It needs to be changed for for the runbook tests as well.

@katbyte
Copy link
Collaborator

katbyte commented Oct 18, 2019

@draggeta,

You should be able to use the commit/tag it was last available in. However long term it would be ideal to add a short one of our own to this repo so in the future we can link to it once merged to master (but not required to get this merged)

@draggeta
Copy link
Contributor Author

@katbyte Should be done now. I don't know why I didn't think of that.

@ghost ghost removed the waiting-response label Oct 18, 2019
@draggeta draggeta requested a review from katbyte October 18, 2019 21:46
@tombuildsstuff tombuildsstuff modified the milestones: v1.36.0, v1.37.0 Oct 24, 2019
Copy link
Collaborator

@katbyte katbyte left a comment

Choose a reason for hiding this comment

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

Thanks for fixing this @draggeta! LGTM 👍

@katbyte katbyte merged commit 7866a93 into hashicorp:master Oct 28, 2019
@katbyte katbyte modified the milestones: v1.37.0, v1.36.0 Oct 28, 2019
katbyte added a commit that referenced this pull request Oct 28, 2019
@ghost
Copy link

ghost commented Oct 29, 2019

This has been released in version 1.36.0 of the provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading. As an example:

provider "azurerm" {
    version = "~> 1.36.0"
}
# ... other configuration ...

@ghost
Copy link

ghost commented Nov 27, 2019

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 [email protected]. Thanks!

@ghost ghost locked and limited conversation to collaborators Nov 27, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New resouce/feature. Link a azurerm schedule with a runbook
3 participants