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

feat(instance): support ipam ips on pnic creation #2660

Merged
merged 6 commits into from
Sep 27, 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
51 changes: 43 additions & 8 deletions docs/resources/instance_private_nic.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,73 @@ Creates and manages Scaleway Instance Private NICs. For more information, see

```terraform
resource "scaleway_instance_private_nic" "pnic01" {
server_id = "fr-par-1/11111111-1111-1111-1111-111111111111"
private_network_id = "fr-par-1/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
server_id = "fr-par-1/11111111-1111-1111-1111-111111111111"
private_network_id = "fr-par-1/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
}
```

### With zone

```terraform
resource scaleway_vpc_private_network pn01 {
name = "private_network_instance"
zone = "fr-par-2"
resource "scaleway_vpc_private_network" "pn01" {
name = "private_network_instance"
region = "fr-par"
}

resource "scaleway_instance_server" "base" {
image = "ubuntu_jammy"
type = "DEV1-S"
zone = scaleway_vpc_private_network.pn01.zone
zone = scaleway_vpc_private_network.pn01.zone
}

resource "scaleway_instance_private_nic" "pnic01" {
server_id = scaleway_instance_server.base.id
server_id = scaleway_instance_server.base.id
private_network_id = scaleway_vpc_private_network.pn01.id
zone = scaleway_vpc_private_network.pn01.zone
zone = scaleway_vpc_private_network.pn01.zone
}
```

### With IPAM IP IDs

```terraform
resource "scaleway_vpc" "vpc01" {
name = "vpc_instance"
}

resource "scaleway_vpc_private_network" "pn01" {
name = "private_network_instance"
ipv4_subnet {
subnet = "172.16.64.0/22"
}
vpc_id = scaleway_vpc.vpc01.id
}

resource "scaleway_ipam_ip" "ip01" {
address = "172.16.64.7"
source {
private_network_id = scaleway_vpc_private_network.pn01.id
}
}

resource "scaleway_instance_server" "server01" {
image = "ubuntu_focal"
type = "PLAY2-MICRO"
}

resource "scaleway_instance_private_nic" "pnic01" {
private_network_id = scaleway_vpc_private_network.pn01.id
server_id = scaleway_instance_server.server01.id
ipam_ip_ids = [scaleway_ipam_ip.ip01.id]
}
```

## Argument Reference

The following arguments are required:

- `server_id` - (Required) The ID of the server associated with.
- `private_network_id` - (Required) The ID of the private network attached to.
- `ipam_ip_ids` - (Optional) IPAM IDs of a pre-reserved IP addresses to assign to the Instance in the requested private network.
- `tags` - (Optional) The tags associated with the private NIC.
- `zone` - (Defaults to [provider](../index.md#zone) `zone`) The [zone](../guides/regions_and_zones.md#zones) in which the server must be created.

Expand Down
10 changes: 10 additions & 0 deletions internal/services/instance/private_nic.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ func ResourcePrivateNIC() *schema.Resource {
Description: "IPAM ip list, should be for internal use only",
ForceNew: true,
},
"ipam_ip_ids": {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
ForceNew: true,
Description: "IPAM IDs of a pre-reserved IP addresses to assign to the Instance in the requested private network",
},
"zone": zonal.Schema(),
},
CustomizeDiff: cdf.LocalityCheck("server_id", "private_network_id"),
Expand All @@ -91,6 +100,7 @@ func ResourceInstancePrivateNICCreate(ctx context.Context, d *schema.ResourceDat
PrivateNetworkID: regional.ExpandID(d.Get("private_network_id").(string)).ID,
Tags: types.ExpandStrings(d.Get("tags")),
IPIDs: types.ExpandStringsPtr(d.Get("ip_ids")),
IpamIPIDs: locality.ExpandIDs(d.Get("ipam_ip_ids")),
}

privateNIC, err := instanceAPI.CreatePrivateNIC(
Expand Down
66 changes: 66 additions & 0 deletions internal/services/instance/private_nic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,72 @@ func TestAccPrivateNIC_Tags(t *testing.T) {
})
}

func TestAccPrivateNIC_WithIPAM(t *testing.T) {
tt := acctest.NewTestTools(t)
defer tt.Cleanup()
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProviderFactories: tt.ProviderFactories,
CheckDestroy: isPrivateNICDestroyed(tt),
Steps: []resource.TestStep{
{
Config: `
resource "scaleway_vpc" "vpc01" {
name = "TestAccScalewayInstancePrivateNIC_IPAM"
}

resource "scaleway_vpc_private_network" "pn01" {
name = "TestAccScalewayInstancePrivateNIC_IPAM"
ipv4_subnet {
subnet = "172.16.64.0/22"
}
vpc_id = scaleway_vpc.vpc01.id
}

resource "scaleway_ipam_ip" "ip01" {
address = "172.16.64.7"
source {
private_network_id = scaleway_vpc_private_network.pn01.id
}
}

resource "scaleway_instance_server" "server01" {
name = "TestAccScalewayInstancePrivateNIC_IPAM"
image = "ubuntu_focal"
type = "PLAY2-MICRO"
}

resource "scaleway_instance_private_nic" "pnic01" {
private_network_id = scaleway_vpc_private_network.pn01.id
server_id = scaleway_instance_server.server01.id
ipam_ip_ids = [scaleway_ipam_ip.ip01.id]
}

data "scaleway_ipam_ip" "by_id" {
resource {
id = scaleway_instance_private_nic.pnic01.id
type = "instance_private_nic"
}
type = "ipv4"
}
`,
Check: resource.ComposeTestCheckFunc(
isPrivateNICPresent(tt, "scaleway_instance_private_nic.pnic01"),
resource.TestCheckResourceAttrPair(
"scaleway_instance_private_nic.pnic01", "private_network_id",
"scaleway_vpc_private_network.pn01", "id"),
resource.TestCheckResourceAttrPair(
"scaleway_instance_private_nic.pnic01", "ipam_ip_ids.0",
"scaleway_ipam_ip.ip01", "id"),
resource.TestCheckResourceAttrPair(
"scaleway_ipam_ip.ip01", "address",
"data.scaleway_ipam_ip.by_id", "address_cidr"),
),
},
},
})
}

func isPrivateNICPresent(tt *acctest.TestTools, n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down
2,920 changes: 2,920 additions & 0 deletions internal/services/instance/testdata/private-nic-with-ipam.cassette.yaml

Large diffs are not rendered by default.

Loading