Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply quotas from manifest for created project #227

Merged
merged 1 commit into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
32 changes: 32 additions & 0 deletions selectel/resource_selectel_vpc_project_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}

Expand Down
45 changes: 45 additions & 0 deletions selectel/resource_selectel_vpc_project_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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" {
Expand Down