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

azurerm_snapshot adding network_access_policy and public_network_access #25421

Merged
merged 7 commits into from
Apr 8, 2024
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
35 changes: 35 additions & 0 deletions internal/services/compute/snapshot_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"log"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
Expand Down Expand Up @@ -83,6 +84,20 @@ func resourceSnapshot() *pluginsdk.Resource {
ForceNew: true,
},

"network_access_policy": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(snapshots.PossibleValuesForNetworkAccessPolicy(), false),
Default: string(snapshots.NetworkAccessPolicyAllowAll),
},

"public_network_access": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(snapshots.PossibleValuesForPublicNetworkAccess(), false),
Default: string(snapshots.PublicNetworkAccessEnabled),
},

"source_resource_id": {
Type: pluginsdk.TypeString,
Optional: true,
Expand Down Expand Up @@ -170,6 +185,14 @@ func resourceSnapshotCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) e
properties.Properties.CreationData.StorageAccountId = utils.String(v.(string))
}

if v, ok := d.GetOk("network_access_policy"); ok {
properties.Properties.NetworkAccessPolicy = pointer.To(snapshots.NetworkAccessPolicy(v.(string)))
}

if v, ok := d.GetOk("public_network_access"); ok {
properties.Properties.PublicNetworkAccess = pointer.To(snapshots.PublicNetworkAccess(v.(string)))
}

diskSizeGB := d.Get("disk_size_gb").(int)
if diskSizeGB > 0 {
properties.Properties.DiskSizeGB = utils.Int64(int64(diskSizeGB))
Expand Down Expand Up @@ -228,6 +251,18 @@ func resourceSnapshotRead(d *pluginsdk.ResourceData, meta interface{}) error {
return fmt.Errorf("setting `encryption_settings`: %+v", err)
}

networkAccessPolicy := snapshots.NetworkAccessPolicyAllowAll
if props.NetworkAccessPolicy != nil {
networkAccessPolicy = *props.NetworkAccessPolicy
}
d.Set("network_access_policy", string(networkAccessPolicy))

publicNetworkAccess := snapshots.PublicNetworkAccessEnabled
if props.PublicNetworkAccess != nil {
publicNetworkAccess = *props.PublicNetworkAccess
}
d.Set("public_network_access", string(publicNetworkAccess))

incrementalEnabled := false
if props.Incremental != nil {
incrementalEnabled = *props.Incremental
Expand Down
102 changes: 97 additions & 5 deletions internal/services/compute/snapshot_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,34 @@ func TestAccSnapshot_fromManagedDisk(t *testing.T) {
})
}

func TestAccSnapshot_networkAccessPolicy(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_snapshot", "test")
r := SnapshotResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.networkAccessPolicy(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
})
}

func TestAccSnapshot_publicNetworkAccess(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_snapshot", "test")
r := SnapshotResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.publicNetworkAccess(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
})
}

func TestAccSnapshot_requiresImport(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_snapshot", "test")
r := SnapshotResource{}
Expand Down Expand Up @@ -245,11 +273,13 @@ resource "azurerm_managed_disk" "test" {
}

resource "azurerm_snapshot" "test" {
name = "acctestss_%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
create_option = "Copy"
source_uri = azurerm_managed_disk.test.id
name = "acctestss_%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
create_option = "Copy"
source_uri = azurerm_managed_disk.test.id
network_access_policy = "AllowAll"
public_network_access = "Enabled"

tags = {
Hello = "World"
Expand Down Expand Up @@ -694,3 +724,65 @@ resource "azurerm_snapshot" "test" {
}
`, data.RandomInteger, data.Locations.Primary)
}

func (SnapshotResource) networkAccessPolicy(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_managed_disk" "test" {
name = "acctestmd-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
storage_account_type = "Standard_LRS"
create_option = "Empty"
disk_size_gb = "10"
}

resource "azurerm_snapshot" "test" {
name = "acctestss_%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
create_option = "Copy"
source_uri = azurerm_managed_disk.test.id
network_access_policy = "AllowAll"
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger)
}

func (SnapshotResource) publicNetworkAccess(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_managed_disk" "test" {
name = "acctestmd-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
storage_account_type = "Standard_LRS"
create_option = "Empty"
disk_size_gb = "10"
}

resource "azurerm_snapshot" "test" {
name = "acctestss_%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
create_option = "Copy"
source_uri = azurerm_managed_disk.test.id
public_network_access = "Disabled"
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger)
}
4 changes: 4 additions & 0 deletions website/docs/r/snapshot.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ The following arguments are supported:

* `incremental_enabled` - (Optional) Specifies if the Snapshot is incremental. Changing this forces a new resource to be created.

* `network_access_policy` - (Optional) Policy for accessing the disk via network. Possible values are `AllowAll`, `AllowPrivate`, or `DenyAll`. Defaults to `AllowAll`.

* `public_network_access` - (Optional) Policy for controlling export on the disk. Possible values are `Enabled` or `Disabled`. Defaults to `Enabled`.

* `tags` - (Optional) A mapping of tags to assign to the resource.

---
Expand Down
Loading