Skip to content

Commit

Permalink
Restructuring Morph and conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulomeeCb committed Jun 19, 2024
1 parent d0e6061 commit de4d278
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 125 deletions.
73 changes: 41 additions & 32 deletions internal/api/network_peer/network_peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,39 @@ type GetNetworkPeeringRecordResponse struct {
// ProviderConfig This provides details about the configuration and the ID of the VPC peer on AWS, GCP.
ProviderConfig json.RawMessage `json:"providerConfig"`

//ProviderConfig ProviderConfig `json:"providerConfig"`

//AWSConfig *AWSConfig `json:"awsConfig"`
//
//GCPConfig *GCPConfig `json:"gcpConfig"`

Status PeeringStatus `json:"status"`
}

// AWSConfig is the AWS config data required to establish a VPC peering relationship.
type ProviderConfig struct {
AWS AWS `json:"aws"`

GCP GCP `json:"gcp"`
}

type AWS struct {
// ProviderId The ID of the VPC peer on GCP.
ProviderId string `json:"ProviderId"`

AWSConfigData AWSConfigData `json:"AWSConfig"`
}

type GCP struct {
// ProviderId The ID of the VPC peer on GCP.
ProviderId string `json:"ProviderId"`

GCPConfigData GCPConfigData `json:"GCPConfig"`
}

// AWSConfigData is the AWS config data required to establish a VPC peering relationship.
//
// Refer to the docs for other limitations to AWS VPC Peering - [ref](https://docs.aws.amazon.com/vpc/latest/peering/vpc-peering-basics.html#vpc-peering-limitations).
type AWSConfig struct {
type AWSConfigData struct {
// AccountId The numeric AWS Account ID or Owner ID.
AccountId string `json:"accountId"`

Expand All @@ -87,13 +109,10 @@ type AWSConfig struct {

// VpcId The alphanumeric VPC ID which starts with \"vpc-\". This is also known as the networkId.
VpcId string `json:"vpcId"`

// ProviderId The ID of the VPC peer on GCP.
ProviderId string `json:"providerId"`
}

// GCPConfig GCP config data required to establish a VPC peering relationship. Refer to the docs for other limitations to GCP VPC Peering - [ref](https://cloud.google.com/vpc/docs/vpc-peering).
type GCPConfig struct {
// GCPConfigData GCP config data required to establish a VPC peering relationship. Refer to the docs for other limitations to GCP VPC Peering - [ref](https://cloud.google.com/vpc/docs/vpc-peering).
type GCPConfigData struct {
// Cidr The GCP VPC CIDR block of network in which your application runs. This cannot overlap with your Capella CIDR Block.
Cidr string `json:"cidr"`

Expand All @@ -109,48 +128,38 @@ type GCPConfig struct {
// It should be in the form of an email that is shown under `gcloud iam service-accounts list` command.
// [Reference](https://cloud.google.com/iam/docs/creating-managing-service-accounts#creating)
ServiceAccount string `json:"serviceAccount"`

// ProviderId The ID of the VPC peer on GCP.
ProviderId string `json:"providerId"`
}

// AsAWS returns the union data inside the GetNetworkPeeringRecordResponse_ProviderConfig as a AWS
func (t GetNetworkPeeringRecordResponse) AsAWS() (AWSConfig, error) {
var body AWSConfig
// AsAWS returns the union data inside the GetNetworkPeeringRecordResponse as a AWS
func (t GetNetworkPeeringRecordResponse) AsAWS() (AWS, error) {
var body AWS
err := json.Unmarshal(t.ProviderConfig, &body)
return body, err
}

// FromAWS overwrites any union data inside the GetNetworkPeeringRecordResponse_ProviderConfig as the provided AWS
func (t *GetNetworkPeeringRecordResponse) FromAWS(v AWSConfig) error {
b, err := json.Marshal(v)
t.ProviderConfig = b
return err
}

// AsGCP returns the union data inside the GetNetworkPeeringRecordResponse_ProviderConfig as a GCP
func (t GetNetworkPeeringRecordResponse) AsGCP() (GCPConfig, error) {
var body GCPConfig
func (t GetNetworkPeeringRecordResponse) AsGCP() (GCP, error) {
var body GCP
err := json.Unmarshal(t.ProviderConfig, &body)
return body, err
}

// FromGCP overwrites any union data inside the GetNetworkPeeringRecordResponse_ProviderConfig as the provided GCP
func (t *GetNetworkPeeringRecordResponse) FromGCP(v GCPConfig) error {
b, err := json.Marshal(v)
t.ProviderConfig = b
return err
}
// AsAWSConfigData returns the union data inside the CreateNetworkPeeringRequest as a AWSConfigData
//func (t CreateNetworkPeeringRequest) AsAWSConfigData() (AWSConfigData, error) {
// var body AWSConfigData
// err := json.Unmarshal(t.ProviderConfig, &body)
// return body, err
//}

// FromAWS overwrites any union data inside the GetNetworkPeeringRecordResponse_ProviderConfig as the provided AWS
func (t *CreateNetworkPeeringRequest) FromAWS(v AWSConfig) error {
// FromAWSConfigData overwrites any union data inside the CreateNetworkPeeringRequest_ProviderConfig as the provided AWSConfigData
func (t *CreateNetworkPeeringRequest) FromAWSConfigData(v AWSConfigData) error {
b, err := json.Marshal(v)
t.ProviderConfig = b
return err
}

// FromGCP overwrites any union data inside the GetNetworkPeeringRecordResponse_ProviderConfig as the provided GCP
func (t *CreateNetworkPeeringRequest) FromGCP(v GCPConfig) error {
// FromGCPConfigData overwrites any union data inside the CreateNetworkPeeringRequest_ProviderConfig as the provided GCPConfigData
func (t *CreateNetworkPeeringRequest) FromGCPConfigData(v GCPConfigData) error {
b, err := json.Marshal(v)
t.ProviderConfig = b
return err
Expand Down
32 changes: 21 additions & 11 deletions internal/datasources/network_peer_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,34 @@ func NetworkPeerSchema() schema.Schema {
Computed: true,
Attributes: map[string]schema.Attribute{
//"provider_id": computedStringAttribute,
"aws_config": schema.SingleNestedAttribute{
"aws": schema.SingleNestedAttribute{
Computed: true,
Attributes: map[string]schema.Attribute{
"account_id": computedStringAttribute,
"vpc_id": computedStringAttribute,
"region": computedStringAttribute,
"cidr": computedStringAttribute,
"provider_id": computedStringAttribute,
"aws_config": schema.SingleNestedAttribute{
Computed: true,
Attributes: map[string]schema.Attribute{
"account_id": computedStringAttribute,
"vpc_id": computedStringAttribute,
"region": computedStringAttribute,
"cidr": computedStringAttribute,
},
},
},
},
"gcp_config": schema.SingleNestedAttribute{
"gcp": schema.SingleNestedAttribute{
Computed: true,
Attributes: map[string]schema.Attribute{
"cidr": computedStringAttribute,
"network_name": computedStringAttribute,
"project_id": computedStringAttribute,
"service_account": computedStringAttribute,
"provider_id": computedStringAttribute,
"provider_id": computedStringAttribute,
"gcp_config": schema.SingleNestedAttribute{
Computed: true,
Attributes: map[string]schema.Attribute{
"cidr": computedStringAttribute,
"network_name": computedStringAttribute,
"project_id": computedStringAttribute,
"service_account": computedStringAttribute,
},
},
},
},
},
Expand Down
3 changes: 1 addition & 2 deletions internal/datasources/network_peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (n *NetworkPeers) Read(ctx context.Context, req datasource.ReadRequest, res
return
}

log.Print("PAULO response", response)
log.Print("***********************RESPONSE******************", response)
for i := range response {
networkPeer := response[i]
audit := providerschema.NewCouchbaseAuditData(networkPeer.Audit)
Expand All @@ -108,7 +108,6 @@ func (n *NetworkPeers) Read(ctx context.Context, req datasource.ReadRequest, res
)
}

log.Print("PAULO networkPeer", networkPeer)
newNetworkPeerData, err := providerschema.NewNetworkPeerData(&networkPeer, organizationId, projectId, clusterId, auditObj)
if err != nil {
resp.Diagnostics.AddError(
Expand Down
31 changes: 14 additions & 17 deletions internal/resources/network_peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"

"github.com/hashicorp/terraform-plugin-framework/path"
Expand Down Expand Up @@ -70,15 +69,14 @@ func (n *NetworkPeer) Create(ctx context.Context, req resource.CreateRequest, re

switch plan.ProviderType.ValueString() {
case "aws":
awsConfig := network_peer_api.AWSConfig{
AccountId: plan.ProviderConfig.AccountId.ValueString(),
Cidr: plan.ProviderConfig.Cidr.ValueString(),
Region: plan.ProviderConfig.Region.ValueString(),
VpcId: plan.ProviderConfig.VpcId.ValueString(),
ProviderId: plan.ProviderConfig.ProviderId.ValueString(),
awsConfig := network_peer_api.AWSConfigData{
AccountId: plan.ProviderConfig.AWSConfig.AccountId.ValueString(),
Cidr: plan.ProviderConfig.AWSConfig.Cidr.ValueString(),
Region: plan.ProviderConfig.AWSConfig.Region.ValueString(),
VpcId: plan.ProviderConfig.AWSConfig.VpcId.ValueString(),
}

err := networkPeerRequest.FromAWS(awsConfig)
err := networkPeerRequest.FromAWSConfigData(awsConfig)
if err != nil {
resp.Diagnostics.AddError(
"Error creating network peer for AWS",
Expand All @@ -88,15 +86,14 @@ func (n *NetworkPeer) Create(ctx context.Context, req resource.CreateRequest, re
}

case "gcp":
gcpConfig := network_peer_api.GCPConfig{
NetworkName: plan.ProviderConfig.NetworkName.ValueString(),
Cidr: plan.ProviderConfig.Cidr.ValueString(),
ProjectId: plan.ProviderConfig.ProjectId.ValueString(),
ServiceAccount: plan.ProviderConfig.ServiceAccount.ValueString(),
ProviderId: plan.ProviderConfig.ProviderId.ValueString(),
gcpConfig := network_peer_api.GCPConfigData{
NetworkName: plan.ProviderConfig.GCPConfig.NetworkName.ValueString(),
Cidr: plan.ProviderConfig.GCPConfig.Cidr.ValueString(),
ProjectId: plan.ProviderConfig.GCPConfig.ProjectId.ValueString(),
ServiceAccount: plan.ProviderConfig.GCPConfig.ServiceAccount.ValueString(),
}

err := networkPeerRequest.FromGCP(gcpConfig)
err := networkPeerRequest.FromGCPConfigData(gcpConfig)
if err != nil {
resp.Diagnostics.AddError(
"Error creating network peer for GCP",
Expand All @@ -106,7 +103,7 @@ func (n *NetworkPeer) Create(ctx context.Context, req resource.CreateRequest, re
}
}

log.Print("*********PAULO********** networkPeerRequest", networkPeerRequest)
//log.Print("*********PAULO********** networkPeerRequest", networkPeerRequest)
var (
organizationId = plan.OrganizationId.ValueString()
projectId = plan.ProjectId.ValueString()
Expand Down Expand Up @@ -300,7 +297,7 @@ func (n *NetworkPeer) Configure(_ context.Context, req resource.ConfigureRequest
}

func (n *NetworkPeer) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("endpoint_id"), req, resp)
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

func (n *NetworkPeer) validateCreateNetworkPeer(plan providerschema.NetworkPeer) error {
Expand Down
12 changes: 6 additions & 6 deletions internal/resources/network_peer_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ func NetworkPeerSchema() schema.Schema {
"aws_config": schema.SingleNestedAttribute{
Optional: true,
Attributes: map[string]schema.Attribute{
"account_id": stringAttribute([]string{optional}),
"vpc_id": stringAttribute([]string{optional}),
"region": stringAttribute([]string{optional}),
"cidr": stringAttribute([]string{required}),
"provider_id": stringAttribute([]string{computed}),
"account_id": stringAttribute([]string{optional}),
"vpc_id": stringAttribute([]string{optional}),
"region": stringAttribute([]string{optional}),
"cidr": stringAttribute([]string{required}),
"aws_provider_id": stringAttribute([]string{computed}),
},
},
"gcp_config": schema.SingleNestedAttribute{
Expand All @@ -52,7 +52,7 @@ func NetworkPeerSchema() schema.Schema {
"network_name": stringAttribute([]string{optional}),
"project_id": stringAttribute([]string{optional}),
"service_account": stringAttribute([]string{optional}),
"provider_id": stringAttribute([]string{computed}),
"gcp_provider_id": stringAttribute([]string{computed}),
},
},
},
Expand Down
Loading

0 comments on commit de4d278

Please sign in to comment.