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

[WIP] New Resource: azurerm_sql_managed_instance #5399

Closed
wants to merge 20 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
1 change: 1 addition & 0 deletions azurerm/internal/services/network/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func (r Registration) SupportedResources() map[string]*schema.Resource {
"azurerm_subnet_route_table_association": resourceArmSubnetRouteTableAssociation(),
"azurerm_subnet_nat_gateway_association": resourceArmSubnetNatGatewayAssociation(),
"azurerm_subnet": resourceArmSubnet(),
"azurerm_subnet_network_intent_policy": resourceArmSubnetNetworkIntentPolicy(),
"azurerm_virtual_hub": resourceArmVirtualHub(),
"azurerm_virtual_network_gateway_connection": resourceArmVirtualNetworkGatewayConnection(),
"azurerm_virtual_network_gateway": resourceArmVirtualNetworkGateway(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package network

import (
"fmt"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-09-01/network"
"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/locks"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmSubnetNetworkIntentPolicy() *schema.Resource {
return &schema.Resource{
Create: resourceArmSubnetNetworkIntentPolicyCreate,
Read: resourceArmSubnetNetworkIntentPolicyRead,
Delete: resourceArmSubnetNetworkIntentPolicyDelete,

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

Schema: map[string]*schema.Schema{
"subnet_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceID,
},

"service_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.NoEmptyStrings,
},
},
}
}

func resourceArmSubnetNetworkIntentPolicyCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Network.SubnetsClient
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()

log.Printf("[INFO] preparing arguments for Subnet <-> Network Security Group Association creation.")

subnetId := d.Get("subnet_id").(string)

parsedSubnetId, err := azure.ParseAzureResourceID(subnetId)
if err != nil {
return err
}

subnetName := parsedSubnetId.Path["subnets"]
virtualNetworkName := parsedSubnetId.Path["virtualNetworks"]
resourceGroup := parsedSubnetId.ResourceGroup
serviceName := d.Get("service_name").(string)

locks.ByName(subnetName, SubnetResourceName)
defer locks.UnlockByName(subnetName, SubnetResourceName)

locks.ByName(virtualNetworkName, VirtualNetworkResourceName)
defer locks.UnlockByName(virtualNetworkName, VirtualNetworkResourceName)

req := network.PrepareNetworkPoliciesRequest{
ServiceName: utils.String(serviceName),
}

future, err := client.PrepareNetworkPolicies(ctx, resourceGroup, virtualNetworkName, subnetName, req)
if err != nil {
return fmt.Errorf("Error updating Route Table Association for Subnet %q (Virtual Network %q / Resource Group %q): %+v", subnetName, virtualNetworkName, resourceGroup, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for completion of Route Table Association for Subnet %q (VN %q / Resource Group %q): %+v", subnetName, virtualNetworkName, resourceGroup, err)
}

read, err := client.Get(ctx, resourceGroup, virtualNetworkName, subnetName, "")
if err != nil {
return fmt.Errorf("Error retrieving Subnet %q (Virtual Network %q / Resource Group %q): %+v", subnetName, virtualNetworkName, resourceGroup, err)
}

id := fmt.Sprintf("%s/serviceName/%s", *read.ID, serviceName)

d.SetId(id)

return nil
}

func resourceArmSubnetNetworkIntentPolicyRead(d *schema.ResourceData, meta interface{}) error {
return nil
}

func resourceArmSubnetNetworkIntentPolicyDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Network.SubnetsClient
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
virtualNetworkName := id.Path["virtualNetworks"]
subnetName := id.Path["subnets"]
serviceName := id.Path["serviceName"]

locks.ByName(virtualNetworkName, VirtualNetworkResourceName)
defer locks.UnlockByName(virtualNetworkName, VirtualNetworkResourceName)

locks.ByName(subnetName, SubnetResourceName)
defer locks.UnlockByName(subnetName, SubnetResourceName)

req := network.UnprepareNetworkPoliciesRequest{
ServiceName: utils.String(serviceName),
}

future, err := client.UnprepareNetworkPolicies(ctx, resourceGroup, virtualNetworkName, subnetName, req)
if err != nil {
return fmt.Errorf("Error removing Network Security Group Association from Subnet %q (Virtual Network %q / Resource Group %q): %+v", subnetName, virtualNetworkName, resourceGroup, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for removal of Network Security Group Association from Subnet %q (Virtual Network %q / Resource Group %q): %+v", subnetName, virtualNetworkName, resourceGroup, err)
}

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package tests

import (
"fmt"
"testing"

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

func TestAccAzureRMSubnetNetworkIntentPolicy_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_subnet_network_intent_policy", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMSubnetDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMSubnetNetworkIntentPolicy_basic(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMSubnetExists(data.ResourceName),
),
},
},
})
}

func testAccAzureRMSubnetNetworkIntentPolicy_basic(data acceptance.TestData) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_virtual_network" "test" {
name = "acctestvirtnet%d"
address_space = ["10.0.0.0/16"]
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
}

resource "azurerm_subnet" "test" {
name = "acctestsubnet%d"
resource_group_name = "${azurerm_resource_group.test.name}"
virtual_network_name = "${azurerm_virtual_network.test.name}"
address_prefix = "10.0.2.0/24"
}

resource "azurerm_subnet_network_intent_policy" "test" {
subnet_id = "${azurerm_subnet.test.id}"
service_name = "Microsoft.Sql/managedInstances"
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger)
}
11 changes: 11 additions & 0 deletions azurerm/internal/services/sql/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/2017-03-01-preview/sql"
msi "github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/2018-06-01-preview/sql"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/common"
)

Expand All @@ -11,6 +12,8 @@ type Client struct {
ElasticPoolsClient *sql.ElasticPoolsClient
FirewallRulesClient *sql.FirewallRulesClient
FailoverGroupsClient *sql.FailoverGroupsClient
InstancePoolsClient *msi.InstancePoolsClient
ManagedInstancesClient *msi.ManagedInstancesClient
ServersClient *sql.ServersClient
ServerAzureADAdministratorsClient *sql.ServerAzureADAdministratorsClient
VirtualNetworkRulesClient *sql.VirtualNetworkRulesClient
Expand All @@ -33,6 +36,12 @@ func NewClient(o *common.ClientOptions) *Client {
FirewallRulesClient := sql.NewFirewallRulesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&FirewallRulesClient.Client, o.ResourceManagerAuthorizer)

InstancePoolsClient := msi.NewInstancePoolsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&InstancePoolsClient.Client, o.ResourceManagerAuthorizer)

ManagedInstancesClient := msi.NewManagedInstancesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ManagedInstancesClient.Client, o.ResourceManagerAuthorizer)

ServersClient := sql.NewServersClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ServersClient.Client, o.ResourceManagerAuthorizer)

Expand All @@ -48,6 +57,8 @@ func NewClient(o *common.ClientOptions) *Client {
ElasticPoolsClient: &ElasticPoolsClient,
FailoverGroupsClient: &FailoverGroupsClient,
FirewallRulesClient: &FirewallRulesClient,
InstancePoolsClient: &InstancePoolsClient,
ManagedInstancesClient: &ManagedInstancesClient,
ServersClient: &ServersClient,
ServerAzureADAdministratorsClient: &ServerAzureADAdministratorsClient,
VirtualNetworkRulesClient: &VirtualNetworkRulesClient,
Expand Down
2 changes: 2 additions & 0 deletions azurerm/internal/services/sql/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func (r Registration) SupportedResources() map[string]*schema.Resource {
"azurerm_sql_elasticpool": resourceArmSqlElasticPool(),
"azurerm_sql_failover_group": resourceArmSqlFailoverGroup(),
"azurerm_sql_firewall_rule": resourceArmSqlFirewallRule(),
"azurerm_sql_instance_pool": resourceArmSqlInstancePool(),
"azurerm_sql_managed_instance": resourceArmSqlMiServer(),
"azurerm_sql_server": resourceArmSqlServer(),
"azurerm_sql_virtual_network_rule": resourceArmSqlVirtualNetworkRule(),
}
Expand Down
Loading