Skip to content

Commit

Permalink
Implement PeeringConnection read to VPC
Browse files Browse the repository at this point in the history
Implement reading peering connection and mapping the values for VPC as a datasource
  • Loading branch information
Khyme committed Jan 19, 2024
1 parent 990316f commit 7b33e8b
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 122 deletions.
11 changes: 5 additions & 6 deletions docs/data-sources/vpcs.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Read-Only:
- `error_message` (String)
- `id` (Number)
- `name` (String)
- `peering_connections` (Attributes List) (see [below for nested schema](#nestedatt--vpcs--peering_connections))
- `peering_connections` (List of Object) (see [below for nested schema](#nestedatt--vpcs--peering_connections))
- `project_id` (String)
- `provisioned_id` (String)
- `region_code` (String)
Expand All @@ -43,17 +43,16 @@ Read-Only:
Read-Only:

- `error_message` (String)
- `id` (Number)
- `peer_vpc` (Attributes List) (see [below for nested schema](#nestedatt--vpcs--peering_connections--peer_vpc))
- `peer_vpc` (Object) (see [below for nested schema](#nestedobjatt--vpcs--peering_connections--peer_vpc))
- `status` (String)
- `vpc_id` (Number)
- `vpc_id` (String)

<a id="nestedatt--vpcs--peering_connections--peer_vpc"></a>
<a id="nestedobjatt--vpcs--peering_connections--peer_vpc"></a>
### Nested Schema for `vpcs.peering_connections.peer_vpc`

Read-Only:

- `account_id` (String)
- `cidr` (String)
- `id` (Number)
- `id` (String)
- `region_code` (String)
12 changes: 0 additions & 12 deletions internal/client/queries/create_vpc.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,6 @@ mutation CreateVPC($projectId: ID!, $name: String!, $cidr: String!, $regionCode:
name
created
updated
# peeringConnections {
# id
# vpcId
# peerVpc {
# id
# accountId
# regionCode
# cidr
# }
# errorMessage
# status
# }
errorMessage
status
regionCode
Expand Down
1 change: 0 additions & 1 deletion internal/client/queries/vpc_by_id.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
created
updated
# peeringConnections {
# id
# vpcId
# peerVpc {
# id
Expand Down
1 change: 0 additions & 1 deletion internal/client/queries/vpc_by_name.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ query GetVPCByName($projectId: ID!, $name: String!) {
created
updated
# peeringConnections {
# id
# vpcId
# peerVpc {
# id
Expand Down
25 changes: 12 additions & 13 deletions internal/client/queries/vpcs.graphql
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
query GetAllVpcs($projectId: ID!) {
query GetAllVPCs($projectId: ID!) {
getAllVpcs(projectId: $projectId) {
id
projectId
cidr
name
created
updated
# peeringConnections {
# id
# vpcId
# peerVpc {
# id
# accountId
# regionCode
# cidr
# }
# errorMessage
# status
# }
peeringConnections {
vpcId
peerVpc {
id
accountId
regionCode
cidr
}
errorMessage
status
}
errorMessage
status
regionCode
Expand Down
10 changes: 5 additions & 5 deletions internal/client/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ type VPC struct {
}

type PeeringConnection struct {
ID string `json:"id"`
VPCID string `json:"vpcId"`
Status string `json:"status"`
ErrorMessage string `json:"errorMessage"`
PeerVPCs []*PeerVPC `json:"peerVPC"`
ID string `json:"id"`
VPCID string `json:"vpcId"`
Status string `json:"status"`
ErrorMessage string `json:"errorMessage"`
PeerVPCs *PeerVPC `json:"peerVPC"`
}

type PeerVPC struct {
Expand Down
164 changes: 80 additions & 84 deletions internal/provider/vpcs_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strconv"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand All @@ -23,43 +24,67 @@ func NewVpcsDataSource() datasource.DataSource {
return &vpcsDataSource{}
}

var (
PeerVpcDSType = map[string]attr.Type{
"id": types.StringType,
"cidr": types.StringType,
"account_id": types.StringType,
"region_code": types.StringType,
}

PeeringConnectionsDSType = types.ObjectType{
AttrTypes: map[string]attr.Type{
"vpc_id": types.StringType,
"status": types.StringType,
"error_message": types.StringType,
"peer_vpc": types.ObjectType{
AttrTypes: map[string]attr.Type{
"id": types.StringType,
"cidr": types.StringType,
"account_id": types.StringType,
"region_code": types.StringType,
},
},
},
}
)

// vpcsDataSource is the data source implementation.
type vpcsDataSource struct {
client *tsClient.Client
}

// vpcsDataSourceModel maps the data source schema data.
type vpcsDataSourceModel struct {
Vpcs []vpcDataSourceModel `tfsdk:"vpcs"`
Vpcs []vpcDSModel `tfsdk:"vpcs"`
// following is a placeholder, required by terraform to run test suite
ID types.String `tfsdk:"id"`
}

// vpcDataSourceModel maps vpcs schema data.
type vpcDataSourceModel struct {
ID types.Int64 `tfsdk:"id"`
ProvisionedID types.String `tfsdk:"provisioned_id"`
ProjectID types.String `tfsdk:"project_id"`
CIDR types.String `tfsdk:"cidr"`
Name types.String `tfsdk:"name"`
RegionCode types.String `tfsdk:"region_code"`
Status types.String `tfsdk:"status"`
ErrorMessage types.String `tfsdk:"error_message"`
Created types.String `tfsdk:"created"`
Updated types.String `tfsdk:"updated"`
PeeringConnections []*peeringConnectionModel `tfsdk:"peering_connections"`
type vpcDSModel struct {
ID types.Int64 `tfsdk:"id"`
ProvisionedID types.String `tfsdk:"provisioned_id"`
ProjectID types.String `tfsdk:"project_id"`
CIDR types.String `tfsdk:"cidr"`
Name types.String `tfsdk:"name"`
RegionCode types.String `tfsdk:"region_code"`
Status types.String `tfsdk:"status"`
ErrorMessage types.String `tfsdk:"error_message"`
Created types.String `tfsdk:"created"`
Updated types.String `tfsdk:"updated"`
PeeringConnections []peeringConnectionDSModel `tfsdk:"peering_connections"`
}

type peeringConnectionModel struct {
ID types.Int64 `tfsdk:"id"`
VpcID types.Int64 `tfsdk:"vpc_id"`
Status types.String `tfsdk:"status"`
ErrorMessage types.String `tfsdk:"error_message"`
PeerVpcs []*peerVpcModel `tfsdk:"peer_vpc"`
type peeringConnectionDSModel struct {
VpcID types.String `tfsdk:"vpc_id"`
Status types.String `tfsdk:"status"`
ErrorMessage types.String `tfsdk:"error_message"`
PeerVpcs types.Object `tfsdk:"peer_vpc"`
}

type peerVpcModel struct {
ID types.Int64 `tfsdk:"id"`
type peerVpcDSModel struct {
ID types.String `tfsdk:"id"`
CIDR types.String `tfsdk:"cidr"`
AccountID types.String `tfsdk:"account_id"`
RegionCode types.String `tfsdk:"region_code"`
Expand Down Expand Up @@ -89,50 +114,41 @@ func (d *vpcsDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
resp.Diagnostics.AddError("Unable to Convert Vpc ID", err.Error())
return
}
vpcState := vpcDataSourceModel{
vpcState := vpcDSModel{
ID: types.Int64Value(vpcId),
Name: types.StringValue(vpc.Name),
ProvisionedID: types.StringValue(vpc.ProvisionedID),
ProjectID: types.StringValue(vpc.ProjectID),
CIDR: types.StringValue(vpc.CIDR),
RegionCode: types.StringValue(vpc.RegionCode),
Status: types.StringValue(vpc.Status),
ErrorMessage: types.StringValue(vpc.ErrorMessage),
Created: types.StringValue(vpc.Created),
Updated: types.StringValue(vpc.Updated),
RegionCode: types.StringValue(vpc.RegionCode),
Created: types.StringValue(vpc.Created),
}

for _, peeringConn := range vpc.PeeringConnections {
peeringConnID, err := strconv.ParseInt(peeringConn.ID, 10, 64)
if err != nil {
resp.Diagnostics.AddError("Unable to Convert Vpc ID", err.Error())
return
}
peeringConnVpcID, err := strconv.ParseInt(peeringConn.VPCID, 10, 64)
if err != nil {
resp.Diagnostics.AddError("Unable to Convert Vpc ID", err.Error())
return
}
peerConn := &peeringConnectionModel{
ID: types.Int64Value(peeringConnID),
VpcID: types.Int64Value(peeringConnVpcID),
Status: types.StringValue(peeringConn.Status),
ErrorMessage: types.StringValue(peeringConn.ErrorMessage),
}
for _, peerVpc := range peeringConn.PeerVPCs {
peerVpcId, err := strconv.ParseInt(peerVpc.ID, 10, 64)
if err != nil {
resp.Diagnostics.AddError("Unable to Convert Vpc ID", err.Error())
return
if len(vpc.PeeringConnections) > 0 {
var pcms []peeringConnectionDSModel
for _, pc := range vpc.PeeringConnections {
var pcm peeringConnectionDSModel
if pc.ErrorMessage != "" {
pcm.ErrorMessage = types.StringValue(pc.ErrorMessage)
}
peerConn.PeerVpcs = append(peerConn.PeerVpcs, &peerVpcModel{
ID: types.Int64Value(peerVpcId),
AccountID: types.StringValue(peerVpc.AccountID),
CIDR: types.StringValue(peerVpc.CIDR),
RegionCode: types.StringValue(peerVpc.RegionCode),
pcm.VpcID = types.StringValue(pc.VPCID)
pcm.Status = types.StringValue(pc.Status)
peerVpcs, errDiag := types.ObjectValueFrom(ctx, PeerVpcDSType, peerVpcDSModel{
ID: types.StringValue(pc.PeerVPCs.ID),
AccountID: types.StringValue(pc.PeerVPCs.AccountID),
CIDR: types.StringValue(pc.PeerVPCs.CIDR),
RegionCode: types.StringValue(pc.PeerVPCs.RegionCode),
})
if errDiag.HasError() {
resp.Diagnostics.Append(errDiag...)
}
pcm.PeerVpcs = peerVpcs
pcms = append(pcms, pcm)
}
vpcState.PeeringConnections = append(vpcState.PeeringConnections, peerConn)
vpcState.PeeringConnections = pcms
}
state.Vpcs = append(state.Vpcs, vpcState)
}
Expand Down Expand Up @@ -196,39 +212,19 @@ func (d *vpcsDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, r
"updated": schema.StringAttribute{
Computed: true,
},
"peering_connections": schema.ListNestedAttribute{
"peering_connections": schema.ListAttribute{
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.Int64Attribute{
Computed: true,
},
"vpc_id": schema.Int64Attribute{
Computed: true,
},
"status": schema.StringAttribute{
Computed: true,
},
"error_message": schema.StringAttribute{
Computed: true,
},
"peer_vpc": schema.ListNestedAttribute{
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.Int64Attribute{
Computed: true,
},
"cidr": schema.StringAttribute{
Computed: true,
},
"region_code": schema.StringAttribute{
Computed: true,
},
"account_id": schema.StringAttribute{
Computed: true,
},
},
ElementType: types.ObjectType{
AttrTypes: map[string]attr.Type{
"vpc_id": types.StringType,
"status": types.StringType,
"error_message": types.StringType,
"peer_vpc": types.ObjectType{
AttrTypes: map[string]attr.Type{
"id": types.StringType,
"cidr": types.StringType,
"region_code": types.StringType,
"account_id": types.StringType,
},
},
},
Expand Down
28 changes: 28 additions & 0 deletions internal/provider/vpcs_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package provider

import (
"testing"

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

func TestVPCDataSource(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Read testing
{
Config: getVPCConfig(t, config.WithName("data-source-test").WithCIDR("10.0.0.0/21").WithRegionCode("us-east-1")),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.timescale_vpcs.data_source", "id"),
resource.TestCheckResourceAttrSet("data.timescale_vpcs.data_source", "name"),
resource.TestCheckResourceAttrSet("data.timescale_vpcs.data_source", "region_code"),
resource.TestCheckResourceAttrSet("data.timescale_vpcs.data_source", "created"),
resource.TestCheckResourceAttrSet("data.timescale_vpcs.data_source", "cidr"),
resource.TestCheckResourceAttrSet("data.timescale_vpcs.data_source", "status"),
resource.TestCheckResourceAttrSet("data.timescale_vpcs.data_source", "project_id"),
),
},
},
})
}

0 comments on commit 7b33e8b

Please sign in to comment.