Skip to content

Commit 224b1d7

Browse files
yfodilCodelax
andauthored
feat(flexibleip): add flexible ips datasource (#2098)
* feat(flexibleip): add flexible ips datasource * add doc * typo * lint * update cassette * typo * update cassette * Update docs/data-sources/flexible_ips.md Co-authored-by: Jules Castéran <[email protected]> * fix --------- Co-authored-by: Jules Castéran <[email protected]>
1 parent 04fd61e commit 224b1d7

7 files changed

+7836
-0
lines changed

docs/data-sources/flexible_ips.md

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
---
2+
subcategory: "Elastic Metal"
3+
page_title: "Scaleway: scaleway_flexible_ips"
4+
---
5+
6+
# scaleway_flexible_ips
7+
8+
Gets information about multiple Flexible IPs.
9+
10+
## Example Usage
11+
12+
```hcl
13+
# Find ips that share the same tags
14+
data "scaleway_flexible_ips" "fips_by_tags" {
15+
tags = [ "a tag" ]
16+
}
17+
18+
# Find ips that share the same Server ID
19+
data "scaleway_baremetal_offer" "my_offer" {
20+
name = "EM-B112X-SSD"
21+
}
22+
23+
resource "scaleway_baremetal_server" "base" {
24+
name = "MyServer"
25+
offer = data.scaleway_baremetal_offer.my_offer.offer_id
26+
install_config_afterward = true
27+
}
28+
29+
resource "scaleway_flexible_ip" "first" {
30+
server_id = scaleway_baremetal_server.base.id
31+
tags = [ "foo", "first" ]
32+
}
33+
34+
resource "scaleway_flexible_ip" "second" {
35+
server_id = scaleway_baremetal_server.base.id
36+
tags = [ "foo", "second" ]
37+
}
38+
39+
data "scaleway_flexible_ips" "fips_by_server_id" {
40+
server_ids = [scaleway_baremetal_server.base.id]
41+
depends_on = [scaleway_flexible_ip.first, scaleway_flexible_ip.second]
42+
}
43+
```
44+
45+
## Argument Reference
46+
47+
- `server_ids` - (Optional) List of server IDs used as filter. IPs with these exact server IDs are listed.
48+
49+
- `tags` - (Optional) List of tags used as filter. IPs with these exact tags are listed.
50+
51+
- `zone` - (Defaults to [provider](../index.md#zone) `zone`) The [zone](../guides/regions_and_zones.md#zones) in which IPs exist.
52+
53+
## Attributes Reference
54+
55+
In addition to all above arguments, the following attributes are exported:
56+
57+
- `ips` - List of found flexible IPS
58+
- `id` - The associated flexible IP ID.
59+
- `description` - The description of the flexible IP.
60+
- `tags` - The list of tags which are attached to the flexible IP.
61+
- `status` - The status of the flexible IP.
62+
- `mac_address` - The MAC address of the server associated with this flexible IP.
63+
- `id` - The MAC address ID.
64+
- `mac_address` - The MAC address of the Virtual MAC.
65+
- `mac_type` - The type of virtual MAC.
66+
- `status` - The status of virtual MAC.
67+
- `created_at` - The date on which the virtual MAC was created (RFC 3339 format).
68+
- `updated_at` - The date on which the virtual MAC was last updated (RFC 3339 format).
69+
- `zone` - (Defaults to [provider](../index.md#zone) `zone`) The [zone](../guides/regions_and_zones.md#zones) in which the MAC address exist.
70+
- `created_at` - The date on which the flexible IP was created (RFC 3339 format).
71+
- `updated_at` - The date on which the flexible IP was last updated (RFC 3339 format).
72+
- `reverse` - The reverse domain associated with this IP.
73+
- `server_id` - The associated server ID if any.
74+
- `organization_id` - (Defaults to [provider](../index.md#organization_id) `organization_id`) The ID of the organization the IP is in.
75+
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the IP is in.

scaleway/data_source_flexible_ips.go

+172
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package scaleway
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
flexibleip "github.com/scaleway/scaleway-sdk-go/api/flexibleip/v1alpha1"
9+
"github.com/scaleway/scaleway-sdk-go/scw"
10+
)
11+
12+
func dataSourceScalewayFlexibleIPs() *schema.Resource {
13+
return &schema.Resource{
14+
ReadContext: dataSourceScalewayFlexibleIPsRead,
15+
Schema: map[string]*schema.Schema{
16+
"server_ids": {
17+
Type: schema.TypeList,
18+
Elem: &schema.Schema{
19+
Type: schema.TypeString,
20+
},
21+
Optional: true,
22+
Description: "Flexible IPs that are attached to these server IDs are listed",
23+
},
24+
"tags": {
25+
Type: schema.TypeList,
26+
Elem: &schema.Schema{
27+
Type: schema.TypeString,
28+
},
29+
Optional: true,
30+
Description: "Flexible IPs with these exact tags are listed",
31+
},
32+
"ips": {
33+
Type: schema.TypeList,
34+
Computed: true,
35+
Elem: &schema.Resource{
36+
Schema: map[string]*schema.Schema{
37+
"id": {
38+
Computed: true,
39+
Type: schema.TypeString,
40+
},
41+
"description": {
42+
Computed: true,
43+
Type: schema.TypeString,
44+
},
45+
"tags": {
46+
Computed: true,
47+
Type: schema.TypeList,
48+
Elem: &schema.Schema{
49+
Type: schema.TypeString,
50+
},
51+
},
52+
"status": {
53+
Computed: true,
54+
Type: schema.TypeString,
55+
},
56+
"ip_address": {
57+
Computed: true,
58+
Type: schema.TypeString,
59+
},
60+
"reverse": {
61+
Computed: true,
62+
Type: schema.TypeString,
63+
},
64+
"mac_address": {
65+
Type: schema.TypeList,
66+
Computed: true,
67+
Description: "The MAC address of the server associated with this flexible IP",
68+
Elem: &schema.Resource{
69+
Schema: map[string]*schema.Schema{
70+
"id": {
71+
Type: schema.TypeString,
72+
Computed: true,
73+
Description: "MAC address ID",
74+
},
75+
"mac_address": {
76+
Type: schema.TypeString,
77+
Computed: true,
78+
Description: "MAC address of the Virtual MAC",
79+
},
80+
"mac_type": {
81+
Type: schema.TypeString,
82+
Computed: true,
83+
Description: "Type of virtual MAC",
84+
},
85+
"status": {
86+
Type: schema.TypeString,
87+
Computed: true,
88+
Description: "Status of virtual MAC",
89+
},
90+
"created_at": {
91+
Type: schema.TypeString,
92+
Computed: true,
93+
Description: "The date on which the virtual MAC was created (RFC 3339 format)",
94+
},
95+
"updated_at": {
96+
Type: schema.TypeString,
97+
Computed: true,
98+
Description: "The date on which the virtual MAC was last updated (RFC 3339 format)",
99+
},
100+
"zone": zoneSchema(),
101+
},
102+
},
103+
},
104+
"created_at": {
105+
Computed: true,
106+
Type: schema.TypeString,
107+
},
108+
"updated_at": {
109+
Computed: true,
110+
Type: schema.TypeString,
111+
},
112+
"zone": zoneComputedSchema(),
113+
"organization_id": organizationIDSchema(),
114+
"project_id": projectIDSchema(),
115+
},
116+
},
117+
},
118+
"zone": zoneSchema(),
119+
"organization_id": organizationIDSchema(),
120+
"project_id": projectIDSchema(),
121+
},
122+
}
123+
}
124+
125+
func dataSourceScalewayFlexibleIPsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
126+
fipAPI, zone, err := fipAPIWithZone(d, meta)
127+
if err != nil {
128+
return diag.FromErr(err)
129+
}
130+
131+
res, err := fipAPI.ListFlexibleIPs(&flexibleip.ListFlexibleIPsRequest{
132+
Zone: zone,
133+
ServerIDs: expandServerIDs(d.Get("server_ids")),
134+
ProjectID: expandStringPtr(d.Get("project_id")),
135+
Tags: expandStrings(d.Get("tags")),
136+
}, scw.WithContext(ctx))
137+
if err != nil {
138+
return diag.FromErr(err)
139+
}
140+
141+
fips := []interface{}(nil)
142+
for _, fip := range res.FlexibleIPs {
143+
rawFip := make(map[string]interface{})
144+
rawFip["id"] = newZonedID(fip.Zone, fip.ID).String()
145+
rawFip["organization_id"] = fip.OrganizationID
146+
rawFip["project_id"] = fip.ProjectID
147+
rawFip["description"] = fip.Description
148+
if len(fip.Tags) > 0 {
149+
rawFip["tags"] = fip.Tags
150+
}
151+
rawFip["created_at"] = flattenTime(fip.CreatedAt)
152+
rawFip["updated_at"] = flattenTime(fip.UpdatedAt)
153+
rawFip["status"] = fip.Status
154+
ip, err := flattenIPNet(fip.IPAddress)
155+
if err != nil {
156+
return diag.FromErr(err)
157+
}
158+
rawFip["ip_address"] = ip
159+
if fip.MacAddress != nil {
160+
rawFip["mac_address"] = flattenFlexibleIPMacAddress(fip.MacAddress)
161+
}
162+
rawFip["reverse"] = fip.Reverse
163+
rawFip["zone"] = string(zone)
164+
165+
fips = append(fips, rawFip)
166+
}
167+
168+
d.SetId(zone.String())
169+
_ = d.Set("ips", fips)
170+
171+
return nil
172+
}
+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package scaleway
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccScalewayDataSourceFlexibleIPs_Basic(t *testing.T) {
10+
tt := NewTestTools(t)
11+
defer tt.Cleanup()
12+
resource.ParallelTest(t, resource.TestCase{
13+
PreCheck: func() { testAccPreCheck(t) },
14+
ProviderFactories: tt.ProviderFactories,
15+
CheckDestroy: testAccCheckScalewayLbIPDestroy(tt),
16+
Steps: []resource.TestStep{
17+
{
18+
Config: `
19+
resource "scaleway_flexible_ip" "first" {
20+
tags = [ "minimal", "first" ]
21+
}
22+
`,
23+
},
24+
{
25+
Config: `
26+
resource "scaleway_flexible_ip" "first" {
27+
tags = [ "minimal", "first" ]
28+
}
29+
30+
resource "scaleway_flexible_ip" "second" {
31+
tags = [ "minimal", "second" ]
32+
}
33+
34+
data "scaleway_flexible_ips" "fips_by_tags" {
35+
tags = [ "minimal" ]
36+
depends_on = [scaleway_flexible_ip.first, scaleway_flexible_ip.second]
37+
}
38+
39+
data "scaleway_flexible_ips" "fips_by_tags_other_zone" {
40+
tags = [ "minimal" ]
41+
zone = "fr-par-2"
42+
depends_on = [scaleway_flexible_ip.first, scaleway_flexible_ip.second]
43+
}
44+
`,
45+
Check: resource.ComposeTestCheckFunc(
46+
resource.TestCheckResourceAttrSet("data.scaleway_flexible_ips.fips_by_tags", "ips.0.id"),
47+
resource.TestCheckResourceAttrSet("data.scaleway_flexible_ips.fips_by_tags", "ips.0.ip_address"),
48+
resource.TestCheckResourceAttrSet("data.scaleway_flexible_ips.fips_by_tags", "ips.1.id"),
49+
resource.TestCheckResourceAttrSet("data.scaleway_flexible_ips.fips_by_tags", "ips.1.ip_address"),
50+
51+
resource.TestCheckNoResourceAttr("data.scaleway_flexible_ips.fips_by_tags_other_zone", "ips.0.id"),
52+
),
53+
},
54+
},
55+
})
56+
}
57+
58+
func TestAccScalewayDataSourceFlexibleIPs_WithBaremetalIDs(t *testing.T) {
59+
tt := NewTestTools(t)
60+
defer tt.Cleanup()
61+
resource.ParallelTest(t, resource.TestCase{
62+
PreCheck: func() { testAccPreCheck(t) },
63+
ProviderFactories: tt.ProviderFactories,
64+
CheckDestroy: testAccCheckScalewayLbIPDestroy(tt),
65+
Steps: []resource.TestStep{
66+
{
67+
Config: `
68+
data "scaleway_baremetal_offer" "my_offer" {
69+
name = "EM-B112X-SSD"
70+
}
71+
72+
resource "scaleway_baremetal_server" "base" {
73+
name = "TestAccScalewayBaremetalServer_WithoutInstallConfig"
74+
offer = data.scaleway_baremetal_offer.my_offer.offer_id
75+
install_config_afterward = true
76+
}
77+
78+
resource "scaleway_flexible_ip" "first" {
79+
tags = [ "minimal", "first" ]
80+
}
81+
`,
82+
},
83+
{
84+
Config: `
85+
data "scaleway_baremetal_offer" "my_offer" {
86+
name = "EM-B112X-SSD"
87+
}
88+
89+
resource "scaleway_baremetal_server" "base" {
90+
name = "TestAccScalewayBaremetalServer_WithoutInstallConfig"
91+
offer = data.scaleway_baremetal_offer.my_offer.offer_id
92+
install_config_afterward = true
93+
}
94+
95+
resource "scaleway_flexible_ip" "first" {
96+
tags = [ "minimal", "first" ]
97+
}
98+
99+
resource "scaleway_flexible_ip" "second" {
100+
tags = [ "minimal", "second" ]
101+
}
102+
`,
103+
},
104+
{
105+
Config: `
106+
data "scaleway_baremetal_offer" "my_offer" {
107+
name = "EM-B112X-SSD"
108+
}
109+
110+
resource "scaleway_baremetal_server" "base" {
111+
name = "TestAccScalewayBaremetalServer_WithoutInstallConfig"
112+
offer = data.scaleway_baremetal_offer.my_offer.offer_id
113+
install_config_afterward = true
114+
}
115+
116+
resource "scaleway_flexible_ip" "first" {
117+
server_id = scaleway_baremetal_server.base.id
118+
tags = [ "minimal", "first" ]
119+
}
120+
121+
resource "scaleway_flexible_ip" "second" {
122+
server_id = scaleway_baremetal_server.base.id
123+
tags = [ "minimal", "second" ]
124+
}
125+
126+
data "scaleway_flexible_ips" "fips_by_server_id" {
127+
server_ids = [scaleway_baremetal_server.base.id]
128+
depends_on = [scaleway_flexible_ip.first, scaleway_flexible_ip.second]
129+
}
130+
`,
131+
Check: resource.ComposeTestCheckFunc(
132+
resource.TestCheckResourceAttrSet("data.scaleway_flexible_ips.fips_by_server_id", "ips.0.id"),
133+
resource.TestCheckResourceAttrSet("data.scaleway_flexible_ips.fips_by_server_id", "ips.0.ip_address"),
134+
resource.TestCheckResourceAttrSet("data.scaleway_flexible_ips.fips_by_server_id", "ips.1.id"),
135+
resource.TestCheckResourceAttrSet("data.scaleway_flexible_ips.fips_by_server_id", "ips.1.ip_address"),
136+
),
137+
},
138+
},
139+
})
140+
}

0 commit comments

Comments
 (0)