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(ipam): support custom resources #2783

Merged
merged 7 commits into from
Oct 28, 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
28 changes: 28 additions & 0 deletions docs/resources/ipam_ip.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,31 @@ resource "scaleway_ipam_ip" "ip01" {
}
```

### Book an IP for a custom resource

```terraform
resource "scaleway_vpc" "vpc01" {
name = "my vpc"
}
resource "scaleway_vpc_private_network" "pn01" {
vpc_id = scaleway_vpc.vpc01.id
ipv4_subnet {
subnet = "172.16.32.0/22"
}
}
resource "scaleway_ipam_ip" "ip01" {
address = "172.16.32.7"
source {
private_network_id = scaleway_vpc_private_network.pn01.id
}
custom_resource {
mac_address = "bc:24:11:74:d0:6a"
}
}
```

## Argument Reference

The following arguments are supported:
Expand All @@ -90,6 +115,9 @@ The following arguments are supported:
- `private_network_id` - The Private Network of the IP (if the IP is a private IP).
- `subnet_id` - The Private Network subnet of the IP (if the IP is a private IP).
- `is_ipv6` - (Optional) Defines whether to request an IPv6 address instead of IPv4.
- `custome_resource` - (Optional) The custom resource to attach to the IP being reserved. An example of a custom resource is a virtual machine hosted on an Elastic Metal server.
- `mac_address` - The MAC address of the custom resource.
- `name` - When the resource is in a Private Network, a DNS record is available to resolve the resource name.
- `region` - (Defaults to [provider](../index.md#region) `region`) The [region](../guides/regions_and_zones.md#regions) of the IP.
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the Project the IP is associated with.

Expand Down
67 changes: 60 additions & 7 deletions internal/services/ipam/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,26 @@ func ResourceIP() *schema.Resource {
},
},
},
"custom_resource": {
Type: schema.TypeList,
Optional: true,
Description: "The custom resource in which to book the IP",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"mac_address": {
Type: schema.TypeString,
Required: true,
Description: "MAC address of the custom resource",
ValidateFunc: validation.IsMACAddress,
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: "When the resource is in a Private Network, a DNS record is available to resolve the resource name",
},
},
},
},
"is_ipv6": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -179,6 +199,10 @@ func ResourceIPAMIPCreate(ctx context.Context, d *schema.ResourceData, m interfa
req.Source = expandIPSource(source)
}

if customResource, ok := d.GetOk("custom_resource"); ok {
req.Resource = expandCustomResource(customResource)
}

res, err := ipamAPI.BookIP(req, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
Expand Down Expand Up @@ -267,13 +291,31 @@ func ResourceIPAMIPUpdate(ctx context.Context, d *schema.ResourceData, m interfa
return diag.FromErr(err)
}

_, err = ipamAPI.UpdateIP(&ipam.UpdateIPRequest{
IPID: ID,
Region: region,
Tags: types.ExpandUpdatedStringsPtr(d.Get("tags")),
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
if d.HasChange("custom_resource") {
oldCustomResourceRaw, newCustomResourceRaw := d.GetChange("custom_resource")
oldCustomResource := expandCustomResource(oldCustomResourceRaw)
newCustomResource := expandCustomResource(newCustomResourceRaw)

_, err = ipamAPI.MoveIP(&ipam.MoveIPRequest{
Region: region,
IPID: ID,
FromResource: oldCustomResource,
ToResource: newCustomResource,
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}
}

if d.HasChange("tags") {
_, err = ipamAPI.UpdateIP(&ipam.UpdateIPRequest{
IPID: ID,
Region: region,
Tags: types.ExpandUpdatedStringsPtr(d.Get("tags")),
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}
}

return ResourceIPAMIPRead(ctx, d, m)
Expand All @@ -285,6 +327,17 @@ func ResourceIPAMIPDelete(ctx context.Context, d *schema.ResourceData, m interfa
return diag.FromErr(err)
}

if customResource, ok := d.GetOk("custom_resource"); ok {
_, err = ipamAPI.DetachIP(&ipam.DetachIPRequest{
Region: region,
IPID: ID,
Resource: expandCustomResource(customResource),
}, scw.WithContext(ctx))
if err != nil && !httperrors.Is404(err) {
return diag.FromErr(err)
}
}

err = ipamAPI.ReleaseIP(&ipam.ReleaseIPRequest{
Region: region,
IPID: ID,
Expand Down
90 changes: 90 additions & 0 deletions internal/services/ipam/ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,96 @@ func TestAccIPAMIP_WithTags(t *testing.T) {
})
}

func TestAccIPAMIP_WithCustomResource(t *testing.T) {
tt := acctest.NewTestTools(t)
defer tt.Cleanup()
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProviderFactories: tt.ProviderFactories,
CheckDestroy: ipamchecks.CheckIPDestroy(tt),
Steps: []resource.TestStep{
{
Config: `
resource "scaleway_vpc" "vpc01" {
name = "my vpc"
}
resource "scaleway_vpc_private_network" "pn01" {
vpc_id = scaleway_vpc.vpc01.id
ipv4_subnet {
subnet = "172.16.32.0/22"
}
}
resource "scaleway_ipam_ip" "ip01" {
source {
private_network_id = scaleway_vpc_private_network.pn01.id
}
custom_resource {
mac_address = "bc:24:11:74:d0:5a"
}
}
`,
Check: resource.ComposeTestCheckFunc(
testAccCheckIPAMIPExists(tt, "scaleway_ipam_ip.ip01"),
resource.TestCheckResourceAttr("scaleway_ipam_ip.ip01", "custom_resource.0.mac_address", "bc:24:11:74:d0:5a"),
),
},
{
Config: `
resource "scaleway_vpc" "vpc01" {
name = "my vpc"
}
resource "scaleway_vpc_private_network" "pn01" {
vpc_id = scaleway_vpc.vpc01.id
ipv4_subnet {
subnet = "172.16.32.0/22"
}
}
resource "scaleway_ipam_ip" "ip01" {
source {
private_network_id = scaleway_vpc_private_network.pn01.id
}
custom_resource {
mac_address = "bc:24:11:74:d0:5b"
}
}
`,
Check: resource.ComposeTestCheckFunc(
testAccCheckIPAMIPExists(tt, "scaleway_ipam_ip.ip01"),
resource.TestCheckResourceAttr("scaleway_ipam_ip.ip01", "custom_resource.0.mac_address", "bc:24:11:74:d0:5b"),
),
},
{
Config: `
resource "scaleway_vpc" "vpc01" {
name = "my vpc"
}
resource "scaleway_vpc_private_network" "pn01" {
vpc_id = scaleway_vpc.vpc01.id
ipv4_subnet {
subnet = "172.16.32.0/22"
}
}
resource "scaleway_ipam_ip" "ip01" {
source {
private_network_id = scaleway_vpc_private_network.pn01.id
}
}
`,
Check: resource.ComposeTestCheckFunc(
testAccCheckIPAMIPExists(tt, "scaleway_ipam_ip.ip01"),
resource.TestCheckNoResourceAttr("scaleway_ipam_ip.ip01", "custom_resource.0.mac_address"),
),
},
},
})
}

func testAccCheckIPAMIPExists(tt *acctest.TestTools, n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down
Loading
Loading