Skip to content

Commit

Permalink
Add user and user_group resources
Browse files Browse the repository at this point in the history
  • Loading branch information
outscale-toa committed Sep 18, 2024
1 parent 3aa9a0f commit 19f23e3
Show file tree
Hide file tree
Showing 5 changed files with 693 additions and 0 deletions.
121 changes: 121 additions & 0 deletions outscale/data_source_outscale_user_groups.go
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
}
76 changes: 76 additions & 0 deletions outscale/data_source_outscale_users.go
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)
}
4 changes: 4 additions & 0 deletions outscale/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ func Provider() *schema.Provider {
"outscale_api_access_rule": ResourceOutscaleApiAccessRule(),
"outscale_api_access_policy": ResourceOutscaleApiAccessPolicy(),
"outscale_main_route_table_link": resourceLinkMainRouteTable(),
"outscale_user": ResourceOutscaleUser(),
"outscale_user_group": ResourceOutscaleUserGroup(),
},
DataSourcesMap: map[string]*schema.Resource{
"outscale_vm": DataSourceOutscaleVM(),
Expand Down Expand Up @@ -192,6 +194,8 @@ func Provider() *schema.Provider {
"outscale_public_catalog": DataSourceOutscalePublicCatalog(),
"outscale_account": DataSourceAccount(),
"outscale_accounts": DataSourceAccounts(),
"outscale_users": DataSourceUsers(),
"outscale_user_groups": DataSourceUserGroups(),
},

ConfigureFunc: providerConfigureClient,
Expand Down
160 changes: 160 additions & 0 deletions outscale/resource_outscale_user.go
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
}
Loading

0 comments on commit 19f23e3

Please sign in to comment.