Skip to content

Commit

Permalink
SCALRCORE-21436 add createParameters, changeParameters [API_BRANCH] […
Browse files Browse the repository at this point in the history
…DB_BRANCH]
  • Loading branch information
DayS1eeper committed May 9, 2022
1 parent cf37185 commit 0fa9f23
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -5,7 +5,7 @@ require (
github.com/hashicorp/hcl v0.0.0-20180404174102-ef8a98b0bbce
github.com/hashicorp/terraform-plugin-sdk v1.17.2
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734
github.com/scalr/go-scalr v0.0.0-20220506134515-3ebfdd2cb2dd
github.com/scalr/go-scalr v0.0.0-20220509095836-2578ecfec9e3
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -299,8 +299,8 @@ github.com/posener/complete v1.2.1 h1:LrvDIY//XNo65Lq84G/akBuMGlawHvGBABv8f/ZN6D
github.com/posener/complete v1.2.1/go.mod h1:6gapUrK/U1TAN7ciCoNRIdVC5sbdBTUh1DKN0g6uH7E=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/scalr/go-scalr v0.0.0-20220506134515-3ebfdd2cb2dd h1:kOTORpHPrNhu7bDiPiRLgeix/a588NnSkZArXuAj87g=
github.com/scalr/go-scalr v0.0.0-20220506134515-3ebfdd2cb2dd/go.mod h1:xMnwfer9UxugeNITZjTpQBwQ/4bw6/JdyDLpGdmyorE=
github.com/scalr/go-scalr v0.0.0-20220509095836-2578ecfec9e3 h1:P03PrnjjQ4eG7E94FvOn0L40v1gpWiVVq1RfdmPWNzk=
github.com/scalr/go-scalr v0.0.0-20220509095836-2578ecfec9e3/go.mod h1:xMnwfer9UxugeNITZjTpQBwQ/4bw6/JdyDLpGdmyorE=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
Expand Down
134 changes: 132 additions & 2 deletions scalr/resource_scalr_provider_configuration.go
@@ -1,14 +1,18 @@
package scalr

import (
"context"
"errors"
"fmt"
"sync"

"github.com/hashicorp/terraform-plugin-sdk/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
scalr "github.com/scalr/go-scalr"
)

const NUM_PARALLEL = 10

func resourceScalrProviderConfiguration() *schema.Resource {
return &schema.Resource{
Create: resourceScalrProviderConfigurationCreate,
Expand Down Expand Up @@ -241,7 +245,7 @@ func resourceScalrProviderConfigurationCreate(d *schema.ResourceData, meta inter
d.SetId(providerConfiguration.ID)

if len(createArgumentOptions) != 0 {
_, err = scalrClient.ProviderConfigurations.CreateParameters(ctx, providerConfiguration.ID, &createArgumentOptions)
_, err = createParameters(ctx, scalrClient, providerConfiguration.ID, &createArgumentOptions)
if err != nil {
defer scalrClient.ProviderConfigurations.Delete(ctx, providerConfiguration.ID)
return fmt.Errorf(
Expand Down Expand Up @@ -459,8 +463,9 @@ func syncArguments(providerConfigurationId string, custom map[string]interface{}
toDelete = append(toDelete, currentArgument.ID)
}
}
_, _, _, err = client.ProviderConfigurations.ChangeParameters(
_, _, _, err = changeParameters(
ctx,
client,
providerConfigurationId,
&toCreate,
&toUpdate,
Expand All @@ -485,3 +490,128 @@ func resourceScalrProviderConfigurationDelete(d *schema.ResourceData, meta inter

return nil
}

// changeParameters is used to change parameters for provider configuratio.
func changeParameters(
ctx context.Context,
client *scalr.Client,
configurationID string,
toCreate *[]scalr.ProviderConfigurationParameterCreateOptions,
toUpdate *[]scalr.ProviderConfigurationParameterUpdateOptions,
toDelete *[]string,
) (
created []scalr.ProviderConfigurationParameter,
updated []scalr.ProviderConfigurationParameter,
deleted []string,
err error,
) {

done := make(chan struct{})
defer close(done)

type result struct {
created *scalr.ProviderConfigurationParameter
updated *scalr.ProviderConfigurationParameter
deleted *string
err error
}
type task struct {
createOption *scalr.ProviderConfigurationParameterCreateOptions
updateOption *scalr.ProviderConfigurationParameterUpdateOptions
deleteId *string
}

inputCh := make(chan task)
var tasks []task

if toDelete != nil {
for i := range *toDelete {
tasks = append(tasks, task{deleteId: &(*toDelete)[i]})
}
}
if toUpdate != nil {
for i := range *toUpdate {
tasks = append(tasks, task{updateOption: &(*toUpdate)[i]})
}
}
if toCreate != nil {
for i := range *toCreate {
tasks = append(tasks, task{createOption: &(*toCreate)[i]})
}
}

if tasks == nil {
return
}

go func() {
defer close(inputCh)
for _, t := range tasks {
select {
case inputCh <- t:

case <-done:
return
}
}
}()

var wg sync.WaitGroup
wg.Add(NUM_PARALLEL)

resultCh := make(chan result)

for i := 0; i < NUM_PARALLEL; i++ {
go func() {
for t := range inputCh {
if t.createOption != nil {
parameter, err := client.ProviderConfigurationParameters.Create(ctx, configurationID, *t.createOption)
resultCh <- result{created: parameter, err: err}
} else if t.updateOption != nil {
parameter, err := client.ProviderConfigurationParameters.Update(ctx, t.updateOption.ID, *t.updateOption)
resultCh <- result{updated: parameter, err: err}
} else {
err := client.ProviderConfigurationParameters.Delete(ctx, *t.deleteId)
resultCh <- result{deleted: t.deleteId, err: err}
}
}
wg.Done()
}()
}

go func() {
wg.Wait()
close(resultCh)
}()

for result := range resultCh {
if result.err != nil {
err = result.err
break
} else if result.created != nil {
created = append(created, *result.created)
} else if result.updated != nil {
updated = append(updated, *result.updated)
} else {
deleted = append(deleted, *result.deleted)
}
}

return
}

// createParameters is used to create parameters for provider configuratio.
func createParameters(
ctx context.Context,
client *scalr.Client,
configurationID string,
optionsList *[]scalr.ProviderConfigurationParameterCreateOptions,
) (
created []scalr.ProviderConfigurationParameter,
err error,
) {
created, _, _, err = changeParameters(
ctx, client, configurationID, optionsList, nil, nil,
)
return
}

0 comments on commit 0fa9f23

Please sign in to comment.