From 15842858b44ba5f54bd34fb95dad76b365fe44b4 Mon Sep 17 00:00:00 2001 From: outscale-rce Date: Tue, 30 Jul 2024 12:43:38 +0000 Subject: [PATCH 1/6] GoSDK to 2.21 permissionLaunch typo and pointer --- go.mod | 8 ++++---- go.sum | 16 ++++++++-------- .../resource_outscale_image_launch_permission.go | 8 ++++---- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/go.mod b/go.mod index 6e8280740..bb7579546 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/outscale/terraform-provider-outscale go 1.22.0 require ( - github.com/aws/aws-sdk-go v1.53.1 + github.com/aws/aws-sdk-go v1.55.1 github.com/davecgh/go-spew v1.1.1 github.com/go-test/deep v1.0.6 github.com/hashicorp/errwrap v1.1.0 @@ -13,7 +13,7 @@ require ( github.com/hashicorp/terraform-plugin-mux v0.12.0 github.com/hashicorp/terraform-plugin-sdk/v2 v2.29.0 github.com/nav-inc/datetime v0.1.3 - github.com/outscale/osc-sdk-go/v2 v2.19.0 + github.com/outscale/osc-sdk-go/v2 v2.21.0 github.com/spf13/cast v1.6.0 ) @@ -58,11 +58,11 @@ require ( golang.org/x/crypto v0.23.0 // indirect golang.org/x/mod v0.12.0 // indirect golang.org/x/net v0.25.0 // indirect - golang.org/x/oauth2 v0.17.0 // indirect + golang.org/x/oauth2 v0.21.0 // indirect golang.org/x/sys v0.20.0 // indirect golang.org/x/text v0.15.0 // indirect google.golang.org/appengine v1.6.8 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240221002015-b0ce06bbee7c // indirect google.golang.org/grpc v1.62.0 // indirect - google.golang.org/protobuf v1.34.1 // indirect + google.golang.org/protobuf v1.34.2 // indirect ) diff --git a/go.sum b/go.sum index 8cf61ce30..30ec40762 100644 --- a/go.sum +++ b/go.sum @@ -13,8 +13,8 @@ github.com/apparentlymart/go-textseg/v12 v12.0.0/go.mod h1:S/4uRK2UtaQttw1GenVJE github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew1u1fNQOlOtuGxQY= github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmmsvpAG721bKi0joRfFdHIWJ4= github.com/aws/aws-sdk-go v1.44.103/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo= -github.com/aws/aws-sdk-go v1.53.1 h1:15/i0m9rE8r1q3P4ooHCfZTJtkxwG2Dwqp9JhPaVbs0= -github.com/aws/aws-sdk-go v1.53.1/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk= +github.com/aws/aws-sdk-go v1.55.1 h1:ZTNPmbRMxaK5RlTJrBullX9r/rF1MPf3yAJOLlwDiT8= +github.com/aws/aws-sdk-go v1.55.1/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU= github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZlaQsNA= github.com/bufbuild/protocompile v0.4.0/go.mod h1:3v93+mbWn/v3xzN+31nwkJfrEpAUwp+BagBSZWx+TP8= github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= @@ -142,8 +142,8 @@ github.com/nav-inc/datetime v0.1.3 h1:PaybPUsScX+Cd3TEa1tYpfwU61deCEhMTlCO2hONm1 github.com/nav-inc/datetime v0.1.3/go.mod h1:gKGf5G+cW7qkTo5TC/sieNyz6lYdrA9cf1PNV+pXIOE= github.com/oklog/run v1.1.0 h1:GEenZ1cK0+q0+wsJew9qUg/DyD8k3JzYsZAi5gYi2mA= github.com/oklog/run v1.1.0/go.mod h1:sVPdnTZT1zYwAJeCMu2Th4T21pA3FPOQRfWjQlk7DVU= -github.com/outscale/osc-sdk-go/v2 v2.19.0 h1:lunUeIB0AfklKztI4LH9yTIvG5t/fnRXVzSAZLX9EjY= -github.com/outscale/osc-sdk-go/v2 v2.19.0/go.mod h1:kzhtUErCzKYl87bZ+kDMphDafmnwbsyFJY9iHF7NgNE= +github.com/outscale/osc-sdk-go/v2 v2.21.0 h1:8MqhexV+ALG76CvY8EpuQlr0+hC5VFrWQY4fHqdT358= +github.com/outscale/osc-sdk-go/v2 v2.21.0/go.mod h1:kzhtUErCzKYl87bZ+kDMphDafmnwbsyFJY9iHF7NgNE= github.com/pjbgf/sha1cd v0.3.0 h1:4D5XXmUUBUl/xQ6IjCkEAbqXskkq/4O7LmGn0AqMDs4= github.com/pjbgf/sha1cd v0.3.0/go.mod h1:nZ1rrWOcGJ5uZgEEVL1VUM9iRQiZvWdbZjkKyFzPPsI= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -197,8 +197,8 @@ golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= -golang.org/x/oauth2 v0.17.0 h1:6m3ZPmLEFdVxKKWnKq4VqZ60gutO35zm+zrAHVmHyDQ= -golang.org/x/oauth2 v0.17.0/go.mod h1:OzPDGQiuQMguemayvdylqddI7qcD9lnSDb+1FiwQ5HA= +golang.org/x/oauth2 v0.21.0 h1:tsimM75w1tF/uws5rbeHzIWxEqElMehnc+iW793zsZs= +golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -256,8 +256,8 @@ google.golang.org/grpc v1.62.0 h1:HQKZ/fa1bXkX1oFOvSjmZEUL8wLSaZTjCcLAlmZRtdk= google.golang.org/grpc v1.62.0/go.mod h1:IWTG0VlJLCh1SkC58F7np9ka9mx/WNkjl4PGJaiq+QE= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg= -google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= +google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= +google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/outscale/resource_outscale_image_launch_permission.go b/outscale/resource_outscale_image_launch_permission.go index 4de6ae4ff..0022ff548 100644 --- a/outscale/resource_outscale_image_launch_permission.go +++ b/outscale/resource_outscale_image_launch_permission.go @@ -140,17 +140,17 @@ func ResourceOutscaleImageLaunchPermissionCreate(d *schema.ResourceData, meta in } log.Printf("Creating Outscale Image Launch Permission, image_id (%+v)", imageID.(string)) - permissionLunch := oscgo.PermissionsOnResourceCreation{} + permissionLaunch := oscgo.PermissionsOnResourceCreation{} if permissionAdditions, ok := d.GetOk("permission_additions"); ok { - permissionLunch.SetAdditions(expandOAPIImagePermission(permissionAdditions)) + permissionLaunch.SetAdditions(expandOAPIImagePermission(permissionAdditions)) } if permissionRemovals, ok := d.GetOk("permission_removals"); ok { - permissionLunch.SetRemovals(expandOAPIImagePermission(permissionRemovals)) + permissionLaunch.SetRemovals(expandOAPIImagePermission(permissionRemovals)) } request := oscgo.UpdateImageRequest{ ImageId: imageID.(string), - PermissionsToLaunch: permissionLunch, + PermissionsToLaunch: &permissionLaunch, } err := resource.Retry(5*time.Minute, func() *resource.RetryError { From 152259cb3ebcdd95536bb792c162f1d5ea131b4e Mon Sep 17 00:00:00 2001 From: outscale-rce Date: Tue, 30 Jul 2024 12:45:45 +0000 Subject: [PATCH 2/6] ReadAccessKey instead of ReadSecretAccessKey --- outscale/resource_outscale_access_key.go | 35 ++++++++++++++---------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/outscale/resource_outscale_access_key.go b/outscale/resource_outscale_access_key.go index 451889930..be35dede7 100644 --- a/outscale/resource_outscale_access_key.go +++ b/outscale/resource_outscale_access_key.go @@ -92,6 +92,10 @@ func ResourceOutscaleAccessKeyCreate(d *schema.ResourceData, meta interface{}) e d.SetId(*resp.GetAccessKey().AccessKeyId) + if err := d.Set("secret_key", *resp.GetAccessKey().SecretKey); err != nil { + return err + } + if d.Get("state").(string) != "ACTIVE" { if err := updateAccessKey(conn, d.Id(), "INACTIVE"); err != nil { return err @@ -104,12 +108,17 @@ func ResourceOutscaleAccessKeyCreate(d *schema.ResourceData, meta interface{}) e func ResourceOutscaleAccessKeyRead(d *schema.ResourceData, meta interface{}) error { conn := meta.(*OutscaleClient).OSCAPI - filter := oscgo.ReadSecretAccessKeyRequest{ - AccessKeyId: d.Id(), + filter := oscgo.FiltersAccessKeys{ + AccessKeyIds: &[]string{d.Id()}, + } + + req := oscgo.ReadAccessKeysRequest{ + Filters: &filter, } - var resp oscgo.ReadSecretAccessKeyResponse + + var resp oscgo.ReadAccessKeysResponse err := resource.Retry(2*time.Minute, func() *resource.RetryError { - rp, httpResp, err := conn.AccessKeyApi.ReadSecretAccessKey(context.Background()).ReadSecretAccessKeyRequest(filter).Execute() + rp, httpResp, err := conn.AccessKeyApi.ReadAccessKeys(context.Background()).ReadAccessKeysRequest(req).Execute() if err != nil { return utils.CheckThrottling(httpResp, err) } @@ -121,28 +130,26 @@ func ResourceOutscaleAccessKeyRead(d *schema.ResourceData, meta interface{}) err return err } - accessKey, ok := resp.GetAccessKeyOk() - if !ok { + accessKey := resp.GetAccessKeys() + if len(accessKey) == 0 { d.SetId("") return nil } - if err := d.Set("access_key_id", accessKey.GetAccessKeyId()); err != nil { + if err := d.Set("access_key_id", accessKey[0].GetAccessKeyId()); err != nil { return err } - if err := d.Set("creation_date", accessKey.GetCreationDate()); err != nil { + if err := d.Set("creation_date", accessKey[0].GetCreationDate()); err != nil { return err } - if err := d.Set("expiration_date", accessKey.GetExpirationDate()); err != nil { + if err := d.Set("expiration_date", accessKey[0].GetExpirationDate()); err != nil { return err } - if err := d.Set("last_modification_date", accessKey.GetLastModificationDate()); err != nil { + if err := d.Set("last_modification_date", accessKey[0].GetLastModificationDate()); err != nil { return err } - if err := d.Set("secret_key", accessKey.GetSecretKey()); err != nil { - return err - } - if err := d.Set("state", accessKey.GetState()); err != nil { + + if err := d.Set("state", accessKey[0].GetState()); err != nil { return err } From bc7b32515698287cfd8d243fa052ac09e6b40e4f Mon Sep 17 00:00:00 2001 From: Thiery Ouattara Date: Thu, 8 Aug 2024 15:59:56 +0000 Subject: [PATCH 3/6] Fix acceptance and intragtion tests --- outscale/data_source_outscale_net_peerings_test.go | 7 +------ outscale/resource_outscale_public_ip_test.go | 1 - outscale/resource_outscale_security_group.go | 8 ++++++++ 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/outscale/data_source_outscale_net_peerings_test.go b/outscale/data_source_outscale_net_peerings_test.go index bb287f460..2080e071d 100644 --- a/outscale/data_source_outscale_net_peerings_test.go +++ b/outscale/data_source_outscale_net_peerings_test.go @@ -43,17 +43,12 @@ const testAccDataSourceOutscaleLinPeeringsConnectionConfig = ` accepter_net_id = outscale_net.outscale_net.net_id source_net_id = outscale_net.outscale_net2.net_id tags { - key = "okht" + key = "name" value = "testacc-peerings-ds" } } - resource "outscale_net_peering" "outscale_net_peering2" { - accepter_net_id = outscale_net.outscale_net.net_id - source_net_id = outscale_net.outscale_net2.net_id - } - data "outscale_net_peerings" "outscale_net_peerings" { filter { name = "net_peering_ids" diff --git a/outscale/resource_outscale_public_ip_test.go b/outscale/resource_outscale_public_ip_test.go index d90530a93..f41dbcfbb 100644 --- a/outscale/resource_outscale_public_ip_test.go +++ b/outscale/resource_outscale_public_ip_test.go @@ -38,7 +38,6 @@ func TestAccOthers_PublicIP_basic(t *testing.T) { } func TestAccVM_PublicIP_instance(t *testing.T) { - t.Parallel() var conf oscgo.PublicIp omi := os.Getenv("OUTSCALE_IMAGEID") region := utils.GetRegion() diff --git a/outscale/resource_outscale_security_group.go b/outscale/resource_outscale_security_group.go index 0ed5d3571..8963ab650 100644 --- a/outscale/resource_outscale_security_group.go +++ b/outscale/resource_outscale_security_group.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "log" + "net/http" "strings" "time" @@ -267,11 +268,18 @@ func ResourceOutscaleSecurityGroupDelete(d *schema.ResourceData, meta interface{ log.Printf("[DEBUG] Security Group destroy: %v", d.Id()) securityGroupID := d.Id() + sg, _, err := readSecurityGroups(conn, securityGroupID) + if err != nil { + return err + } return resource.Retry(5*time.Minute, func() *resource.RetryError { _, httpResp, err := conn.SecurityGroupApi.DeleteSecurityGroup(context.Background()).DeleteSecurityGroupRequest(oscgo.DeleteSecurityGroupRequest{ SecurityGroupId: &securityGroupID, }).Execute() if err != nil { + if sg.GetNetId() != "" && httpResp.StatusCode == http.StatusConflict { + return utils.CheckThrottling(httpResp, err) + } if strings.Contains(err.Error(), "DependencyProblem") { return resource.RetryableError(err) } From 5a0ef248c8477db0c7bb6a94274a0992c6d4e039 Mon Sep 17 00:00:00 2001 From: Thiery Ouattara Date: Fri, 9 Aug 2024 15:37:06 +0000 Subject: [PATCH 4/6] Update go.mod and go.sum --- go.mod | 4 ++-- go.sum | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index bb7579546..2d0ab0a2f 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/outscale/terraform-provider-outscale go 1.22.0 require ( - github.com/aws/aws-sdk-go v1.55.1 + github.com/aws/aws-sdk-go v1.55.5 github.com/davecgh/go-spew v1.1.1 github.com/go-test/deep v1.0.6 github.com/hashicorp/errwrap v1.1.0 @@ -58,7 +58,7 @@ require ( golang.org/x/crypto v0.23.0 // indirect golang.org/x/mod v0.12.0 // indirect golang.org/x/net v0.25.0 // indirect - golang.org/x/oauth2 v0.21.0 // indirect + golang.org/x/oauth2 v0.22.0 // indirect golang.org/x/sys v0.20.0 // indirect golang.org/x/text v0.15.0 // indirect google.golang.org/appengine v1.6.8 // indirect diff --git a/go.sum b/go.sum index 30ec40762..b5754e1d8 100644 --- a/go.sum +++ b/go.sum @@ -13,8 +13,8 @@ github.com/apparentlymart/go-textseg/v12 v12.0.0/go.mod h1:S/4uRK2UtaQttw1GenVJE github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew1u1fNQOlOtuGxQY= github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmmsvpAG721bKi0joRfFdHIWJ4= github.com/aws/aws-sdk-go v1.44.103/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo= -github.com/aws/aws-sdk-go v1.55.1 h1:ZTNPmbRMxaK5RlTJrBullX9r/rF1MPf3yAJOLlwDiT8= -github.com/aws/aws-sdk-go v1.55.1/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU= +github.com/aws/aws-sdk-go v1.55.5 h1:KKUZBfBoyqy5d3swXyiC7Q76ic40rYcbqH7qjh59kzU= +github.com/aws/aws-sdk-go v1.55.5/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU= github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZlaQsNA= github.com/bufbuild/protocompile v0.4.0/go.mod h1:3v93+mbWn/v3xzN+31nwkJfrEpAUwp+BagBSZWx+TP8= github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= @@ -197,8 +197,8 @@ golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= -golang.org/x/oauth2 v0.21.0 h1:tsimM75w1tF/uws5rbeHzIWxEqElMehnc+iW793zsZs= -golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= +golang.org/x/oauth2 v0.22.0 h1:BzDx2FehcG7jJwgWLELCdmLuxk2i+x9UDpSiss2u0ZA= +golang.org/x/oauth2 v0.22.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= From 594abe46dfb7e16737dfd73470b46e4aefb3e7aa Mon Sep 17 00:00:00 2001 From: Thiery Ouattara Date: Fri, 9 Aug 2024 15:36:08 +0000 Subject: [PATCH 5/6] Update .gitignore --- .gitignore | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.gitignore b/.gitignore index 197fcd5b7..b30efa89a 100644 --- a/.gitignore +++ b/.gitignore @@ -34,6 +34,11 @@ terraformStatePull.json /tests/old.py /tests/tmp.txt /tests/qa_provider_oapi/terraform.d/ +outscale/certificate.key +outscale/certificate.pem +outscale/gen-cert-test.tf +terraform-provider-outscale_v0.5.32 +tests/qa_provider_oapi/data/cert_example/ # docs docs/generation_log.txt From 7353aebfc4845daf87d5687dc8af48ec90a04327 Mon Sep 17 00:00:00 2001 From: Thiery Ouattara Date: Fri, 9 Aug 2024 15:34:02 +0000 Subject: [PATCH 6/6] 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 = "/" +}