diff --git a/go.mod b/go.mod index 03633424..8f55c082 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.1 github.com/selectel/dbaas-go v0.7.0 github.com/selectel/domains-go v0.4.0 - github.com/selectel/go-selvpcclient/v2 v2.1.0 + github.com/selectel/go-selvpcclient/v2 v2.1.1 github.com/selectel/mks-go v0.12.0 github.com/stretchr/testify v1.7.2 ) diff --git a/go.sum b/go.sum index 3fde901c..dd8def2a 100644 --- a/go.sum +++ b/go.sum @@ -168,8 +168,8 @@ github.com/selectel/dbaas-go v0.7.0 h1:IBEU7EAPlkQ7wGjtIz9bppthpTZLGn3E6TVX7JWMt github.com/selectel/dbaas-go v0.7.0/go.mod h1:8D945oFzpx94v08zIb4s1bRTPCdPoNVnBu4umMYFJrQ= github.com/selectel/domains-go v0.4.0 h1:mVUeJK8oW9XMizft7Vu4OCyvjbzq4+o+zHgzJ2ZxnIY= github.com/selectel/domains-go v0.4.0/go.mod h1:AhXhwyMSTkpEWFiBLUvzFP76W+WN+ZblwmjLJLt7y58= -github.com/selectel/go-selvpcclient/v2 v2.1.0 h1:6CblJtXrL03IbBSgVrX2HU92cfiaPMB7v8kbS1mQN3M= -github.com/selectel/go-selvpcclient/v2 v2.1.0/go.mod h1:kFPnYYxcgJHybnmYEmZ9S+G0MNe8wBmYhhCkEqYjAuc= +github.com/selectel/go-selvpcclient/v2 v2.1.1 h1:dW8AEDeDkMCBb94NMCiNq/vK4n+f6kcGKsUuMwBcq+A= +github.com/selectel/go-selvpcclient/v2 v2.1.1/go.mod h1:kFPnYYxcgJHybnmYEmZ9S+G0MNe8wBmYhhCkEqYjAuc= github.com/selectel/mks-go v0.12.0 h1:nLWHK8BXkhFlXvjFqf7WRrdAfvmrOhQzDSLx7BGa6aM= github.com/selectel/mks-go v0.12.0/go.mod h1:FcFqF3WvZIhztyAt1+ZySKf0zWmCEvg9e2gRwxVyQOw= github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= diff --git a/selectel/resource_selectel_vpc_project_v2.go b/selectel/resource_selectel_vpc_project_v2.go index d87f22d2..6eb38247 100644 --- a/selectel/resource_selectel_vpc_project_v2.go +++ b/selectel/resource_selectel_vpc_project_v2.go @@ -147,6 +147,10 @@ func resourceVPCProjectV2Create(ctx context.Context, d *schema.ResourceData, met opts.Name = d.Get("name").(string) + if d.HasChange("quotas") { + opts.SkipQuotasInit = true + } + log.Print(msgCreate(objectProject, opts)) project, _, err := projects.Create(ctx, resellV2Client, opts) if err != nil { @@ -155,6 +159,34 @@ func resourceVPCProjectV2Create(ctx context.Context, d *schema.ResourceData, met d.SetId(project.ID) + if d.HasChange("quotas") { + quotaSet := d.Get("quotas").(*schema.Set) + projectQuotasOpts, err := resourceVPCProjectV2QuotasOptsFromSet(quotaSet) + if err != nil { + return diag.FromErr(errParseProjectV2Quotas(err)) + } + + log.Print(msgUpdate(objectProjectQuotas, d.Id(), projectQuotasOpts)) + accountName := strings.Split(config.Token, "_")[1] + tokenOpts := tokens.TokenOpts{AccountName: accountName} + + token, _, err := tokens.Create(ctx, resellV2Client, tokenOpts) + if err != nil { + return diag.FromErr(errCreatingObject(objectToken, err)) + } + + openstackClient := v2.NewOpenstackClient(token.ID) + identityManager := quotamanager.NewIdentityManager(resellV2Client, openstackClient, accountName) + quotaManagerClient := config.quotaManagerRegionalClient(identityManager) + + for region, updateQuotas := range projectQuotasOpts { + _, _, err := quotas.UpdateProjectQuotas(ctx, quotaManagerClient, d.Id(), region, updateQuotas) + if err != nil { + return diag.FromErr(errUpdatingObject(objectProjectQuotas, d.Id(), err)) + } + } + } + return resourceVPCProjectV2Read(ctx, d, meta) } diff --git a/selectel/resource_selectel_vpc_project_v2_test.go b/selectel/resource_selectel_vpc_project_v2_test.go index b42c34c4..ea02636d 100644 --- a/selectel/resource_selectel_vpc_project_v2_test.go +++ b/selectel/resource_selectel_vpc_project_v2_test.go @@ -74,6 +74,23 @@ func TestAccVPCV2ProjectBasic(t *testing.T) { }) } +func TestAccVPCV2ProjectWithSpecificQuotas(t *testing.T) { + projectName := acctest.RandomWithPrefix("tf-acc") + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccSelectelPreCheck(t) }, + ProviderFactories: testAccProviders, + CheckDestroy: testAccCheckVPCV2ProjectDestroy, + Steps: []resource.TestStep{ + { + Config: testAccVPCV2ProjectWithSpecificQuotas(projectName), + Check: resource.TestCheckResourceAttr( + "selectel_vpc_project_v2.project_tf_acc_test_2", "quotas.#", "2"), + }, + }, + }) +} + func testAccCheckVPCV2ProjectDestroy(s *terraform.State) error { config := testAccProvider.Meta().(*Config) resellV2Client := config.resellV2Client() @@ -130,6 +147,34 @@ resource "selectel_vpc_project_v2" "project_tf_acc_test_1" { }`, name) } +func testAccVPCV2ProjectWithSpecificQuotas(name string) string { + return fmt.Sprintf(` +resource "selectel_vpc_project_v2" "project_tf_acc_test_2" { + name = "%s" + quotas { + resource_name = "compute_cores" + resource_quotas { + region = "ru-1" + zone = "ru-1b" + value = 4 + } + resource_quotas { + region = "ru-2" + zone = "ru-2b" + value = 6 + } + } + quotas { + resource_name = "volume_gigabytes_basic" + resource_quotas { + region = "ru-2" + zone = "ru-2a" + value = 2 + } + } +}`, name) +} + func testAccVPCV2ProjectUpdate1(name, customURL string) string { return fmt.Sprintf(` resource "selectel_vpc_project_v2" "project_tf_acc_test_1" {