-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
mssql: add resources azurerm_mssql_firewall_rule and azurerm_mssql_virtual_network_rule #10954
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ac0f149
mssql: add resources azurerm_mssql_firewall_rule and azurerm_mssql_vi…
manicminer 602510f
add import id validation
jackofallops 4bb0148
update firewall rule acctest name
jackofallops f1f0aec
update struct names in line with package
jackofallops File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
azurerm/internal/services/mssql/mssql_firewall_rule_resource.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
package mssql | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/v3.0/sql" | ||
"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/tf" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/mssql/validate" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/sql/parse" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func resourceMsSqlFirewallRule() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceMsSqlFirewallRuleCreateUpdate, | ||
Read: resourceMsSqlFirewallRuleRead, | ||
Update: resourceMsSqlFirewallRuleCreateUpdate, | ||
Delete: resourceMsSqlFirewallRuleDelete, | ||
|
||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
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{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"server_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validate.ServerID, | ||
}, | ||
|
||
"start_ip_address": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.All( | ||
validation.IsIPAddress, | ||
validation.StringIsNotEmpty, | ||
), | ||
}, | ||
|
||
"end_ip_address": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.All( | ||
validation.IsIPAddress, | ||
validation.StringIsNotEmpty, | ||
), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceMsSqlFirewallRuleCreateUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).MSSQL.FirewallRulesClient | ||
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
serverId, err := parse.ServerID(d.Get("server_id").(string)) | ||
if err != nil { | ||
return fmt.Errorf("parsing server ID %q: %+v", d.Get("server_id"), err) | ||
} | ||
|
||
id := parse.NewFirewallRuleID(serverId.SubscriptionId, serverId.ResourceGroup, serverId.Name, d.Get("name").(string)) | ||
|
||
if d.IsNewResource() { | ||
existing, err := client.Get(ctx, id.ResourceGroup, id.ServerName, id.Name) | ||
if err != nil { | ||
if !utils.ResponseWasNotFound(existing.Response) { | ||
return fmt.Errorf("checking for presence of existing MSSQL %s: %+v", id.String(), err) | ||
} | ||
} | ||
|
||
if existing.ID != nil && *existing.ID != "" { | ||
return tf.ImportAsExistsError("azurerm_mssql_firewall_rule", id.ID()) | ||
} | ||
} | ||
|
||
parameters := sql.FirewallRule{ | ||
FirewallRuleProperties: &sql.FirewallRuleProperties{ | ||
StartIPAddress: utils.String(d.Get("start_ip_address").(string)), | ||
EndIPAddress: utils.String(d.Get("end_ip_address").(string)), | ||
}, | ||
} | ||
|
||
if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.ServerName, id.Name, parameters); err != nil { | ||
return fmt.Errorf("creating MSSQL %s: %+v", id.String(), err) | ||
} | ||
|
||
d.SetId(id.ID()) | ||
|
||
return resourceMsSqlFirewallRuleRead(d, meta) | ||
} | ||
|
||
func resourceMsSqlFirewallRuleRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).MSSQL.FirewallRulesClient | ||
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
id, err := parse.FirewallRuleID(d.Id()) | ||
if err != nil { | ||
return fmt.Errorf("parsing ID %q: %+v", d.Id(), err) | ||
} | ||
|
||
resp, err := client.Get(ctx, id.ResourceGroup, id.ServerName, id.Name) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
log.Printf("[INFO] MSSQL %s was not found - removing from state", id.String()) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("retrieving MSSQL %s: %+v", id.String(), err) | ||
} | ||
|
||
d.Set("name", id.Name) | ||
|
||
serverId := parse.NewServerID(id.SubscriptionId, id.ResourceGroup, id.ServerName) | ||
d.Set("server_id", serverId.ID()) | ||
|
||
d.Set("start_ip_address", resp.StartIPAddress) | ||
d.Set("end_ip_address", resp.EndIPAddress) | ||
|
||
return nil | ||
} | ||
|
||
func resourceMsSqlFirewallRuleDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).MSSQL.FirewallRulesClient | ||
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
id, err := parse.FirewallRuleID(d.Id()) | ||
if err != nil { | ||
return fmt.Errorf("parsing ID %q: %+v", d.Id(), err) | ||
} | ||
|
||
resp, err := client.Delete(ctx, id.ResourceGroup, id.ServerName, id.Name) | ||
if err != nil { | ||
if !utils.ResponseWasNotFound(resp) { | ||
return fmt.Errorf("deleting MSSQL %s: %+v", id.String(), err) | ||
} | ||
} | ||
|
||
return nil | ||
} |
148 changes: 148 additions & 0 deletions
148
azurerm/internal/services/mssql/mssql_firewall_rule_resource_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package mssql_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"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/acceptance/check" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/mssql/parse" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
type SqlFirewallRuleResource struct{} | ||
|
||
func TestAccSqlFirewallRule_basic(t *testing.T) { | ||
data := acceptance.BuildTestData(t, "azurerm_mssql_firewall_rule", "test") | ||
r := SqlFirewallRuleResource{} | ||
|
||
data.ResourceTest(t, r, []resource.TestStep{ | ||
{ | ||
Config: r.basic(data), | ||
Check: resource.ComposeTestCheckFunc( | ||
check.That(data.ResourceName).ExistsInAzure(r), | ||
), | ||
}, | ||
data.ImportStep(), | ||
}) | ||
} | ||
|
||
func TestAccSqlFirewallRule_update(t *testing.T) { | ||
data := acceptance.BuildTestData(t, "azurerm_mssql_firewall_rule", "test") | ||
r := SqlFirewallRuleResource{} | ||
|
||
data.ResourceTest(t, r, []resource.TestStep{ | ||
{ | ||
Config: r.basic(data), | ||
Check: resource.ComposeTestCheckFunc( | ||
check.That(data.ResourceName).ExistsInAzure(r), | ||
), | ||
}, | ||
data.ImportStep(), | ||
{ | ||
Config: r.updated(data), | ||
Check: resource.ComposeTestCheckFunc( | ||
check.That(data.ResourceName).ExistsInAzure(r), | ||
), | ||
}, | ||
data.ImportStep(), | ||
}) | ||
} | ||
|
||
func TestAccSqlFirewallRule_requiresImport(t *testing.T) { | ||
data := acceptance.BuildTestData(t, "azurerm_mssql_firewall_rule", "test") | ||
r := SqlFirewallRuleResource{} | ||
|
||
data.ResourceTest(t, r, []resource.TestStep{ | ||
{ | ||
Config: r.basic(data), | ||
Check: resource.ComposeTestCheckFunc( | ||
check.That(data.ResourceName).ExistsInAzure(r), | ||
), | ||
}, | ||
data.RequiresImportErrorStep(r.requiresImport), | ||
}) | ||
} | ||
|
||
func (r SqlFirewallRuleResource) Exists(ctx context.Context, client *clients.Client, state *terraform.InstanceState) (*bool, error) { | ||
id, err := parse.FirewallRuleID(state.ID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp, err := client.MSSQL.FirewallRulesClient.Get(ctx, id.ResourceGroup, id.ServerName, id.Name) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
return utils.Bool(false), nil | ||
} | ||
return nil, fmt.Errorf("retrieving MSSQL %s: %+v", id.String(), err) | ||
} | ||
|
||
return utils.Bool(true), nil | ||
} | ||
|
||
func (SqlFirewallRuleResource) template(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
provider "azurerm" { | ||
features {} | ||
} | ||
|
||
resource "azurerm_resource_group" "test" { | ||
name = "acctestRG-%[1]d" | ||
location = "%[2]s" | ||
} | ||
|
||
resource "azurerm_mssql_server" "test" { | ||
name = "acctestsqlserver%[1]d" | ||
resource_group_name = azurerm_resource_group.test.name | ||
location = azurerm_resource_group.test.location | ||
version = "12.0" | ||
administrator_login = "msincredible" | ||
administrator_login_password = "P@55W0rD!!%[3]s" | ||
} | ||
`, data.RandomInteger, data.Locations.Primary, data.RandomString) | ||
} | ||
|
||
func (r SqlFirewallRuleResource) basic(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
%[1]s | ||
|
||
resource "azurerm_mssql_firewall_rule" "test" { | ||
name = "acctestsqlserver%[2]d" | ||
server_id = azurerm_mssql_server.test.id | ||
start_ip_address = "0.0.0.0" | ||
end_ip_address = "255.255.255.255" | ||
} | ||
`, r.template(data), data.RandomInteger) | ||
} | ||
|
||
func (r SqlFirewallRuleResource) updated(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
%[1]s | ||
|
||
resource "azurerm_mssql_firewall_rule" "test" { | ||
name = "acctestsqlserver%[2]d" | ||
server_id = azurerm_mssql_server.test.id | ||
start_ip_address = "10.0.17.62" | ||
end_ip_address = "10.0.17.64" | ||
} | ||
`, r.template(data), data.RandomInteger) | ||
} | ||
|
||
func (r SqlFirewallRuleResource) requiresImport(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
%[1]s | ||
|
||
resource "azurerm_mssql_firewall_rule" "import" { | ||
name = azurerm_mssql_firewall_rule.test.name | ||
server_id = azurerm_mssql_firewall_rule.test.server_id | ||
start_ip_address = azurerm_mssql_firewall_rule.test.start_ip_address | ||
end_ip_address = azurerm_mssql_firewall_rule.test.end_ip_address | ||
} | ||
`, r.basic(data)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should use
ValidateResourceIDPriorToImport
here.