From c275d930359d68af9f1d6bc1bbfcd7f6e581e911 Mon Sep 17 00:00:00 2001 From: kt Date: Fri, 16 Mar 2018 02:08:16 -0700 Subject: [PATCH 1/3] Added new data source scheduler_job_collection --- .../data_source_scheduler_job_collection.go | 90 +++++++++++++++++++ ...ta_source_scheduler_job_collection_test.go | 63 +++++++++++++ azurerm/provider.go | 1 + .../resource_arm_scheduler_job_collection.go | 7 +- ...ource_arm_scheduler_job_collection_test.go | 53 +++++++---- website/azurerm.erb | 4 + .../d/scheduler_job_collection.html.markdown | 56 ++++++++++++ 7 files changed, 255 insertions(+), 19 deletions(-) create mode 100644 azurerm/data_source_scheduler_job_collection.go create mode 100644 azurerm/data_source_scheduler_job_collection_test.go create mode 100644 website/docs/d/scheduler_job_collection.html.markdown diff --git a/azurerm/data_source_scheduler_job_collection.go b/azurerm/data_source_scheduler_job_collection.go new file mode 100644 index 000000000000..dd18506c5a66 --- /dev/null +++ b/azurerm/data_source_scheduler_job_collection.go @@ -0,0 +1,90 @@ +package azurerm + +import ( + "fmt" + + "github.com/hashicorp/terraform/helper/schema" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func dataSourceArmSchedulerJobCollection() *schema.Resource { + return &schema.Resource{ + Read: dataSourceArmSchedulerJobCollectionRead, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + }, + + "location": locationForDataSourceSchema(), + + "resource_group_name": resourceGroupNameForDataSourceSchema(), + + "tags": tagsForDataSourceSchema(), + + "sku": { + Type: schema.TypeString, + Computed: true, + }, + + "state": { + Type: schema.TypeString, + Computed: true, + }, + + "quota": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + + //max_job_occurrence doesn't seem to do anything and always remains empty + + "max_job_count": { + Type: schema.TypeInt, + Computed: true, + }, + + "max_recurrence_frequency": { + Type: schema.TypeString, + Computed: true, + }, + + //this is MaxRecurrance.Interval, property is named this as the documentation in the api states: + // Gets or sets the interval between retries. + "max_retry_interval": { + Type: schema.TypeInt, + Computed: true, + }, + }, + }, + }, + }, + } +} + +func dataSourceArmSchedulerJobCollectionRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ArmClient).schedulerJobCollectionsClient + ctx := meta.(*ArmClient).StopContext + + resourceGroup := d.Get("resource_group_name").(string) + name := d.Get("name").(string) + + collection, err := client.Get(ctx, resourceGroup, name) + if err != nil { + if utils.ResponseWasNotFound(collection.Response) { + d.SetId("") + return nil + } + + return fmt.Errorf("Error making Read request on Scheduler Job Collection %q (Resource Group %q): %+v", name, resourceGroup, err) + } + + d.SetId(*collection.ID) + + return resourceArmSchedulerJobCollectionPopulate(d, resourceGroup, &collection) +} diff --git a/azurerm/data_source_scheduler_job_collection_test.go b/azurerm/data_source_scheduler_job_collection_test.go new file mode 100644 index 000000000000..aebffec1377c --- /dev/null +++ b/azurerm/data_source_scheduler_job_collection_test.go @@ -0,0 +1,63 @@ +package azurerm + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccDataSourceAzureRMSchedulerJobCollection_basic(t *testing.T) { + dataSourceName := "data.azurerm_scheduler_job_collection.test" + ri := acctest.RandInt() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceSchedulerJobCollection_basic(ri, testLocation()), + Check: checkAccAzureRMSchedulerJobCollection_basic(dataSourceName), + }, + }, + }) +} + +func TestAccDataSourceAzureRMSchedulerJobCollection_complete(t *testing.T) { + dataSourceName := "data.azurerm_scheduler_job_collection.test" + ri := acctest.RandInt() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceSchedulerJobCollection_complete(ri, testLocation()), + Check: checkAccAzureRMSchedulerJobCollection_complete(dataSourceName), + }, + }, + }) +} + +func testAccDataSourceSchedulerJobCollection_basic(rInt int, location string) string { + return fmt.Sprintf(` +%s + +data "azurerm_scheduler_job_collection" "test" { + name = "${azurerm_scheduler_job_collection.test.name}" + resource_group_name = "${azurerm_resource_group.test.name}" +} +`, testAccAzureRMSchedulerJobCollection_basic(rInt, location, "")) +} + +func testAccDataSourceSchedulerJobCollection_complete(rInt int, location string) string { + return fmt.Sprintf(` +%s + +data "azurerm_scheduler_job_collection" "test" { + name = "${azurerm_scheduler_job_collection.test.name}" + resource_group_name = "${azurerm_resource_group.test.name}" +} +`, testAccAzureRMSchedulerJobCollection_complete(rInt, location)) +} diff --git a/azurerm/provider.go b/azurerm/provider.go index f807e9a3f113..d532308e4557 100644 --- a/azurerm/provider.go +++ b/azurerm/provider.go @@ -94,6 +94,7 @@ func Provider() terraform.ResourceProvider { "azurerm_public_ips": dataSourceArmPublicIPs(), "azurerm_resource_group": dataSourceArmResourceGroup(), "azurerm_role_definition": dataSourceArmRoleDefinition(), + "azurerm_scheduler_job_collection": dataSourceArmSchedulerJobCollection(), "azurerm_storage_account": dataSourceArmStorageAccount(), "azurerm_snapshot": dataSourceArmSnapshot(), "azurerm_subnet": dataSourceArmSubnet(), diff --git a/azurerm/resource_arm_scheduler_job_collection.go b/azurerm/resource_arm_scheduler_job_collection.go index f668e496c8ee..bdcd61a103c1 100644 --- a/azurerm/resource_arm_scheduler_job_collection.go +++ b/azurerm/resource_arm_scheduler_job_collection.go @@ -3,6 +3,7 @@ package azurerm import ( "fmt" "log" + "regexp" "github.com/Azure/azure-sdk-for-go/services/scheduler/mgmt/2016-03-01/scheduler" @@ -28,6 +29,10 @@ func resourceArmSchedulerJobCollection() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: true, + ValidateFunc: validation.StringMatch( + regexp.MustCompile("^[a-zA-Z][-_a-zA-Z0-9]{0,99}$"), + "Job Collection Name name must be 1 - 100 characters long, start with a letter and contain only letters, numbers, hyphens and underscores.", + ), }, "location": locationSchema(), @@ -89,7 +94,7 @@ func resourceArmSchedulerJobCollection() *schema.Resource { }, true), }, - //this sets MaxRecurrance.Interval, and the documentation in the api states: + //this is MaxRecurrance.Interval, property is named this as the documentation in the api states: // Gets or sets the interval between retries. "max_retry_interval": { Type: schema.TypeInt, diff --git a/azurerm/resource_arm_scheduler_job_collection_test.go b/azurerm/resource_arm_scheduler_job_collection_test.go index a18e5e05fcdf..438867d82791 100644 --- a/azurerm/resource_arm_scheduler_job_collection_test.go +++ b/azurerm/resource_arm_scheduler_job_collection_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/Azure/azure-sdk-for-go/services/scheduler/mgmt/2016-03-01/scheduler" + "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" @@ -23,11 +24,7 @@ func TestAccAzureRMSchedulerJobCollection_basic(t *testing.T) { Steps: []resource.TestStep{ { Config: config, - Check: resource.ComposeTestCheckFunc( - testCheckAzureRMSchedulerJobCollectionExists(resourceName), - resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), - resource.TestCheckResourceAttr(resourceName, "state", string(scheduler.Enabled)), - ), + Check: checkAccAzureRMSchedulerJobCollection_basic(resourceName), }, }, }) @@ -46,22 +43,11 @@ func TestAccAzureRMSchedulerJobCollection_complete(t *testing.T) { Steps: []resource.TestStep{ { Config: preConfig, - Check: resource.ComposeTestCheckFunc( - testCheckAzureRMSchedulerJobCollectionExists(resourceName), - resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), - resource.TestCheckResourceAttr(resourceName, "state", string(scheduler.Enabled)), - ), + Check: checkAccAzureRMSchedulerJobCollection_basic(resourceName), }, { Config: config, - Check: resource.ComposeTestCheckFunc( - testCheckAzureRMSchedulerJobCollectionExists(resourceName), - resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), - resource.TestCheckResourceAttr(resourceName, "state", string(scheduler.Disabled)), - resource.TestCheckResourceAttr(resourceName, "quota.0.max_job_count", "10"), - resource.TestCheckResourceAttr(resourceName, "quota.0.max_retry_interval", "10"), - resource.TestCheckResourceAttr(resourceName, "quota.0.max_recurrence_frequency", "hour"), - ), + Check: checkAccAzureRMSchedulerJobCollection_complete(resourceName), }, }, }) @@ -152,3 +138,34 @@ func testAccAzureRMSchedulerJobCollection_complete(rInt int, location string) st } `) } + +func checkAccAzureRMSchedulerJobCollection_base(resourceName string) resource.TestCheckFunc { + return resource.ComposeTestCheckFunc( + testCheckAzureRMSchedulerJobCollectionExists(resourceName), + resource.TestCheckResourceAttrSet(resourceName, "name"), + resource.TestCheckResourceAttrSet(resourceName, "location"), + resource.TestCheckResourceAttrSet(resourceName, "resource_group_name"), + resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), + ) +} + +func checkAccAzureRMSchedulerJobCollection_basic(resourceName string) resource.TestCheckFunc { + return resource.ComposeAggregateTestCheckFunc( + checkAccAzureRMSchedulerJobCollection_base(resourceName), + resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(resourceName, "state", string(scheduler.Enabled)), + ), + ) +} + +func checkAccAzureRMSchedulerJobCollection_complete(resourceName string) resource.TestCheckFunc { + return resource.ComposeAggregateTestCheckFunc( + checkAccAzureRMSchedulerJobCollection_base(resourceName), + resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(resourceName, "state", string(scheduler.Disabled)), + resource.TestCheckResourceAttr(resourceName, "quota.0.max_job_count", "10"), + resource.TestCheckResourceAttr(resourceName, "quota.0.max_retry_interval", "10"), + resource.TestCheckResourceAttr(resourceName, "quota.0.max_recurrence_frequency", "hour"), + ), + ) +} diff --git a/website/azurerm.erb b/website/azurerm.erb index a9a407d38f0f..c5b5ea452cc0 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -95,6 +95,10 @@ azurerm_role_definition + > + azurerm_scheduler_job_collection + + > azurerm_storage_account diff --git a/website/docs/d/scheduler_job_collection.html.markdown b/website/docs/d/scheduler_job_collection.html.markdown new file mode 100644 index 000000000000..3fdb1cdbd122 --- /dev/null +++ b/website/docs/d/scheduler_job_collection.html.markdown @@ -0,0 +1,56 @@ +--- +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_scheduler_job_collection" +sidebar_current: "docs-azurerm-datasource-scheduler_job_collection" +description: |- + Get information about the specified schelduer job collection. +--- + +# Data Source: azurerm_scheduler_job_collection + +Use this data source to access the properties of an Azure scheduler job collection. + +## Example Usage + +```hcl +data "azurerm_scheduler_job_collection" "test" { + name = "tfex-job-collection" + resource_group_name = "tfex-job-collection-rg" +} + +output "job_collection_state" { + value = "${data.azurerm_scheduler_job_collection.jobs.state}" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) Specifies the name of the Scheduler Job Collection. Changing this forces a new resource to be created. + +* `resource_group_name` - (Required) The name of the resource group in which to create the Scheduler Job Collection. Changing this forces a new resource to be created. + +## Attributes Reference + +The following attributes are exported: + +* `id` - The ID of the Scheduler Job Collection. + +* `location` - The Azure location where the resource exists. + +* `tags` - A mapping of tags to assign to the resource. + +* `sku` - The Job Collection's pricing level's SKU. Possible values include: `Standard`, `Free`, `P10Premium`, `P20Premium`. + +* `state` - The Job Collection's state. Possible values include: `Enabled`, `Disabled`, `Suspended`. + +* `quota` - The Job collection quotas as documented in the `quota` block below. + +The `quota` block supports: + +* `max_job_count` - Sets the maximum number of jobs in the collection. + +* `max_recurrence_frequency` - The maximum frequency of recurrence. Possible values include: `Minute`, `Hour`, `Day`, `Week`, `Month` + +* `max_retry_interval` - The maximum interval between retries. From 95f95ed3c383c6ee604cb934b9ce48c67b900fcf Mon Sep 17 00:00:00 2001 From: kt Date: Fri, 16 Mar 2018 11:47:04 -0700 Subject: [PATCH 2/3] Minor change to tests --- ...ource_arm_scheduler_job_collection_test.go | 28 ++++++++----------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/azurerm/resource_arm_scheduler_job_collection_test.go b/azurerm/resource_arm_scheduler_job_collection_test.go index 438867d82791..8da692f6e334 100644 --- a/azurerm/resource_arm_scheduler_job_collection_test.go +++ b/azurerm/resource_arm_scheduler_job_collection_test.go @@ -139,33 +139,27 @@ func testAccAzureRMSchedulerJobCollection_complete(rInt int, location string) st `) } -func checkAccAzureRMSchedulerJobCollection_base(resourceName string) resource.TestCheckFunc { +func checkAccAzureRMSchedulerJobCollection_basic(resourceName string) resource.TestCheckFunc { return resource.ComposeTestCheckFunc( testCheckAzureRMSchedulerJobCollectionExists(resourceName), resource.TestCheckResourceAttrSet(resourceName, "name"), resource.TestCheckResourceAttrSet(resourceName, "location"), resource.TestCheckResourceAttrSet(resourceName, "resource_group_name"), resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), - ) -} - -func checkAccAzureRMSchedulerJobCollection_basic(resourceName string) resource.TestCheckFunc { - return resource.ComposeAggregateTestCheckFunc( - checkAccAzureRMSchedulerJobCollection_base(resourceName), - resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(resourceName, "state", string(scheduler.Enabled)), - ), + resource.TestCheckResourceAttr(resourceName, "state", string(scheduler.Enabled)), ) } func checkAccAzureRMSchedulerJobCollection_complete(resourceName string) resource.TestCheckFunc { return resource.ComposeAggregateTestCheckFunc( - checkAccAzureRMSchedulerJobCollection_base(resourceName), - resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(resourceName, "state", string(scheduler.Disabled)), - resource.TestCheckResourceAttr(resourceName, "quota.0.max_job_count", "10"), - resource.TestCheckResourceAttr(resourceName, "quota.0.max_retry_interval", "10"), - resource.TestCheckResourceAttr(resourceName, "quota.0.max_recurrence_frequency", "hour"), - ), + testCheckAzureRMSchedulerJobCollectionExists(resourceName), + resource.TestCheckResourceAttrSet(resourceName, "name"), + resource.TestCheckResourceAttrSet(resourceName, "location"), + resource.TestCheckResourceAttrSet(resourceName, "resource_group_name"), + resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), + resource.TestCheckResourceAttr(resourceName, "state", string(scheduler.Disabled)), + resource.TestCheckResourceAttr(resourceName, "quota.0.max_job_count", "10"), + resource.TestCheckResourceAttr(resourceName, "quota.0.max_retry_interval", "10"), + resource.TestCheckResourceAttr(resourceName, "quota.0.max_recurrence_frequency", "hour"), ) } From 6ad99fc39b5a3181cb4a5985819fc12e31f0d779 Mon Sep 17 00:00:00 2001 From: kt Date: Mon, 19 Mar 2018 00:31:31 -0700 Subject: [PATCH 3/3] Updated the azurerm_scheduler_job_collection data source with requested changes --- .../data_source_scheduler_job_collection.go | 20 ++++++++++++++++++- .../d/scheduler_job_collection.html.markdown | 14 ++++++------- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/azurerm/data_source_scheduler_job_collection.go b/azurerm/data_source_scheduler_job_collection.go index dd18506c5a66..ce1bfe1ef8fc 100644 --- a/azurerm/data_source_scheduler_job_collection.go +++ b/azurerm/data_source_scheduler_job_collection.go @@ -86,5 +86,23 @@ func dataSourceArmSchedulerJobCollectionRead(d *schema.ResourceData, meta interf d.SetId(*collection.ID) - return resourceArmSchedulerJobCollectionPopulate(d, resourceGroup, &collection) + //standard properties + d.Set("name", collection.Name) + d.Set("location", azureRMNormalizeLocation(*collection.Location)) + d.Set("resource_group_name", resourceGroup) + flattenAndSetTags(d, collection.Tags) + + //resource specific + if properties := collection.Properties; properties != nil { + if sku := properties.Sku; sku != nil { + d.Set("sku", sku.Name) + } + d.Set("state", string(properties.State)) + + if err := d.Set("quota", flattenAzureArmSchedulerJobCollectionQuota(properties.Quota)); err != nil { + return fmt.Errorf("Error flattening quota for Job Collection %q (Resource Group %q): %+v", collection.Name, resourceGroup, err) + } + } + + return nil } diff --git a/website/docs/d/scheduler_job_collection.html.markdown b/website/docs/d/scheduler_job_collection.html.markdown index 3fdb1cdbd122..83727ba78864 100644 --- a/website/docs/d/scheduler_job_collection.html.markdown +++ b/website/docs/d/scheduler_job_collection.html.markdown @@ -3,7 +3,7 @@ layout: "azurerm" page_title: "Azure Resource Manager: azurerm_scheduler_job_collection" sidebar_current: "docs-azurerm-datasource-scheduler_job_collection" description: |- - Get information about the specified schelduer job collection. + Get information about the specified scheduler job collection. --- # Data Source: azurerm_scheduler_job_collection @@ -27,9 +27,9 @@ output "job_collection_state" { The following arguments are supported: -* `name` - (Required) Specifies the name of the Scheduler Job Collection. Changing this forces a new resource to be created. +* `name` - (Required) Specifies the name of the Scheduler Job Collection. -* `resource_group_name` - (Required) The name of the resource group in which to create the Scheduler Job Collection. Changing this forces a new resource to be created. +* `resource_group_name` - (Required) Specifies the name of the resource group in which the Scheduler Job Collection resides. ## Attributes Reference @@ -39,11 +39,11 @@ The following attributes are exported: * `location` - The Azure location where the resource exists. -* `tags` - A mapping of tags to assign to the resource. +* `tags` - A mapping of tags assigned to the resource. -* `sku` - The Job Collection's pricing level's SKU. Possible values include: `Standard`, `Free`, `P10Premium`, `P20Premium`. +* `sku` - The Job Collection's pricing level's SKU. -* `state` - The Job Collection's state. Possible values include: `Enabled`, `Disabled`, `Suspended`. +* `state` - The Job Collection's state. * `quota` - The Job collection quotas as documented in the `quota` block below. @@ -51,6 +51,6 @@ The `quota` block supports: * `max_job_count` - Sets the maximum number of jobs in the collection. -* `max_recurrence_frequency` - The maximum frequency of recurrence. Possible values include: `Minute`, `Hour`, `Day`, `Week`, `Month` +* `max_recurrence_frequency` - The maximum frequency of recurrence. * `max_retry_interval` - The maximum interval between retries.