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

HCCP91 hvn routes data source #115

Merged
merged 12 commits into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from 10 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
49 changes: 49 additions & 0 deletions docs/data-sources/hvn_route.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "hcp_hvn_route Data Source - terraform-provider-hcp"
subcategory: ""
description: |-
The HVN route data source provides information about an existing HVN route.
---

# hcp_hvn_route (Data Source)

The HVN route data source provides information about an existing HVN route.

## Example Usage

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

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

### Required

- **destination_cidr** (String) The destination CIDR of the HVN route
- **hvn** (String) The `self_link` of the HashiCorp Virtual Network (HVN).

### 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.
- **target_link** (String) A unique URL identifying the target of the HVN route.

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

Optional:

- **default** (String)


4 changes: 4 additions & 0 deletions examples/data-sources/hcp_hvn_route/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
data "hcp_hvn_route" "example" {
hvn = var.hvn
destination_cidr = var.destination_cidr
}
9 changes: 9 additions & 0 deletions examples/data-sources/hcp_hvn_route/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
variable "hvn" {
description = "The `self_link` of the HashiCorp Virtual Network (HVN)."
type = string
}

variable "destination_cidr" {
description = "The destination CIDR of the HVN route."
type = string
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/go-openapi/strfmt v0.20.1
github.com/google/uuid v1.2.0
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/hcp-sdk-go v0.6.0
github.com/hashicorp/hcp-sdk-go v0.7.0
github.com/hashicorp/terraform-plugin-docs v0.4.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.5.0
github.com/stretchr/testify v1.7.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,8 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/hcl/v2 v2.3.0 h1:iRly8YaMwTBAKhn1Ybk7VSdzbnopghktCD031P8ggUE=
github.com/hashicorp/hcl/v2 v2.3.0/go.mod h1:d+FwDBbOLvpAM3Z6J7gPj/VoAGkNe/gm352ZhjJ/Zv8=
github.com/hashicorp/hcp-sdk-go v0.6.0 h1:vG6ZV0hOTZoYK4o/ZsqkgrhLFc3JIWG0y8l1thinQiA=
github.com/hashicorp/hcp-sdk-go v0.6.0/go.mod h1:vpV5eSGZVmfCFcksi4AH8d/QSybuyLSH5UQcwmnRUQk=
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/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
30 changes: 30 additions & 0 deletions internal/clients/hvn_route.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package clients

import (
"context"

"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"
)

// 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
listHVNRoutesParams.HvnLocationOrganizationID = loc.OrganizationID
listHVNRoutesParams.HvnLocationProjectID = loc.ProjectID
listHVNRoutesParams.Destination = &destination
listHVNRoutesParams.TargetID = &targetID
listHVNRoutesParams.TargetType = &targetType

listHVNRoutesResponse, err := client.Network.ListHVNRoutes(listHVNRoutesParams, nil)
if err != nil {
return nil, err
}

return listHVNRoutesResponse.Payload.Routes, nil
}
101 changes: 101 additions & 0 deletions internal/provider/data_source_hvn_route.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package provider

import (
"context"
"log"

sharedmodels "github.com/hashicorp/hcp-sdk-go/clients/cloud-shared/v1/models"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-hcp/internal/clients"
)

func dataSourceHVNRoute() *schema.Resource {
return &schema.Resource{
Description: "The HVN route data source provides information about an existing HVN route.",
ReadContext: dataSourceHVNRouteRead,
Timeouts: &schema.ResourceTimeout{
Default: &hvnRouteDefaultTimeout,
},
Schema: map[string]*schema.Schema{
// Required inputs
"hvn": {
Description: "The `self_link` of the HashiCorp Virtual Network (HVN).",
Type: schema.TypeString,
Required: true,
},
"destination_cidr": {
nombiezinja marked this conversation as resolved.
Show resolved Hide resolved
Description: "The destination CIDR of the HVN route",
Type: schema.TypeString,
nombiezinja marked this conversation as resolved.
Show resolved Hide resolved
Required: true,
},
// Computed outputs
"self_link": {
Description: "A unique URL identifying the HVN route.",
Type: schema.TypeString,
Computed: true,
},
"target_link": {
Description: "A unique URL identifying the target of the HVN route.",
Type: schema.TypeString,
nombiezinja marked this conversation as resolved.
Show resolved Hide resolved
Computed: true,
},
"state": {
Description: "The state of the HVN route.",
Type: schema.TypeString,
Computed: true,
},
"created_at": {
Description: "The time that the HVN route was created.",
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceHVNRouteRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*clients.Client)

hvn := d.Get("hvn").(string)
var hvnLink *sharedmodels.HashicorpCloudLocationLink

hvnLink, err := parseLinkURL(hvn, HvnResourceType)
if err != nil {
return diag.FromErr(err)
}

loc := &sharedmodels.HashicorpCloudLocationLocation{
OrganizationID: client.Config.OrganizationID,
ProjectID: client.Config.ProjectID,
}
destination := d.Get("destination_cidr").(string)

log.Printf("[INFO] Reading HVN route for HVN (%s) with destination_cidr=%s ", hvn, destination)
route, err := clients.ListHVNRoutes(ctx, client, hvnLink.ID, destination, "", "", loc)
if err != nil {
return diag.Errorf("unable to retrieve HVN route for HVN (%s) with destination_cidr=%s: %v",
hvn, destination, err)
}

// ListHVNRoutes call should return 1 and only 1 HVN route.
if len(route) > 1 {
return diag.Errorf("Unexpected number of HVN routes returned for destination_cidr=%s: %d", destination, len(route))
nombiezinja marked this conversation as resolved.
Show resolved Hide resolved
}
if len(route) == 0 {
return diag.Errorf("No HVN route found for destionation_cidr=%s", destination)
}

link := newLink(loc, HVNRouteResourceType, route[0].ID)
url, err := linkURL(link)
if err != nil {
return diag.FromErr(err)
}
d.SetId(url)

if err := setHVNRouteResourceData(d, route[0], loc); err != nil {
return diag.FromErr(err)
}

return nil
}
3 changes: 3 additions & 0 deletions internal/provider/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ const (
// TgwAttachmentResourceType is the resource type of a TGW attachment
TgwAttachmentResourceType = "hashicorp.network.tgw-attachment"

// HVNRouteResourceType is the resource type of an HVN route
HVNRouteResourceType = "hashicorp.network.route"

// ConsulSnapshotResourceType is the resource type of a Consul snapshot
ConsulSnapshotResourceType = "hashicorp.consul.snapshot"

Expand Down
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func New() func() *schema.Provider {
"hcp_consul_cluster": dataSourceConsulCluster(),
"hcp_consul_versions": dataSourceConsulVersions(),
"hcp_hvn": dataSourceHvn(),
"hcp_hvn_route": dataSourceHVNRoute(),
"hcp_vault_cluster": dataSourceVaultCluster(),
},
ResourcesMap: map[string]*schema.Resource{
Expand Down
51 changes: 51 additions & 0 deletions internal/provider/resource_hvn_route.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package provider

import (
"time"

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/schema"
)

var hvnRouteDefaultTimeout = time.Minute * 1

func setHVNRouteResourceData(d *schema.ResourceData, route *networkmodels.HashicorpCloudNetwork20200907HVNRoute,
loc *sharedmodels.HashicorpCloudLocationLocation) error {

// Set self_link for the HVN route.
link := newLink(loc, HVNRouteResourceType, route.ID)
selfLink, err := linkURL(link)
if err != nil {
return err
}

if err := d.Set("self_link", selfLink); err != nil {
return err
}

// Set self_link identifying the target of the HVN route.
hvnLink := newLink(loc, route.Target.HvnConnection.Type, route.Target.HvnConnection.ID)
targetLink, err := linkURL(hvnLink)
if err != nil {
return err
}

if err := d.Set("target_link", targetLink); err != nil {
return err
}

if err := d.Set("destination_cidr", route.Destination); err != nil {
return err
}

if err := d.Set("state", route.State); err != nil {
return err
}

if err := d.Set("created_at", route.CreatedAt.String()); err != nil {
return err
}

return nil
}