Skip to content

Commit e8d1e37

Browse files
authored
feat(ipam): add resource type attribute (#2100)
* feat(ipam): add resource type filter * doc typo
1 parent 224b1d7 commit e8d1e37

File tree

6 files changed

+450
-344
lines changed

6 files changed

+450
-344
lines changed

.github/workflows/acceptance-tests.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ jobs:
2020
- Iam
2121
- Instance
2222
- Iot
23+
- IPAM
2324
- K8S
2425
- Lb
2526
- Marketplace

docs/data-sources/ipam_ip.md

+13-3
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,19 @@ resource "scaleway_instance_private_nic" "nic" {
1818
private_network_id = scaleway_vpc_private_network.pn.id
1919
}
2020
21-
# Find server IPv4 using private-nic mac address
22-
data "scaleway_ipam_ip" "ip" {
21+
# Find server private IPv4 using private-nic mac address
22+
data "scaleway_ipam_ip" "by_mac" {
2323
mac_address = scaleway_instance_private_nic.nic.mac_address
2424
type = "ipv4"
2525
}
26+
27+
# Find server private IPv4 using private-nic id
28+
data "scaleway_ipam_ip" "by_id" {
29+
resource_id = scaleway_instance_private_nic.nic.id
30+
resource_type = "instance_private_nic"
31+
type = "ipv4"
32+
}
33+
2634
```
2735

2836
## Argument Reference
@@ -31,7 +39,9 @@ data "scaleway_ipam_ip" "ip" {
3139

3240
- `private_network_id` - (Optional) The ID of the private network the IP belong to.
3341

34-
- `resource_id` - (Optional) The ID of the resource that the IP is bound to.
42+
- `resource_id` - (Optional) The ID of the resource that the IP is bound to. Require `resource_type`
43+
44+
- `resource_type` - (Optional) The type of the resource to get the IP from. Required with `resource_id`. [Documentation](https://pkg.go.dev/github.com/scaleway/scaleway-sdk-go@master/api/ipam/v1alpha1#pkg-constants) with type list.
3545

3646
- `mac_address` - (Optional) The Mac Address linked to the IP.
3747

scaleway/data_source_ipam_ip.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ func dataSourceScalewayIPAMIP() *schema.Resource {
2424
Type: schema.TypeString,
2525
Optional: true,
2626
},
27+
"resource_type": {
28+
Type: schema.TypeString,
29+
Optional: true,
30+
RequiredWith: []string{"resource_id"},
31+
Default: ipam.ResourceTypeUnknownType,
32+
},
2733
"mac_address": {
2834
Type: schema.TypeString,
2935
Optional: true,
@@ -71,8 +77,8 @@ func dataSourceScalewayIPAMIPRead(ctx context.Context, d *schema.ResourceData, m
7177
PrivateNetworkID: expandStringPtr(d.Get("private_network_id")),
7278
SubnetID: nil,
7379
Attached: nil,
74-
ResourceID: expandStringPtr(d.Get("resource_id")),
75-
ResourceType: ipam.ResourceTypeUnknownType,
80+
ResourceID: expandStringPtr(expandLastID(d.Get("resource_id"))),
81+
ResourceType: ipam.ResourceType(d.Get("resource_type").(string)),
7682
MacAddress: expandStringPtr(d.Get("mac_address")),
7783
ResourceName: nil,
7884
ResourceIDs: nil,

scaleway/data_source_ipam_ip_test.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,20 @@ func TestAccScalewayDataSourceIPAMIP_Instance(t *testing.T) {
3737
server_id = scaleway_instance_server.main.id
3838
}
3939
40-
data "scaleway_ipam_ip" "main" {
40+
data "scaleway_ipam_ip" "by_mac" {
4141
mac_address = scaleway_instance_private_nic.main.mac_address
4242
type = "ipv4"
43+
}
44+
45+
data "scaleway_ipam_ip" "by_id" {
46+
resource_id = scaleway_instance_private_nic.main.id
47+
resource_type = "instance_private_nic"
48+
type = "ipv4"
4349
}`,
4450
Check: resource.ComposeTestCheckFunc(
45-
resource.TestCheckResourceAttrSet("data.scaleway_ipam_ip.main", "address"),
51+
resource.TestCheckResourceAttrSet("data.scaleway_ipam_ip.by_mac", "address"),
52+
resource.TestCheckResourceAttrSet("data.scaleway_ipam_ip.by_id", "address"),
53+
resource.TestCheckResourceAttrPair("data.scaleway_ipam_ip.by_mac", "address", "data.scaleway_ipam_ip.by_id", "address"),
4654
),
4755
},
4856
},

scaleway/helpers_ipam.go

+21
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package scaleway
22

33
import (
4+
"strings"
5+
46
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
57
ipam "github.com/scaleway/scaleway-sdk-go/api/ipam/v1alpha1"
68
"github.com/scaleway/scaleway-sdk-go/scw"
9+
"github.com/scaleway/scaleway-sdk-go/validation"
710
)
811

912
// ipamAPIWithRegion returns a new ipam API and the region
@@ -18,3 +21,21 @@ func ipamAPIWithRegion(d *schema.ResourceData, m interface{}) (*ipam.API, scw.Re
1821

1922
return ipamAPI, region, nil
2023
}
24+
25+
// expandLastID expand the last ID in a potential composed ID
26+
// region/id1/id2 -> id2
27+
// region/id1 -> id1
28+
// region/id1/invalid -> id1
29+
// id1 -> id1
30+
// invalid -> invalid
31+
func expandLastID(i interface{}) string {
32+
composedID := i.(string)
33+
elems := strings.Split(composedID, "/")
34+
for i := len(elems) - 1; i >= 0; i-- {
35+
if validation.IsUUID(elems[i]) {
36+
return elems[i]
37+
}
38+
}
39+
40+
return composedID
41+
}

scaleway/testdata/data-source-ipamip-instance.cassette.yaml

+397-337
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)