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 instance console languages-ip-volumes data source and documen… #31

Closed
wants to merge 11 commits into from
57 changes: 25 additions & 32 deletions ibm/service/power/data_source_ibm_pi_instance_console_languages.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,52 @@ package power
import (
"context"

"github.com/IBM-Cloud/power-go-client/helpers"
"github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

const (
ConsoleLanguages = "console_languages"
ConsoleLanguageCode = "code"
ConsoleLanguageDesc = "language"
)

/*
Datasource to get the list of available console languages for an instance
*/
// Datasource to list available console languages for an instance
func DataSourceIBMPIInstanceConsoleLanguages() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMPIInstanceConsoleLanguagesRead,
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.PIInstanceName: {
Type: schema.TypeString,
Arg_InstanceName: {
Description: "The unique identifier or name of the instance.",
Required: true,
Description: "The unique identifier or name of the instance",
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},

// Computed Attributes
ConsoleLanguages: {
Type: schema.TypeList,
Computed: true,
// Attributes
Attr_ConsoleLanguages: {
Computed: true,
Description: "List of all the Console Languages.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
ConsoleLanguageCode: {
Type: schema.TypeString,
Attr_Code: {
Computed: true,
Description: "language code",
},
ConsoleLanguageDesc: {
Description: "Language code.",
Type: schema.TypeString,
},
Attr_Language: {
Computed: true,
Description: "language description",
Description: "Language description.",
Type: schema.TypeString,
},
},
},
Type: schema.TypeList,
},
},
}
Expand All @@ -70,8 +63,8 @@ func dataSourceIBMPIInstanceConsoleLanguagesRead(ctx context.Context, d *schema.
return diag.FromErr(err)
}

cloudInstanceID := d.Get(helpers.PICloudInstanceId).(string)
instanceName := d.Get(helpers.PIInstanceName).(string)
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)
instanceName := d.Get(Arg_InstanceName).(string)

client := instance.NewIBMPIInstanceClient(ctx, sess, cloudInstanceID)
languages, err := client.GetConsoleLanguages(instanceName)
Expand All @@ -86,12 +79,12 @@ func dataSourceIBMPIInstanceConsoleLanguagesRead(ctx context.Context, d *schema.
result := make([]map[string]interface{}, 0, len(languages.ConsoleLanguages))
for _, language := range languages.ConsoleLanguages {
l := map[string]interface{}{
ConsoleLanguageCode: *language.Code,
ConsoleLanguageDesc: language.Language,
Attr_Code: *language.Code,
Attr_Language: language.Language,
}
result = append(result, l)
}
d.Set(ConsoleLanguages, result)
d.Set(Attr_ConsoleLanguages, result)
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func TestAccIBMPIInstanceConsoleLanguages(t *testing.T) {

func testAccCheckIBMPIInstanceConsoleLanguagesConfig() string {
return fmt.Sprintf(`
data "ibm_pi_console_languages" "example" {
pi_cloud_instance_id = "%s"
pi_instance_name = "%s"
}`, acc.Pi_cloud_instance_id, acc.Pi_instance_name)
data "ibm_pi_console_languages" "example" {
pi_cloud_instance_id = "%s"
pi_instance_name = "%s"
}`, acc.Pi_cloud_instance_id, acc.Pi_instance_name)
}
80 changes: 43 additions & 37 deletions ibm/service/power/data_source_ibm_pi_instance_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,59 +10,66 @@ import (
"strconv"

"github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/power-go-client/helpers"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"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 DataSourceIBMPIInstanceIP() *schema.Resource {

return &schema.Resource{
ReadContext: dataSourceIBMPIInstancesIPRead,
Schema: map[string]*schema.Schema{
helpers.PIInstanceName: {
Type: schema.TypeString,
// Arguments
Arg_CloudInstanceID: {
Description: "The GUID of the service instance associated with an account.",
Required: true,
Description: "Server Name to be used for pvminstances",
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
helpers.PICloudInstanceId: {
Type: schema.TypeString,
Arg_InstanceName: {
Description: "The unique identifier or name of the instance.",
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
helpers.PINetworkName: {
Arg_NetworkName: {
Description: "The subnet that the instance belongs to.",
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.NoZeroValues,
},

// Computed attributes
"ip": {
Type: schema.TypeString,
Computed: true,
// Attributes
Attr_ExternalIP: {
Computed: true,
Description: "The external IP of the network that is attached to this instance.",
Type: schema.TypeString,
},
"ipoctet": {
Type: schema.TypeString,
Computed: true,
Attr_IP: {
Computed: true,
Description: "The IP address that is attached to this instance from the subnet.",
Type: schema.TypeString,
},
"macaddress": {
Type: schema.TypeString,
Computed: true,
Attr_IPOctet: {
Computed: true,
Description: "The IP octet of the network that is attached to this instance.",
Type: schema.TypeString,
},
"network_id": {
Type: schema.TypeString,
Computed: true,
Attr_MacAddress: {
Computed: true,
Description: "The MAC address of the network that is attached to this instance.",
Type: schema.TypeString,
},
"type": {
Type: schema.TypeString,
Computed: true,
Attr_NetworkID: {
Computed: true,
Description: "ID of the network.",
Type: schema.TypeString,
},
"external_ip": {
Type: schema.TypeString,
Computed: true,
Attr_Type: {
Computed: true,
Description: "The type of the network that is attached to this instance.",
Type: schema.TypeString,
},
},
}
Expand All @@ -74,11 +81,11 @@ func dataSourceIBMPIInstancesIPRead(ctx context.Context, d *schema.ResourceData,
return diag.FromErr(err)
}

cloudInstanceID := d.Get(helpers.PICloudInstanceId).(string)
networkName := d.Get(helpers.PINetworkName).(string)
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)
networkName := d.Get(Arg_NetworkName).(string)
powerC := instance.NewIBMPIInstanceClient(ctx, sess, cloudInstanceID)

powervmdata, err := powerC.Get(d.Get(helpers.PIInstanceName).(string))
powervmdata, err := powerC.Get(d.Get(Arg_InstanceName).(string))
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -87,17 +94,16 @@ func dataSourceIBMPIInstancesIPRead(ctx context.Context, d *schema.ResourceData,
if network.NetworkName == networkName {
log.Printf("Printing the ip %s", network.IPAddress)
d.SetId(network.NetworkID)
d.Set("ip", network.IPAddress)
d.Set("network_id", network.NetworkID)
d.Set("macaddress", network.MacAddress)
d.Set("external_ip", network.ExternalIP)
d.Set("type", network.Type)
d.Set(Attr_ExternalIP, network.ExternalIP)
d.Set(Attr_IP, network.IPAddress)
d.Set(Attr_MacAddress, network.MacAddress)
d.Set(Attr_NetworkID, network.NetworkID)
d.Set(Attr_Type, network.Type)

IPObject := net.ParseIP(network.IPAddress).To4()
if len(IPObject) > 0 {
d.Set("ipoctet", strconv.Itoa(int(IPObject[3])))
d.Set(Attr_IPOctet, strconv.Itoa(int(IPObject[3])))
}

return nil
}
}
Expand Down
11 changes: 5 additions & 6 deletions ibm/service/power/data_source_ibm_pi_instance_ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ func TestAccIBMPIInstanceIPDataSource_basic(t *testing.T) {

func testAccCheckIBMPIInstanceIPDataSourceConfig() string {
return fmt.Sprintf(`
data "ibm_pi_instance_ip" "testacc_ds_instance_ip" {
pi_network_name = "%[1]s"
pi_instance_name = "%[2]s"
pi_cloud_instance_id = "%[3]s"
}
`, acc.Pi_network_name, acc.Pi_instance_name, acc.Pi_cloud_instance_id)
data "ibm_pi_instance_ip" "testacc_ds_instance_ip" {
pi_network_name = "%[1]s"
pi_instance_name = "%[2]s"
pi_cloud_instance_id = "%[3]s"
}`, acc.Pi_network_name, acc.Pi_instance_name, acc.Pi_cloud_instance_id)
}
Loading
Loading