Skip to content

Commit

Permalink
Pull request #1233: change project structure in yandex-framework pkg …
Browse files Browse the repository at this point in the history
…\ remove utils package \ change yandex-framework test structure

Merge in CLOUD/terraform-provider-yandex-mirror from terraform-provider-framework-resources-refactor to master

Squashed commit of the following:

commit 9a8260d50c024a197d7a60102df6994349c5f7f5
Author: Mikhail Smolin <[email protected]>
Date:   Mon May 13 15:52:42 2024 +0200

    add todo for min func

commit 74b531c010aca4e24e910925b5f6afb6edb428bd
Author: Mikhail Smolin <[email protected]>
Date:   Mon Apr 29 19:19:34 2024 +0200

    change project structure in yandex-framework pkg \ remove utils package \ change yandex-framework test structure
  • Loading branch information
Mikhail Smolin committed May 15, 2024
1 parent 726c614 commit c938f03
Show file tree
Hide file tree
Showing 55 changed files with 682 additions and 654 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ linters-settings:

issues:
new-from-rev: master # Analyse only differense between master->${git branch git branch --show-current}
max-per-linter: 0
max-same-issues: 0
exclude-rules:
- linters:
Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package main
import (
"context"
"flag"

"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tfprotov6/tf6server"
"github.com/hashicorp/terraform-plugin-mux/tf5to6server"
"github.com/hashicorp/terraform-plugin-mux/tf6muxserver"

"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/yandex-cloud/terraform-provider-yandex/yandex"
yandex_framework "github.com/yandex-cloud/terraform-provider-yandex/yandex-framework"
yandex_framework "github.com/yandex-cloud/terraform-provider-yandex/yandex-framework/provider"
)

func NewMuxProviderServer(ctx context.Context) (func() tfprotov6.ProviderServer, error) {
Expand Down
9 changes: 9 additions & 0 deletions yandex-framework/math/math.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package math

// TODO (miksmolin) will have been replaced with function from std package after go 1.21 update
func Min(a, b int) int {
if a < b {
return a
}
return b
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package provider_config
package config

import (
"context"
Expand Down Expand Up @@ -30,6 +30,10 @@ import (
"github.com/yandex-cloud/terraform-provider-yandex/pkg/logging"
)

const (
DefaultTimeout = 1 * time.Minute
)

const (
defaultExponentialBackoffBase = 50 * time.Millisecond
defaultExponentialBackoffCap = 1 * time.Minute
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package yandex_framework
package provider

import (
"context"
Expand All @@ -7,11 +7,6 @@ import (
"os"
"strconv"

yandex_datasphere_community "github.com/yandex-cloud/terraform-provider-yandex/yandex-framework/yandex-datasphere/community"
yandex_datasphere_project "github.com/yandex-cloud/terraform-provider-yandex/yandex-framework/yandex-datasphere/project"
yandex_mdb_mongodb_database "github.com/yandex-cloud/terraform-provider-yandex/yandex-framework/yandex-mdb-mongodb-database"
yandex_mdb_mongodb_user "github.com/yandex-cloud/terraform-provider-yandex/yandex-framework/yandex-mdb-mongodb-user"

"github.com/hashicorp/terraform-plugin-framework-validators/providervalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/path"
Expand All @@ -20,10 +15,13 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/yandex-cloud/terraform-provider-yandex/common"
provider_config "github.com/yandex-cloud/terraform-provider-yandex/yandex-framework/provider-config"
yandex_billing_cloud_binding "github.com/yandex-cloud/terraform-provider-yandex/yandex-framework/yandex-billing-cloud-binding"
provider_config "github.com/yandex-cloud/terraform-provider-yandex/yandex-framework/provider/config"
"github.com/yandex-cloud/terraform-provider-yandex/yandex-framework/services/billing"
"github.com/yandex-cloud/terraform-provider-yandex/yandex-framework/services/datasphere/community"
"github.com/yandex-cloud/terraform-provider-yandex/yandex-framework/services/datasphere/project"
"github.com/yandex-cloud/terraform-provider-yandex/yandex-framework/services/mdb/mongodb/database"
"github.com/yandex-cloud/terraform-provider-yandex/yandex-framework/services/mdb/mongodb/user"
)

type saKeyValidator struct{}
Expand Down Expand Up @@ -237,30 +235,30 @@ func (p *Provider) Configure(ctx context.Context, req provider.ConfigureRequest,
func (p *Provider) Resources(_ context.Context) []func() resource.Resource {
return []func() resource.Resource{
func() resource.Resource {
return yandex_billing_cloud_binding.NewResource(
yandex_billing_cloud_binding.BindingServiceInstanceCloudType,
yandex_billing_cloud_binding.BindingServiceInstanceCloudIdFieldName)
return billing.NewResource(
billing.BindingServiceInstanceCloudType,
billing.BindingServiceInstanceCloudIdFieldName)
},
yandex_datasphere_project.NewResource,
yandex_datasphere_project.NewIamBinding,
yandex_datasphere_community.NewResource,
yandex_datasphere_community.NewIamBinding,
yandex_mdb_mongodb_database.NewResource,
yandex_mdb_mongodb_user.NewResource,
project.NewResource,
project.NewIamBinding,
community.NewResource,
community.NewIamBinding,
database.NewResource,
user.NewResource,
}
}

func (p *Provider) DataSources(_ context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
func() datasource.DataSource {
return yandex_billing_cloud_binding.NewDataSource(
yandex_billing_cloud_binding.BindingServiceInstanceCloudType,
yandex_billing_cloud_binding.BindingServiceInstanceCloudIdFieldName)
return billing.NewDataSource(
billing.BindingServiceInstanceCloudType,
billing.BindingServiceInstanceCloudIdFieldName)
},
yandex_datasphere_project.NewDataSource,
yandex_datasphere_community.NewDataSource,
yandex_mdb_mongodb_database.NewDataSource,
yandex_mdb_mongodb_user.NewDataSource,
project.NewDataSource,
community.NewDataSource,
database.NewDataSource,
user.NewDataSource,
}
}

Expand Down
21 changes: 21 additions & 0 deletions yandex-framework/resourceid/id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package resourceid

import (
"fmt"
"strings"
)

func Construct(clusterID string, resourceName string) string {
return fmt.Sprintf("%s:%s", clusterID, resourceName)
}

func Deconstruct(resourceID string) (string, string, error) {
parts := strings.SplitN(resourceID, ":", 2)
if len(parts) != 2 {
return "", "", fmt.Errorf("Invalid resource id format: %q", resourceID)
}

clusterID := parts[0]
resourceName := parts[1]
return clusterID, resourceName, nil
}
44 changes: 44 additions & 0 deletions yandex-framework/retry/retry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package retry

import (
"context"
"fmt"
"regexp"

"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/yandex-cloud/go-genproto/yandex/cloud/operation"
ycsdk "github.com/yandex-cloud/go-sdk"
sdkoperation "github.com/yandex-cloud/go-sdk/operation"
"google.golang.org/grpc/status"
)

func ConflictingOperation(ctx context.Context, sdk *ycsdk.SDK, action func() (*operation.Operation, error)) (*sdkoperation.Operation, error) {
for {
op, err := sdk.WrapOperation(action())
if err == nil {
return op, nil
}

operationID := ""
message := status.Convert(err).Message()
submatchGoApi := regexp.MustCompile(`conflicting operation "(.+)" detected`).FindStringSubmatch(message)
submatchPyApi := regexp.MustCompile(`Conflicting operation (.+) detected`).FindStringSubmatch(message)
if len(submatchGoApi) > 0 {
operationID = submatchGoApi[1]
} else if len(submatchPyApi) > 0 {
operationID = submatchPyApi[1]
} else {
return op, err
}

tflog.Debug(ctx, fmt.Sprintf("Waiting for conflicting operation %q to complete", operationID))
req := &operation.GetOperationRequest{OperationId: operationID}
op, err = sdk.WrapOperation(sdk.Operation().Get(ctx, req))
if err != nil {
return nil, err
}

_ = op.Wait(ctx)
tflog.Debug(ctx, fmt.Sprintf("Conflicting operation %q has completed. Going to retry initial action.", operationID))
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package yandex_billing_cloud_binding
package billing

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"

"github.com/yandex-cloud/terraform-provider-yandex/yandex-framework/provider-config"
provider_config "github.com/yandex-cloud/terraform-provider-yandex/yandex-framework/provider/config"
)

type bindingDataSource struct {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package yandex_billing_cloud_binding
package billing

import (
"context"
Expand All @@ -11,9 +11,8 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/yandex-cloud/go-genproto/yandex/cloud/billing/v1"
provider_config "github.com/yandex-cloud/terraform-provider-yandex/yandex-framework/provider/config"
"google.golang.org/protobuf/encoding/protojson"

"github.com/yandex-cloud/terraform-provider-yandex/yandex-framework/provider-config"
)

type bindingResource struct {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package yandex_billing_cloud_binding
package billing

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package yandex_datasphere_community
package community

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/yandex-cloud/go-genproto/yandex/cloud/datasphere/v2"
"github.com/yandex-cloud/terraform-provider-yandex/yandex-framework/provider-config"
provider_config "github.com/yandex-cloud/terraform-provider-yandex/yandex-framework/provider/config"
)

type communityDataSource struct {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package yandex_datasphere_community
package community

import (
"github.com/hashicorp/terraform-plugin-framework/resource"
iam_binding "github.com/yandex-cloud/terraform-provider-yandex/yandex-framework/iam"
iam_binding "github.com/yandex-cloud/terraform-provider-yandex/yandex-framework/services/datasphere/iam"
)

func NewIamBinding() resource.Resource {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
package yandex_datasphere_community
package community

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/yandex-cloud/go-genproto/yandex/cloud/access"
iam_binging "github.com/yandex-cloud/terraform-provider-yandex/yandex-framework/iam"
provider_config "github.com/yandex-cloud/terraform-provider-yandex/yandex-framework/provider-config"
"github.com/yandex-cloud/terraform-provider-yandex/yandex-framework/utils"
"github.com/yandex-cloud/terraform-provider-yandex/yandex-framework/math"
provider_config "github.com/yandex-cloud/terraform-provider-yandex/yandex-framework/provider/config"
"github.com/yandex-cloud/terraform-provider-yandex/yandex-framework/services/datasphere"
"github.com/yandex-cloud/terraform-provider-yandex/yandex-framework/services/datasphere/iam"
)

type CommunityIAMUpdater struct {
CommunityId string
ProviderConfig *provider_config.Config
}

func newCommunityIamUpdater() iam_binging.ResourceIamUpdater {
func newCommunityIamUpdater() iam.ResourceIamUpdater {
return &CommunityIAMUpdater{}
}

Expand Down Expand Up @@ -59,27 +61,27 @@ func (u *CommunityIAMUpdater) Configure(_ context.Context, req resource.Configur
u.ProviderConfig = providerConfig
}

func (u *CommunityIAMUpdater) Initialize(ctx context.Context, state iam_binging.Extractable, diag *diag.Diagnostics) {
func (u *CommunityIAMUpdater) Initialize(ctx context.Context, state iam.Extractable, diag *diag.Diagnostics) {
var id types.String
diag.Append(state.GetAttribute(ctx, path.Root("community_id"), &id)...)
u.CommunityId = id.ValueString()
}

func (u *CommunityIAMUpdater) GetResourceIamPolicy(ctx context.Context) (*iam_binging.Policy, error) {
func (u *CommunityIAMUpdater) GetResourceIamPolicy(ctx context.Context) (*iam.Policy, error) {
bindings, err := u.GeAccessBindings(ctx, u.CommunityId)
if err != nil {
return nil, err
}
return &iam_binging.Policy{Bindings: bindings}, nil
return &iam.Policy{Bindings: bindings}, nil
}

func (u *CommunityIAMUpdater) SetResourceIamPolicy(ctx context.Context, policy *iam_binging.Policy) error {
func (u *CommunityIAMUpdater) SetResourceIamPolicy(ctx context.Context, policy *iam.Policy) error {
req := &access.SetAccessBindingsRequest{
ResourceId: u.CommunityId,
AccessBindings: policy.Bindings,
}

ctx, cancel := context.WithTimeout(ctx, utils.DefaultTimeout)
ctx, cancel := context.WithTimeout(ctx, provider_config.DefaultTimeout)
defer cancel()

op, err := u.ProviderConfig.SDK.WrapOperation(
Expand All @@ -97,15 +99,15 @@ func (u *CommunityIAMUpdater) SetResourceIamPolicy(ctx context.Context, policy *
return nil
}

func (u *CommunityIAMUpdater) UpdateResourceIamPolicy(ctx context.Context, policy *iam_binging.PolicyDelta) error {
func (u *CommunityIAMUpdater) UpdateResourceIamPolicy(ctx context.Context, policy *iam.PolicyDelta) error {
bSize := 1000
deltas := policy.Deltas
dLen := len(deltas)

for i := 0; i < iam_binging.CountBatches(dLen, bSize); i++ {
for i := 0; i < iam.CountBatches(dLen, bSize); i++ {
req := &access.UpdateAccessBindingsRequest{
ResourceId: u.CommunityId,
AccessBindingDeltas: deltas[i*bSize : utils.Min((i+1)*bSize, dLen)],
AccessBindingDeltas: deltas[i*bSize : math.Min((i+1)*bSize, dLen)],
}

op, err := u.ProviderConfig.SDK.WrapOperation(
Expand Down Expand Up @@ -141,7 +143,7 @@ func (u *CommunityIAMUpdater) GeAccessBindings(ctx context.Context, id string) (
ctx,
&access.ListAccessBindingsRequest{
ResourceId: id,
PageSize: utils.DefaultPageSize,
PageSize: datasphere.DefaultPageSize,
PageToken: pageToken,
},
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package yandex_datasphere_community
package community

import (
"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
Expand Down
Loading

0 comments on commit c938f03

Please sign in to comment.