From 7353aebfc4845daf87d5687dc8af48ec90a04327 Mon Sep 17 00:00:00 2001 From: Thiery Ouattara Date: Fri, 9 Aug 2024 15:34:02 +0000 Subject: [PATCH] Add user resource and tests --- outscale/provider.go | 1 + outscale/resource_outscale_user.go | 155 ++++++++++++++++++ outscale/resource_outscale_user_test.go | 73 +++++++++ .../step1.user_resource_attributes_ok.ref | 29 ++++ .../step1.user_resource_attributes_ok.tf | 4 + .../step2.user_resource_attributes_ok.ref | 29 ++++ .../step2.user_resource_attributes_ok.tf | 4 + 7 files changed, 295 insertions(+) create mode 100644 outscale/resource_outscale_user.go create mode 100644 outscale/resource_outscale_user_test.go create mode 100644 tests/qa_provider_oapi/data/user/TF-01_user_resource_attributes_ok/step1.user_resource_attributes_ok.ref create mode 100644 tests/qa_provider_oapi/data/user/TF-01_user_resource_attributes_ok/step1.user_resource_attributes_ok.tf create mode 100644 tests/qa_provider_oapi/data/user/TF-01_user_resource_attributes_ok/step2.user_resource_attributes_ok.ref create mode 100644 tests/qa_provider_oapi/data/user/TF-01_user_resource_attributes_ok/step2.user_resource_attributes_ok.tf diff --git a/outscale/provider.go b/outscale/provider.go index 8e8757a2a..d9dbb8b13 100644 --- a/outscale/provider.go +++ b/outscale/provider.go @@ -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(), diff --git a/outscale/resource_outscale_user.go b/outscale/resource_outscale_user.go new file mode 100644 index 000000000..740894612 --- /dev/null +++ b/outscale/resource_outscale_user.go @@ -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 +} diff --git a/outscale/resource_outscale_user_test.go b/outscale/resource_outscale_user_test.go new file mode 100644 index 000000000..e853de90e --- /dev/null +++ b/outscale/resource_outscale_user_test.go @@ -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) +} diff --git a/tests/qa_provider_oapi/data/user/TF-01_user_resource_attributes_ok/step1.user_resource_attributes_ok.ref b/tests/qa_provider_oapi/data/user/TF-01_user_resource_attributes_ok/step1.user_resource_attributes_ok.ref new file mode 100644 index 000000000..a52545b4c --- /dev/null +++ b/tests/qa_provider_oapi/data/user/TF-01_user_resource_attributes_ok/step1.user_resource_attributes_ok.ref @@ -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": "########" +} \ No newline at end of file diff --git a/tests/qa_provider_oapi/data/user/TF-01_user_resource_attributes_ok/step1.user_resource_attributes_ok.tf b/tests/qa_provider_oapi/data/user/TF-01_user_resource_attributes_ok/step1.user_resource_attributes_ok.tf new file mode 100644 index 000000000..09b10093f --- /dev/null +++ b/tests/qa_provider_oapi/data/user/TF-01_user_resource_attributes_ok/step1.user_resource_attributes_ok.tf @@ -0,0 +1,4 @@ +resource "outscale_user" "userInteg" { + user_name = "test_integ" + path = "/" +} diff --git a/tests/qa_provider_oapi/data/user/TF-01_user_resource_attributes_ok/step2.user_resource_attributes_ok.ref b/tests/qa_provider_oapi/data/user/TF-01_user_resource_attributes_ok/step2.user_resource_attributes_ok.ref new file mode 100644 index 000000000..fbf01cad6 --- /dev/null +++ b/tests/qa_provider_oapi/data/user/TF-01_user_resource_attributes_ok/step2.user_resource_attributes_ok.ref @@ -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": "########" +} \ No newline at end of file diff --git a/tests/qa_provider_oapi/data/user/TF-01_user_resource_attributes_ok/step2.user_resource_attributes_ok.tf b/tests/qa_provider_oapi/data/user/TF-01_user_resource_attributes_ok/step2.user_resource_attributes_ok.tf new file mode 100644 index 000000000..de46ed943 --- /dev/null +++ b/tests/qa_provider_oapi/data/user/TF-01_user_resource_attributes_ok/step2.user_resource_attributes_ok.tf @@ -0,0 +1,4 @@ +resource "outscale_user" "userInteg" { + user_name = "test_integ_update" + path = "/" +}