Skip to content

Commit

Permalink
Merge pull request #50 from ans-group/volumes-computed-iops
Browse files Browse the repository at this point in the history
add ecloud_iops datasource, remove validation func from instance iops
  • Loading branch information
0x4c6565 authored Jul 19, 2023
2 parents 0d8fd5c + bfdd62e commit 8b70cb9
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 25 deletions.
25 changes: 25 additions & 0 deletions docs/data-sources/ecloud_iops.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# ecloud_iops Data Source

This resource represents an eCloud IOPS tier

## Example Usage

```hcl
data "ecloud_iops" "iops-300" {
number = 300
}
```

## Argument Reference

- `iops_id`: ID of IOPS tier
- `availability_zone_id`: ID of Availability Zone that tier is available in
- `name`: Name of IOPS tier
- `level`: IOPS level/limit

## Attributes Reference

`id` is set to IOPS tier ID

- `name`: Name of IOPS tier
- `level`: IOPS level/limit
76 changes: 76 additions & 0 deletions ecloud/data_source_iops.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package ecloud

import (
"errors"
"fmt"
"strconv"

"github.com/ans-group/sdk-go/pkg/connection"
ecloudservice "github.com/ans-group/sdk-go/pkg/service/ecloud"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceIOPS() *schema.Resource {
return &schema.Resource{
Read: dataSourceIOPSRead,

Schema: map[string]*schema.Schema{
"availability_zone_id": {
Type: schema.TypeString,
Optional: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"level": {
Type: schema.TypeInt,
Optional: true,
},
},
}
}

func dataSourceIOPSRead(d *schema.ResourceData, meta interface{}) error {
service := meta.(ecloudservice.ECloudService)

params := connection.APIRequestParameters{}

if id, ok := d.GetOk("iops_id"); ok {
params.WithFilter(*connection.NewAPIRequestFiltering("id", connection.EQOperator, []string{id.(string)}))
}
if name, ok := d.GetOk("name"); ok {
params.WithFilter(*connection.NewAPIRequestFiltering("name", connection.EQOperator, []string{name.(string)}))
}
if level, ok := d.GetOk("level"); ok {
params.WithFilter(*connection.NewAPIRequestFiltering("level", connection.EQOperator, []string{strconv.Itoa(level.(int))}))
}

var tiers []ecloudservice.IOPSTier
var err error
if azID, ok := d.GetOk("availability_zone_id"); ok {
tiers, err = service.GetAvailabilityZoneIOPSTiers(azID.(string), params)
if err != nil {
return fmt.Errorf("Error retrieving availability zone IOPS tiers: %s", err)
}
} else {
tiers, err = service.GetIOPSTiers(params)
if err != nil {
return fmt.Errorf("Error retrieving IOPS tiers: %s", err)
}
}

if len(tiers) < 1 {
return errors.New("No IOPS tiers found with provided arguments")
}

if len(tiers) > 1 {
return errors.New("More than 1 IOPS tier found with provided arguments")
}

d.SetId(tiers[0].ID)
d.Set("name", tiers[0].Name)
d.Set("level", tiers[0].Level)

return nil
}
36 changes: 36 additions & 0 deletions ecloud/data_source_iops_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ecloud

import (
"fmt"
"strconv"
"testing"

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

func TestAccDataSourceIOPS_basic(t *testing.T) {
level := 300
config := testAccDataSourceIOPSConfig_basic(level)
resourceName := "data.ecloud_iops.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "level", strconv.Itoa(level)),
),
},
},
})
}

func testAccDataSourceIOPSConfig_basic(level int) string {
return fmt.Sprintf(`
data "ecloud_iops" "test" {
level = "%d"
}
`, level)
}
1 change: 1 addition & 0 deletions ecloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func Provider() *schema.Provider {
"ecloud_affinityrule_member": dataSourceAffinityRuleMember(),
"ecloud_resourcetier": dataSourceResourceTier(),
"ecloud_natoverloadrule": dataSourceNATOverloadRule(),
"ecloud_iops": dataSourceIOPS(),
},
ResourcesMap: map[string]*schema.Resource{
"ecloud_vpc": resourceVPC(),
Expand Down
18 changes: 0 additions & 18 deletions ecloud/resource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,6 @@ func resourceInstance() *schema.Resource {
"volume_iops": {
Type: schema.TypeInt,
Optional: true,
Default: 300,
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
v := val.(int)
iops := []int{300, 600, 1200, 2500}
intInSlice := func(slice []int, value int) bool {
for _, s := range slice {
if s == value {
return true
}
}
return false
}

if !intInSlice(iops, v) {
errs = append(errs, fmt.Errorf("%q must be a valid IOPS value [300, 600, 1200, 2500], got: %d", key, v))
}
return
},
},
"volume_id": {
Type: schema.TypeString,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/ukfast/terraform-provider-ecloud
go 1.18

require (
github.com/ans-group/sdk-go v1.16.5
github.com/ans-group/sdk-go v1.16.6
github.com/hashicorp/terraform-plugin-sdk/v2 v2.16.0
github.com/stretchr/testify v1.8.1
)
Expand Down
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,8 @@ github.com/agext/levenshtein v1.2.2/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/ans-group/go-durationstring v1.2.0 h1:UJIuQATkp0t1rBvZsHRwki33YHV9E+Ulro+3NbMB7MM=
github.com/ans-group/go-durationstring v1.2.0/go.mod h1:QGF9Mdpq9058QXaut8r55QWu6lcHX6i/GvF1PZVkV6o=
github.com/ans-group/sdk-go v1.16.3 h1:kxuEwIc6Z/0X0xGtpEN2KBJ3X24D4MkaCgD1Q0hWKHA=
github.com/ans-group/sdk-go v1.16.3/go.mod h1:p1vrXBxHPvMOGlS4sFUSgeLeKAl9vIe/lJ6UaExe49A=
github.com/ans-group/sdk-go v1.16.4 h1:Bqo0dJbpGlfDpfm+amkkOn9Oru6pIU2xkdP8ZXhKlXo=
github.com/ans-group/sdk-go v1.16.4/go.mod h1:p1vrXBxHPvMOGlS4sFUSgeLeKAl9vIe/lJ6UaExe49A=
github.com/ans-group/sdk-go v1.16.5 h1:FoBcdhM209iS2EfjVfq98GrDJNlJRu2mygZ0Xi8Mb3Q=
github.com/ans-group/sdk-go v1.16.5/go.mod h1:p1vrXBxHPvMOGlS4sFUSgeLeKAl9vIe/lJ6UaExe49A=
github.com/ans-group/sdk-go v1.16.6 h1:WPdKk8Ze8SDh2afStBWmaLdufPhLxYgGdEKThIboD24=
github.com/ans-group/sdk-go v1.16.6/go.mod h1:p1vrXBxHPvMOGlS4sFUSgeLeKAl9vIe/lJ6UaExe49A=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/apparentlymart/go-cidr v1.1.0 h1:2mAhrMoF+nhXqxTzSZMUzDHkLjmIHC+Zzn4tdgBZjnU=
github.com/apparentlymart/go-cidr v1.1.0/go.mod h1:EBcsNrHc3zQeuaeCeCtQruQm+n9/YjEn/vI25Lg7Gwc=
Expand Down

0 comments on commit 8b70cb9

Please sign in to comment.