Skip to content

Commit

Permalink
Add EIM user resource/datasources and update access_key
Browse files Browse the repository at this point in the history
  • Loading branch information
outscale-toa committed Dec 19, 2024
1 parent ada216e commit 489b0fe
Show file tree
Hide file tree
Showing 21 changed files with 958 additions and 49 deletions.
24 changes: 17 additions & 7 deletions outscale/data_source_outscale_access_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func DataSourceOutscaleAccessKey() *schema.Resource {
Read: DataSourceOutscaleAccessKeyRead,
Schema: map[string]*schema.Schema{
"filter": dataSourceFiltersSchema(),
"user_name": {
Type: schema.TypeString,
Optional: true,
},
"access_key_id": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -54,12 +58,14 @@ func DataSourceOutscaleAccessKeyRead(d *schema.ResourceData, meta interface{}) e
filters, filtersOk := d.GetOk("filter")
accessKeyID, accessKeyOk := d.GetOk("access_key_id")
state, stateOk := d.GetOk("state")
userName, userNameOk := d.GetOk("user_name")

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

filterReq := &oscgo.FiltersAccessKeys{}

if filtersOk {
filterReq = buildOutscaleDataSourceAccessKeyFilters(filters.(*schema.Set))
}
Expand All @@ -69,11 +75,15 @@ func DataSourceOutscaleAccessKeyRead(d *schema.ResourceData, meta interface{}) e
if stateOk {
filterReq.SetStates([]string{state.(string)})
}

req := oscgo.ReadAccessKeysRequest{}
req.SetFilters(*filterReq)
if userNameOk {
req.SetUserName(userName.(string))
}
var resp oscgo.ReadAccessKeysResponse
var err error
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
rp, httpResp, err := conn.AccessKeyApi.ReadAccessKeys(context.Background()).ReadAccessKeysRequest(oscgo.ReadAccessKeysRequest{Filters: filterReq}).Execute()

err := resource.Retry(5*time.Minute, func() *resource.RetryError {
rp, httpResp, err := conn.AccessKeyApi.ReadAccessKeys(context.Background()).ReadAccessKeysRequest(req).Execute()
if err != nil {
return utils.CheckThrottling(httpResp, err)
}
Expand All @@ -85,7 +95,7 @@ func DataSourceOutscaleAccessKeyRead(d *schema.ResourceData, meta interface{}) e
}

if len(resp.GetAccessKeys()) == 0 {
return fmt.Errorf("Unable to find Access Key")
return fmt.Errorf("unable to find Access Key")
}

if len(resp.GetAccessKeys()) > 1 {
Expand Down
25 changes: 14 additions & 11 deletions outscale/data_source_outscale_access_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func DataSourceOutscaleAccessKeys() *schema.Resource {
ValidateFunc: validation.StringInSlice([]string{"ACTIVE", "INACTIVE"}, false),
},
},
"user_name": {
Type: schema.TypeString,
Optional: true,
},
"access_keys": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -73,12 +77,8 @@ func DataSourceOutscaleAccessKeysRead(d *schema.ResourceData, meta interface{})
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))
}
Expand All @@ -88,13 +88,16 @@ func DataSourceOutscaleAccessKeysRead(d *schema.ResourceData, meta interface{})
if stateOk {
filterReq.SetStates(utils.InterfaceSliceToStringSlice(state.([]interface{})))
}
req := oscgo.ReadAccessKeysRequest{
Filters: filterReq,
}

if userName := d.Get("user_name").(string); userName != "" {
req.SetUserName(userName)
}
var resp oscgo.ReadAccessKeysResponse
var err error
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
rp, httpResp, err := conn.AccessKeyApi.ReadAccessKeys(context.Background()).ReadAccessKeysRequest(oscgo.ReadAccessKeysRequest{
Filters: filterReq,
}).Execute()
err := resource.Retry(5*time.Minute, func() *resource.RetryError {
rp, httpResp, err := conn.AccessKeyApi.ReadAccessKeys(context.Background()).ReadAccessKeysRequest(req).Execute()
if err != nil {
return utils.CheckThrottling(httpResp, err)
}
Expand All @@ -106,7 +109,7 @@ func DataSourceOutscaleAccessKeysRead(d *schema.ResourceData, meta interface{})
}

if len(resp.GetAccessKeys()) == 0 {
return fmt.Errorf("Unable to find Access Keys")
return fmt.Errorf("unable to find Access Keys")
}

if err := d.Set("access_keys", flattenAccessKeys(resp.GetAccessKeys())); err != nil {
Expand Down
119 changes: 119 additions & 0 deletions outscale/data_source_outscale_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package outscale

import (
"context"
"fmt"
"log"
"time"

oscgo "github.com/outscale/osc-sdk-go/v2"
"github.com/outscale/terraform-provider-outscale/utils"

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

func DataSourceUser() *schema.Resource {
return &schema.Resource{
Read: DataSourceUserRead,
Schema: map[string]*schema.Schema{
"filter": dataSourceFiltersSchema(),
"user_name": {
Type: schema.TypeString,
Computed: true,
},
"user_id": {
Type: schema.TypeString,
Computed: true,
},
"user_email": {
Type: schema.TypeString,
Computed: true,
},
"path": {
Type: schema.TypeString,
Computed: true,
},
"creation_date": {
Type: schema.TypeString,
Computed: true,
},
"last_modification_date": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func DataSourceUserRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*OutscaleClient).OSCAPI
filters, filtersOk := d.GetOk("filter")
if !filtersOk {
return fmt.Errorf("filters: user_ids must be assigned")
}
req := oscgo.NewReadUsersRequest()
filterReq := buildUsersFilters(filters.(*schema.Set))
req.SetFilters(*filterReq)
var resp oscgo.ReadUsersResponse

err := resource.Retry(2*time.Minute, func() *resource.RetryError {
rp, httpResp, err := conn.UserApi.ReadUsers(context.Background()).ReadUsersRequest(*req).Execute()
if err != nil {
return utils.CheckThrottling(httpResp, err)
}
resp = rp
return nil
})

if err != nil {
return err
}
users := resp.GetUsers()
d.SetId(resource.UniqueId())
if len(users) == 0 {
return fmt.Errorf("Unable to find user")
}
if len(users) > 1 {
return fmt.Errorf("Find To many users")
}

if err := d.Set("user_name", users[0].GetUserName()); err != nil {
return err
}
if err := d.Set("user_email", users[0].GetUserEmail()); err != nil {
return err
}
if err := d.Set("user_id", users[0].GetUserId()); err != nil {
return err
}
if err := d.Set("path", users[0].GetPath()); err != nil {
return err
}
if err := d.Set("creation_date", users[0].GetCreationDate()); err != nil {
return err
}
if err := d.Set("last_modification_date", users[0].GetLastModificationDate()); err != nil {
return err
}
return nil
}

func buildUsersFilters(set *schema.Set) *oscgo.FiltersUsers {
var filters oscgo.FiltersUsers
for _, v := range set.List() {
m := v.(map[string]interface{})
var filterValues []string
for _, e := range m["values"].([]interface{}) {
filterValues = append(filterValues, e.(string))
}

switch name := m["name"].(string); name {
case "user_ids":
filters.SetUserIds(filterValues)
default:
log.Printf("[Debug] Unknown Filter Name: %s.", name)
}
}
return &filters
}
38 changes: 38 additions & 0 deletions outscale/data_source_outscale_user_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package outscale

import (
"testing"

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

func TestAccOthers_data_user_basic(t *testing.T) {
t.Parallel()
resourceName := "data.outscale_user.basicTestUser"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataUserBasicConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "user_id"),
),
},
},
})
}

const testAccDataUserBasicConfig = `
resource "outscale_user" "basic_dataUser" {
user_name = "ACC_user_data1"
path = "/"
}
data "outscale_user" "basicTestUser" {
filter {
name = "user_ids"
values = [outscale_user.basic_dataUser.user_id]
}
}
`
97 changes: 97 additions & 0 deletions outscale/data_source_outscale_users.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package outscale

import (
"context"
"fmt"
"time"

oscgo "github.com/outscale/osc-sdk-go/v2"
"github.com/outscale/terraform-provider-outscale/utils"

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

func DataSourceUsers() *schema.Resource {
return &schema.Resource{
Read: DataSourceUsersRead,
Schema: map[string]*schema.Schema{
"filter": dataSourceFiltersSchema(),
"users": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"user_name": {
Type: schema.TypeString,
Computed: true,
},
"user_id": {
Type: schema.TypeString,
Computed: true,
},
"user_email": {
Type: schema.TypeString,
Computed: true,
},
"path": {
Type: schema.TypeString,
Computed: true,
},
"creation_date": {
Type: schema.TypeString,
Computed: true,
},
"last_modification_date": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

func DataSourceUsersRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*OutscaleClient).OSCAPI
filters, filtersOk := d.GetOk("filter")
req := oscgo.NewReadUsersRequest()
if filtersOk {
filterReq := buildUsersFilters(filters.(*schema.Set))
req.SetFilters(*filterReq)
}
var resp oscgo.ReadUsersResponse

err := resource.Retry(2*time.Minute, func() *resource.RetryError {
rp, httpResp, err := conn.UserApi.ReadUsers(context.Background()).ReadUsersRequest(*req).Execute()
if err != nil {
return utils.CheckThrottling(httpResp, err)
}
resp = rp
return nil
})

if err != nil {
return err
}
users := resp.GetUsers()
d.SetId(resource.UniqueId())
if len(users) == 0 {
return fmt.Errorf("Unable to find users")
}
d.SetId(resource.UniqueId())
usersToSet := make([]map[string]interface{}, len(users))
for i, v := range users {
user := make(map[string]interface{})

user["user_id"] = v.GetUserId()
user["user_name"] = v.GetUserName()
user["user_email"] = v.GetUserEmail()
user["path"] = v.GetPath()
user["creation_date"] = v.GetCreationDate()
user["last_modification_date"] = v.GetLastModificationDate()
usersToSet[i] = user
}
return d.Set("users", usersToSet)
}
Loading

0 comments on commit 489b0fe

Please sign in to comment.