From 69e5bb81c03041c840cbee54af148398b8966555 Mon Sep 17 00:00:00 2001 From: magodo Date: Thu, 5 Mar 2020 13:47:18 +0800 Subject: [PATCH 1/4] New Resource & Data Source: azurerm_database_migration_project --- .../databasemigration/client/client.go | 6 + .../data_source_database_migration_project.go | 88 ++++++++ .../data_source_database_migration_service.go | 2 +- .../databasemigration/registration.go | 2 + ...resource_arm_database_migration_project.go | 190 ++++++++++++++++ ...resource_arm_database_migration_service.go | 2 +- ..._source_database_migration_project_test.go | 40 ++++ ...rce_arm_database_migration_project_test.go | 206 ++++++++++++++++++ .../services/databasemigration/validation.go | 14 +- website/azurerm.erb | 8 + .../database_migration_project.html.markdown | 61 ++++++ .../database_migration_project.html.markdown | 94 ++++++++ 12 files changed, 710 insertions(+), 3 deletions(-) create mode 100644 azurerm/internal/services/databasemigration/data_source_database_migration_project.go create mode 100644 azurerm/internal/services/databasemigration/resource_arm_database_migration_project.go create mode 100644 azurerm/internal/services/databasemigration/tests/data_source_database_migration_project_test.go create mode 100644 azurerm/internal/services/databasemigration/tests/resource_arm_database_migration_project_test.go create mode 100644 website/docs/d/database_migration_project.html.markdown create mode 100644 website/docs/r/database_migration_project.html.markdown diff --git a/azurerm/internal/services/databasemigration/client/client.go b/azurerm/internal/services/databasemigration/client/client.go index e15f08de5b57..90dba0b7f494 100644 --- a/azurerm/internal/services/databasemigration/client/client.go +++ b/azurerm/internal/services/databasemigration/client/client.go @@ -7,12 +7,18 @@ import ( type Client struct { ServicesClient *datamigration.ServicesClient + ProjectsClient *datamigration.ProjectsClient } func NewClient(o *common.ClientOptions) *Client { servicesClient := datamigration.NewServicesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) o.ConfigureClient(&servicesClient.Client, o.ResourceManagerAuthorizer) + + projectsClient := datamigration.NewProjectsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) + o.ConfigureClient(&projectsClient.Client, o.ResourceManagerAuthorizer) + return &Client{ ServicesClient: &servicesClient, + ProjectsClient: &projectsClient, } } diff --git a/azurerm/internal/services/databasemigration/data_source_database_migration_project.go b/azurerm/internal/services/databasemigration/data_source_database_migration_project.go new file mode 100644 index 000000000000..d40318fa9488 --- /dev/null +++ b/azurerm/internal/services/databasemigration/data_source_database_migration_project.go @@ -0,0 +1,88 @@ +package databasemigration + +import ( + "fmt" + + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func dataSourceArmDatabaseMigrationProject() *schema.Resource { + return &schema.Resource{ + Read: dataSourceArmDatabaseMigrationProjectRead, + + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validateDatabaseMigrationProjectName, + }, + + "service_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validateDatabaseMigrationServiceName, + }, + + "resource_group_name": azure.SchemaResourceGroupNameForDataSource(), + + "location": azure.SchemaLocationForDataSource(), + + "source_platform": { + Type: schema.TypeString, + Computed: true, + }, + + "target_platform": { + Type: schema.TypeString, + Computed: true, + }, + + "tags": tags.SchemaDataSource(), + }, + } +} + +func dataSourceArmDatabaseMigrationProjectRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).DatabaseMigration.ProjectsClient + ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) + defer cancel() + + name := d.Get("name").(string) + serviceName := d.Get("service_name").(string) + resourceGroup := d.Get("resource_group_name").(string) + + resp, err := client.Get(ctx, resourceGroup, serviceName, name) + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + return fmt.Errorf("Error: Database Migration Project (Project Name %q / Service Name %q / Group Name %q) was not found", name, serviceName, resourceGroup) + } + return fmt.Errorf("Error reading Database Migration Project (Project Name %q / Service Name %q / Group Name %q): %+v", name, serviceName, resourceGroup, err) + } + + if resp.ID == nil || *resp.ID == "" { + return fmt.Errorf("Cannot read Database Migration Project (Project Name %q / Service Name %q / Group Name %q) ID", name, serviceName, resourceGroup) + } + d.SetId(*resp.ID) + + d.Set("resource_group_name", resourceGroup) + + location := "" + if resp.Location != nil { + location = azure.NormalizeLocation(*resp.Location) + } + d.Set("location", location) + + if prop := resp.ProjectProperties; prop != nil { + d.Set("source_platform", string(prop.SourcePlatform)) + d.Set("target_platform", string(prop.TargetPlatform)) + } + + return nil +} diff --git a/azurerm/internal/services/databasemigration/data_source_database_migration_service.go b/azurerm/internal/services/databasemigration/data_source_database_migration_service.go index 076c12d92369..515b9b1f9dc1 100644 --- a/azurerm/internal/services/databasemigration/data_source_database_migration_service.go +++ b/azurerm/internal/services/databasemigration/data_source_database_migration_service.go @@ -25,7 +25,7 @@ func dataSourceArmDatabaseMigrationService() *schema.Resource { "name": { Type: schema.TypeString, Required: true, - ValidateFunc: validateDatabasesMigrationServiceName, + ValidateFunc: validateDatabaseMigrationServiceName, }, "resource_group_name": azure.SchemaResourceGroupNameForDataSource(), diff --git a/azurerm/internal/services/databasemigration/registration.go b/azurerm/internal/services/databasemigration/registration.go index 9a41c8047e65..7a2c32aae2a3 100644 --- a/azurerm/internal/services/databasemigration/registration.go +++ b/azurerm/internal/services/databasemigration/registration.go @@ -22,6 +22,7 @@ func (r Registration) WebsiteCategories() []string { func (r Registration) SupportedDataSources() map[string]*schema.Resource { return map[string]*schema.Resource{ "azurerm_database_migration_service": dataSourceArmDatabaseMigrationService(), + "azurerm_database_migration_project": dataSourceArmDatabaseMigrationProject(), } } @@ -29,6 +30,7 @@ func (r Registration) SupportedDataSources() map[string]*schema.Resource { func (r Registration) SupportedResources() map[string]*schema.Resource { resources := map[string]*schema.Resource{ "azurerm_database_migration_service": resourceArmDatabaseMigrationService(), + "azurerm_database_migration_project": resourceArmDatabaseMigrationProject(), } return resources diff --git a/azurerm/internal/services/databasemigration/resource_arm_database_migration_project.go b/azurerm/internal/services/databasemigration/resource_arm_database_migration_project.go new file mode 100644 index 000000000000..aed26913e979 --- /dev/null +++ b/azurerm/internal/services/databasemigration/resource_arm_database_migration_project.go @@ -0,0 +1,190 @@ +package databasemigration + +import ( + "fmt" + "log" + + "github.com/Azure/azure-sdk-for-go/services/datamigration/mgmt/2018-04-19/datamigration" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/helper/validation" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func resourceArmDatabaseMigrationProject() *schema.Resource { + return &schema.Resource{ + Create: resourceArmDatabaseMigrationProjectCreateUpdate, + Read: resourceArmDatabaseMigrationProjectRead, + Update: resourceArmDatabaseMigrationProjectCreateUpdate, + Delete: resourceArmDatabaseMigrationProjectDelete, + + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validateDatabaseMigrationProjectName, + }, + + "service_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validateDatabaseMigrationServiceName, + }, + + "resource_group_name": azure.SchemaResourceGroupName(), + + "location": azure.SchemaLocation(), + + "source_platform": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice([]string{ + // Now that go sdk only export SQL as source platform type, we only allow it here. + string(datamigration.ProjectSourcePlatformSQL), + }, false), + }, + + "target_platform": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice([]string{ + // Now that go sdk only export SQL as source platform type, we only allow it here. + string(datamigration.ProjectTargetPlatformSQLDB), + }, false), + }, + + "tags": tags.Schema(), + }, + } +} + +func resourceArmDatabaseMigrationProjectCreateUpdate(d *schema.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).DatabaseMigration.ProjectsClient + ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d) + defer cancel() + + name := d.Get("name").(string) + resourceGroup := d.Get("resource_group_name").(string) + serviceName := d.Get("service_name").(string) + + if features.ShouldResourcesBeImported() && d.IsNewResource() { + existing, err := client.Get(ctx, resourceGroup, serviceName, name) + if err != nil { + if !utils.ResponseWasNotFound(existing.Response) { + return fmt.Errorf("Error checking for present of existing Database Migration Project (Project Name: %q / Service Name %q / Group Name %q): %+v", name, serviceName, resourceGroup, err) + } + } + if existing.ID != nil && *existing.ID != "" { + return tf.ImportAsExistsError("azurerm_database_migration_project", *existing.ID) + } + } + + location := azure.NormalizeLocation(d.Get("location").(string)) + sourcePlatform := d.Get("source_platform").(string) + targetPlatform := d.Get("target_platform").(string) + t := d.Get("tags").(map[string]interface{}) + + parameters := datamigration.Project{ + Location: utils.String(location), + ProjectProperties: &datamigration.ProjectProperties{ + SourcePlatform: datamigration.ProjectSourcePlatform(sourcePlatform), + TargetPlatform: datamigration.ProjectTargetPlatform(targetPlatform), + }, + Tags: tags.Expand(t), + } + + if _, err := client.CreateOrUpdate(ctx, parameters, resourceGroup, serviceName, name); err != nil { + return fmt.Errorf("Error creating Database Migration Project (Project Name %q / Service Name %q / Group Name %q): %+v", name, serviceName, resourceGroup, err) + } + + resp, err := client.Get(ctx, resourceGroup, serviceName, name) + if err != nil { + return fmt.Errorf("Error retrieving Database Migration Project (Project Name %q / Service Name %q / Group Name %q): %+v", name, serviceName, resourceGroup, err) + } + if resp.ID == nil || *resp.ID == "" { + return fmt.Errorf("Cannot read Database Migration Project (Project Name %q / Service Name %q / Group Name %q) ID", name, serviceName, resourceGroup) + } + d.SetId(*resp.ID) + + return resourceArmDatabaseMigrationProjectRead(d, meta) +} + +func resourceArmDatabaseMigrationProjectRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).DatabaseMigration.ProjectsClient + ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) + defer cancel() + + id, err := azure.ParseAzureResourceID(d.Id()) + if err != nil { + return err + } + name := id.Path["projects"] + resourceGroup := id.ResourceGroup + serviceName := id.Path["services"] + + resp, err := client.Get(ctx, resourceGroup, serviceName, name) + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + log.Printf("[INFO] Database Migration Project %q does not exist - removing from state", d.Id()) + d.SetId("") + return nil + } + return fmt.Errorf("Error reading Database Migration Project (Project Name %q / Service Name %q / Group Name %q): %+v", name, serviceName, resourceGroup, err) + } + if resp.ID == nil || *resp.ID == "" { + return fmt.Errorf("Cannot read Database Migration Project (Project Name %q / Service Name %q / Group Name %q) ID", name, serviceName, resourceGroup) + } + + d.SetId(*resp.ID) + + d.Set("name", resp.Name) + d.Set("service_name", serviceName) + d.Set("resource_group_name", resourceGroup) + + location := "" + if resp.Location != nil { + location = azure.NormalizeLocation(*resp.Location) + } + d.Set("location", location) + + if prop := resp.ProjectProperties; prop != nil { + d.Set("source_platform", string(prop.SourcePlatform)) + d.Set("target_platform", string(prop.TargetPlatform)) + } + + return tags.FlattenAndSet(d, resp.Tags) +} + +func resourceArmDatabaseMigrationProjectDelete(d *schema.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).DatabaseMigration.ProjectsClient + ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) + defer cancel() + + id, err := azure.ParseAzureResourceID(d.Id()) + if err != nil { + return err + } + resourceGroup := id.ResourceGroup + serviceName := id.Path["services"] + name := id.Path["projects"] + + deleteRunningTasks := true + if _, err := client.Delete(ctx, resourceGroup, serviceName, name, &deleteRunningTasks); err != nil { + return fmt.Errorf("Error deleting Database Migration Project (Project Name %q / Service Name %q / Group Name %q): %+v", name, serviceName, resourceGroup, err) + } + + return nil +} diff --git a/azurerm/internal/services/databasemigration/resource_arm_database_migration_service.go b/azurerm/internal/services/databasemigration/resource_arm_database_migration_service.go index 2f5457d74e02..4fb27f013f6f 100644 --- a/azurerm/internal/services/databasemigration/resource_arm_database_migration_service.go +++ b/azurerm/internal/services/databasemigration/resource_arm_database_migration_service.go @@ -45,7 +45,7 @@ func resourceArmDatabaseMigrationService() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: true, - ValidateFunc: validateDatabasesMigrationServiceName, + ValidateFunc: validateDatabaseMigrationServiceName, }, "location": azure.SchemaLocation(), diff --git a/azurerm/internal/services/databasemigration/tests/data_source_database_migration_project_test.go b/azurerm/internal/services/databasemigration/tests/data_source_database_migration_project_test.go new file mode 100644 index 000000000000..40976953aa8d --- /dev/null +++ b/azurerm/internal/services/databasemigration/tests/data_source_database_migration_project_test.go @@ -0,0 +1,40 @@ +package azurerm + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance" +) + +func TestAccDataSourceAzureRMDatabaseMigrationProject_basic(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_database_migration_project", "test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.PreCheck(t) }, + Providers: acceptance.SupportedProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceDatabaseMigrationProject_basic(data), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(data.ResourceName, "source_platform", "SQL"), + resource.TestCheckResourceAttr(data.ResourceName, "target_platform", "SQLDB"), + ), + }, + }, + }) +} + +func testAccDataSourceDatabaseMigrationProject_basic(data acceptance.TestData) string { + config := testAccAzureRMDatabaseMigrationProject_basic(data) + return fmt.Sprintf(` +%s + +data "azurerm_database_migration_project" "test" { + name = azurerm_database_migration_project.test.name + service_name = azurerm_database_migration_project.test.service_name + resource_group_name = azurerm_database_migration_project.test.resource_group_name +} +`, config) +} diff --git a/azurerm/internal/services/databasemigration/tests/resource_arm_database_migration_project_test.go b/azurerm/internal/services/databasemigration/tests/resource_arm_database_migration_project_test.go new file mode 100644 index 000000000000..db47cd56adef --- /dev/null +++ b/azurerm/internal/services/databasemigration/tests/resource_arm_database_migration_project_test.go @@ -0,0 +1,206 @@ +package azurerm + +import ( + "fmt" + "testing" + + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features" + + "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 TestAccAzureRMDatabaseMigrationProject_basic(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_database_migration_project", "test") + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.PreCheck(t) }, + Providers: acceptance.SupportedProviders, + CheckDestroy: testCheckAzureRMDatabaseMigrationProjectDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMDatabaseMigrationProject_basic(data), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMDatabaseMigrationProjectExists(data.ResourceName), + resource.TestCheckResourceAttr(data.ResourceName, "source_platform", "SQL"), + resource.TestCheckResourceAttr(data.ResourceName, "target_platform", "SQLDB"), + ), + }, + data.ImportStep(), + }, + }) +} + +func TestAccAzureRMDatabaseMigrationProject_complete(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_database_migration_project", "test") + resource.Test(t, resource.TestCase{ + PreCheck: func() { acceptance.PreCheck(t) }, + Providers: acceptance.SupportedProviders, + CheckDestroy: testCheckAzureRMDatabaseMigrationProjectDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMDatabaseMigrationProject_complete(data), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMDatabaseMigrationProjectExists(data.ResourceName), + resource.TestCheckResourceAttr(data.ResourceName, "source_platform", "SQL"), + resource.TestCheckResourceAttr(data.ResourceName, "target_platform", "SQLDB"), + resource.TestCheckResourceAttr(data.ResourceName, "tags.name", "test"), + ), + }, + data.ImportStep(), + }, + }) +} + +func TestAccAzureRMDatabaseMigrationProject_requiresImport(t *testing.T) { + if !features.ShouldResourcesBeImported() { + t.Skip("Skipping since resources aren't required to be imported") + return + } + + data := acceptance.BuildTestData(t, "azurerm_database_migration_project", "test") + + resource.Test(t, resource.TestCase{ + PreCheck: func() { acceptance.PreCheck(t) }, + Providers: acceptance.SupportedProviders, + CheckDestroy: testCheckAzureRMDatabaseMigrationProjectDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMDatabaseMigrationProject_basic(data), + }, + data.RequiresImportErrorStep(testAccAzureRMDatabaseMigrationProject_requiresImport), + }, + }) +} + +func TestAccAzureRMDatabaseMigrationProject_update(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_database_migration_project", "test") + resource.Test(t, resource.TestCase{ + PreCheck: func() { acceptance.PreCheck(t) }, + Providers: acceptance.SupportedProviders, + CheckDestroy: testCheckAzureRMDatabaseMigrationProjectDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMDatabaseMigrationProject_basic(data), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMDatabaseMigrationProjectExists(data.ResourceName), + ), + }, + data.ImportStep(), + { + Config: testAccAzureRMDatabaseMigrationProject_complete(data), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMDatabaseMigrationProjectExists(data.ResourceName), + resource.TestCheckResourceAttr(data.ResourceName, "tags.name", "test"), + ), + }, + data.ImportStep(), + }, + }) +} + +func testCheckAzureRMDatabaseMigrationProjectExists(resourceName string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[resourceName] + if !ok { + return fmt.Errorf("Database Migration Project not found: %s", resourceName) + } + + name := rs.Primary.Attributes["name"] + resourceGroup := rs.Primary.Attributes["resource_group_name"] + serviceName := rs.Primary.Attributes["service_name"] + + client := acceptance.AzureProvider.Meta().(*clients.Client).DatabaseMigration.ProjectsClient + ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext + + if resp, err := client.Get(ctx, resourceGroup, serviceName, name); err != nil { + if utils.ResponseWasNotFound(resp.Response) { + return fmt.Errorf("Bad: Database Migration Project (Project Name %q / Service Name %q / Group Name %q) does not exist", name, serviceName, resourceGroup) + } + return fmt.Errorf("Bad: Get on ProjectsClient: %+v", err) + } + + return nil + } +} + +func testCheckAzureRMDatabaseMigrationProjectDestroy(s *terraform.State) error { + client := acceptance.AzureProvider.Meta().(*clients.Client).DatabaseMigration.ProjectsClient + ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext + + for _, rs := range s.RootModule().Resources { + if rs.Type != "azurerm_database_migration_project" { + continue + } + + name := rs.Primary.Attributes["name"] + resourceGroup := rs.Primary.Attributes["resource_group_name"] + serviceName := rs.Primary.Attributes["service_name"] + + if resp, err := client.Get(ctx, resourceGroup, serviceName, name); err != nil { + if !utils.ResponseWasNotFound(resp.Response) { + return fmt.Errorf("Bad: Get on ProjectsClient: %+v", err) + } + } + + return nil + } + + return nil +} + +func testAccAzureRMDatabaseMigrationProject_basic(data acceptance.TestData) string { + template := testAccAzureRMDatabaseMigrationService_basic(data) + + return fmt.Sprintf(` +%s + +resource "azurerm_database_migration_project" "test" { + name = "acctestDbmsProject-%d" + service_name = azurerm_database_migration_service.test.name + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + source_platform = "SQL" + target_platform = "SQLDB" +} +`, template, data.RandomInteger) +} + +func testAccAzureRMDatabaseMigrationProject_complete(data acceptance.TestData) string { + template := testAccAzureRMDatabaseMigrationService_basic(data) + + return fmt.Sprintf(` +%s + +resource "azurerm_database_migration_project" "test" { + name = "acctestDbmsProject-%d" + service_name = azurerm_database_migration_service.test.name + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + source_platform = "SQL" + target_platform = "SQLDB" + tags = { + name = "test" + } +} +`, template, data.RandomInteger) +} + +func testAccAzureRMDatabaseMigrationProject_requiresImport(data acceptance.TestData) string { + template := testAccAzureRMDatabaseMigrationProject_basic(data) + return fmt.Sprintf(` +%s + +resource "azurerm_database_migration_project" "import" { + name = azurerm_database_migration_project.test.name + service_name = azurerm_database_migration_project.test.service_name + resource_group_name = azurerm_database_migration_project.test.resource_group_name + location = azurerm_database_migration_project.test.location + source_platform = azurerm_database_migration_project.test.source_platform + target_platform = azurerm_database_migration_project.test.target_platform +} +`, template) +} diff --git a/azurerm/internal/services/databasemigration/validation.go b/azurerm/internal/services/databasemigration/validation.go index 0e325b3e67ab..025529197cc5 100644 --- a/azurerm/internal/services/databasemigration/validation.go +++ b/azurerm/internal/services/databasemigration/validation.go @@ -5,7 +5,19 @@ import ( "regexp" ) -func validateDatabasesMigrationServiceName(i interface{}, k string) ([]string, []error) { +func validateDatabaseMigrationServiceName(i interface{}, k string) ([]string, []error) { + v, ok := i.(string) + if !ok { + return nil, []error{fmt.Errorf("expected type of %q to be string", k)} + } + validName := regexp.MustCompile(`^[\d\w]+[\d\w\-_.]*$`) + if !validName.MatchString(v) { + return nil, []error{fmt.Errorf("invalid format of %q", k)} + } + return nil, nil +} + +func validateDatabaseMigrationProjectName(i interface{}, k string) ([]string, []error) { v, ok := i.(string) if !ok { return nil, []error{fmt.Errorf("expected type of %q to be string", k)} diff --git a/website/azurerm.erb b/website/azurerm.erb index 124f703c1f8d..b70ddc5137a4 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -162,6 +162,10 @@ azurerm_dns_zone +
  • + azurerm_database_migration_project +
  • +
  • azurerm_database_migration_service
  • @@ -1189,6 +1193,10 @@
  • Database Migration Resources