Skip to content

Commit

Permalink
Add user resource and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
outscale-toa committed Aug 9, 2024
1 parent 594abe4 commit 7353aeb
Show file tree
Hide file tree
Showing 7 changed files with 295 additions and 0 deletions.
1 change: 1 addition & 0 deletions outscale/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func Provider() *schema.Provider {
"outscale_api_access_rule": ResourceOutscaleApiAccessRule(),
"outscale_api_access_policy": ResourceOutscaleApiAccessPolicy(),
"outscale_main_route_table_link": resourceLinkMainRouteTable(),
"outscale_user": ResourceOutscaleUser(),
},
DataSourcesMap: map[string]*schema.Resource{
"outscale_vm": DataSourceOutscaleVM(),
Expand Down
155 changes: 155 additions & 0 deletions outscale/resource_outscale_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
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))

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(*resp.GetUser().UserId)

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.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{}

if d.HasChange("user_name") {
oldN, newN := d.GetChange("user_name")
oldName := oldN.(string)
newName := newN.(string)
req.UserName = oldName
req.NewUserName = &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
}
73 changes: 73 additions & 0 deletions outscale/resource_outscale_user_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package outscale

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccOthers_User_basic(t *testing.T) {
t.Parallel()
resourceName := "outscale_user.basic_user"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccOutscaleUserBasicConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "user_name"),
resource.TestCheckResourceAttr(resourceName, "path", "/"),
),
},
},
})
}

func TestAccOthers_User_update(t *testing.T) {
t.Parallel()
resourceName := "outscale_user.update_user"
name := "TestACC_user1"
newName := "TestACC_user2"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccOutscaleUserUpdatedConfig(name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "user_name"),
resource.TestCheckResourceAttr(resourceName, "path", "/"),
resource.TestCheckResourceAttr(resourceName, "user_name", name),
),
},
{
Config: testAccOutscaleUserUpdatedConfig(newName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "path"),
resource.TestCheckResourceAttrSet(resourceName, "user_name"),
resource.TestCheckResourceAttr(resourceName, "path", "/"),
resource.TestCheckResourceAttr(resourceName, "user_name", newName),
),
},
},
})
}

const testAccOutscaleUserBasicConfig = `
resource "outscale_user" "basic_user" {
user_name = "ACC_test1"
path = "/"
}`

func testAccOutscaleUserUpdatedConfig(name string) string {
return fmt.Sprintf(`
resource "outscale_user" "update_user" {
user_name = "%s"
path = "/"
}
`, name)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"version": "########",
"terraform_version": "########",
"serial": "########",
"lineage": "########",
"outputs": {},
"resources": [
{
"mode": "managed",
"type": "outscale_user",
"name": "userInteg",
"provider": "provider[\"registry.terraform.io/outscale/outscale\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"id": "##id-0##",
"path": "/",
"user_id": "##id-0##",
"user_name": "test_integ"
},
"sensitive_attributes": [],
"private": "bnVsbA=="
}
]
}
],
"check_results": "########"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resource "outscale_user" "userInteg" {
user_name = "test_integ"
path = "/"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"version": "########",
"terraform_version": "########",
"serial": "########",
"lineage": "########",
"outputs": {},
"resources": [
{
"mode": "managed",
"type": "outscale_user",
"name": "userInteg",
"provider": "provider[\"registry.terraform.io/outscale/outscale\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"id": "##id-0##",
"path": "/",
"user_id": "##id-0##",
"user_name": "test_integ_update"
},
"sensitive_attributes": [],
"private": "bnVsbA=="
}
]
}
],
"check_results": "########"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resource "outscale_user" "userInteg" {
user_name = "test_integ_update"
path = "/"
}

0 comments on commit 7353aeb

Please sign in to comment.