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(lb): add LB IP data source #493

Merged
merged 1 commit into from
Jun 17, 2020
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
66 changes: 66 additions & 0 deletions scaleway/data_source_lb_ip_beta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package scaleway

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/scaleway/scaleway-sdk-go/api/lb/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

func dataSourceScalewayLbIPBeta() *schema.Resource {
// Generate datasource schema from resource
dsSchema := datasourceSchemaFromResourceSchema(resourceScalewayLbIPBeta().Schema)

dsSchema["ip_address"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "The IP adress",
ConflictsWith: []string{"ip_id"},
}
dsSchema["ip_id"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "The ID of the IP address",
ConflictsWith: []string{"ip_address"},
ValidateFunc: validationUUIDorUUIDWithLocality(),
}

return &schema.Resource{
Read: dataSourceScalewayLbIPBetaRead,
Schema: dsSchema,
}
}

func dataSourceScalewayLbIPBetaRead(d *schema.ResourceData, m interface{}) error {
api, region, err := lbAPIWithRegion(d, m)
if err != nil {
return err
}

ipID, ok := d.GetOk("ip_id")
if !ok { // Get IP by region and IP address.
res, err := api.ListIPs(&lb.ListIPsRequest{
Region: region,
IPAddress: scw.StringPtr(d.Get("ip_address").(string)),
})
if err != nil {
return err
}
if len(res.IPs) == 0 {
return fmt.Errorf("no ips found with the address %s", d.Get("ip_address"))
}
if len(res.IPs) > 1 {
return fmt.Errorf("%d ips found with the same address %s", len(res.IPs), d.Get("ip_address"))
}
ipID = res.IPs[0].ID
}

regionalID := datasourceNewRegionalizedID(ipID, region)
d.SetId(regionalID)
err = d.Set("ip_id", regionalID)
if err != nil {
return err
}
return resourceScalewayLbIPBetaRead(d, m)
}
36 changes: 36 additions & 0 deletions scaleway/data_source_lb_ip_beta_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package scaleway

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccScalewayDataSourceLbIPBeta_Basic(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckScalewayLbIPBetaDestroy,
Steps: []resource.TestStep{
{
Config: `
resource "scaleway_lb_ip_beta" "test" {
}

data "scaleway_lb_ip_beta" "test" {
ip_address = "${scaleway_lb_ip_beta.test.ip_address}"
}

data "scaleway_lb_ip_beta" "test2" {
ip_id = "${scaleway_lb_ip_beta.test.id}"
}
`,
Check: resource.ComposeTestCheckFunc(
testAccCheckScalewayLbIPBetaExists("data.scaleway_lb_ip_beta.test"),
resource.TestCheckResourceAttrPair("data.scaleway_lb_ip_beta.test", "ip_address", "scaleway_lb_ip_beta.test", "ip_address"),
resource.TestCheckResourceAttrPair("data.scaleway_lb_ip_beta.test2", "ip_address", "scaleway_lb_ip_beta.test", "ip_address"),
),
},
},
})
}
1 change: 1 addition & 0 deletions scaleway/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ func Provider() terraform.ResourceProvider {
"scaleway_instance_image": dataSourceScalewayInstanceImage(),
"scaleway_instance_volume": dataSourceScalewayInstanceVolume(),
"scaleway_baremetal_offer": dataSourceScalewayBaremetalOffer(),
"scaleway_lb_ip_beta": dataSourceScalewayLbIPBeta(),
"scaleway_marketplace_image_beta": dataSourceScalewayMarketplaceImageBeta(),
"scaleway_registry_namespace_beta": dataSourceScalewayRegistryNamespaceBeta(),
"scaleway_registry_image_beta": dataSourceScalewayRegistryImageBeta(),
Expand Down
2 changes: 1 addition & 1 deletion scaleway/resource_lb_ip_beta.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func resourceScalewayLbIPBeta() *schema.Resource {
"ip_address": {
Type: schema.TypeString,
Computed: true,
Description: "The load-balance public IP address",
Description: "The load-balancer public IP address",
},
"lb_id": {
Type: schema.TypeString,
Expand Down
44 changes: 44 additions & 0 deletions website/docs/d/lb_ip_beta.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
layout: "scaleway"
page_title: "Scaleway: scaleway_lb_ip_beta"
description: |-
Gets information about a Load Balancer IP.
---

# scaleway_lb_ip_beta

Gets information about a Load Balancer IP.

## Example Usage

```hcl
# Get info by IP address
data "scaleway_lb_ip_beta" "my_ip" {
ip_address = "0.0.0.0"
}

# Get info by IP ID
data "scaleway_lb_ip_beta" "my_up" {
ip_id = "11111111-1111-1111-1111-111111111111"
}
```

## Argument Reference

- `ip_address` - (Optional) The IP address.
Only one of `ip_address` and `lb_id` should be specified.

- `lb_id` - (Optional) The IP ID.
Only one of `ip_address` and `ip_id` should be specified.

- `region` - (Defaults to [provider](../index.html#region) `region`) The [region](../guides/regions_and_zones.html#zones) in which the LB IP exists.

- `organization_id` - (Defaults to [provider](../index.html#organization_id) `organization_id`) The ID of the organization the LB IP is associated with.

## Attributes Reference

In addition to all above arguments, the following attributes are exported:

- `reverse` - The reverse domain associated with this IP.

- `lb_id` - The associated load-balance ID if any
9 changes: 9 additions & 0 deletions website/scaleway.erb
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@
</ul>
</li>

<li>
<a href="#">Load-Balancer Data Sources (Beta)</a>
<ul class="nav nav-visible">
<li>
<a href="/docs/providers/scaleway/d/lb_ip_beta.html">scaleway_lb_ip_beta</a>
</li>
</ul>
</li>

<li>
<a href="#">Database Resources (Beta)</a>
<ul class="nav nav-visible">
Expand Down