Skip to content

Commit

Permalink
Merge pull request #990 from terraform-providers/f-ds-scheduler_job_c…
Browse files Browse the repository at this point in the history
…ollection

New Data Source: `azurerm_scheduler_job_collection`
  • Loading branch information
tombuildsstuff authored Mar 22, 2018
2 parents 7c7b8f3 + 6ad99fc commit c253114
Show file tree
Hide file tree
Showing 7 changed files with 268 additions and 20 deletions.
108 changes: 108 additions & 0 deletions azurerm/data_source_scheduler_job_collection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
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)

//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
}
63 changes: 63 additions & 0 deletions azurerm/data_source_scheduler_job_collection_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
3 changes: 2 additions & 1 deletion azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ func Provider() terraform.ResourceProvider {
"azurerm_public_ips": dataSourceArmPublicIPs(),
"azurerm_resource_group": dataSourceArmResourceGroup(),
"azurerm_role_definition": dataSourceArmRoleDefinition(),
"azurerm_storage_account": dataSourceArmStorageAccount(),
"azurerm_scheduler_job_collection": dataSourceArmSchedulerJobCollection(),
"azurerm_snapshot": dataSourceArmSnapshot(),
"azurerm_storage_account": dataSourceArmStorageAccount(),
"azurerm_subnet": dataSourceArmSubnet(),
"azurerm_subscription": dataSourceArmSubscription(),
"azurerm_subscriptions": dataSourceArmSubscriptions(),
Expand Down
7 changes: 6 additions & 1 deletion azurerm/resource_arm_scheduler_job_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package azurerm
import (
"fmt"
"log"
"regexp"

"github.com/Azure/azure-sdk-for-go/services/scheduler/mgmt/2016-03-01/scheduler"

Expand All @@ -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(),
Expand Down Expand Up @@ -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,
Expand Down
47 changes: 29 additions & 18 deletions azurerm/resource_arm_scheduler_job_collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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),
},
},
})
Expand All @@ -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),
},
},
})
Expand Down Expand Up @@ -152,3 +138,28 @@ func testAccAzureRMSchedulerJobCollection_complete(rInt int, location string) st
}
`)
}

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"),
resource.TestCheckResourceAttr(resourceName, "state", string(scheduler.Enabled)),
)
}

func checkAccAzureRMSchedulerJobCollection_complete(resourceName string) resource.TestCheckFunc {
return resource.ComposeAggregateTestCheckFunc(
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"),
)
}
4 changes: 4 additions & 0 deletions website/azurerm.erb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@
<a href="/docs/providers/azurerm/d/role_definition.html">azurerm_role_definition</a>
</li>

<li<%= sidebar_current("docs-azurerm-datasource-scheduler-job-collection") %>>
<a href="/docs/providers/azurerm/d/scheduler-job-collection.html">azurerm_scheduler_job_collection</a>
</li>

<li<%= sidebar_current("docs-azurerm-datasource-storage-account") %>>
<a href="/docs/providers/azurerm/d/storage_account.html">azurerm_storage_account</a>
</li>
Expand Down
56 changes: 56 additions & 0 deletions website/docs/d/scheduler_job_collection.html.markdown
Original file line number Diff line number Diff line change
@@ -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 scheduler 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.

* `resource_group_name` - (Required) Specifies the name of the resource group in which the Scheduler Job Collection resides.

## 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 assigned to the resource.

* `sku` - The Job Collection's pricing level's SKU.

* `state` - The Job Collection's state.

* `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.

* `max_retry_interval` - The maximum interval between retries.

0 comments on commit c253114

Please sign in to comment.