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

Add configurable sleep/timeout for GCP VPC peering #247

Merged
merged 5 commits into from
Dec 18, 2023
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
36 changes: 28 additions & 8 deletions cloudamqp/data_source_cloudamqp_vpc_gcp_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,41 @@ func dataSourceVpcGcpInfo() *schema.Resource {
Computed: true,
Description: "VPC network uri",
},
"sleep": {
Type: schema.TypeInt,
Optional: true,
Default: 10,
Description: "Configurable sleep in seconds between retries when reading peering",
},
"timeout": {
Type: schema.TypeInt,
Optional: true,
Default: 1800,
Description: "Configurable timeout time (seconds) before retries times out",
},
},
}
}

func dataSourceVpcGcpInfoRead(d *schema.ResourceData, meta interface{}) error {
api := meta.(*api.API)
data := make(map[string]interface{})
err := errors.New("")
log.Printf("[DEBUG] cloudamqp::data::vpc_gcp_info::request instance_id: %v, vpc_id: %v", d.Get("instance_id"), d.Get("vpc_id"))
if d.Get("instance_id") == 0 && d.Get("vpc_id") == nil {
var (
api = meta.(*api.API)
data = make(map[string]interface{})
err = errors.New("")
instance_id = d.Get("instance_id").(int)
vpc_id = d.Get("vpc_id").(string)
sleep = d.Get("sleep").(int)
timeout = d.Get("timeout").(int)
)

log.Printf("[DEBUG] cloudamqp::data::vpc_gcp_info::request instance_id: %v, vpc_id: %v",
instance_id, vpc_id)
if instance_id == 0 && vpc_id == "" {
return errors.New("you need to specify either instance_id or vpc_id")
} else if d.Get("instance_id") != 0 {
data, err = api.ReadVpcGcpInfo(d.Get("instance_id").(int))
} else if instance_id != 0 {
data, err = api.ReadVpcGcpInfo(instance_id, sleep, timeout)
} else if d.Get("vpc_id") != nil {
data, err = api.ReadVpcGcpInfoWithVpcId(d.Get("vpc_id").(string))
data, err = api.ReadVpcGcpInfoWithVpcId(vpc_id, sleep, timeout)
}

if err != nil {
Expand Down
30 changes: 23 additions & 7 deletions cloudamqp/resource_cloudamqp_vpc_gcp_peering.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ func resourceVpcGcpPeering() *schema.Resource {
Computed: true,
Description: "VPC peering auto created routes",
},
"sleep": {
Type: schema.TypeInt,
Optional: true,
Default: 10,
Description: "Configurable sleep in seconds between retries when requesting or reading peering",
},
"timeout": {
Type: schema.TypeInt,
Optional: true,
Default: 1800,
Description: "Configurable timeout time (seconds) before retries times out",
},
},
}
}
Expand All @@ -65,6 +77,8 @@ func resourceCreateVpcGcpPeering(d *schema.ResourceData, meta interface{}) error
waitOnStatus = d.Get("wait_on_peering_status").(bool)
data map[string]interface{}
err = errors.New("")
sleep = d.Get("sleep").(int)
timeout = d.Get("timeout").(int)
)

for _, k := range keys {
Expand All @@ -78,9 +92,9 @@ func resourceCreateVpcGcpPeering(d *schema.ResourceData, meta interface{}) error
if instanceID == 0 && vpcID == "" {
return errors.New("you need to specify either instance_id or vpc_id")
} else if instanceID != 0 {
data, err = api.RequestVpcGcpPeering(instanceID, params, waitOnStatus)
data, err = api.RequestVpcGcpPeering(instanceID, params, waitOnStatus, sleep, timeout)
} else if vpcID != "" {
data, err = api.RequestVpcGcpPeeringWithVpcId(vpcID, params, waitOnStatus)
data, err = api.RequestVpcGcpPeeringWithVpcId(vpcID, params, waitOnStatus, sleep, timeout)
}

if err != nil {
Expand All @@ -98,17 +112,19 @@ func resourceCreateVpcGcpPeering(d *schema.ResourceData, meta interface{}) error

func resourceReadVpcGcpPeering(d *schema.ResourceData, meta interface{}) error {
var (
api = meta.(*api.API)
data map[string]interface{}
err = errors.New("")
api = meta.(*api.API)
data map[string]interface{}
err = errors.New("")
sleep = d.Get("sleep").(int)
timeout = d.Get("timeout").(int)
)

if d.Get("instance_id") == 0 && d.Get("vpc_id") == nil {
return errors.New("you need to specify either instance_id or vpc_id")
} else if d.Get("instance_id") != 0 {
data, err = api.ReadVpcGcpPeering(d.Get("instance_id").(int), d.Id())
data, err = api.ReadVpcGcpPeering(d.Get("instance_id").(int), sleep, timeout)
} else if d.Get("vpc_id") != nil {
data, err = api.ReadVpcGcpPeeringWithVpcId(d.Get("vpc_id").(string), d.Id())
data, err = api.ReadVpcGcpPeeringWithVpcId(d.Get("vpc_id").(string), sleep, timeout)
}
log.Printf("[DEBUG] cloudamqp::vpc_gcp_peering::read data: %v", data)

Expand Down
5 changes: 5 additions & 0 deletions docs/data-sources/vpc_gcp_info.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ data "cloudamqp_vpc_gcp_info" "vpc_info" {
instance_id = cloudamqp_instance.instance.id
}
```

</details>

<details>
Expand All @@ -39,6 +40,7 @@ data "cloudamqp_vpc_gcp_info" "vpc_info" {
# instance_id = cloudamqp_instance.instance.id
}
```

</details>

## Argument reference
Expand All @@ -53,6 +55,9 @@ data "cloudamqp_vpc_gcp_info" "vpc_info" {

***Note: Added as optional in version v1.16.0 and will be required in next major version (v2.0)***

* `sleep` - (Optional) Configurable sleep time (seconds) between retries when reading peering. Default set to 10 seconds.
* `timeout` - (Optional) - Configurable timeout time (seconds) before retries times out. Default set to 1800 seconds.

## Attributes reference

All attributes reference are computed
Expand Down
23 changes: 16 additions & 7 deletions docs/resources/vpc_gcp_peering.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ description: |-
This resouce creates a VPC peering configuration for the CloudAMQP instance. The configuration will connect to another VPC network hosted on Google Cloud Platform (GCP). See the [GCP documentation](https://cloud.google.com/vpc/docs/using-vpc-peering) for more information on how to create the VPC peering configuration.

~> **Note:** Creating a VPC peering will automatically add firewall rules for the peered subnet.

<details>
<summary>
<i>Default VPC peering firewall rule</i>
Expand All @@ -23,6 +24,7 @@ rules {
services = ["AMQP", "AMQPS", "HTTPS", "STREAM", "STREAM_SSL"]
}
```

</details>

Pricing is available at [cloudamqp.com](https://www.cloudamqp.com/plans.html).
Expand Down Expand Up @@ -64,6 +66,7 @@ resource "cloudamqp_vpc_gcp_peering" "vpc_peering_request" {
peer_network_uri = "https://www.googleapis.com/compute/v1/projects/<PROJECT-NAME>/global/networks/<NETWORK-NAME>"
}
```

</details>

<details>
Expand Down Expand Up @@ -111,6 +114,7 @@ resource "cloudamqp_vpc_gcp_peering" "vpc_peering_request" {
peer_network_uri = "https://www.googleapis.com/compute/v1/projects/<PROJECT-NAME>/global/networks/<NETWORK-NAME>"
}
```

</details>

<details>
Expand All @@ -121,6 +125,7 @@ resource "cloudamqp_vpc_gcp_peering" "vpc_peering_request" {
</summary>

Default peering request, no need to set `wait_on_peering_status`. It's default set to false and will not wait on peering status.

```hcl
resource "cloudamqp_vpc_gcp_peering" "vpc_peering_request" {
vpc_id = cloudamqp_vpc.vpc.id
Expand All @@ -129,6 +134,7 @@ resource "cloudamqp_vpc_gcp_peering" "vpc_peering_request" {
```

Peering request and waiting for peering status.

```hcl
resource "cloudamqp_vpc_gcp_peering" "vpc_peering_request" {
vpc_id = cloudamqp_vpc.vpc.id
Expand All @@ -143,19 +149,20 @@ resource "cloudamqp_vpc_gcp_peering" "vpc_peering_request" {

*Note: this resource require either `instance_id` or `vpc_id` from v1.16.0*

* `instance_id` - (Optional) The CloudAMQP instance identifier.
* `instance_id` - (Optional) The CloudAMQP instance identifier. *Deprecated from v1.16.0*

***Depreacted: Changed from required to optional in v1.16.0, will be removed in next major version (v2.0)***

* `vpc_id` - (Optional) The managed VPC identifier.

***Note: Added as optional in version v1.16.0, will be required in next major version (v2.0)***
* `vpc_id` - (Optional) The managed VPC identifier. *Available from v1.16.0*

* `peer_network_uri`- (Required) Network uri of the VPC network to which you will peer with.

* `wait_on_peering_status` - (Optional) Makes the resource wait until the peering is connected.
Default set to false. *Available from v1.28.0*

* `sleep` - (Optional) Configurable sleep time (seconds) between retries when requesting or reading
peering. Default set to 10 seconds. *Available from v1.29.0*

***Note: Added as optional in version v1.28.0. Default set to false and will not wait until the peering is done from both VPCs***
* `timeout` - (Optional) - Configurable timeout time (seconds) before retries times out. Default set
to 1800 seconds. *Available from v1.29.0*

## Attributes Reference

Expand Down Expand Up @@ -226,6 +233,7 @@ resource "cloudamqp_security_firewall" "firewall_settings" {
]
}
```

</details>

<details>
Expand Down Expand Up @@ -268,4 +276,5 @@ resource "cloudamqp_security_firewall" "firewall_settings" {
]
}
```

</details>
Loading