Skip to content

Commit

Permalink
chore(bms): fix codecheck for bms (#3490)
Browse files Browse the repository at this point in the history
  • Loading branch information
dangyy0626 authored Oct 19, 2023
1 parent e8a84b7 commit 9e9bb0c
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
)

Expand Down Expand Up @@ -33,7 +34,7 @@ func testAccCheckBmsFlavorDataSourceID(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Can't find bms flavors data source: %s", n)
return fmt.Errorf("can't find bms flavors data source: %s", n)
}

if rs.Primary.ID == "" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"fmt"
"testing"

"github.com/chnsz/golangsdk/openstack/bms/v1/baremetalservers"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

"github.com/chnsz/golangsdk/openstack/bms/v1/baremetalservers"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance/common"
Expand Down Expand Up @@ -53,10 +54,10 @@ func TestAccBmsInstance_basic(t *testing.T) {
}

func testAccCheckBmsInstanceDestroy(s *terraform.State) error {
config := acceptance.TestAccProvider.Meta().(*config.Config)
bmsClient, err := config.BmsV1Client(acceptance.HW_REGION_NAME)
cfg := acceptance.TestAccProvider.Meta().(*config.Config)
bmsClient, err := cfg.BmsV1Client(acceptance.HW_REGION_NAME)
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud bms client: %s", err)
return fmt.Errorf("error creating bms client: %s", err)
}

for _, rs := range s.RootModule().Resources {
Expand All @@ -67,7 +68,7 @@ func testAccCheckBmsInstanceDestroy(s *terraform.State) error {
server, err := baremetalservers.Get(bmsClient, rs.Primary.ID).Extract()
if err == nil {
if server.Status != "DELETED" {
return fmt.Errorf("Instance still exists")
return fmt.Errorf("instance still exists")
}
}
}
Expand All @@ -79,17 +80,17 @@ func testAccCheckBmsInstanceExists(n string, instance *baremetalservers.CloudSer
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
return fmt.Errorf("not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No ID is set")
return fmt.Errorf("no ID is set")
}

config := acceptance.TestAccProvider.Meta().(*config.Config)
bmsClient, err := config.BmsV1Client(acceptance.HW_REGION_NAME)
cfg := acceptance.TestAccProvider.Meta().(*config.Config)
bmsClient, err := cfg.BmsV1Client(acceptance.HW_REGION_NAME)
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud bms client: %s", err)
return fmt.Errorf("error creating bms client: %s", err)
}

found, err := baremetalservers.Get(bmsClient, rs.Primary.ID).Extract()
Expand All @@ -98,7 +99,7 @@ func testAccCheckBmsInstanceExists(n string, instance *baremetalservers.CloudSer
}

if found.ID != rs.Primary.ID {
return fmt.Errorf("Instance not found")
return fmt.Errorf("instance not found")
}

*instance = *found
Expand Down
32 changes: 17 additions & 15 deletions huaweicloud/services/bms/data_source_huaweicloud_bms_flavors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ package bms

import (
"context"
"log"
"strconv"
"strings"

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

"github.com/chnsz/golangsdk/openstack/bms/v1/flavors"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/hashcode"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils/fmtp"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils/logp"
)

func DataSourceBmsFlavors() *schema.Resource {
Expand Down Expand Up @@ -76,11 +77,11 @@ func DataSourceBmsFlavors() *schema.Resource {
}

func dataSourceBmsFlavorsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*config.Config)
region := config.GetRegion(d)
bmsClient, err := config.BmsV1Client(region)
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
bmsClient, err := cfg.BmsV1Client(region)
if err != nil {
return fmtp.DiagErrorf("Error creating HuaweiCloud BMS client: %s", err)
return diag.Errorf("error creating BMS client: %s", err)
}

az := d.Get("availability_zone").(string)
Expand All @@ -90,7 +91,7 @@ func dataSourceBmsFlavorsRead(_ context.Context, d *schema.ResourceData, meta in

allFlavors, err := flavors.List(bmsClient, listOpts).Extract()
if err != nil {
return fmtp.DiagErrorf("Unable to retrieve BMS flavors: %s ", err)
return diag.Errorf("unable to retrieve BMS flavors: %s ", err)
}

var vcpus string
Expand All @@ -107,9 +108,9 @@ func dataSourceBmsFlavorsRead(_ context.Context, d *schema.ResourceData, meta in

filterFlavors, err := utils.FilterSliceWithField(allFlavors, filter)
if err != nil {
return fmtp.DiagErrorf("filter BMS flavors failed: %s", err)
return diag.Errorf("filter BMS flavors failed: %s", err)
}
logp.Printf("filter %d bms flavors from %d through options %v", len(filterFlavors), len(allFlavors), filter)
log.Printf("filter %d bms flavors from %d through options %v", len(filterFlavors), len(allFlavors), filter)

var ids []string
var resultFlavors []map[string]interface{}
Expand Down Expand Up @@ -139,20 +140,21 @@ func dataSourceBmsFlavorsRead(_ context.Context, d *schema.ResourceData, meta in
}

if len(resultFlavors) < 1 {
return fmtp.DiagErrorf("Your query returned no results. " +
return diag.Errorf("your query returned no results. " +
"Please change your search criteria and try again.")
}

d.SetId(hashcode.Strings(ids))
d.Set("region", region)
d.Set("flavors", resultFlavors)

return nil
mErr := multierror.Append(nil,
d.Set("region", region),
d.Set("flavors", resultFlavors),
)
return diag.FromErr(mErr.ErrorOrNil())
}

func flattenBmsFlavor(flavor flavors.Flavor) map[string]interface{} {
vcpus, _ := strconv.Atoi(flavor.VCPUs)
ram := int(flavor.RAM / 1024)
ram := flavor.RAM / 1024

return map[string]interface{}{
"id": flavor.ID,
Expand Down
Loading

0 comments on commit 9e9bb0c

Please sign in to comment.