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

Improve schema management #248

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
44 changes: 2 additions & 42 deletions outscale/data_source_outscale_access_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,65 +10,25 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-outscale/utils"
)

func dataSourceOutscaleAccessKey() *schema.Resource {
return &schema.Resource{
Read: dataSourceOutscaleAccessKeyRead,
Schema: map[string]*schema.Schema{
"filter": dataSourceFiltersSchema(),
"access_key_id": {
Type: schema.TypeString,
Optional: true,
},
"creation_date": {
Type: schema.TypeString,
Computed: true,
},
"expiration_date": {
Type: schema.TypeString,
Computed: true,
},
"last_modification_date": {
Type: schema.TypeString,
Computed: true,
},
"state": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"ACTIVE", "INACTIVE"}, false),
},
"request_id": {
Type: schema.TypeString,
Computed: true,
},
},
Read: dataSourceOutscaleAccessKeyRead,
Schema: getDataSourceSchemas(AccessKeySchema()),
}
}

func dataSourceOutscaleAccessKeyRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*OutscaleClient).OSCAPI

filters, filtersOk := d.GetOk("filter")
accessKeyID, accessKeyOk := d.GetOk("access_key_id")
state, stateOk := d.GetOk("state")

if !filtersOk && !accessKeyOk && !stateOk {
return fmt.Errorf("One of filters, access_key_id or state must be assigned")
}

filterReq := &oscgo.FiltersAccessKeys{}
if filtersOk {
filterReq = buildOutscaleDataSourceAccessKeyFilters(filters.(*schema.Set))
}
if accessKeyOk {
filterReq.SetAccessKeyIds([]string{accessKeyID.(string)})
}
if stateOk {
filterReq.SetStates([]string{state.(string)})
}

var resp oscgo.ReadAccessKeysResponse
var err error
Expand Down
36 changes: 2 additions & 34 deletions outscale/data_source_outscale_access_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,9 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccOutscaleDataSourceAccessKey_basic(t *testing.T) {
t.Parallel()
dataSourceName := "outscale_access_key.outscale_access_key"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccClientAccessKeyDataSourceBasic(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "access_key_id"),
resource.TestCheckResourceAttrSet(dataSourceName, "creation_date"),
resource.TestCheckResourceAttrSet(dataSourceName, "last_modification_date"),
resource.TestCheckResourceAttrSet(dataSourceName, "secret_key"),
resource.TestCheckResourceAttrSet(dataSourceName, "state"),
),
},
},
})
}

func TestAccOutscaleDataSourceAccessKey_withFilters(t *testing.T) {
t.Parallel()
dataSourceName := "outscale_access_key.outscale_access_key"
dataSourceName := "outscale_access_key.access_key"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -50,21 +28,11 @@ func TestAccOutscaleDataSourceAccessKey_withFilters(t *testing.T) {
})
}

func testAccClientAccessKeyDataSourceBasic() string {
return `
resource "outscale_access_key" "outscale_access_key" {}

data "outscale_access_key" "outscale_access_key" {
access_key_id = outscale_access_key.outscale_access_key.id
}
`
}

func testAccClientAccessKeyDataSourceWithFilters() string {
return `
resource "outscale_access_key" "outscale_access_key" {}

data "outscale_access_key" "outscale_access_key" {
Copy link
Contributor

Choose a reason for hiding this comment

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

But you change outscale_access_key to access_key, which change the resource name, so it's a breaking change.

Copy link
Contributor Author

@FredericBerot-Armand FredericBerot-Armand Nov 25, 2022

Choose a reason for hiding this comment

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

No, i change the name of the example, not the datasource.

But this PR is just for test some thing actually ^^"

data "outscale_access_key" "access_key" {
filter {
name = "access_key_ids"
values = [outscale_access_key.outscale_access_key.id]
Expand Down
64 changes: 2 additions & 62 deletions outscale/data_source_outscale_access_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,85 +9,25 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-outscale/utils"
)

func dataSourceOutscaleAccessKeys() *schema.Resource {
return &schema.Resource{
Read: dataSourceOutscaleAccessKeysRead,
Schema: map[string]*schema.Schema{
"filter": dataSourceFiltersSchema(),
"access_key_ids": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"states": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{"ACTIVE", "INACTIVE"}, false),
},
},
"access_keys": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"access_key_id": {
Type: schema.TypeString,
Computed: true,
},
"creation_date": {
Type: schema.TypeString,
Computed: true,
},
"expiration_date": {
Type: schema.TypeString,
Computed: true,
},
"last_modification_date": {
Type: schema.TypeString,
Computed: true,
},
"state": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"request_id": {
Type: schema.TypeString,
Computed: true,
},
},
Read: dataSourceOutscaleAccessKeysRead,
Schema: GetDataSourcesSchema("access_keys", AccessKeySchema()),
}
}

func dataSourceOutscaleAccessKeysRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*OutscaleClient).OSCAPI

filters, filtersOk := d.GetOk("filter")
accessKeyID, accessKeyOk := d.GetOk("access_key_ids")
state, stateOk := d.GetOk("states")

if !filtersOk && !accessKeyOk && !stateOk {
return fmt.Errorf("One of filters, access_key_ids or states must be assigned")
}

filterReq := &oscgo.FiltersAccessKeys{}
if filtersOk {
filterReq = buildOutscaleDataSourceAccessKeyFilters(filters.(*schema.Set))
}
if accessKeyOk {
filterReq.SetAccessKeyIds(expandStringValueList(accessKeyID.([]interface{})))
}
if stateOk {
filterReq.SetStates(expandStringValueList(state.([]interface{})))
}

var resp oscgo.ReadAccessKeysResponse
var err error
Expand Down
43 changes: 11 additions & 32 deletions outscale/data_source_outscale_access_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,38 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccOutscaleDataSourceAccessKeys_basic(t *testing.T) {
func TestAccOutscaleDataSourceAccessKeys(t *testing.T) {
t.Parallel()
dataSourceName := "data.outscale_access_keys.outscale_access_key"
dataSourceName := "data.outscale_access_keys.access_keys_with_filter"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccClientAccessKeysDataSourceBasic(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "access_keys.#"),
resource.TestCheckResourceAttrSet(dataSourceName, "access_key_ids.#"),
),
},
},
})
}

func TestAccOutscaleDataSourceAccessKeys_withFilters(t *testing.T) {
t.Parallel()
dataSourceName := "data.outscale_access_keys.outscale_access_key"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccClientAccessKeysDataSourceWithFilters(),
Config: testAccClientAccessKeysDataSource(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "access_keys.#"),
resource.TestCheckResourceAttrSet(dataSourceName, "filter.#"),
resource.TestCheckResourceAttrSet("data.outscale_access_keys.access_keys", "access_keys.#"),
),
},
},
})
}

func testAccClientAccessKeysDataSourceBasic() string {
func testAccClientAccessKeysDataSource() string {
return `
resource "outscale_access_key" "outscale_access_key" {}

data "outscale_access_keys" "outscale_access_key" {
access_key_ids = ["${outscale_access_key.outscale_access_key.id}"]
data "outscale_access_keys" "access_keys_with_filter" {
filter {
name = "access_key_ids"
values = ["${outscale_access_key.outscale_access_key.id}"]
}
}
`
}

func testAccClientAccessKeysDataSourceWithFilters() string {
return `
resource "outscale_access_key" "outscale_access_key" {}

data "outscale_access_keys" "outscale_access_key" {
data "outscale_access_keys" "access_keys" {
filter {
name = "access_key_ids"
values = ["${outscale_access_key.outscale_access_key.id}"]
Expand Down
Loading