Skip to content

Commit

Permalink
azurerm_scheduler_job todo & misc cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
katbyte committed Apr 27, 2018
1 parent 020df2c commit 7a07134
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 51 deletions.
38 changes: 10 additions & 28 deletions azurerm/resource_arm_scheduler_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,9 @@ func resourceArmSchedulerJob() *schema.Resource {

//actions
"action_web": resourceArmSchedulerJobActionWebSchema("action_web"),
//"action_servicebus": resourceArmSchedulerJobActionServicebusSchema("action_servicebus", []string{"action_web", "action_storagequeue"}),

//each action can also be an error action
"error_action_web": resourceArmSchedulerJobActionWebSchema("error_action_web"),
//"error_action_servicebus": resourceArmSchedulerJobActionServicebusSchema("action_servicebus", []string{"action_web", "action_servicebus"}),

//retry policy
"retry": {
Expand All @@ -67,19 +65,9 @@ func resourceArmSchedulerJob() *schema.Resource {
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
//none cannot be used with either of these values todo keep?
/*"type": {
Type: schema.TypeString,
Optional: true,
Default: string(scheduler.Fixed),
DiffSuppressFunc: internal.CaseDifference,
ValidateFunc: validation.StringInSlice([]string{
string(scheduler.Fixed),
string(scheduler.None),
}, true),
},*/

//silently fails if the duration is not in the correct format
//todo validation
"interval": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -128,9 +116,10 @@ func resourceArmSchedulerJob() *schema.Resource {
},

"count": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(1, 2147483647), //silently fails/produces odd results at >2147483647
Type: schema.TypeInt,
Optional: true,
//silently fails/produces odd results at >2147483647
ValidateFunc: validation.IntBetween(1, 2147483647),
},

"end_time": {
Expand Down Expand Up @@ -398,9 +387,7 @@ func resourceArmSchedulerJobActionWebSchema(propertyName string) *schema.Schema
func resourceArmSchedulerJobCustomizeDiff(diff *schema.ResourceDiff, v interface{}) error {

_, hasWeb := diff.GetOk("action_web")
//_, hasService := diff.GetOk("action_servicebus")
//_, hasStorage := diff.GetOk("action_storagequeue")
if !hasWeb { //&& !hasService && !hasStorage
if !hasWeb {
return fmt.Errorf("One of `action_web`, `action_servicebus` or `action_storagequeue` must be set")
}

Expand All @@ -410,7 +397,7 @@ func resourceArmSchedulerJobCustomizeDiff(diff *schema.ResourceDiff, v interface
//if neither count nor end time is set the API will silently fail
_, hasCount := recurrence["count"]
_, hasEnd := recurrence["end_time"]
if !hasCount && !hasEnd { //&& !hasService && !hasStorage
if !hasCount && !hasEnd {
return fmt.Errorf("One of `count` or `end_time` must be set for the 'recurrence' block.")
}

Expand Down Expand Up @@ -482,9 +469,6 @@ func resourceArmSchedulerJobCreateUpdate(d *schema.ResourceData, meta interface{
//action
if b, ok := d.GetOk("action_web"); ok {
job.Properties.Action.Request, job.Properties.Action.Type = expandAzureArmSchedulerJobActionRequest(b)
} else {
//todo should never get here because of the customize diff, so is this required?
return fmt.Errorf("One of `action_web`, `action_servicebus` or `action_storagequeue` must be set for scheduler job %q (Resource Group %q)", name, resourceGroup)
}

//error action
Expand Down Expand Up @@ -663,7 +647,6 @@ func resourceArmSchedulerJobDelete(d *schema.ResourceData, meta interface{}) err
}

//expand (terraform -> API)

func expandAzureArmSchedulerJobActionRequest(b interface{}) (*scheduler.HTTPRequest, scheduler.JobActionType) {

block := b.([]interface{})[0].(map[string]interface{})
Expand All @@ -673,7 +656,7 @@ func expandAzureArmSchedulerJobActionRequest(b interface{}) (*scheduler.HTTPRequ
request := scheduler.HTTPRequest{
URI: &url,
Method: utils.String(block["method"].(string)),
Headers: &map[string]*string{},
Headers: map[string]*string{},
}

// determine type from the url, the property validation must ensure this
Expand All @@ -689,7 +672,7 @@ func expandAzureArmSchedulerJobActionRequest(b interface{}) (*scheduler.HTTPRequ
//load headers
//if v, ok := block["headers"].(map[string]interface{}); ok { //check doesn't seem to be needed
for k, v := range block["headers"].(map[string]interface{}) {
(*request.Headers)[k] = utils.String(v.(string))
(request.Headers)[k] = utils.String(v.(string))
}

//only valid for a set
Expand Down Expand Up @@ -828,7 +811,7 @@ func flattenAzureArmSchedulerJobActionRequest(request *scheduler.HTTPRequest, ob

if v := request.Headers; v != nil {
headers := map[string]interface{}{}
for k, v := range *v {
for k, v := range v {
headers[k] = *v
}

Expand Down Expand Up @@ -977,7 +960,6 @@ func resourceAzureRMSchedulerJobMonthlyOccurrenceHash(v interface{}) int {
return hashcode.String(buf.String())
}

//todo move these somewhere?
func sliceToSetInt32(slice []int32) *schema.Set {
set := &schema.Set{F: set.HashInt}
for _, v := range slice {
Expand Down
27 changes: 7 additions & 20 deletions azurerm/resource_arm_scheduler_job_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ func resourceArmSchedulerJobCollection() *schema.Resource {
"quota": {
Type: schema.TypeList,
Optional: true,
MinItems: 1,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand All @@ -95,21 +94,12 @@ func resourceArmSchedulerJobCollection() *schema.Resource {
}, true),
},

//while in the documentation the MaxRecurrance.Interval is stated to "Gets or sets the interval between retries."
//it does appear it is the max interval allowed for a recurrence
//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,
Optional: true,
Deprecated: "Renamed to match azure",
ConflictsWith: []string{"quota.0.max_recurrence_interval"},
ValidateFunc: validation.IntAtLeast(1), //changes depending on the frequency, unknown maximums
},

"max_recurrence_interval": {
Type: schema.TypeInt,
Optional: true,
ConflictsWith: []string{"quota.0.max_retry_interval"},
ValidateFunc: validation.IntAtLeast(1), //changes depending on the frequency, unknown maximums
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntAtLeast(1), //changes depending on the frequency, unknown maximums
},
},
},
Expand Down Expand Up @@ -257,10 +247,8 @@ func expandAzureArmSchedulerJobCollectionQuota(d *schema.ResourceData) *schedule
}
if v, ok := quotaBlock["max_recurrence_frequency"].(string); ok {
quota.MaxRecurrence.Frequency = scheduler.RecurrenceFrequency(v)
} else if v, ok := quotaBlock["max_retry_interval"].(int); ok {
quota.MaxRecurrence.Interval = utils.Int32(int32(v))
}
if v, ok := quotaBlock["max_recurrence_interval"].(int); ok {
if v, ok := quotaBlock["max_retry_interval"].(int); ok {
quota.MaxRecurrence.Interval = utils.Int32(int32(v))
}

Expand All @@ -283,8 +271,7 @@ func flattenAzureArmSchedulerJobCollectionQuota(quota *scheduler.JobCollectionQu
}
if recurrence := quota.MaxRecurrence; recurrence != nil {
if v := recurrence.Interval; v != nil {
//quotaBlock["max_retry_interval"] = *v //todo fix
quotaBlock["max_recurrence_interval"] = *v
quotaBlock["max_retry_interval"] = *v
}

quotaBlock["max_recurrence_frequency"] = string(recurrence.Frequency)
Expand Down
2 changes: 0 additions & 2 deletions azurerm/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"github.com/satori/uuid"
)

//move to package!

func validateRFC3339Date(v interface{}, k string) (ws []string, errors []error) {
dateString := v.(string)

Expand Down
2 changes: 1 addition & 1 deletion examples/scheduler-jobs/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ resource "azurerm_scheduler_job" "web-recurring_weekly-auth_cert" {
}


resource "azurerm_scheduler_job" "web-recurring_monthly-error_actioin" {
resource "azurerm_scheduler_job" "web-recurring_monthly-error_action" {
name = "tfex-web-recurring_monthly-auth_ad"
resource_group_name = "${azurerm_resource_group.rg.name}"
job_collection_name = "${azurerm_scheduler_job_collection.jc.name}"
Expand Down

0 comments on commit 7a07134

Please sign in to comment.