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

HCCP-91 HVN route resource and import #122

Merged
merged 28 commits into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
dcfe682
added create, scaffolded read and delete for hvn route resource
May 12, 2021
445d62b
Adds delete for HVN route resource
May 13, 2021
5dec7d5
Adds hvn route import function
May 14, 2021
ed76e54
Handle both peering and tgw attachment resource types in HVN route re…
May 15, 2021
8364bae
Regenerate docs, add example s for hvn route resource
May 17, 2021
5bbf138
Re-run go generate after adding example
May 17, 2021
b030359
ACreate hvn route function checks for target existence before proceeding
May 17, 2021
235d72e
Add peering to example for hvn route, regenerate docs
smaant May 20, 2021
35af916
Resolves comments - better logging and commenting for HVN route resource
May 18, 2021
23d8ae0
removed unnecessary validation
smaant May 19, 2021
5ba25eb
removed todos
smaant May 19, 2021
3e19e4a
removed tgw attachment from hvn route example
smaant May 19, 2021
995b810
added examples of the hvn route target
smaant May 19, 2021
4bd2115
moved hvn route creation into clients
smaant May 19, 2021
0c9c24e
simplified parsing target_link for the hvn route resource
smaant May 19, 2021
67c3270
dropped checking for hvn route existance
smaant May 19, 2021
e9c4555
fixed hvn routes import
smaant May 20, 2021
c84778b
gofmt hvn_route.go
smaant May 20, 2021
70a6596
redo hvn route import to use route ID
smaant May 20, 2021
bad98b9
go mod tidy
smaant May 20, 2021
0915176
redo hvn route datasource to use route ID
smaant May 20, 2021
e41c160
renamed hvn -> hvn_link
smaant May 20, 2021
7d21b5b
refactored WaitForHVNRouteToBeActive
smaant May 20, 2021
6e77699
unified hvn route errors/logs
smaant May 20, 2021
a39f99b
small refactoring
smaant May 20, 2021
981ce8f
improved logs
smaant May 21, 2021
0eb28ea
messages improvements
smaant May 21, 2021
48dd8e7
regenarated docs
smaant May 21, 2021
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
9 changes: 5 additions & 4 deletions docs/data-sources/hvn_route.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ The HVN route data source provides information about an existing HVN route.

```terraform
data "hcp_hvn_route" "example" {
hvn = var.hvn
destination_cidr = var.destination_cidr
hvn_link = var.hvn_link
destination_cidr = var.hvn_route_id
}
```

Expand All @@ -25,8 +25,8 @@ data "hcp_hvn_route" "example" {

### Required

- **destination_cidr** (String) The destination CIDR of the HVN route
- **hvn** (String) The `self_link` of the HashiCorp Virtual Network (HVN).
- **hvn_link** (String) The `self_link` of the HashiCorp Virtual Network (HVN).
- **hvn_route_id** (String) The HVN route ID
smaant marked this conversation as resolved.
Show resolved Hide resolved

### Optional

Expand All @@ -36,6 +36,7 @@ data "hcp_hvn_route" "example" {
### Read-Only

- **created_at** (String) The time that the HVN route was created.
- **destination_cidr** (String) The destination CIDR of the HVN route
smaant marked this conversation as resolved.
Show resolved Hide resolved
- **self_link** (String) A unique URL identifying the HVN route.
- **state** (String) The state of the HVN route.
- **target_link** (String) A unique URL identifying the target of the HVN route.
Expand Down
89 changes: 89 additions & 0 deletions docs/resources/hvn_route.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "hcp_hvn_route Resource - terraform-provider-hcp"
subcategory: ""
description: |-
The HVN route resource allows you to manage an HVN route.
---

# hcp_hvn_route (Resource)

The HVN route resource allows you to manage an HVN route.

## Example Usage
smaant marked this conversation as resolved.
Show resolved Hide resolved

```terraform
provider "aws" {
region = "us-west-2"
}

resource "hcp_hvn" "main" {
hvn_id = "main-hvn"
cloud_provider = "aws"
region = "us-west-2"
cidr_block = "172.25.16.0/20"
}

// Creating a peering and a route for it.
resource "aws_vpc" "peer" {
cidr_block = "192.168.0.0/20"
}

resource "hcp_aws_network_peering" "example" {
peering_id = "peer-example"
hvn_id = hcp_hvn.main.hvn_id
peer_vpc_id = aws_vpc.peer.id
peer_account_id = aws_vpc.peer.owner_id
peer_vpc_region = "us-west-2"
}

resource "aws_vpc_peering_connection_accepter" "peer" {
vpc_peering_connection_id = hcp_aws_network_peering.example.provider_peering_id
auto_accept = true
}

resource "hcp_hvn_route" "example-peering-route" {
hvn_link = hcp_hvn.main.self_link
hvn_route_id = "peering-route"
destination_cidr = aws_vpc.peer.cidr_block
target_link = hcp_aws_network_peering.example.self_link
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- **destination_cidr** (String) The destination CIDR of the HVN route
smaant marked this conversation as resolved.
Show resolved Hide resolved
- **hvn_link** (String) The `self_link` of the HashiCorp Virtual Network (HVN).
- **hvn_route_id** (String) The ID of the HVN route.
- **target_link** (String) A unique URL identifying the target of the HVN route. Examples of the target: [`aws_network_peering`](aws_network_peering.md), [`aws_transit_gateway_attachment`](aws_transit_gateway_attachment.md)
smaant marked this conversation as resolved.
Show resolved Hide resolved

### Optional

- **id** (String) The ID of this resource.
- **timeouts** (Block, Optional) (see [below for nested schema](#nestedblock--timeouts))

### Read-Only

- **created_at** (String) The time that the HVN route was created.
- **self_link** (String) A unique URL identifying the HVN route.
- **state** (String) The state of the HVN route.

<a id="nestedblock--timeouts"></a>
### Nested Schema for `timeouts`

Optional:

- **create** (String)
- **default** (String)

## Import

Import is supported using the following syntax:

```shell
# The import ID is {hvn_id}:{hvn_route_id}
terraform import hcp_hvn_route.example main-hvn:example-hvn-route
```
4 changes: 2 additions & 2 deletions examples/data-sources/hcp_hvn_route/data-source.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
data "hcp_hvn_route" "example" {
hvn = var.hvn
destination_cidr = var.destination_cidr
hvn_link = var.hvn_link
destination_cidr = var.hvn_route_id
}
6 changes: 3 additions & 3 deletions examples/data-sources/hcp_hvn_route/variables.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
variable "hvn" {
variable "hvn_link" {
description = "The `self_link` of the HashiCorp Virtual Network (HVN)."
type = string
}

variable "destination_cidr" {
description = "The destination CIDR of the HVN route."
variable "hvn_route_id" {
description = "The HVN route ID."
smaant marked this conversation as resolved.
Show resolved Hide resolved
type = string
}
2 changes: 2 additions & 0 deletions examples/resources/hcp_hvn_route/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# The import ID is {hvn_id}:{hvn_route_id}
terraform import hcp_hvn_route.example main-hvn:example-hvn-route
35 changes: 35 additions & 0 deletions examples/resources/hcp_hvn_route/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
provider "aws" {
nombiezinja marked this conversation as resolved.
Show resolved Hide resolved
region = "us-west-2"
}

resource "hcp_hvn" "main" {
hvn_id = "main-hvn"
cloud_provider = "aws"
region = "us-west-2"
cidr_block = "172.25.16.0/20"
}

// Creating a peering and a route for it.
resource "aws_vpc" "peer" {
cidr_block = "192.168.0.0/20"
}

resource "hcp_aws_network_peering" "example" {
peering_id = "peer-example"
hvn_id = hcp_hvn.main.hvn_id
peer_vpc_id = aws_vpc.peer.id
peer_account_id = aws_vpc.peer.owner_id
peer_vpc_region = "us-west-2"
}

resource "aws_vpc_peering_connection_accepter" "peer" {
vpc_peering_connection_id = hcp_aws_network_peering.example.provider_peering_id
auto_accept = true
}

resource "hcp_hvn_route" "example-peering-route" {
hvn_link = hcp_hvn.main.self_link
hvn_route_id = "peering-route"
destination_cidr = aws_vpc.peer.cidr_block
target_link = hcp_aws_network_peering.example.self_link
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/google/uuid v1.2.0
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/hcl/v2 v2.8.2 // indirect
github.com/hashicorp/hcp-sdk-go v0.7.0
github.com/hashicorp/hcp-sdk-go v0.8.0
github.com/hashicorp/terraform-exec v0.13.3 // indirect
github.com/hashicorp/terraform-plugin-docs v0.4.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.5.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,8 @@ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
github.com/hashicorp/hcl/v2 v2.3.0/go.mod h1:d+FwDBbOLvpAM3Z6J7gPj/VoAGkNe/gm352ZhjJ/Zv8=
github.com/hashicorp/hcl/v2 v2.8.2 h1:wmFle3D1vu0okesm8BTLVDyJ6/OL9DCLUwn0b2OptiY=
github.com/hashicorp/hcl/v2 v2.8.2/go.mod h1:bQTN5mpo+jewjJgh8jr0JUguIi7qPHUF6yIfAEN3jqY=
github.com/hashicorp/hcp-sdk-go v0.7.0 h1:OtbcR/rMBlfK5BLowHIPe0HJtb0rEs8FyRAzS+xH9vI=
github.com/hashicorp/hcp-sdk-go v0.7.0/go.mod h1:M+kmFj0s4KWNA5GVOgLhNtCTu3ypTR+QjWYIMgedA5Q=
github.com/hashicorp/hcp-sdk-go v0.8.0 h1:P7mMk2h87BYJ6dk851pD3WvnuXa17hxvutA5slxCWGU=
github.com/hashicorp/hcp-sdk-go v0.8.0/go.mod h1:M+kmFj0s4KWNA5GVOgLhNtCTu3ypTR+QjWYIMgedA5Q=
github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/terraform-exec v0.12.0/go.mod h1:SGhto91bVRlgXQWcJ5znSz+29UZIa8kpBbkGwQ+g9E8=
Expand Down
124 changes: 124 additions & 0 deletions internal/clients/hvn_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,68 @@ package clients

import (
"context"
"fmt"
"log"
"time"

"github.com/hashicorp/hcp-sdk-go/clients/cloud-network/preview/2020-09-07/client/network_service"
networkmodels "github.com/hashicorp/hcp-sdk-go/clients/cloud-network/preview/2020-09-07/models"
sharedmodels "github.com/hashicorp/hcp-sdk-go/clients/cloud-shared/v1/models"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

// CreateHVNRoute creates a new HVN route
func CreateHVNRoute(ctx context.Context, client *Client,
id string,
hvn *sharedmodels.HashicorpCloudLocationLink,
destination string,
target *sharedmodels.HashicorpCloudLocationLink,
location *sharedmodels.HashicorpCloudLocationLocation) (*networkmodels.HashicorpCloudNetwork20200907CreateHVNRouteResponse, error) {

hvnRouteParams := network_service.NewCreateHVNRouteParams()
hvnRouteParams.Context = ctx
hvnRouteParams.HvnLocationOrganizationID = location.OrganizationID
hvnRouteParams.HvnLocationProjectID = location.ProjectID
hvnRouteParams.HvnID = hvn.ID
hvnRouteParams.Body = &networkmodels.HashicorpCloudNetwork20200907CreateHVNRouteRequest{
Destination: destination,
Hvn: hvn,
ID: id,
Target: &networkmodels.HashicorpCloudNetwork20200907HVNRouteTarget{
HvnConnection: target,
},
}
log.Printf("[INFO] Creating HVN route for HVN (%s) with destination CIDR %s", hvn.ID, destination)
hvnRouteResp, err := client.Network.CreateHVNRoute(hvnRouteParams, nil)
if err != nil {
return nil, fmt.Errorf("unable to create HVN route for HVN (%s) with destination CIDR %s: %v", hvn.ID, destination, err)
}

return hvnRouteResp.Payload, nil
}

// GetHVNRoute returns specific HVN route by its ID
func GetHVNRoute(ctx context.Context, client *Client, hvnID, routeID string, loc *sharedmodels.HashicorpCloudLocationLocation) (*networkmodels.HashicorpCloudNetwork20200907HVNRoute, error) {
getHVNRouteParams := network_service.NewGetHVNRouteParams()
getHVNRouteParams.Context = ctx
getHVNRouteParams.HvnID = hvnID
getHVNRouteParams.ID = routeID
getHVNRouteParams.HvnLocationOrganizationID = loc.OrganizationID
getHVNRouteParams.HvnLocationProjectID = loc.ProjectID

getHVNRouteResponse, err := client.Network.GetHVNRoute(getHVNRouteParams, nil)
if err != nil {
return nil, err
}

return getHVNRouteResponse.Payload.Route, nil
}

// ListHVNRoutes lists the routes for an HVN.
func ListHVNRoutes(ctx context.Context, client *Client, hvnID string,
destination string, targetID string, targetType string,
loc *sharedmodels.HashicorpCloudLocationLocation) ([]*networkmodels.HashicorpCloudNetwork20200907HVNRoute, error) {

listHVNRoutesParams := network_service.NewListHVNRoutesParams()
listHVNRoutesParams.Context = ctx
listHVNRoutesParams.HvnID = hvnID
Expand All @@ -28,3 +80,75 @@ func ListHVNRoutes(ctx context.Context, client *Client, hvnID string,

return listHVNRoutesResponse.Payload.Routes, nil
}

// DeleteSnapshotByID deletes an HVN route by its ID
func DeleteHVNRouteByID(ctx context.Context, client *Client, hvnID string,
hvnRouteID string, loc *sharedmodels.HashicorpCloudLocationLocation) (*networkmodels.HashicorpCloudNetwork20200907DeleteHVNRouteResponse, error) {

deleteHVNRouteParams := network_service.NewDeleteHVNRouteParams()

deleteHVNRouteParams.Context = ctx
deleteHVNRouteParams.ID = hvnRouteID
deleteHVNRouteParams.HvnID = hvnID
deleteHVNRouteParams.HvnLocationOrganizationID = loc.OrganizationID
deleteHVNRouteParams.HvnLocationProjectID = loc.ProjectID

deleteHVNRouteResponse, err := client.Network.DeleteHVNRoute(deleteHVNRouteParams, nil)
if err != nil {
return nil, err
}

return deleteHVNRouteResponse.Payload, nil
}

const (
// HvnRouteStateCreating is the CREATING state of an HVN route
HvnRouteStateCreating = string(networkmodels.HashicorpCloudNetwork20200907HVNRouteStateCREATING)

// HvnRouteStateActive is the ACTIVE state of an HVN route
HvnRouteStateActive = string(networkmodels.HashicorpCloudNetwork20200907HVNRouteStateACTIVE)

// HvnRouteStatePending is the PENDING state of an HVN route
HvnRouteStatePending = string(networkmodels.HashicorpCloudNetwork20200907HVNRouteStatePENDING)
)

// hvnRouteRefreshState refreshes the state of the HVN route
func hvnRouteRefreshState(ctx context.Context, client *Client, hvnID, routeID string, loc *sharedmodels.HashicorpCloudLocationLocation) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
route, err := GetHVNRoute(ctx, client, hvnID, routeID, loc)
if err != nil {
return nil, "", err
}

return route, string(route.State), nil
}
}

// WaitForHVNRouteToBeActive will poll the GET HVN route endpoint until
// the state is ACTIVE, ctx is canceled, or an error occurs.
func WaitForHVNRouteToBeActive(ctx context.Context, client *Client,
hvnID string,
routeID string,
loc *sharedmodels.HashicorpCloudLocationLocation,
timeout time.Duration) (*networkmodels.HashicorpCloudNetwork20200907HVNRoute, error) {

stateChangeConf := resource.StateChangeConf{
Pending: []string{
HvnRouteStateCreating,
HvnRouteStatePending,
},
Target: []string{
HvnRouteStateActive,
},
Refresh: hvnRouteRefreshState(ctx, client, hvnID, routeID, loc),
Timeout: timeout,
PollInterval: 5 * time.Second,
}

result, err := stateChangeConf.WaitForStateContext(ctx)
if err != nil {
return nil, fmt.Errorf("error waiting for the HVN route (%s) to become 'ACTIVE': %+v", routeID, err)
}

return result.(*networkmodels.HashicorpCloudNetwork20200907HVNRoute), nil
}
Loading