Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
Khyme committed Jan 18, 2024
1 parent 257b8f9 commit 2af1611
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 82 deletions.
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
137 changes: 92 additions & 45 deletions internal/provider/vpc_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,36 @@ type vpcResourceModel struct {
Created types.String `tfsdk:"created"`
Updated types.String `tfsdk:"updated"`
// PeeringConnections types.List `tfsdk:"peering_connections"`
Array types.List `tfsdk:"array"`
}

// type peeringConnectionResourceModel struct {
// ID types.Int64 `tfsdk:"id"`
// VpcID types.Int64 `tfsdk:"vpc_id"`
// Status types.String `tfsdk:"status"`
// ErrorMessage types.String `tfsdk:"error_message"`
// PeerVpcs types.List `tfsdk:"peer_vpc"`
// }
type peeringConnectionResourceModel struct {

Check failure on line 62 in internal/provider/vpc_resource.go

View workflow job for this annotation

GitHub Actions / lint

type `peeringConnectionResourceModel` is unused (unused)
ID types.Int64 `tfsdk:"id"`
VpcID types.Int64 `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"`
CIDR types.String `tfsdk:"cidr"`
AccountID types.String `tfsdk:"account_id"`
RegionCode types.String `tfsdk:"region_code"`
}

var (
PeerVpcType = types.ObjectType{
AttrTypes: map[string]attr.Type{
"id": types.Int64Type,
"cidr": types.StringType,
"account_id": types.StringType,
"region_code": types.StringType,
},
// PeerVpcType = types.ObjectType{
// AttrTypes: map[string]attr.Type{
// "id": types.Int64Type,
// "cidr": types.StringType,
// "account_id": types.StringType,
// "region_code": types.StringType,
// },
// }

PeeringConnectionsListType = types.ListType{
ElemType: PeeringConnectionsType,
}

PeeringConnectionsType = types.ObjectType{
Expand All @@ -82,7 +94,7 @@ var (
"vpc_id": types.Int64Type,
"status": types.StringType,
"error_message": types.StringType,
"peer_vpc": types.ListType{ElemType: PeerVpcType},
// "peer_vpc": types.ListType{ElemType: PeerVpcType},
},
}
)
Expand All @@ -94,17 +106,16 @@ func (r *vpcResource) Metadata(_ context.Context, req resource.MetadataRequest,

// Read refreshes the Terraform state with the latest data.
func (r *vpcResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
tflog.Trace(ctx, "VpcResource.Read")
var state vpcResourceModel
var vpc *tsClient.VPC
var err error
// Read Terraform prior state plan into the model
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)

resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}

var vpc *tsClient.VPC
var err error

if !state.Name.IsNull() {
tflog.Info(ctx, "Getting VPC by name: "+state.Name.ValueString())
vpc, err = r.client.GetVPCByName(ctx, state.Name.ValueString())
Expand All @@ -120,14 +131,50 @@ func (r *vpcResource) Read(ctx context.Context, req resource.ReadRequest, resp *
if err != nil {
resp.Diagnostics.AddError("Parse Error", "could not parse vpcID")
}
//resp.Diagnostics.AddError("Parse Error", "could not parse vpcID")
ctx = tflog.SetField(ctx, "vpc_id", vpcId)

state.ID = types.Int64Value(vpcId)
state.Created = types.StringValue(vpc.Created)
state.ProjectID = types.StringValue(vpc.ProjectID)
state.CIDR = types.StringValue(vpc.CIDR)
state.RegionCode = types.StringValue(vpc.RegionCode)
resourceModel := vpcToResource(resp.Diagnostics, vpc, state)

ips, diag := types.ListValueFrom(ctx, types.StringType, []string{"1", "2", "3"})
if diag.HasError() {
resp.Diagnostics.Append(diag...)
return
}

model := vpcResourceModel{
ID: state.ID,
ProjectID: state.ProjectID,
Created: state.Created,
RegionCode: state.RegionCode,
CIDR: state.CIDR,
Name: types.StringValue(vpc.Name),
ProvisionedID: types.StringValue(vpc.ProvisionedID),
Status: types.StringValue(vpc.Status),
ErrorMessage: types.StringValue(vpc.ErrorMessage),
Updated: types.StringValue(vpc.Updated),
Array: ips,
}
// var p []*peeringConnectionResourceModel
// p = append(p, &peeringConnectionResourceModel{
// ID: types.Int64Value(12),
// VpcID: types.Int64Value(223),
// Status: types.StringValue("aaea"),
// ErrorMessage: types.StringValue("aaeza"),
// })
// pc, diag := types.ListValueFrom(ctx, PeeringConnectionsListType, p)
// if diag.HasError() {
// resp.Diagnostics.Append(diag...)
// return
// }
// model.PeeringConnections = pc

// Save updated plan into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, resourceModel)...)
resp.Diagnostics.Append(resp.State.Set(ctx, model)...)
if resp.Diagnostics.HasError() {
tflog.Error(ctx, fmt.Sprintf("error updating terraform state %v", resp.Diagnostics.Errors()))
return
Expand Down Expand Up @@ -289,18 +336,14 @@ func (r *vpcResource) Configure(ctx context.Context, req resource.ConfigureReque
if req.ProviderData == nil {
return
}

client, ok := req.ProviderData.(*tsClient.Client)

if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *tsClient.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)

return
}

r.client = client
}

Expand Down Expand Up @@ -368,6 +411,10 @@ func (r *vpcResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *
stringplanmodifier.UseStateForUnknown(),
},
},
"array": schema.ListAttribute{
Computed: true,
ElementType: types.StringType,
},
// "peering_connections": schema.ListNestedAttribute{
// Optional: true,
// Computed: true,
Expand All @@ -385,25 +432,25 @@ func (r *vpcResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *
// "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,
// },
// },
// },
// },
// // "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,
// // },
// // },
// // },
// // },
// },
// },
// },
Expand Down
58 changes: 26 additions & 32 deletions internal/provider/vpcs_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"

tsClient "github.com/timescale/terraform-provider-timescale/internal/client"
)
Expand Down Expand Up @@ -37,32 +38,25 @@ type vpcsDataSourceModel struct {

// 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"`
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 types.List `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 peerVpcModel struct {
ID types.Int64 `tfsdk:"id"`
CIDR types.String `tfsdk:"cidr"`
AccountID types.String `tfsdk:"account_id"`
RegionCode types.String `tfsdk:"region_code"`
ID types.Int64 `tfsdk:"id"`
VpcID types.Int64 `tfsdk:"vpc_id"`
Status types.String `tfsdk:"status"`
ErrorMessage types.String `tfsdk:"error_message"`
PeerVpcs types.Object `tfsdk:"peer_vpc"`
}

// Metadata returns the data source type name.
Expand Down Expand Up @@ -119,20 +113,20 @@ func (d *vpcsDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
Status: types.StringValue(peeringConn.Status),
ErrorMessage: types.StringValue(peeringConn.ErrorMessage),
}
for _, peerVpc := range peeringConn.PeerVPCs {
peerVpcId, err := strconv.ParseInt(peerVpc.ID, 10, 64)
if peerVPC := peeringConn.PeerVPCs; peerVPC != nil {
peerVPCId, err := strconv.ParseInt(peerVPC.ID, 10, 64)
if err != nil {
resp.Diagnostics.AddError("Unable to Convert Vpc ID", err.Error())
return
}
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),
})
peerVPC := peerVpcModel{
ID: types.Int64Value(peerVPCId),
AccountID: types.StringValue(peerVPC.AccountID),
CIDR: types.StringValue(peerVPC.CIDR),
RegionCode: types.StringValue(peerVPC.RegionCode),
}
peerConn.PeerVpcs.As(ctx, &peerVPC, basetypes.ObjectAsOptions{})
}
vpcState.PeeringConnections = append(vpcState.PeeringConnections, peerConn)
}
state.Vpcs = append(state.Vpcs, vpcState)
}
Expand Down

0 comments on commit 2af1611

Please sign in to comment.