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 azurerm_data_factory_trigger_tumbling_window resource #9127

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions azurerm/helpers/validate/datafactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,14 @@ func DataFactoryName() schema.SchemaValidateFunc {
return warnings, errors
}
}

func TriggerDelayTimespan() schema.SchemaValidateFunc {
return func(i interface{}, k string) (warnings []string, errors []error) {
value := i.(string)
if !regexp.MustCompile(`^\-?((\d+)\.)?(\d\d):(60|([0-5][0-9])):(60|([0-5][0-9]))`).MatchString(value) {
errors = append(errors, fmt.Errorf("invalid timespan, must be of format hh:mm:ss %q: %q", k, value))
}

return warnings, errors
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ package datafactory_test

import (
"fmt"
"net/http"
"testing"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func TestAccAzureRMDataFactoryTriggerSchedule_basic(t *testing.T) {
Expand All @@ -19,12 +15,12 @@ func TestAccAzureRMDataFactoryTriggerSchedule_basic(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMDataFactoryTriggerScheduleDestroy,
CheckDestroy: testCheckAzureRMDataFactoryTriggerDestroy("azurerm_data_factory_trigger_schedule"),
Steps: []resource.TestStep{
{
Config: testAccAzureRMDataFactoryTriggerSchedule_basic(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDataFactoryTriggerScheduleExists(data.ResourceName),
testCheckAzureRMDataFactoryTriggerExists(data.ResourceName),
),
},
data.ImportStep(),
Expand All @@ -40,84 +36,26 @@ func TestAccAzureRMDataFactoryTriggerSchedule_complete(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMDataFactoryTriggerScheduleDestroy,
CheckDestroy: testCheckAzureRMDataFactoryTriggerDestroy("azurerm_data_factory_trigger_schedule"),
Steps: []resource.TestStep{
{
Config: testAccAzureRMDataFactoryTriggerSchedule_basic(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDataFactoryTriggerScheduleExists(data.ResourceName),
testCheckAzureRMDataFactoryTriggerExists(data.ResourceName),
),
},
data.ImportStep(),
{
Config: testAccAzureRMDataFactoryTriggerSchedule_update(data, endTime),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDataFactoryTriggerScheduleExists(data.ResourceName),
testCheckAzureRMDataFactoryTriggerExists(data.ResourceName),
),
},
data.ImportStep(),
},
})
}

func testCheckAzureRMDataFactoryTriggerScheduleExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := acceptance.AzureProvider.Meta().(*clients.Client).DataFactory.TriggersClient
ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext

// Ensure we have enough information in state to look up in API
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}

name := rs.Primary.Attributes["name"]
resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
dataFactoryName := rs.Primary.Attributes["data_factory_name"]
if !hasResourceGroup {
return fmt.Errorf("Bad: no resource group found in state for Data Factory: %s", name)
}

resp, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "")
if err != nil {
return fmt.Errorf("Bad: Get on dataFactory.TriggersClient: %+v", err)
}

if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Bad: Data Factory Trigger Schdule %q (data factory name: %q / resource group: %q) does not exist", name, dataFactoryName, resourceGroup)
}

return nil
}
}

func testCheckAzureRMDataFactoryTriggerScheduleDestroy(s *terraform.State) error {
client := acceptance.AzureProvider.Meta().(*clients.Client).DataFactory.TriggersClient
ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext

for _, rs := range s.RootModule().Resources {
if rs.Type != "azurerm_data_factory_trigger_schedule" {
continue
}

name := rs.Primary.Attributes["name"]
resourceGroup := rs.Primary.Attributes["resource_group_name"]
dataFactoryName := rs.Primary.Attributes["data_factory_name"]

resp, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "")

if err != nil {
return nil
}

if resp.StatusCode != http.StatusNotFound {
return fmt.Errorf("Data Factory Trigger Schedule still exists:\n%#v", resp.Properties)
}
}

return nil
}

func testAccAzureRMDataFactoryTriggerSchedule_basic(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down
125 changes: 125 additions & 0 deletions azurerm/internal/services/datafactory/data_factory_trigger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package datafactory_test

import (
"fmt"
"net/http"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func findTriggerByName(name string, s *terraform.State) (*string, *string, *string, error) {
// Ensure we have enough information in state to look up in API
rs, ok := s.RootModule().Resources[name]
if !ok {
return nil, nil, nil, fmt.Errorf("Not found: %s", name)
}

triggerName := rs.Primary.Attributes["name"]
resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
dataFactoryName := rs.Primary.Attributes["data_factory_name"]
if !hasResourceGroup {
return nil, nil, nil, fmt.Errorf("Bad: no resource group found in state for Data Factory: %s", name)
}

return &triggerName, &dataFactoryName, &resourceGroup, nil
}

func testCheckAzureRMDataFactoryTriggerStarts(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := acceptance.AzureProvider.Meta().(*clients.Client).DataFactory.TriggersClient
ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext

name, dataFactoryName, resourceGroup, err := findTriggerByName(name, s)
if err != nil {
return err
}

future, err := client.Start(ctx, *resourceGroup, *dataFactoryName, *name)
if err != nil {
return err
}
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return err
}

return nil
}
}

func testCheckAzureRMDataFactoryTriggerStops(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := acceptance.AzureProvider.Meta().(*clients.Client).DataFactory.TriggersClient
ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext

name, dataFactoryName, resourceGroup, err := findTriggerByName(name, s)
if err != nil {
return err
}

future, err := client.Stop(ctx, *resourceGroup, *dataFactoryName, *name)
if err != nil {
return err
}
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return err
}

return nil
}
}

func testCheckAzureRMDataFactoryTriggerExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := acceptance.AzureProvider.Meta().(*clients.Client).DataFactory.TriggersClient
ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext

name, dataFactoryName, resourceGroup, err := findTriggerByName(name, s)
if err != nil {
return err
}

resp, err := client.Get(ctx, *resourceGroup, *dataFactoryName, *name, "")
if err != nil {
return fmt.Errorf("Bad: Get on dataFactory.TriggersClient: %+v", err)
}

if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Bad: Data Factory Trigger %q (data factory name: %q / resource group: %q) does not exist", *name, *dataFactoryName, *resourceGroup)
}

return nil
}
}

func testCheckAzureRMDataFactoryTriggerDestroy(resource_type string) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := acceptance.AzureProvider.Meta().(*clients.Client).DataFactory.TriggersClient
ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext

for _, rs := range s.RootModule().Resources {
if rs.Type != resource_type {
continue
}

name := rs.Primary.Attributes["name"]
resourceGroup := rs.Primary.Attributes["resource_group_name"]
dataFactoryName := rs.Primary.Attributes["data_factory_name"]

resp, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "")

if err != nil {
return nil
}

if resp.StatusCode != http.StatusNotFound {
return fmt.Errorf("Data Factory Trigger still exists:\n%#v", resp.Properties)
}
}

return nil
}
}
Loading