Skip to content

Commit

Permalink
Reduce access_key
Browse files Browse the repository at this point in the history
  • Loading branch information
FredericBerot-Armand committed Dec 6, 2022
1 parent 9423826 commit c953c1c
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 208 deletions.
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" {
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
75 changes: 37 additions & 38 deletions outscale/resource_outscale_access_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,42 @@ import (
"github.com/terraform-providers/terraform-provider-outscale/utils"
)

func AccessKeySchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"access_key_id": {
Type: schema.TypeString,
Computed: true,
},
"creation_date": {
Type: schema.TypeString,
Computed: true,
},
"expiration_date": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
date1, _ := datetime.Parse(new, time.UTC)
date2, _ := datetime.Parse(old, time.UTC)
return date1.Equal(date2)
},
},
"last_modification_date": {
Type: schema.TypeString,
Computed: true,
},
"secret_key": {
Type: schema.TypeString,
Computed: true,
},
"state": {
Type: schema.TypeString,
Optional: true,
Default: "ACTIVE",
ValidateFunc: validation.StringInSlice([]string{"ACTIVE", "INACTIVE"}, false),
},
}
}

func resourceOutscaleAccessKey() *schema.Resource {
return &schema.Resource{
Create: resourceOutscaleAccessKeyCreate,
Expand All @@ -22,44 +58,7 @@ func resourceOutscaleAccessKey() *schema.Resource {
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

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,
Optional: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
date1, _ := datetime.Parse(new, time.UTC)
date2, _ := datetime.Parse(old, time.UTC)
return date1.Equal(date2)
},
},
"last_modification_date": {
Type: schema.TypeString,
Computed: true,
},
"secret_key": {
Type: schema.TypeString,
Computed: true,
},
"state": {
Type: schema.TypeString,
Optional: true,
Default: "ACTIVE",
ValidateFunc: validation.StringInSlice([]string{"ACTIVE", "INACTIVE"}, false),
},
"request_id": {
Type: schema.TypeString,
Computed: true,
},
},
Schema: GetResourceSchema(AccessKeySchema()),
}
}

Expand Down

0 comments on commit c953c1c

Please sign in to comment.