Skip to content

Commit

Permalink
feat(ipam): add resource type attribute (#2100)
Browse files Browse the repository at this point in the history
* feat(ipam): add resource type filter

* doc typo
  • Loading branch information
Codelax authored Aug 21, 2023
1 parent 224b1d7 commit e8d1e37
Show file tree
Hide file tree
Showing 6 changed files with 450 additions and 344 deletions.
1 change: 1 addition & 0 deletions .github/workflows/acceptance-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ jobs:
- Iam
- Instance
- Iot
- IPAM
- K8S
- Lb
- Marketplace
Expand Down
16 changes: 13 additions & 3 deletions docs/data-sources/ipam_ip.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,19 @@ resource "scaleway_instance_private_nic" "nic" {
private_network_id = scaleway_vpc_private_network.pn.id
}
# Find server IPv4 using private-nic mac address
data "scaleway_ipam_ip" "ip" {
# Find server private IPv4 using private-nic mac address
data "scaleway_ipam_ip" "by_mac" {
mac_address = scaleway_instance_private_nic.nic.mac_address
type = "ipv4"
}
# Find server private IPv4 using private-nic id
data "scaleway_ipam_ip" "by_id" {
resource_id = scaleway_instance_private_nic.nic.id
resource_type = "instance_private_nic"
type = "ipv4"
}
```

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

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

- `resource_id` - (Optional) The ID of the resource that the IP is bound to.
- `resource_id` - (Optional) The ID of the resource that the IP is bound to. Require `resource_type`

- `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.

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

Expand Down
10 changes: 8 additions & 2 deletions scaleway/data_source_ipam_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ func dataSourceScalewayIPAMIP() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"resource_type": {
Type: schema.TypeString,
Optional: true,
RequiredWith: []string{"resource_id"},
Default: ipam.ResourceTypeUnknownType,
},
"mac_address": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -71,8 +77,8 @@ func dataSourceScalewayIPAMIPRead(ctx context.Context, d *schema.ResourceData, m
PrivateNetworkID: expandStringPtr(d.Get("private_network_id")),
SubnetID: nil,
Attached: nil,
ResourceID: expandStringPtr(d.Get("resource_id")),
ResourceType: ipam.ResourceTypeUnknownType,
ResourceID: expandStringPtr(expandLastID(d.Get("resource_id"))),
ResourceType: ipam.ResourceType(d.Get("resource_type").(string)),
MacAddress: expandStringPtr(d.Get("mac_address")),
ResourceName: nil,
ResourceIDs: nil,
Expand Down
12 changes: 10 additions & 2 deletions scaleway/data_source_ipam_ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,20 @@ func TestAccScalewayDataSourceIPAMIP_Instance(t *testing.T) {
server_id = scaleway_instance_server.main.id
}
data "scaleway_ipam_ip" "main" {
data "scaleway_ipam_ip" "by_mac" {
mac_address = scaleway_instance_private_nic.main.mac_address
type = "ipv4"
}
data "scaleway_ipam_ip" "by_id" {
resource_id = scaleway_instance_private_nic.main.id
resource_type = "instance_private_nic"
type = "ipv4"
}`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.scaleway_ipam_ip.main", "address"),
resource.TestCheckResourceAttrSet("data.scaleway_ipam_ip.by_mac", "address"),
resource.TestCheckResourceAttrSet("data.scaleway_ipam_ip.by_id", "address"),
resource.TestCheckResourceAttrPair("data.scaleway_ipam_ip.by_mac", "address", "data.scaleway_ipam_ip.by_id", "address"),
),
},
},
Expand Down
21 changes: 21 additions & 0 deletions scaleway/helpers_ipam.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package scaleway

import (
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
ipam "github.com/scaleway/scaleway-sdk-go/api/ipam/v1alpha1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/scaleway-sdk-go/validation"
)

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

return ipamAPI, region, nil
}

// expandLastID expand the last ID in a potential composed ID
// region/id1/id2 -> id2
// region/id1 -> id1
// region/id1/invalid -> id1
// id1 -> id1
// invalid -> invalid
func expandLastID(i interface{}) string {
composedID := i.(string)
elems := strings.Split(composedID, "/")
for i := len(elems) - 1; i >= 0; i-- {
if validation.IsUUID(elems[i]) {
return elems[i]
}
}

return composedID
}
734 changes: 397 additions & 337 deletions scaleway/testdata/data-source-ipamip-instance.cassette.yaml

Large diffs are not rendered by default.

0 comments on commit e8d1e37

Please sign in to comment.