-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add EIM user resource/datasources and update access_key
- Loading branch information
1 parent
ada216e
commit 489b0fe
Showing
21 changed files
with
958 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.