forked from scaleway/scaleway-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pat-rule): add data source pat rules (scaleway#1093)
- Loading branch information
Showing
6 changed files
with
2,032 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
--- | ||
page_title: "Scaleway: scaleway_vpc_public_gateway_pat_rule" | ||
description: |- Get information about Scaleway VPC Public Gateway PAT rule. | ||
--- | ||
|
||
# scaleway_vpc_public_gateway_pat_rule | ||
|
||
Gets information about a public gateway PAT rule. For further information please check the | ||
API [documentation](https://developers.scaleway.com/en/products/vpc-gw/api/v1/#get-8faeea) | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
resource scaleway_vpc_public_gateway pg01 { | ||
type = "VPC-GW-S" | ||
} | ||
resource scaleway_vpc_public_gateway_dhcp dhcp01 { | ||
subnet = "192.168.1.0/24" | ||
} | ||
resource scaleway_vpc_private_network pn01 { | ||
name = "pn_test_network" | ||
} | ||
resource scaleway_vpc_gateway_network gn01 { | ||
gateway_id = scaleway_vpc_public_gateway.pg01.id | ||
private_network_id = scaleway_vpc_private_network.pn01.id | ||
dhcp_id = scaleway_vpc_public_gateway_dhcp.dhcp01.id | ||
depends_on = [scaleway_vpc_private_network.pn01] | ||
cleanup_dhcp = true | ||
enable_masquerade = true | ||
} | ||
resource scaleway_vpc_public_gateway_pat_rule main { | ||
gateway_id = scaleway_vpc_public_gateway.pg01.id | ||
private_ip = scaleway_vpc_public_gateway_dhcp.dhcp01.address | ||
private_port = 42 | ||
public_port = 42 | ||
protocol = "both" | ||
depends_on = [scaleway_vpc_gateway_network.gn01, scaleway_vpc_private_network.pn01] | ||
} | ||
data "scaleway_vpc_public_gateway_pat_rule" "main" { | ||
pat_rule_id = "${scaleway_vpc_public_gateway_pat_rule.main.id}" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
- `pat_rule_id` (Required) The ID of the PAT rule to retrieve | ||
- `zone` - (Defaults to [provider](../index.md#zone) `zone`) The [zone](../guides/regions_and_zones.md#zones) in which | ||
the image exists. | ||
|
||
## Attributes Reference | ||
|
||
`id` is set to the ID of the found public gateway PAT RULE. | ||
|
||
The following arguments are exported: | ||
|
||
- `gateway_id` - The ID of the public gateway. | ||
- `private_ip` - The Private IP to forward data to (IP address). | ||
- `public_port` - The Public port to listen on. | ||
- `private_port` - The Private port to translate to. | ||
- `protocol` - The Protocol the rule should apply to. Possible values are both, tcp and udp. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package scaleway | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/scaleway/scaleway-sdk-go/api/vpcgw/v1" | ||
"github.com/scaleway/scaleway-sdk-go/scw" | ||
) | ||
|
||
func dataSourceScalewayVPCPublicGatewayPATRule() *schema.Resource { | ||
// Generate datasource schema from resource | ||
dsSchema := datasourceSchemaFromResourceSchema(resourceScalewayVPCPublicGatewayPATRule().Schema) | ||
|
||
dsSchema["pat_rule_id"] = &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "The ID of the public gateway PAT rule", | ||
ValidateFunc: validationUUIDorUUIDWithLocality(), | ||
} | ||
|
||
// Set 'Optional' schema elements | ||
addOptionalFieldsToSchema(dsSchema, "zone") | ||
|
||
return &schema.Resource{ | ||
Schema: dsSchema, | ||
ReadContext: dataSourceScalewayVPCPublicGatewayPATRuleRead, | ||
} | ||
} | ||
|
||
func dataSourceScalewayVPCPublicGatewayPATRuleRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
vpcgwAPI, zone, err := vpcgwAPIWithZone(d, meta) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
patRuleIDRaw := d.Get("pat_rule_id") | ||
|
||
zonedID := datasourceNewZonedID(patRuleIDRaw, zone) | ||
d.SetId(zonedID) | ||
_ = d.Set("pat_rule_id", zonedID) | ||
|
||
// check if pat rule exist | ||
_, err = vpcgwAPI.GetPATRule(&vpcgw.GetPATRuleRequest{ | ||
PatRuleID: expandID(patRuleIDRaw), | ||
Zone: zone, | ||
}, scw.WithContext(ctx)) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
return resourceScalewayVPCPublicGatewayPATRuleRead(ctx, d, meta) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package scaleway | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccScalewayDataSourceVPCPublicGatewayPATRule_Basic(t *testing.T) { | ||
tt := NewTestTools(t) | ||
defer tt.Cleanup() | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProviderFactories: tt.ProviderFactories, | ||
CheckDestroy: testAccCheckScalewayVPCPublicGatewayPATRuleDestroy(tt), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: ` | ||
resource scaleway_vpc_public_gateway pg01 { | ||
type = "VPC-GW-S" | ||
} | ||
resource scaleway_vpc_public_gateway_dhcp dhcp01 { | ||
subnet = "192.168.1.0/24" | ||
} | ||
resource scaleway_vpc_private_network pn01 { | ||
name = "pn_test_network" | ||
} | ||
`, | ||
}, | ||
{ | ||
Config: ` | ||
resource scaleway_vpc_public_gateway pg01 { | ||
type = "VPC-GW-S" | ||
} | ||
resource scaleway_vpc_public_gateway_dhcp dhcp01 { | ||
subnet = "192.168.1.0/24" | ||
} | ||
resource scaleway_vpc_private_network pn01 { | ||
name = "pn_test_network" | ||
} | ||
resource scaleway_vpc_gateway_network gn01 { | ||
gateway_id = scaleway_vpc_public_gateway.pg01.id | ||
private_network_id = scaleway_vpc_private_network.pn01.id | ||
dhcp_id = scaleway_vpc_public_gateway_dhcp.dhcp01.id | ||
depends_on = [scaleway_vpc_private_network.pn01] | ||
cleanup_dhcp = true | ||
enable_masquerade = true | ||
} | ||
resource scaleway_vpc_public_gateway_pat_rule main { | ||
gateway_id = scaleway_vpc_public_gateway.pg01.id | ||
private_ip = scaleway_vpc_public_gateway_dhcp.dhcp01.address | ||
private_port = 42 | ||
public_port = 42 | ||
protocol = "both" | ||
depends_on = [scaleway_vpc_gateway_network.gn01, scaleway_vpc_private_network.pn01] | ||
} | ||
data "scaleway_vpc_public_gateway_pat_rule" "main" { | ||
pat_rule_id = "${scaleway_vpc_public_gateway_pat_rule.main.id}" | ||
} | ||
`, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckScalewayVPCPublicGatewayPATRuleExists( | ||
tt, | ||
"scaleway_vpc_public_gateway_pat_rule.main", | ||
), | ||
resource.TestCheckResourceAttrPair( | ||
"data.scaleway_vpc_public_gateway_pat_rule.main", "gateway_id", | ||
"scaleway_vpc_public_gateway_pat_rule.main", "gateway_id"), | ||
resource.TestCheckResourceAttrPair( | ||
"data.scaleway_vpc_public_gateway_pat_rule.main", "private_ip", | ||
"scaleway_vpc_public_gateway_pat_rule.main", "private_ip"), | ||
resource.TestCheckResourceAttrPair( | ||
"data.scaleway_vpc_public_gateway_pat_rule.main", "created_at", | ||
"scaleway_vpc_public_gateway_pat_rule.main", "created_at"), | ||
resource.TestCheckResourceAttrPair( | ||
"data.scaleway_vpc_public_gateway_pat_rule.main", "updated_at", | ||
"scaleway_vpc_public_gateway_pat_rule.main", "updated_at"), | ||
resource.TestCheckResourceAttrPair( | ||
"data.scaleway_vpc_public_gateway_pat_rule.main", "protocol", | ||
"scaleway_vpc_public_gateway_pat_rule.main", "protocol"), | ||
resource.TestCheckResourceAttrPair( | ||
"data.scaleway_vpc_public_gateway_pat_rule.main", "public_port", | ||
"scaleway_vpc_public_gateway_pat_rule.main", "public_port"), | ||
resource.TestCheckResourceAttrPair( | ||
"data.scaleway_vpc_public_gateway_pat_rule.main", "private_port", | ||
"scaleway_vpc_public_gateway_pat_rule.main", "private_port"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.