-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from volcengine/feat/ecs
Feat/ecs
- Loading branch information
Showing
10 changed files
with
359 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package common | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/volcengine/terraform-provider-volcengine/logger" | ||
"golang.org/x/sync/semaphore" | ||
"golang.org/x/time/rate" | ||
) | ||
|
||
type UnsubscribeEnabled interface { | ||
// UnsubscribeInfo 判断是否需要退订 | ||
UnsubscribeInfo(*schema.ResourceData, *schema.Resource) (*UnsubscribeInfo, error) | ||
} | ||
|
||
type UnsubscribeInfo struct { | ||
Products []string | ||
InstanceId string | ||
NeedUnsubscribe bool | ||
productIndex int | ||
} | ||
|
||
var unsubscribeRate *Rate | ||
|
||
func init() { | ||
unsubscribeRate = &Rate{ | ||
Limiter: rate.NewLimiter(10, 10), | ||
Semaphore: semaphore.NewWeighted(20), | ||
} | ||
} | ||
|
||
type UnsubscribeService struct { | ||
Client *SdkClient | ||
} | ||
|
||
func NewUnsubscribeService(c *SdkClient) *UnsubscribeService { | ||
return &UnsubscribeService{ | ||
Client: c, | ||
} | ||
} | ||
|
||
func (u *UnsubscribeService) UnsubscribeInstance(info *UnsubscribeInfo) []Callback { | ||
var call []Callback | ||
if len(info.Products) == 0 || info.InstanceId == "" { | ||
return call | ||
} | ||
unsubscribe := Callback{ | ||
Call: SdkCall{ | ||
Action: "UnsubscribeInstance", | ||
ConvertMode: RequestConvertIgnore, | ||
ContentType: ContentTypeJson, | ||
BeforeCall: func(d *schema.ResourceData, client *SdkClient, call SdkCall) (bool, error) { | ||
(*call.SdkParam)["InstanceID"] = info.InstanceId | ||
(*call.SdkParam)["UnsubscribeRelatedInstance"] = true | ||
(*call.SdkParam)["ClientToken"] = uuid.New().String() | ||
return true, nil | ||
}, | ||
ExecuteCall: func(d *schema.ResourceData, client *SdkClient, call SdkCall) (*map[string]interface{}, error) { | ||
defer func() { | ||
unsubscribeRate.Semaphore.Release(1) | ||
}() | ||
var err error | ||
ctx := context.Background() | ||
err = unsubscribeRate.Limiter.Wait(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = unsubscribeRate.Semaphore.Acquire(ctx, 1) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
(*call.SdkParam)["Product"] = info.Products[info.productIndex] | ||
logger.Debug(logger.ReqFormat, call.Action, call.SdkParam) | ||
return u.Client.UniversalClient.DoCall(u.getUniversalInfo(call.Action), call.SdkParam) | ||
}, | ||
AfterCall: func(d *schema.ResourceData, client *SdkClient, resp *map[string]interface{}, call SdkCall) error { | ||
return nil | ||
}, | ||
CallError: func(d *schema.ResourceData, client *SdkClient, call SdkCall, baseErr error) error { | ||
return resource.Retry(15*time.Minute, func() *resource.RetryError { | ||
if UnsubscribeProductError(baseErr) { | ||
info.productIndex = info.productIndex + 1 | ||
if info.productIndex >= len(info.Products) { | ||
return resource.NonRetryableError(fmt.Errorf(" Can not support this instance %s Unsubscribe", info.InstanceId)) | ||
} | ||
_, callErr := call.ExecuteCall(d, client, call) | ||
if callErr == nil { | ||
return nil | ||
} | ||
return resource.RetryableError(callErr) | ||
} else { | ||
return resource.NonRetryableError(baseErr) | ||
} | ||
}) | ||
}, | ||
}, | ||
} | ||
call = append(call, unsubscribe) | ||
return call | ||
} | ||
|
||
func (u *UnsubscribeService) getUniversalInfo(actionName string) UniversalInfo { | ||
return UniversalInfo{ | ||
ServiceName: "billing", | ||
Version: "2022-01-01", | ||
HttpMethod: POST, | ||
Action: actionName, | ||
ContentType: ApplicationJSON, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
resource "volcengine_network_interface" "foo" { | ||
subnet_id = "subnet-im67x70vxla88gbssz1hy1z2" | ||
security_group_ids = ["sg-im67wp9lx3i88gbssz3d22b2"] | ||
primary_ip_address = "192.168.0.253" | ||
subnet_id = "subnet-2fe79j7c8o5c059gp68ksxr93" | ||
security_group_ids = ["sg-2fepz3c793g1s59gp67y21r34"] | ||
primary_ip_address = "192.168.5.253" | ||
network_interface_name = "tf-test-up" | ||
description = "tf-test-up" | ||
port_security_enabled = false | ||
project_name = "default" | ||
private_ip_address = ["192.168.5.2"] | ||
//secondary_private_ip_address_count = 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.