Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Cloud connection refactor data source and documentation #5053

Merged
merged 7 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 81 additions & 90 deletions ibm/service/power/data_source_ibm_pi_cloud_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,116 +7,104 @@ import (
"context"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

"github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/power-go-client/helpers"
"github.com/IBM-Cloud/power-go-client/power/models"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
)

const (
PICloudConnectionId = "cloud_connection_id"
PICloudConnectionName = "name"
PICloudConnectionSpeed = "speed"
PICloudConnectionGlobalRouting = "global_routing"
PICloudConnectionMetered = "metered"
PICloudConnectionStatus = "status"
PICloudConnectionClassicEnabled = "classic_enabled"
PICloudConnectionUserIPAddress = "user_ip_address"
PICloudConnectionIBMIPAddress = "ibm_ip_address"
PICloudConnectionPort = "port"
PICloudConnectionNetworks = "networks"
PICloudConnectionClassicGreDest = "gre_destination_address"
PICloudConnectionClassicGreSource = "gre_source_address"
PICloudConnectionVPCEnabled = "vpc_enabled"
PICloudConnectionVPCCRNs = "vpc_crns"
PICloudConnectionConnectionMode = "connection_mode"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func DataSourceIBMPICloudConnection() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMPICloudConnectionRead,
Schema: map[string]*schema.Schema{
helpers.PICloudInstanceId: {
Type: schema.TypeString,
// Arguments
Arg_CloudInstanceID: {
Description: "The GUID of the service instance associated with an account.",
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
helpers.PICloudConnectionName: {
Type: schema.TypeString,
Arg_CloudConnectionName: {
Description: "The cloud connection name to be used.",
Required: true,
Description: "Cloud Connection Name to be used",
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},

// Computed Attributes
PICloudConnectionSpeed: {
Type: schema.TypeInt,
Computed: true,
// Attributes
Attr_ClassicEnabled: {
Computed: true,
Description: "Enable classic endpoint destination.",
Type: schema.TypeBool,
},
PICloudConnectionGlobalRouting: {
Type: schema.TypeBool,
Computed: true,
Attr_ConnectionMode: {
Computed: true,
Description: "Type of service the gateway is attached to.",
Type: schema.TypeString,
},
PICloudConnectionMetered: {
Type: schema.TypeBool,
Computed: true,
Attr_GlobalRouting: {
Computed: true,
Description: "Enable global routing for this cloud connection.",
Type: schema.TypeBool,
},
PICloudConnectionStatus: {
Type: schema.TypeString,
Computed: true,
Attr_GreDestinationAddress: {
Computed: true,
Description: "GRE destination IP address.",
Type: schema.TypeString,
},
PICloudConnectionIBMIPAddress: {
Type: schema.TypeString,
Computed: true,
Attr_GreSourceAddress: {
Computed: true,
Description: "GRE auto-assigned source IP address.",
Type: schema.TypeString,
},
PICloudConnectionUserIPAddress: {
Type: schema.TypeString,
Computed: true,
Attr_IBMIPAddress: {
Computed: true,
Description: "The IBM IP address.",
Type: schema.TypeString,
},
PICloudConnectionPort: {
Type: schema.TypeString,
Computed: true,
Attr_Metered: {
Computed: true,
Description: "Enable metering for this cloud connection.",
Type: schema.TypeBool,
},
PICloudConnectionNetworks: {
Type: schema.TypeSet,
Attr_Networks: {
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Set of Networks attached to this cloud connection",
Elem: &schema.Schema{Type: schema.TypeString},
Type: schema.TypeSet,
},
PICloudConnectionClassicEnabled: {
Type: schema.TypeBool,
Attr_Port: {
Computed: true,
Description: "Enable classic endpoint destination",
},
PICloudConnectionClassicGreDest: {
Description: "Port.",
Type: schema.TypeString,
},
Attr_Speed: {
Computed: true,
Description: "GRE destination IP address",
Description: "Speed of the cloud connection (speed in megabits per second)",
Type: schema.TypeInt,
},
PICloudConnectionClassicGreSource: {
Type: schema.TypeString,
Attr_Status: {
Computed: true,
Description: "GRE auto-assigned source IP address",
Description: "Link status.",
Type: schema.TypeString,
},
PICloudConnectionVPCEnabled: {
Type: schema.TypeBool,
Attr_UserIPAddress: {
Computed: true,
Description: "Enable VPC for this cloud connection",
Description: "User IP address.",
Type: schema.TypeString,
},
PICloudConnectionVPCCRNs: {
Type: schema.TypeSet,
Attr_VPCCRNs: {
Computed: true,
Description: "Set of VPCs attached to this cloud connection.",
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Set of VPCs attached to this cloud connection",
Type: schema.TypeSet,
},
PICloudConnectionConnectionMode: {
Type: schema.TypeString,
Attr_VPCEnabled: {
Computed: true,
Description: "Type of service the gateway is attached to",
Description: "Enable VPC for this cloud connection.",
Type: schema.TypeBool,
},
},
}
Expand All @@ -128,8 +116,8 @@ func dataSourceIBMPICloudConnectionRead(ctx context.Context, d *schema.ResourceD
return diag.FromErr(err)
}

cloudInstanceID := d.Get(helpers.PICloudInstanceId).(string)
cloudConnectionName := d.Get(helpers.PICloudConnectionName).(string)
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)
cloudConnectionName := d.Get(Arg_CloudConnectionName).(string)
client := instance.NewIBMPICloudConnectionClient(ctx, sess, cloudInstanceID)

// Get API does not work with name for Cloud Connection hence using GetAll (max 2)
Expand Down Expand Up @@ -165,41 +153,44 @@ func dataSourceIBMPICloudConnectionRead(ctx context.Context, d *schema.ResourceD
}

d.SetId(*cloudConnection.CloudConnectionID)
d.Set(helpers.PICloudConnectionName, cloudConnection.Name)
d.Set(PICloudConnectionGlobalRouting, cloudConnection.GlobalRouting)
d.Set(PICloudConnectionMetered, cloudConnection.Metered)
d.Set(PICloudConnectionIBMIPAddress, cloudConnection.IbmIPAddress)
d.Set(PICloudConnectionUserIPAddress, cloudConnection.UserIPAddress)
d.Set(PICloudConnectionStatus, cloudConnection.LinkStatus)
d.Set(PICloudConnectionPort, cloudConnection.Port)
d.Set(PICloudConnectionSpeed, cloudConnection.Speed)
d.Set(helpers.PICloudInstanceId, cloudInstanceID)
d.Set(PICloudConnectionConnectionMode, cloudConnection.ConnectionMode)

d.Set(Arg_CloudInstanceID, cloudInstanceID)
d.Set(Arg_CloudConnectionName, cloudConnection.Name)

d.Set(Attr_GlobalRouting, cloudConnection.GlobalRouting)
d.Set(Attr_Metered, cloudConnection.Metered)
d.Set(Attr_IBMIPAddress, cloudConnection.IbmIPAddress)
d.Set(Attr_UserIPAddress, cloudConnection.UserIPAddress)
d.Set(Attr_Status, cloudConnection.LinkStatus)
d.Set(Attr_Port, cloudConnection.Port)
d.Set(Attr_Speed, cloudConnection.Speed)
d.Set(Attr_ConnectionMode, cloudConnection.ConnectionMode)
if cloudConnection.Networks != nil {
networks := make([]string, len(cloudConnection.Networks))
for i, ccNetwork := range cloudConnection.Networks {
if ccNetwork != nil {
networks[i] = *ccNetwork.NetworkID
}
}
d.Set(PICloudConnectionNetworks, networks)
d.Set(Attr_Networks, networks)
}
if cloudConnection.Classic != nil {
d.Set(PICloudConnectionClassicEnabled, cloudConnection.Classic.Enabled)
d.Set(Attr_ClassicEnabled, cloudConnection.Classic.Enabled)
if cloudConnection.Classic.Gre != nil {
d.Set(PICloudConnectionClassicGreDest, cloudConnection.Classic.Gre.DestIPAddress)
d.Set(PICloudConnectionClassicGreSource, cloudConnection.Classic.Gre.SourceIPAddress)
d.Set(Attr_GreDestinationAddress, cloudConnection.Classic.Gre.DestIPAddress)
d.Set(Attr_GreSourceAddress, cloudConnection.Classic.Gre.SourceIPAddress)
}
}
if cloudConnection.Vpc != nil {
d.Set(PICloudConnectionVPCEnabled, cloudConnection.Vpc.Enabled)
d.Set(Attr_VPCEnabled, cloudConnection.Vpc.Enabled)
if cloudConnection.Vpc.Vpcs != nil && len(cloudConnection.Vpc.Vpcs) > 0 {
vpcCRNs := make([]string, len(cloudConnection.Vpc.Vpcs))
for i, vpc := range cloudConnection.Vpc.Vpcs {
vpcCRNs[i] = *vpc.VpcID
}
d.Set(PICloudConnectionVPCCRNs, vpcCRNs)
d.Set(Attr_VPCCRNs, vpcCRNs)
}
}

return nil
}
9 changes: 4 additions & 5 deletions ibm/service/power/data_source_ibm_pi_cloud_connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ func TestAccIBMPICloudConnectionDataSource_basic(t *testing.T) {

func testAccCheckIBMPICloudConnectionDataSourceConfig() string {
return fmt.Sprintf(`
data "ibm_pi_cloud_connection" "example" {
pi_cloud_connection_name = "%s"
pi_cloud_instance_id = "%s"
}
`, acc.PiCloudConnectionName, acc.Pi_cloud_instance_id)
data "ibm_pi_cloud_connection" "example" {
pi_cloud_connection_name = "%s"
pi_cloud_instance_id = "%s"
}`, acc.PiCloudConnectionName, acc.Pi_cloud_instance_id)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why a new line?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a new line, this part of the same line. The line number is the same, but view is squished so it appears to be on a new line.

}
Loading
Loading