Skip to content

Commit

Permalink
Fix Access key tests
Browse files Browse the repository at this point in the history
  • Loading branch information
outscale-toa committed Oct 14, 2024
1 parent 93b7cc4 commit e6aa8ed
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 8 deletions.
102 changes: 102 additions & 0 deletions outscale/data_source_outscale_user_groups_per_user.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions outscale/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
22 changes: 14 additions & 8 deletions outscale/resource_outscale_access_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down

0 comments on commit e6aa8ed

Please sign in to comment.