-
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.
- Loading branch information
1 parent
3aa9a0f
commit 19f23e3
Showing
5 changed files
with
693 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package outscale | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"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 DataSourceUserGroups() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: DataSourceUserGroupsRead, | ||
Schema: map[string]*schema.Schema{ | ||
"filter": dataSourceFiltersSchema(), | ||
"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 DataSourceUserGroupsRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*OutscaleClient).OSCAPI | ||
|
||
filters, filtersOk := d.GetOk("filter") | ||
|
||
if !filtersOk { | ||
return fmt.Errorf("One of filters, user_group_id or path_refix must be assigned") | ||
} | ||
|
||
filterReq := buildUserGroupsFilters(filters.(*schema.Set)) | ||
req := oscgo.ReadUserGroupsRequest{} | ||
req.SetFilters(*filterReq) | ||
var resp oscgo.ReadUserGroupsResponse | ||
err := resource.Retry(2*time.Minute, func() *resource.RetryError { | ||
rp, httpResp, err := conn.UserGroupApi.ReadUserGroups(context.Background()).ReadUserGroupsRequest(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) | ||
} | ||
|
||
func buildUserGroupsFilters(set *schema.Set) *oscgo.FiltersUserGroup { | ||
var filters oscgo.FiltersUserGroup | ||
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 "path_prefix": | ||
filters.SetPathPrefix(filterValues[0]) | ||
case "user_group_ids": | ||
filters.SetUserGroupIds(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,76 @@ | ||
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{ | ||
"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, | ||
}, | ||
"path": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func DataSourceUsersRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*OutscaleClient).OSCAPI | ||
req := oscgo.NewReadUsersRequest() | ||
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["path"] = v.GetPath() | ||
usersToSet[i] = user | ||
} | ||
return d.Set("users", usersToSet) | ||
} |
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,160 @@ | ||
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 ResourceOutscaleUser() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: ResourceOutscaleUserCreate, | ||
Read: ResourceOutscaleUserRead, | ||
Update: ResourceOutscaleUserUpdate, | ||
Delete: ResourceOutscaleUserDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"user_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"user_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"path": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func ResourceOutscaleUserCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*OutscaleClient).OSCAPI | ||
|
||
req := oscgo.NewCreateUserRequest(d.Get("user_name").(string)) | ||
if path := d.Get("path").(string); path != "" { | ||
req.Path = &path | ||
} | ||
|
||
var resp oscgo.CreateUserResponse | ||
err := resource.Retry(5*time.Minute, func() *resource.RetryError { | ||
rp, httpResp, err := conn.UserApi.CreateUser(context.Background()).CreateUserRequest(*req).Execute() | ||
if err != nil { | ||
return utils.CheckThrottling(httpResp, err) | ||
} | ||
resp = rp | ||
return nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
d.SetId(resource.UniqueId()) | ||
if err := d.Set("user_id", *resp.GetUser().UserId); err != nil { | ||
return err | ||
} | ||
|
||
return ResourceOutscaleUserRead(d, meta) | ||
} | ||
|
||
func ResourceOutscaleUserRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*OutscaleClient).OSCAPI | ||
req := oscgo.NewReadUsersRequest() | ||
|
||
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() | ||
if len(users) == 0 { | ||
d.SetId("") | ||
return nil | ||
} | ||
for _, user := range users { | ||
if user.GetUserId() == d.Get("user_id") { | ||
|
||
if err := d.Set("user_name", user.GetUserName()); err != nil { | ||
return err | ||
} | ||
if err := d.Set("user_id", user.GetUserId()); err != nil { | ||
return err | ||
} | ||
if err := d.Set("path", user.GetPath()); err != nil { | ||
return err | ||
} | ||
break | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func ResourceOutscaleUserUpdate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*OutscaleClient).OSCAPI | ||
req := oscgo.UpdateUserRequest{} | ||
|
||
oldN, newN := d.GetChange("user_name") | ||
if oldName := oldN.(string); oldName != "" { | ||
req.SetUserName(oldName) | ||
} | ||
if newName := newN.(string); newName != "" && oldN.(string) != newN.(string) { | ||
req.SetNewUserName(newName) | ||
} | ||
|
||
if d.HasChange("path") { | ||
path := d.Get("path").(string) | ||
req.NewPath = &path | ||
} | ||
|
||
err := resource.Retry(2*time.Minute, func() *resource.RetryError { | ||
_, httpResp, err := conn.UserApi.UpdateUser(context.Background()).UpdateUserRequest(req).Execute() | ||
if err != nil { | ||
return utils.CheckThrottling(httpResp, err) | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
return ResourceOutscaleUserRead(d, meta) | ||
} | ||
|
||
func ResourceOutscaleUserDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*OutscaleClient).OSCAPI | ||
|
||
req := oscgo.DeleteUserRequest{ | ||
UserName: d.Get("user_name").(string), | ||
} | ||
|
||
var err error | ||
err = resource.Retry(5*time.Minute, func() *resource.RetryError { | ||
_, httpResp, err := conn.UserApi.DeleteUser(context.Background()).DeleteUserRequest(req).Execute() | ||
if err != nil { | ||
return utils.CheckThrottling(httpResp, err) | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("Error deleting Outscale Access Key %s: %s", d.Id(), err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.