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

New Resource & Data Source: azurerm_database_migration_project #5993

Merged
merged 4 commits into from
Mar 5, 2020
Merged
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
6 changes: 6 additions & 0 deletions azurerm/internal/services/databasemigration/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package databasemigration

import (
"fmt"
"time"

"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,

Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(5 * time.Minute),
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateDatabaseMigrationProjectName,
},

"service_name": {
Type: schema.TypeString,
Required: 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
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func dataSourceArmDatabaseMigrationService() *schema.Resource {
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateDatabasesMigrationServiceName,
ValidateFunc: validateDatabaseMigrationServiceName,
},

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package parse

import (
"fmt"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
)

type DatabaseMigrationProjectId struct {
ResourceGroup string
Service string
Name string
}

func DatabaseMigrationProjectID(input string) (*DatabaseMigrationProjectId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("[ERROR] Unable to parse Database Migration Project ID %q: %+v", input, err)
}

project := DatabaseMigrationProjectId{
ResourceGroup: id.ResourceGroup,
}

if project.Service, err = id.PopSegment("services"); err != nil {
return nil, err
}

if project.Name, err = id.PopSegment("projects"); err != nil {
return nil, err
}

if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &project, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package parse

import (
"testing"
)

func TestDatabaseMigrationProjectID(t *testing.T) {
testData := []struct {
Name string
Input string
Error bool
Expect *DatabaseMigrationProjectId
}{
{
Name: "Empty",
Input: "",
Error: true,
},
{
Name: "No Resource Groups Segment",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000",
Error: true,
},
{
Name: "No Resource Groups Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/",
Error: true,
},
{
Name: "No Resource Group ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/",
Error: true,
},
{
Name: "Missing service name",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/services",
Error: true,
},
{
Name: "No Projects Segment",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/services/service1",
Error: true,
},
{
Name: "Missing project name",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/services/service1/projects",
Error: true,
},
{
Name: "Service name",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Datamigration/services/service1/projects/project1",
Expect: &DatabaseMigrationProjectId{
ResourceGroup: "resGroup1",
Service: "service1",
Name: "project1",
},
},
{
Name: "Wrong Casing",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Datamigration/Services/service1/projects/project1",
Error: true,
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Name)

actual, err := DatabaseMigrationProjectID(v.Input)
if err != nil {
if v.Error {
continue
}

t.Fatalf("Expected a value but got an error: %s", err)
}

if actual.ResourceGroup != v.Expect.ResourceGroup {
t.Fatalf("Expected %q but got %q for Resource Group", v.Expect.ResourceGroup, actual.ResourceGroup)
}

if actual.Service != v.Expect.Service {
t.Fatalf("Expected %q but got %q for Service", v.Expect.Service, actual.Service)
}

if actual.Name != v.Expect.Name {
t.Fatalf("Expected %q but got %q for Name", v.Expect.Name, actual.Name)
}
}
}
2 changes: 2 additions & 0 deletions azurerm/internal/services/databasemigration/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ 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(),
}
}

// SupportedResources returns the supported Resources supported by this Service
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
Expand Down
Loading