From de4d278b18f7183a0db9584eaa3c82ebffb0c60f Mon Sep 17 00:00:00 2001 From: PaulomeeCb Date: Wed, 19 Jun 2024 14:29:41 -0700 Subject: [PATCH] Restructuring Morph and conversions --- internal/api/network_peer/network_peer.go | 73 +++++----- internal/datasources/network_peer_schema.go | 32 +++-- internal/datasources/network_peers.go | 3 +- internal/resources/network_peer.go | 31 ++-- internal/resources/network_peer_schema.go | 12 +- internal/schema/network_peer.go | 148 ++++++++++++-------- 6 files changed, 174 insertions(+), 125 deletions(-) diff --git a/internal/api/network_peer/network_peer.go b/internal/api/network_peer/network_peer.go index a69a5b0e..12a545f1 100644 --- a/internal/api/network_peer/network_peer.go +++ b/internal/api/network_peer/network_peer.go @@ -65,6 +65,8 @@ 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"` @@ -72,10 +74,30 @@ type GetNetworkPeeringRecordResponse struct { 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"` @@ -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"` @@ -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 diff --git a/internal/datasources/network_peer_schema.go b/internal/datasources/network_peer_schema.go index 29acb40a..0de60ca1 100644 --- a/internal/datasources/network_peer_schema.go +++ b/internal/datasources/network_peer_schema.go @@ -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, + }, + }, }, }, }, diff --git a/internal/datasources/network_peers.go b/internal/datasources/network_peers.go index c2aa45b1..c5f22057 100644 --- a/internal/datasources/network_peers.go +++ b/internal/datasources/network_peers.go @@ -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) @@ -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( diff --git a/internal/resources/network_peer.go b/internal/resources/network_peer.go index 84b9e959..99301c3e 100644 --- a/internal/resources/network_peer.go +++ b/internal/resources/network_peer.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "fmt" - "log" "net/http" "github.com/hashicorp/terraform-plugin-framework/path" @@ -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", @@ -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", @@ -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() @@ -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 { diff --git a/internal/resources/network_peer_schema.go b/internal/resources/network_peer_schema.go index da11dffb..ed743dee 100644 --- a/internal/resources/network_peer_schema.go +++ b/internal/resources/network_peer_schema.go @@ -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{ @@ -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}), }, }, }, diff --git a/internal/schema/network_peer.go b/internal/schema/network_peer.go index 94b2f9bb..ac85485a 100644 --- a/internal/schema/network_peer.go +++ b/internal/schema/network_peer.go @@ -2,8 +2,8 @@ package schema import ( "context" - "encoding/json" "fmt" + "log" "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/types" @@ -60,43 +60,51 @@ type PeeringStatus struct { // ProviderConfig provides details about the configuration and the ID of the VPC peer on AWS, GCP. type ProviderConfig struct { // AWSConfig 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). - //AWSConfig AWSConfig `tfsdk:"aws_config"` + AWSConfig AWSConfig `tfsdk:"aws_config"` // 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). - //GCPConfig GCPConfig `tfsdk:"gcp_config"` + GCPConfig GCPConfig `tfsdk:"gcp_config"` // ProviderId The ID of the VPC peer on AWS or GCP. //ProviderId types.String `tfsdk:"provider_id"` // AccountId The numeric AWS Account ID or Owner ID. - AccountId types.String `tfsdk:"account_id"` - - // Cidr The AWS VPC CIDR block of network in which your application runs. This cannot overlap with your Capella CIDR Block. - Cidr types.String `tfsdk:"cidr"` - - // Region The AWS region where your VPC is deployed. - Region types.String `tfsdk:"region"` - - // VpcId The alphanumeric VPC ID which starts with \"vpc-\". This is also known as the networkId. - VpcId types.String `tfsdk:"vpc_id"` - - // NetworkName The name of the network that you want to peer with. - NetworkName types.String `tfsdk:"network_name"` - - // ProjectId The unique identifier for your GCP project. - ProjectId types.String `tfsdk:"project_id"` - - // ServiceAccount is the service account created or assigned on the external VPC project. GCP Service Account with below permissions - // - DNS Admin - // - Compute.NetworkAdmin - // 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 types.String `tfsdk:"service_account"` - - // ProviderId The ID of the VPC peer on GCP. - ProviderId types.String `tfsdk:"provider_id"` + //AccountId types.String `tfsdk:"account_id"` + // + //// Cidr The AWS VPC CIDR block of network in which your application runs. This cannot overlap with your Capella CIDR Block. + //Cidr types.String `tfsdk:"cidr"` + // + //// Region The AWS region where your VPC is deployed. + //Region types.String `tfsdk:"region"` + // + //// VpcId The alphanumeric VPC ID which starts with \"vpc-\". This is also known as the networkId. + //VpcId types.String `tfsdk:"vpc_id"` + // + //// NetworkName The name of the network that you want to peer with. + //NetworkName types.String `tfsdk:"network_name"` + // + //// ProjectId The unique identifier for your GCP project. + //ProjectId types.String `tfsdk:"project_id"` + // + //// ServiceAccount is the service account created or assigned on the external VPC project. GCP Service Account with below permissions + //// - DNS Admin + //// - Compute.NetworkAdmin + //// 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 types.String `tfsdk:"service_account"` + // + //// ProviderId The ID of the VPC peer on GCP. + //ProviderId types.String `tfsdk:"provider_id"` } +//type AWS struct { +// // AWSConfig 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). +// AWSConfig AWSConfig `tfsdk:"aws_config"` +// +// // ProviderId The ID of the VPC peer on AWS. +// AWSProviderId types.String `tfsdk:"aws_provider_id"` +//} + // AWSConfig 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). @@ -262,42 +270,42 @@ func NewNetworkPeer(ctx context.Context, networkPeer *network_peer_api.GetNetwor } func morphToProviderConfig(networkPeer *network_peer_api.GetNetworkPeeringRecordResponse) (ProviderConfig, error) { - var rawConfig map[string]interface{} + //var rawConfig map[string]interface{} - // Unmarshal the provider config into a map - err := json.Unmarshal(networkPeer.ProviderConfig, &rawConfig) - if err != nil { - return ProviderConfig{}, err - } + //Unmarshal the provider config into a map + //err := json.Unmarshal(networkPeer.ProviderConfig, &rawConfig) + //if err != nil { + // return ProviderConfig{}, err + //} var newProviderConfig ProviderConfig // Check for the existence of keys and unmarshal accordingly for aws and gcp config. - if _, ok := rawConfig["aws_config"]; ok { - awsConfig, err := networkPeer.AsAWS() - if err != nil { - return ProviderConfig{}, fmt.Errorf("%s: %w", errors.ErrReadingAWSConfig, err) - } - newProviderConfig.AccountId = types.StringValue(awsConfig.AccountId) - newProviderConfig.VpcId = types.StringValue(awsConfig.VpcId) - newProviderConfig.Cidr = types.StringValue(awsConfig.Cidr) - newProviderConfig.Region = types.StringValue(awsConfig.Region) - newProviderConfig.ProviderId = types.StringValue(awsConfig.ProviderId) - - } else if _, ok := rawConfig["gcp_config"]; ok { - gcpConfig, err := networkPeer.AsGCP() - if err != nil { - return ProviderConfig{}, fmt.Errorf("%s: %w", errors.ErrReadingGCPConfig, err) - } - newProviderConfig.ProjectId = types.StringValue(gcpConfig.ProjectId) - newProviderConfig.NetworkName = types.StringValue(gcpConfig.NetworkName) - newProviderConfig.Cidr = types.StringValue(gcpConfig.Cidr) - newProviderConfig.ServiceAccount = types.StringValue(gcpConfig.ServiceAccount) - newProviderConfig.ProviderId = types.StringValue(gcpConfig.ProviderId) + //if _, ok := rawConfig["aws_config"]; ok { + aws, err := networkPeer.AsAWS() + if err != nil { + return ProviderConfig{}, fmt.Errorf("%s: %w", errors.ErrReadingAWSConfig, err) } + log.Print("*************PAULO MORPH************", aws) - return newProviderConfig, nil + newProviderConfig.AWSConfig.ProviderId = types.StringValue(aws.ProviderId) + newProviderConfig.AWSConfig.AccountId = types.StringValue(aws.AWSConfigData.AccountId) + newProviderConfig.AWSConfig.VpcId = types.StringValue(aws.AWSConfigData.VpcId) + newProviderConfig.AWSConfig.Cidr = types.StringValue(aws.AWSConfigData.Cidr) + newProviderConfig.AWSConfig.Region = types.StringValue(aws.AWSConfigData.Region) + //} else if _, ok := rawConfig["gcp_config"]; ok { + gcp, err := networkPeer.AsGCP() + if err != nil { + return ProviderConfig{}, fmt.Errorf("%s: %w", errors.ErrReadingGCPConfig, err) + } + newProviderConfig.GCPConfig.ProjectId = types.StringValue(gcp.GCPConfigData.ProjectId) + newProviderConfig.GCPConfig.NetworkName = types.StringValue(gcp.GCPConfigData.NetworkName) + newProviderConfig.GCPConfig.Cidr = types.StringValue(gcp.GCPConfigData.Cidr) + newProviderConfig.GCPConfig.ServiceAccount = types.StringValue(gcp.GCPConfigData.ServiceAccount) + newProviderConfig.GCPConfig.ProviderId = types.StringValue(gcp.ProviderId) + //} + return newProviderConfig, nil } // AttributeTypes returns a mapping of field names to their respective attribute types for the CouchbaseServer struct. @@ -368,5 +376,31 @@ func NewNetworkPeerData(networkPeer *network_peer_api.GetNetworkPeeringRecordRes } newNetworkPeerData.ProviderConfig = newProviderConfig + log.Print("***************PROVIDER CONFIG*******************", newProviderConfig) return &newNetworkPeerData, nil } + +//func morphToTerraformConfig(networkPeer *network_peer_api.GetNetworkPeeringRecordResponse) (ProviderConfig, error) { +// var newConfig ProviderConfig +// newConfig = ProviderConfig{} +// +// //if networkPeer.ProviderConfig.AWSConfig != nil { +// newConfig.AWSConfig = AWSConfig{ +// AccountId: types.StringValue(networkPeer.ProviderConfig.AWSConfig.AccountId), +// VpcId: types.StringValue(networkPeer.ProviderConfig.AWSConfig.VpcId), +// Region: types.StringValue(networkPeer.ProviderConfig.AWSConfig.Region), +// Cidr: types.StringValue(networkPeer.ProviderConfig.AWSConfig.Cidr), +// ProviderId: types.StringValue(networkPeer.ProviderConfig.AWSConfig.ProviderId), +// } +// +// //} else if networkPeer.ProviderConfig.GCPConfig != nil { +// //newConfig.GCPConfig = GCPConfig{ +// // NetworkName: types.StringValue(networkPeer.ProviderConfig.GCPConfig.NetworkName), +// // ProjectId: types.StringValue(networkPeer.ProviderConfig.GCPConfig.ProjectId), +// // Cidr: types.StringValue(networkPeer.ProviderConfig.GCPConfig.Cidr), +// // ServiceAccount: types.StringValue(networkPeer.ProviderConfig.GCPConfig.ServiceAccount), +// // ProviderId: types.StringValue(networkPeer.ProviderConfig.GCPConfig.ProviderId), +// //} +// //} +// return newConfig, nil +//}