Skip to content

Commit

Permalink
New Resource: azurerm_scheduler_job (#1172)
Browse files Browse the repository at this point in the history
* Intial commit of scheduler_job

* azurerm_scheduler_job todo & misc cleanup

* travis CI fixes

travis CI fixes

* azurerm_scheduler_job: removed populate function & other small changes

* aszurerm_schduler_job: update tests & validation

* Updated example URLs

* schduler jobs & collection: formatted examples

* azurerm_scheduler_job: schema adjustments

* azurerm_scheduler_job: resolving PR comments

* azurerm_scheduler_job: First round of PR fixes

* azurerm_scheduler_job: Futher PR fixes

* scheduler job: address PR comments

* scheduler job - minor fix to tests

* Adding tests for the validation functions

* inline-ing the check methods

* Fixing the highlight in the sidebar

* Fixing merge issues

* Fixing a crash when loading the environment
  • Loading branch information
katbyte authored and tombuildsstuff committed Jul 3, 2018
1 parent 7897d07 commit 3cc5cdb
Show file tree
Hide file tree
Showing 13 changed files with 2,256 additions and 20 deletions.
23 changes: 14 additions & 9 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ type ArmClient struct {
resourceGroupsClient resources.GroupsClient
subscriptionsClient subscriptions.Client

//Scheduler
schedulerJobCollectionsClient scheduler.JobCollectionsClient
schedulerJobsClient scheduler.JobsClient

// Search
searchServicesClient search.ServicesClient

Expand All @@ -200,9 +204,6 @@ type ArmClient struct {
serviceBusSubscriptionsClient servicebus.SubscriptionsClient
serviceBusSubscriptionRulesClient servicebus.RulesClient

//Scheduler
schedulerJobCollectionsClient scheduler.JobCollectionsClient

// Storage
storageServiceClient storage.AccountsClient
storageUsageClient storage.UsageClient
Expand Down Expand Up @@ -868,6 +869,16 @@ func (c *ArmClient) registerResourcesClients(endpoint, subscriptionId string, au
c.subscriptionsClient = subscriptionsClient
}

func (c *ArmClient) registerSchedulerClients(endpoint, subscriptionId string, auth autorest.Authorizer) {
jobCollectionsClient := scheduler.NewJobCollectionsClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&jobCollectionsClient.Client, auth)
c.schedulerJobCollectionsClient = jobCollectionsClient

jobsClient := scheduler.NewJobsClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&jobsClient.Client, auth)
c.schedulerJobsClient = jobsClient
}

func (c *ArmClient) registerSearchClients(endpoint, subscriptionId string, auth autorest.Authorizer) {
searchClient := search.NewServicesClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&searchClient.Client, auth)
Expand Down Expand Up @@ -896,12 +907,6 @@ func (c *ArmClient) registerServiceBusClients(endpoint, subscriptionId string, a
c.serviceBusSubscriptionRulesClient = subscriptionRulesClient
}

func (c *ArmClient) registerSchedulerClients(endpoint, subscriptionId string, auth autorest.Authorizer) {
jobsClient := scheduler.NewJobCollectionsClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&jobsClient.Client, auth)
c.schedulerJobCollectionsClient = jobsClient
}

func (c *ArmClient) registerStorageClients(endpoint, subscriptionId string, auth autorest.Authorizer) {
accountsClient := storage.NewAccountsClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&accountsClient.Client, auth)
Expand Down
35 changes: 35 additions & 0 deletions azurerm/helpers/set/set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package set

import (
"strconv"
"strings"

"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
)

func HashInt(v interface{}) int {
return hashcode.String(strconv.Itoa(v.(int)))
}

func HashStringIgnoreCase(v interface{}) int {
return hashcode.String(strings.ToLower(v.(string)))
}

func FromInt32Slice(slice []int32) *schema.Set {
set := &schema.Set{F: HashInt}
for _, v := range slice {
set.Add(int(v))
}

return set
}

func ToSliceInt32P(set *schema.Set) *[]int32 {
var slice []int32
for _, m := range set.List() {
slice = append(slice, int32(m.(int)))
}

return &slice
}
71 changes: 71 additions & 0 deletions azurerm/helpers/validate/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package validate

import (
"fmt"
"net/url"

"strings"

"github.com/hashicorp/terraform/helper/schema"
)

func IntBetweenAndNot(min, max, not int) schema.SchemaValidateFunc {
return func(i interface{}, k string) (_ []string, errors []error) {
v, ok := i.(int)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %q to be int", k))
return
}

if v < min || v > max {
errors = append(errors, fmt.Errorf("expected %s to be in the range (%d - %d), got %d", k, min, max, v))
return
}

if v == not {
errors = append(errors, fmt.Errorf("expected %s to not be %d, got %d", k, not, v))
return
}

return
}
}

func UrlIsHttpOrHttps() schema.SchemaValidateFunc {
return UrlWithScheme([]string{"http", "https"})
}

func UrlWithScheme(validSchemes []string) schema.SchemaValidateFunc {
return func(i interface{}, k string) (_ []string, errors []error) {
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %q to be string", k))
return
}

url, err := url.Parse(v)
if err != nil {
errors = append(errors, fmt.Errorf("%q url is in an invalid format: %q (%+v)", k, i, err))
return
}

if url.Host == "" {
errors = append(errors, fmt.Errorf("%q url has no host: %q", k, url))
}

found := false
for _, s := range validSchemes {
if strings.EqualFold(url.Scheme, s) {
found = true
break
}
}

if !found {
schemes := strings.Join(validSchemes, ",")
errors = append(errors, fmt.Errorf("URL scheme %q isn't valid - supported scheme's are %q", url.Scheme, schemes))
}

return
}
}
45 changes: 45 additions & 0 deletions azurerm/helpers/validate/validate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package validate

import "testing"

func TestUrlWithScheme(t *testing.T) {
validSchemes := []string{"example"}
testCases := []struct {
Url string
ShouldHaveError bool
}{
{
Url: "example://mysite.com",
ShouldHaveError: false,
},
{
Url: "http://mysite.com",
ShouldHaveError: true,
},
{
Url: "example://",
ShouldHaveError: true,
},
{
Url: "example://validhost",
ShouldHaveError: false,
},
}

t.Run("TestUrlWithScheme", func(t *testing.T) {
for _, v := range testCases {
_, errors := UrlWithScheme(validSchemes)(v.Url, "field_name")

hasErrors := len(errors) > 0
if v.ShouldHaveError && !hasErrors {
t.Fatalf("Expected an error but didn't get one for %q", v.Url)
return
}

if !v.ShouldHaveError && hasErrors {
t.Fatalf("Expected %q to return no errors, but got some %+v", v.Url, errors)
return
}
}
})
}
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_servicebus_topic": resourceArmServiceBusTopic(),
"azurerm_servicebus_topic_authorization_rule": resourceArmServiceBusTopicAuthorizationRule(),
"azurerm_snapshot": resourceArmSnapshot(),
"azurerm_scheduler_job": resourceArmSchedulerJob(),
"azurerm_scheduler_job_collection": resourceArmSchedulerJobCollection(),
"azurerm_sql_database": resourceArmSqlDatabase(),
"azurerm_sql_elasticpool": resourceArmSqlElasticPool(),
Expand Down
Loading

0 comments on commit 3cc5cdb

Please sign in to comment.