-
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
594abe4
commit 7353aeb
Showing
7 changed files
with
295 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
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,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 | ||
} |
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,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) | ||
} |
29 changes: 29 additions & 0 deletions
29
...er_oapi/data/user/TF-01_user_resource_attributes_ok/step1.user_resource_attributes_ok.ref
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,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": "########" | ||
} |
4 changes: 4 additions & 0 deletions
4
...der_oapi/data/user/TF-01_user_resource_attributes_ok/step1.user_resource_attributes_ok.tf
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,4 @@ | ||
resource "outscale_user" "userInteg" { | ||
user_name = "test_integ" | ||
path = "/" | ||
} |
29 changes: 29 additions & 0 deletions
29
...er_oapi/data/user/TF-01_user_resource_attributes_ok/step2.user_resource_attributes_ok.ref
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,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": "########" | ||
} |
4 changes: 4 additions & 0 deletions
4
...der_oapi/data/user/TF-01_user_resource_attributes_ok/step2.user_resource_attributes_ok.tf
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,4 @@ | ||
resource "outscale_user" "userInteg" { | ||
user_name = "test_integ_update" | ||
path = "/" | ||
} |