Skip to content

Commit

Permalink
Restructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulomeeCb committed Jun 20, 2024
1 parent de4d278 commit 7495053
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 87 deletions.
4 changes: 2 additions & 2 deletions internal/api/network_peer/network_peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,14 @@ func (t GetNetworkPeeringRecordResponse) AsGCP() (GCP, error) {
//}

// FromAWSConfigData overwrites any union data inside the CreateNetworkPeeringRequest_ProviderConfig as the provided AWSConfigData

Check failure on line 154 in internal/api/network_peer/network_peer.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Comment should end in a period (godot)
func (t *CreateNetworkPeeringRequest) FromAWSConfigData(v AWSConfigData) error {
func (t CreateNetworkPeeringRequest) FromAWSConfigData(v AWSConfigData) error {
b, err := json.Marshal(v)
t.ProviderConfig = b

Check failure on line 157 in internal/api/network_peer/network_peer.go

View workflow job for this annotation

GitHub Actions / golangci-lint

SA4005: ineffective assignment to field CreateNetworkPeeringRequest.ProviderConfig (staticcheck)
return err
}

// FromGCPConfigData overwrites any union data inside the CreateNetworkPeeringRequest_ProviderConfig as the provided GCPConfigData

Check failure on line 161 in internal/api/network_peer/network_peer.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Comment should end in a period (godot)
func (t *CreateNetworkPeeringRequest) FromGCPConfigData(v GCPConfigData) error {
func (t CreateNetworkPeeringRequest) FromGCPConfigData(v GCPConfigData) error {
b, err := json.Marshal(v)
t.ProviderConfig = b

Check failure on line 164 in internal/api/network_peer/network_peer.go

View workflow job for this annotation

GitHub Actions / golangci-lint

SA4005: ineffective assignment to field CreateNetworkPeeringRequest.ProviderConfig (staticcheck)
return err
Expand Down
32 changes: 11 additions & 21 deletions internal/datasources/network_peer_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,24 @@ func NetworkPeerSchema() schema.Schema {
Computed: true,
Attributes: map[string]schema.Attribute{
//"provider_id": computedStringAttribute,
"aws": schema.SingleNestedAttribute{
"aws_config": 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": schema.SingleNestedAttribute{
"gcp_config": schema.SingleNestedAttribute{
Computed: true,
Attributes: map[string]schema.Attribute{
"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,
},
},
"cidr": computedStringAttribute,
"network_name": computedStringAttribute,
"project_id": computedStringAttribute,
"service_account": computedStringAttribute,
"provider_id": computedStringAttribute,
},
},
},
Expand Down
64 changes: 34 additions & 30 deletions internal/resources/network_peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"

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

switch plan.ProviderType.ValueString() {
case "aws":
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(),
if plan.ProviderConfig.AWSConfig != nil {
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.FromAWSConfigData(awsConfig)
if err != nil {
resp.Diagnostics.AddError(
"Error creating network peer for AWS",
errors.ErrConvertingProviderConfig.Error(),
)
return
}
}

err := networkPeerRequest.FromAWSConfigData(awsConfig)
if err != nil {
resp.Diagnostics.AddError(
"Error creating network peer for AWS",
errors.ErrConvertingProviderConfig.Error(),
)
return
}

case "gcp":
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.FromGCPConfigData(gcpConfig)
if err != nil {
resp.Diagnostics.AddError(
"Error creating network peer for GCP",
errors.ErrConvertingProviderConfig.Error(),
)
return
if plan.ProviderConfig.GCPConfig != nil {
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.FromGCPConfigData(gcpConfig)
if err != nil {
resp.Diagnostics.AddError(
"Error creating network peer for GCP",
errors.ErrConvertingProviderConfig.Error(),
)
return
}
}
}
log.Print("*********PAULO********** networkPeerRequest", networkPeerRequest)

//log.Print("*********PAULO********** networkPeerRequest", networkPeerRequest)
var (
organizationId = plan.OrganizationId.ValueString()
projectId = plan.ProjectId.ValueString()
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}),
"aws_provider_id": stringAttribute([]string{computed}),
"account_id": stringAttribute([]string{optional}),
"vpc_id": stringAttribute([]string{optional}),
"region": stringAttribute([]string{optional}),
"cidr": stringAttribute([]string{required}),
"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}),
"gcp_provider_id": stringAttribute([]string{computed}),
"provider_id": stringAttribute([]string{computed}),
},
},
},
Expand Down
63 changes: 35 additions & 28 deletions internal/schema/network_peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ 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"`
Expand Down Expand Up @@ -270,41 +270,48 @@ func NewNetworkPeer(ctx context.Context, networkPeer *network_peer_api.GetNetwor
}

func morphToProviderConfig(networkPeer *network_peer_api.GetNetworkPeeringRecordResponse) (ProviderConfig, error) {
//var rawConfig map[string]interface{}

//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 {
aws, err := networkPeer.AsAWS()
if err != nil {
if err == nil && aws.AWSConfigData.VpcId != "" {
newProviderConfig.AWSConfig = &AWSConfig{
ProviderId: types.StringValue(aws.ProviderId),
AccountId: types.StringValue(aws.AWSConfigData.AccountId),
VpcId: types.StringValue(aws.AWSConfigData.VpcId),
Cidr: types.StringValue(aws.AWSConfigData.Cidr),
Region: types.StringValue(aws.AWSConfigData.Region),
}
//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)

return newProviderConfig, nil
} else if err != nil {
return ProviderConfig{}, fmt.Errorf("%s: %w", errors.ErrReadingAWSConfig, err)
}
log.Print("*************PAULO MORPH************", aws)

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)
log.Print("*************PAULO MORPH************", aws)

//} else if _, ok := rawConfig["gcp_config"]; ok {
gcp, err := networkPeer.AsGCP()
if err != nil {
if err == nil && gcp.GCPConfigData.ProjectId != "" {
newProviderConfig.GCPConfig = &GCPConfig{
ProviderId: types.StringValue(gcp.ProviderId),
Cidr: types.StringValue(gcp.GCPConfigData.Cidr),
ProjectId: types.StringValue(gcp.GCPConfigData.ProjectId),
NetworkName: types.StringValue(gcp.GCPConfigData.NetworkName),
ServiceAccount: types.StringValue(gcp.GCPConfigData.ServiceAccount),
}
//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
} else 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
}

Expand Down

0 comments on commit 7495053

Please sign in to comment.