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

resource/alicloud_threat_detection_instance: Improves the invoking api method and supports refreshing credential automatically #8126

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 4 additions & 2 deletions alicloud/connectivity/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ var irregularProductEndpoint = map[string]string{
"bssopenapi": BssOpenAPIEndpointDomestic,
"esa": "esa.cn-hangzhou.aliyuncs.com",
"cas": "cas.aliyuncs.com",
"sas": "tds.aliyuncs.com",
}

// irregularProductEndpointForIntlRegion specially records those product codes that
Expand All @@ -333,7 +334,9 @@ var irregularProductEndpoint = map[string]string{
// Key: product code, its value equals to the gateway code of the API after converting it to lowercase and using underscores
// Value: product endpoint
// The priority of this configuration is higher than location service, lower than user environment variable configuration
var irregularProductEndpointForIntlRegion = map[string]string{}
var irregularProductEndpointForIntlRegion = map[string]string{
"sas": SaSOpenAPIEndpointInternational,
}

// irregularProductEndpointForIntlAccount specially records those product codes that
// cannot be parsed out by the location service and sensitive to account type.
Expand Down Expand Up @@ -402,7 +405,6 @@ var regularProductEndpoint = map[string]string{
"ecd": "ecd.%s.aliyuncs.com",
"cloudphone": "cloudphone.%s.aliyuncs.com",
"dataworks_public": "dataworks.%s.aliyuncs.com",
"sas": "tds.aliyuncs.com",
"ehpc": "ehpc.%s.aliyuncs.com",
"ens": "ens.aliyuncs.com",
"iot": "iot.%s.aliyuncs.com",
Expand Down
76 changes: 25 additions & 51 deletions alicloud/resource_alicloud_threat_detection_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"time"

"github.com/PaesslerAG/jsonpath"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -215,10 +214,8 @@ func resourceAliCloudThreatDetectionInstanceCreate(d *schema.ResourceData, meta
var request map[string]interface{}
var response map[string]interface{}
query := make(map[string]interface{})
conn, err := client.NewBssopenapiClient()
if err != nil {
return WrapError(err)
}
var err error
var endpoint string
request = make(map[string]interface{})

request["ClientToken"] = buildClientToken(action)
Expand Down Expand Up @@ -382,39 +379,31 @@ func resourceAliCloudThreatDetectionInstanceCreate(d *schema.ResourceData, meta

request["ProductType"] = "sas"
request["ProductCode"] = "sas"
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
if client.IsInternationalAccount() {
request["ProductType"] = ""
}
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2017-12-14"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPostWithEndpoint("BssOpenApi", "2017-12-14", action, query, request, true, endpoint)
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
if IsExpectedErrors(err, []string{"NotApplicable"}) {
conn.Endpoint = String(connectivity.BssOpenAPIEndpointInternational)
if !client.IsInternationalAccount() && IsExpectedErrors(err, []string{"NotApplicable"}) {
request["ProductType"] = ""
endpoint = connectivity.BssOpenAPIEndpointInternational
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
addDebug(action, response, request)

if fmt.Sprint(response["Code"]) != "Success" {
return resource.NonRetryableError(WrapError(fmt.Errorf("%s failed, response: %v", action, response)))
}

return nil
})
addDebug(action, response, request)

if err != nil {
return WrapErrorf(err, DefaultErrorMsg, "alicloud_threat_detection_instance", action, AlibabaCloudSdkGoERROR)
}
if fmt.Sprint(response["Code"]) != "Success" {
return WrapError(fmt.Errorf("%s failed, response: %v", action, response))
}

id, _ := jsonpath.Get("$.Data.InstanceId", response)
d.SetId(fmt.Sprint(id))
Expand Down Expand Up @@ -544,15 +533,10 @@ func resourceAliCloudThreatDetectionInstanceRead(d *schema.ResourceData, meta in
func resourceAliCloudThreatDetectionInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
var request map[string]interface{}
var response map[string]interface{}
var query map[string]interface{}
update := false
d.Partial(true)
action := "ModifyInstance"
conn, err := client.NewBssopenapiClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
query = make(map[string]interface{})
query["InstanceId"] = d.Id()
Expand Down Expand Up @@ -716,41 +700,34 @@ func resourceAliCloudThreatDetectionInstanceUpdate(d *schema.ResourceData, meta
request["ProductType"] = "sas"
request["ProductCode"] = "sas"
if update {
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
var endpoint string
if client.IsInternationalAccount() {
request["ProductType"] = ""
}
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2017-12-14"), StringPointer("AK"), query, request, &runtime)
err := resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
response, err := client.RpcPostWithEndpoint("BssOpenApi", "2017-12-14", action, query, request, true, endpoint)
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
if IsExpectedErrors(err, []string{"NotApplicable"}) {
conn.Endpoint = String(connectivity.BssOpenAPIEndpointInternational)
if !client.IsInternationalAccount() && IsExpectedErrors(err, []string{"NotApplicable"}) {
request["ProductType"] = ""
endpoint = connectivity.BssOpenAPIEndpointInternational
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}

if fmt.Sprint(response["Code"]) != "Success" {
return resource.NonRetryableError(WrapError(fmt.Errorf("%s failed, response: %v", action, response)))
}

addDebug(action, response, request)
return nil
})
addDebug(action, response, request)
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR)
}
}
update = false
action = "SetRenewal"
conn, err = client.NewBssopenapiClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
query = make(map[string]interface{})
query["InstanceIDs"] = d.Id()
Expand All @@ -776,29 +753,26 @@ func resourceAliCloudThreatDetectionInstanceUpdate(d *schema.ResourceData, meta
request["SubscriptionType"] = v
}
if update {
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
var endpoint string
if client.IsInternationalAccount() {
request["ProductType"] = ""
}
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2017-12-14"), StringPointer("AK"), query, request, &runtime)
err := resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
response, err := client.RpcPostWithEndpoint("BssOpenApi", "2017-12-14", action, query, request, true, endpoint)
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
if IsExpectedErrors(err, []string{"NotApplicable"}) {
conn.Endpoint = String(connectivity.BssOpenAPIEndpointInternational)
if !client.IsInternationalAccount() && IsExpectedErrors(err, []string{"NotApplicable"}) {
request["ProductType"] = ""
endpoint = connectivity.BssOpenAPIEndpointInternational
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
addDebug(action, response, request)

if fmt.Sprint(response["Code"]) != "Success" {
return resource.NonRetryableError(WrapError(fmt.Errorf("%s failed, response: %v", action, response)))
}

return nil
})
if err != nil {
Expand Down
16 changes: 11 additions & 5 deletions alicloud/resource_alicloud_threat_detection_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// Skip Test,Because each account can only be opened once
func SkipTestAccAliCloudThreatDetectionInstance_basic1826(t *testing.T) {
func TestAccAliCloudThreatDetectionInstance_basic1826(t *testing.T) {
var v map[string]interface{}
resourceId := "alicloud_threat_detection_instance.default"
ra := resourceAttrInit(resourceId, AlicloudThreatDetectionInstanceMap1826)
Expand All @@ -25,6 +25,7 @@ func SkipTestAccAliCloudThreatDetectionInstance_basic1826(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckForCleanUpInstances(t, "", "sas", "sas", "sas", "")
},
IDRefreshName: resourceId,
Providers: testAccProviders,
Expand Down Expand Up @@ -145,7 +146,7 @@ variable "name" {

// Test ThreatDetection Instance. >>> Resource test cases, automatically generated.
// Case 4253
func SkipTestAccAliCloudThreatDetectionInstance_basic4253(t *testing.T) {
func TestAccAliCloudThreatDetectionInstance_basic4253(t *testing.T) {
var v map[string]interface{}
resourceId := "alicloud_threat_detection_instance.default"
ra := resourceAttrInit(resourceId, AlicloudThreatDetectionInstanceMap4253)
Expand All @@ -160,6 +161,7 @@ func SkipTestAccAliCloudThreatDetectionInstance_basic4253(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckForCleanUpInstances(t, "", "sas", "sas", "sas", "")
},
IDRefreshName: resourceId,
Providers: testAccProviders,
Expand Down Expand Up @@ -615,7 +617,7 @@ variable "name" {
}

// Case 4253 twin
func SkipTestAccAliCloudThreatDetectionInstance_basic4253_twin(t *testing.T) {
func TestAccAliCloudThreatDetectionInstance_basic4253_twin(t *testing.T) {
var v map[string]interface{}
resourceId := "alicloud_threat_detection_instance.default"
ra := resourceAttrInit(resourceId, AlicloudThreatDetectionInstanceMap4253)
Expand All @@ -630,6 +632,7 @@ func SkipTestAccAliCloudThreatDetectionInstance_basic4253_twin(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckForCleanUpInstances(t, "", "sas", "sas", "sas", "")
},
IDRefreshName: resourceId,
Providers: testAccProviders,
Expand Down Expand Up @@ -704,7 +707,7 @@ func SkipTestAccAliCloudThreatDetectionInstance_basic4253_twin(t *testing.T) {
})
}

func SkipTestAccAliCloudThreatDetectionInstance_basic4253_intl(t *testing.T) {
func TestAccAliCloudThreatDetectionInstance_basic4253_intl(t *testing.T) {
var v map[string]interface{}
resourceId := "alicloud_threat_detection_instance.default"
ra := resourceAttrInit(resourceId, AlicloudThreatDetectionInstanceMap4253)
Expand All @@ -719,6 +722,8 @@ func SkipTestAccAliCloudThreatDetectionInstance_basic4253_intl(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckWithAccountSiteType(t, IntlSite)
testAccPreCheckForCleanUpInstances(t, "", "sas", "sas", "sas", "")
},
IDRefreshName: resourceId,
Providers: testAccProviders,
Expand Down Expand Up @@ -801,7 +806,7 @@ func SkipTestAccAliCloudThreatDetectionInstance_basic4253_intl(t *testing.T) {
})
}

func SkipTestAccAliCloudThreatDetectionInstance_basic4253_twin_fix(t *testing.T) {
func TestAccAliCloudThreatDetectionInstance_basic4253_twin_fix(t *testing.T) {
var v map[string]interface{}
resourceId := "alicloud_threat_detection_instance.default"
ra := resourceAttrInit(resourceId, AlicloudThreatDetectionInstanceMap4253)
Expand All @@ -816,6 +821,7 @@ func SkipTestAccAliCloudThreatDetectionInstance_basic4253_twin_fix(t *testing.T)
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckForCleanUpInstances(t, "", "sas", "sas", "sas", "")
},
IDRefreshName: resourceId,
Providers: testAccProviders,
Expand Down
43 changes: 14 additions & 29 deletions alicloud/service_alicloud_threat_detection_v2.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package alicloud

import (
"errors"
"fmt"
"strings"
"time"
Expand All @@ -23,35 +22,27 @@ func (s *ThreatDetectionServiceV2) DescribeThreatDetectionInstance(id string) (o
var request map[string]interface{}
var response map[string]interface{}
var query map[string]interface{}
var endpoint string
action := "GetInstance"
conn, err := client.NewThreatdetectionClient()
if err != nil {
return object, WrapError(err)
}
request = make(map[string]interface{})
query = make(map[string]interface{})
query["InstanceId"] = id

request["CommodityCode"] = "sas"
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(1*time.Minute, func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2018-12-03"), StringPointer("AK"), query, request, &runtime)

response, err = client.RpcPostWithEndpoint("Sas", "2018-12-03", action, query, request, true, endpoint)
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
if !client.IsInternationalAccount() && IsExpectedErrors(err, []string{"NODATA"}) {
endpoint = connectivity.SaSOpenAPIEndpointInternational
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}

if fmt.Sprint(response["Code"]) == "NODATA" {
conn.Endpoint = String(connectivity.SaSOpenAPIEndpointInternational)
return resource.RetryableError(errors.New("NODATA"))
}

addDebug(action, response, request)
return nil
})
Expand All @@ -74,43 +65,37 @@ func (s *ThreatDetectionServiceV2) DescribeQueryAvailableInstances(id string) (o
client := s.client
var request map[string]interface{}
var response map[string]interface{}
var endpoint string
var query map[string]interface{}
action := "QueryAvailableInstances"
conn, err := client.NewBssopenapiClient()
if err != nil {
return object, WrapError(err)
}
request = make(map[string]interface{})
query = make(map[string]interface{})
query["InstanceIDs"] = id

request["ProductCode"] = "sas"
request["ProductType"] = "sas"
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
if client.IsInternationalAccount() {
request["ProductType"] = ""
}
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(1*time.Minute, func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2017-12-14"), StringPointer("AK"), query, request, &runtime)

response, err = client.RpcPostWithEndpoint("BssOpenApi", "2017-12-14", action, query, request, true, endpoint)
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
if IsExpectedErrors(err, []string{"NotApplicable"}) {
conn.Endpoint = String(connectivity.BssOpenAPIEndpointInternational)
if !client.IsInternationalAccount() && IsExpectedErrors(err, []string{"NotApplicable"}) {
request["ProductType"] = ""
endpoint = connectivity.BssOpenAPIEndpointInternational
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
addDebug(action, response, request)
return nil
})
addDebug(action, response, request)
if err != nil {
if IsExpectedErrors(err, []string{}) {
return object, WrapErrorf(Error(GetNotFoundMessage("Instance", id)), NotFoundMsg, ProviderERROR, fmt.Sprint(response["RequestId"]))
}
return object, WrapErrorf(err, DefaultErrorMsg, id, action, AlibabaCloudSdkGoERROR)
}

Expand Down
Loading