Skip to content

Commit

Permalink
New Resource: alicloud_mongodb_private_srv_network_address.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenHanZhang committed Dec 25, 2024
1 parent ddcaa0e commit cf06abe
Show file tree
Hide file tree
Showing 5 changed files with 443 additions and 0 deletions.
1 change: 1 addition & 0 deletions alicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,7 @@ func Provider() terraform.ResourceProvider {
"alicloud_cms_site_monitors": dataSourceAliCloudCloudMonitorServiceSiteMonitors(),
},
ResourcesMap: map[string]*schema.Resource{
"alicloud_mongodb_private_srv_network_address": resourceAliCloudMongodbPrivateSrvNetworkAddress(),
"alicloud_schedulerx_app_group": resourceAliCloudSchedulerxAppGroup(),
"alicloud_schedulerx_job": resourceAliCloudSchedulerxJob(),
"alicloud_express_connect_router_grant_association": resourceAliCloudExpressConnectRouterGrantAssociation(),
Expand Down
158 changes: 158 additions & 0 deletions alicloud/resource_alicloud_mongodb_private_srv_network_address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
// Package alicloud. This file is generated automatically. Please do not modify it manually, thank you!
package alicloud

import (
"fmt"
"log"
"time"

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"
)

func resourceAliCloudMongodbPrivateSrvNetworkAddress() *schema.Resource {
return &schema.Resource{
Create: resourceAliCloudMongodbPrivateSrvNetworkAddressCreate,
Read: resourceAliCloudMongodbPrivateSrvNetworkAddressRead,
Delete: resourceAliCloudMongodbPrivateSrvNetworkAddressDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(5 * time.Minute),
Delete: schema.DefaultTimeout(5 * time.Minute),
},
Schema: map[string]*schema.Schema{
"db_instance_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"private_srv_connection_string_uri": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceAliCloudMongodbPrivateSrvNetworkAddressCreate(d *schema.ResourceData, meta interface{}) error {

client := meta.(*connectivity.AliyunClient)

action := "AllocateDBInstanceSrvNetworkAddress"
var request map[string]interface{}
var response map[string]interface{}
query := make(map[string]interface{})
conn, err := client.NewDdsClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
if v, ok := d.GetOk("db_instance_id"); ok {
request["DBInstanceId"] = v
}
request["RegionId"] = client.RegionId

request["SrvConnectionType"] = "vpc"
request["NodeId"] = "ConnectionStringURI"
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
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("2015-12-01"), StringPointer("AK"), query, request, &runtime)
if err != nil {
if IsExpectedErrors(err, []string{"InvalidStatus.NotFound"}) || NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
addDebug(action, response, request)

if err != nil {
return WrapErrorf(err, DefaultErrorMsg, "alicloud_mongodb_private_srv_network_address", action, AlibabaCloudSdkGoERROR)
}

d.SetId(fmt.Sprint(request["DBInstanceId"]))

mongodbServiceV2 := MongodbServiceV2{client}
stateConf := BuildStateConf([]string{}, []string{"#CHECKSET"}, d.Timeout(schema.TimeoutCreate), 30*time.Second, mongodbServiceV2.MongodbPrivateSrvNetworkAddressStateRefreshFunc(d.Id(), "#PrivateSrvConnectionStringUri", []string{}))
if _, err := stateConf.WaitForState(); err != nil {
return WrapErrorf(err, IdMsg, d.Id())
}

return resourceAliCloudMongodbPrivateSrvNetworkAddressRead(d, meta)
}

func resourceAliCloudMongodbPrivateSrvNetworkAddressRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
mongodbServiceV2 := MongodbServiceV2{client}

objectRaw, err := mongodbServiceV2.DescribeMongodbPrivateSrvNetworkAddress(d.Id())
if err != nil {
if !d.IsNewResource() && NotFoundError(err) {
log.Printf("[DEBUG] Resource alicloud_mongodb_private_srv_network_address DescribeMongodbPrivateSrvNetworkAddress Failed!!! %s", err)
d.SetId("")
return nil
}
return WrapError(err)
}

if objectRaw["PrivateSrvConnectionStringUri"] != nil {
d.Set("private_srv_connection_string_uri", objectRaw["PrivateSrvConnectionStringUri"])
}

d.Set("db_instance_id", d.Id())

return nil
}

func resourceAliCloudMongodbPrivateSrvNetworkAddressDelete(d *schema.ResourceData, meta interface{}) error {

client := meta.(*connectivity.AliyunClient)
action := "ReleaseNodePrivateNetworkAddress"
var request map[string]interface{}
var response map[string]interface{}
query := make(map[string]interface{})
conn, err := client.NewDdsClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
request["DBInstanceId"] = d.Id()
request["RegionId"] = client.RegionId

request["NodeId"] = "ConnectionStringURI"
request["ConnectionType"] = "SRV"
request["NetworkType"] = "VPC"
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2015-12-01"), StringPointer("AK"), query, request, &runtime)

if err != nil {
if IsExpectedErrors(err, []string{"OperationDenied.DBInstanceStatus"}) || NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
addDebug(action, response, request)

if err != nil {
if NotFoundError(err) {
return nil
}
return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR)
}

return nil
}
100 changes: 100 additions & 0 deletions alicloud/resource_alicloud_mongodb_private_srv_network_address_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package alicloud

import (
"fmt"
"testing"

"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

// Test Mongodb PrivateSrvNetworkAddress. >>> Resource test cases, automatically generated.
// Case 私有网络srv测试 9657
func TestAccAliCloudMongodbPrivateSrvNetworkAddress_basic9657(t *testing.T) {
var v map[string]interface{}
resourceId := "alicloud_mongodb_private_srv_network_address.default"
ra := resourceAttrInit(resourceId, AlicloudMongodbPrivateSrvNetworkAddressMap9657)
rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} {
return &MongodbServiceV2{testAccProvider.Meta().(*connectivity.AliyunClient)}
}, "DescribeMongodbPrivateSrvNetworkAddress")
rac := resourceAttrCheckInit(rc, ra)
testAccCheck := rac.resourceAttrMapUpdateSet()
rand := acctest.RandIntRange(10000, 99999)
name := fmt.Sprintf("tf-testacc%smongodbprivatesrvnetworkaddress%d", defaultRegionToTest, rand)
testAccConfig := resourceTestAccConfigFunc(resourceId, name, AlicloudMongodbPrivateSrvNetworkAddressBasicDependence9657)
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheckWithRegions(t, true, []connectivity.Region{"cn-shanghai"})
testAccPreCheck(t)
},
IDRefreshName: resourceId,
Providers: testAccProviders,
CheckDestroy: rac.checkResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccConfig(map[string]interface{}{
"db_instance_id": "${alicloud_mongodb_instance.defaultHrZmxC.id}",
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
"db_instance_id": CHECKSET,
}),
),
},
{
ResourceName: resourceId,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{},
},
},
})
}

var AlicloudMongodbPrivateSrvNetworkAddressMap9657 = map[string]string{
"private_srv_connection_string_uri": CHECKSET,
}

func AlicloudMongodbPrivateSrvNetworkAddressBasicDependence9657(name string) string {
return fmt.Sprintf(`
variable "name" {
default = "%s"
}
variable "zone_id" {
default = "cn-shanghai-b"
}
variable "region_id" {
default = "cn-shanghai"
}
resource "alicloud_vpc" "defaultie35CW" {
cidr_block = "10.0.0.0/8"
vpc_name = var.name
}
resource "alicloud_vswitch" "defaultg0DCAR" {
vpc_id = alicloud_vpc.defaultie35CW.id
zone_id = var.zone_id
cidr_block = "10.0.0.0/24"
}
resource "alicloud_mongodb_instance" "defaultHrZmxC" {
engine_version = "4.4"
storage_type = "cloud_essd1"
vswitch_id = alicloud_vswitch.defaultg0DCAR.id
db_instance_storage = "20"
vpc_id = alicloud_vpc.defaultie35CW.id
db_instance_class = "mdb.shard.4x.large.d"
storage_engine = "WiredTiger"
network_type = "VPC"
zone_id = var.zone_id
}
`, name)
}

// Test Mongodb PrivateSrvNetworkAddress. <<< Resource test cases, automatically generated.
92 changes: 92 additions & 0 deletions alicloud/service_alicloud_mongodb_v2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package alicloud

import (
"fmt"
"strings"
"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"
)

type MongodbServiceV2 struct {
client *connectivity.AliyunClient
}

// DescribeMongodbPrivateSrvNetworkAddress <<< Encapsulated get interface for Mongodb PrivateSrvNetworkAddress.

func (s *MongodbServiceV2) DescribeMongodbPrivateSrvNetworkAddress(id string) (object map[string]interface{}, err error) {
client := s.client
var request map[string]interface{}
var response map[string]interface{}
var query map[string]interface{}
action := "DescribeAllNetworkAddress"
conn, err := client.NewDdsClient()
if err != nil {
return object, WrapError(err)
}
request = make(map[string]interface{})
query = make(map[string]interface{})
request["DBInstanceId"] = id
request["RegionId"] = client.RegionId

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("2015-12-01"), StringPointer("AK"), query, request, &runtime)

if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
addDebug(action, response, request)
if err != nil {
return object, WrapErrorf(err, DefaultErrorMsg, id, action, AlibabaCloudSdkGoERROR)
}

currentStatus := response["PrivateSrvConnectionStringUri"]
if currentStatus == nil {
return object, WrapErrorf(Error(GetNotFoundMessage("PrivateSrvNetworkAddress", id)), NotFoundMsg, response)
}

return response, nil
}

func (s *MongodbServiceV2) MongodbPrivateSrvNetworkAddressStateRefreshFunc(id string, field string, failStates []string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
object, err := s.DescribeMongodbPrivateSrvNetworkAddress(id)
if err != nil {
if NotFoundError(err) {
return object, "", nil
}
return nil, "", WrapError(err)
}

v, err := jsonpath.Get(field, object)
currentStatus := fmt.Sprint(v)

if strings.HasPrefix(field, "#") {
v, _ := jsonpath.Get(strings.TrimPrefix(field, "#"), object)
if v != nil {
currentStatus = "#CHECKSET"
}
}

for _, failState := range failStates {
if currentStatus == failState {
return object, currentStatus, WrapError(Error(FailedToReachTargetStatus, currentStatus))
}
}
return object, currentStatus, nil
}
}

// DescribeMongodbPrivateSrvNetworkAddress >>> Encapsulated.
Loading

0 comments on commit cf06abe

Please sign in to comment.