From e6aa8ede244c9f09da6ad35b2b9e4e6d7fe0102a Mon Sep 17 00:00:00 2001 From: Thiery Ouattara Date: Fri, 11 Oct 2024 12:57:16 +0000 Subject: [PATCH] Fix Access key tests --- ...ta_source_outscale_user_groups_per_user.go | 102 ++++++++++++++++++ outscale/provider.go | 1 + outscale/resource_outscale_access_key_test.go | 22 ++-- 3 files changed, 117 insertions(+), 8 deletions(-) create mode 100644 outscale/data_source_outscale_user_groups_per_user.go diff --git a/outscale/data_source_outscale_user_groups_per_user.go b/outscale/data_source_outscale_user_groups_per_user.go new file mode 100644 index 000000000..b74073ee1 --- /dev/null +++ b/outscale/data_source_outscale_user_groups_per_user.go @@ -0,0 +1,102 @@ +package outscale + +import ( + "context" + "fmt" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + oscgo "github.com/outscale/osc-sdk-go/v2" + "github.com/outscale/terraform-provider-outscale/utils" +) + +func DataSourceUserGroupsPerUser() *schema.Resource { + return &schema.Resource{ + Read: DataSourceUserGroupsPerUserRead, + Schema: map[string]*schema.Schema{ + "user_name": { + Type: schema.TypeString, + Required: true, + }, + "user_path": { + Type: schema.TypeString, + Optional: true, + }, + "user_groups": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "user_group_name": { + Type: schema.TypeString, + Computed: true, + }, + "path": { + Type: schema.TypeString, + Computed: true, + }, + "user_group_id": { + Type: schema.TypeString, + Computed: true, + }, + "orn": { + Type: schema.TypeString, + Computed: true, + }, + "creation_date": { + Type: schema.TypeString, + Computed: true, + }, + "last_modification_date": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + }, + } +} + +func DataSourceUserGroupsPerUserRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*OutscaleClient).OSCAPI + + req := oscgo.NewReadUserGroupsPerUserRequest(d.Get("user_name").(string)) + userPath := d.Get("user_path").(string) + if userPath != "" { + req.SetUserPath(userPath) + } + var resp oscgo.ReadUserGroupsPerUserResponse + err := resource.Retry(2*time.Minute, func() *resource.RetryError { + rp, httpResp, err := conn.UserGroupApi.ReadUserGroupsPerUser(context.Background()).ReadUserGroupsPerUserRequest(*req).Execute() + if err != nil { + return utils.CheckThrottling(httpResp, err) + } + resp = rp + return nil + }) + + if err != nil { + return err + } + + if _, ok := resp.GetUserGroupsOk(); !ok { + return fmt.Errorf("Unable to find user groups") + } + d.SetId(resource.UniqueId()) + userGps := resp.GetUserGroups() + userGroups := make([]map[string]interface{}, len(userGps)) + + for i, v := range userGps { + userGroup := make(map[string]interface{}) + userGroup["user_group_name"] = v.GetName() + userGroup["user_group_id"] = v.GetUserGroupId() + userGroup["path"] = v.GetPath() + userGroup["orn"] = v.GetOrn() + userGroup["creation_date"] = v.GetCreationDate() + userGroup["last_modification_date"] = v.GetLastModificationDate() + userGroups[i] = userGroup + } + return d.Set("user_groups", userGroups) +} diff --git a/outscale/provider.go b/outscale/provider.go index 0e7dada11..6a0e5cf42 100644 --- a/outscale/provider.go +++ b/outscale/provider.go @@ -199,6 +199,7 @@ func Provider() *schema.Provider { "outscale_users": DataSourceUsers(), "outscale_user": DataSourceUser(), "outscale_user_groups": DataSourceUserGroups(), + "outscale_user_groups_per_user": DataSourceUserGroupsPerUser(), "outscale_user_group": DataSourceUserGroup(), "outscale_policy": DataSourcePolicy(), "outscale_policies": DataSourcePolicies(), diff --git a/outscale/resource_outscale_access_key_test.go b/outscale/resource_outscale_access_key_test.go index 8e0fe308a..77af4f01a 100644 --- a/outscale/resource_outscale_access_key_test.go +++ b/outscale/resource_outscale_access_key_test.go @@ -170,12 +170,15 @@ func testAccCheckOutscaleAccessKeyExists(resourceName string) resource.TestCheck return fmt.Errorf("No Access ID is set") } conn := testAccProvider.Meta().(*OutscaleClient).OSCAPI - - filter := oscgo.ReadSecretAccessKeyRequest{ - AccessKeyId: rs.Primary.ID, + filter := oscgo.FiltersAccessKeys{ + AccessKeyIds: &[]string{rs.Primary.ID}, + } + req := oscgo.ReadAccessKeysRequest{ + Filters: &filter, } + err := resource.Retry(2*time.Minute, func() *resource.RetryError { - _, httpResp, err := conn.AccessKeyApi.ReadSecretAccessKey(context.Background()).ReadSecretAccessKeyRequest(filter).Execute() + _, httpResp, err := conn.AccessKeyApi.ReadAccessKeys(context.Background()).ReadAccessKeysRequest(req).Execute() if err != nil { return utils.CheckThrottling(httpResp, err) } @@ -195,12 +198,15 @@ func testAccCheckOutscaleAccessKeyDestroy(s *terraform.State) error { if rs.Type != "outscale_access_key" { continue } - - filter := oscgo.ReadSecretAccessKeyRequest{ - AccessKeyId: rs.Primary.ID, + filter := oscgo.FiltersAccessKeys{ + AccessKeyIds: &[]string{rs.Primary.ID}, + } + req := oscgo.ReadAccessKeysRequest{ + Filters: &filter, } + err := resource.Retry(2*time.Minute, func() *resource.RetryError { - _, httpResp, err := conn.AccessKeyApi.ReadSecretAccessKey(context.Background()).ReadSecretAccessKeyRequest(filter).Execute() + _, httpResp, err := conn.AccessKeyApi.ReadAccessKeys(context.Background()).ReadAccessKeysRequest(req).Execute() if err != nil { return utils.CheckThrottling(httpResp, err) }