alibabacloudstack/data_source_apsarastack_polardb_instances.go (489 lines of code) (raw):

package alibabacloudstack // Generated By apsara-orchestration-generator // Product POLARDB Resouce DBInstance // generated by datasource_main.go2.jinja2 import ( "encoding/json" "fmt" "strconv" "github.com/aliyun/terraform-provider-alibabacloudstack/alibabacloudstack/connectivity" "github.com/aliyun/terraform-provider-alibabacloudstack/alibabacloudstack/errmsgs" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) func dataSourceAlibabacloudStackPolardbDbInstances() *schema.Resource { return &schema.Resource{ Read: dataSourceAlibabacloudStackPolardbDbInstancesRead, Schema: map[string]*schema.Schema{ "ids": { Type: schema.TypeList, Optional: true, Elem: &schema.Schema{Type: schema.TypeString}, Computed: true, ForceNew: true, MinItems: 1, }, "network_type": { Type: schema.TypeString, Optional: true, }, "db_instance_type": { Type: schema.TypeString, Optional: true, }, "vswitch_id": { Type: schema.TypeString, Optional: true, }, "vpc_id": { Type: schema.TypeString, Optional: true, }, "db_instance_class": { Type: schema.TypeString, Optional: true, }, "payment_type": { Type: schema.TypeString, Optional: true, }, "status": { Type: schema.TypeString, Optional: true, }, "db_instance_id": { Type: schema.TypeString, Optional: true, }, "engine_version": { Type: schema.TypeString, Optional: true, }, "resource_group_id": { Type: schema.TypeString, Optional: true, }, "region_id": { Type: schema.TypeString, Required: true, }, "engine": { Type: schema.TypeString, Optional: true, }, "output_file": { Type: schema.TypeString, Optional: true, }, "db_instances": { Type: schema.TypeList, Computed: true, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "id": { Type: schema.TypeString, Computed: true, }, "auto_pay": { Type: schema.TypeBool, Computed: true, }, "auto_renew": { Type: schema.TypeString, Computed: true, }, "auto_upgrade_minor_version": { Type: schema.TypeString, Computed: true, }, "business_info": { Type: schema.TypeString, Computed: true, }, "category": { Type: schema.TypeString, Computed: true, }, "classic_expired_days": { Type: schema.TypeInt, Computed: true, }, "commodity_code": { Type: schema.TypeString, Computed: true, }, "connection_mode": { Type: schema.TypeString, Computed: true, }, "connection_string_prefix": { Type: schema.TypeString, Computed: true, }, "connection_string_type": { Type: schema.TypeString, Computed: true, }, "current_connection_string": { Type: schema.TypeString, Computed: true, }, "db_instance_description": { Type: schema.TypeString, Computed: true, }, "db_instance_storage": { Type: schema.TypeInt, Computed: true, }, "db_instance_type": { Type: schema.TypeString, Computed: true, }, "db_instance_class": { Type: schema.TypeString, Computed: true, }, "db_instance_id": { Type: schema.TypeString, Computed: true, }, "db_instance_net_type": { Type: schema.TypeString, Computed: true, }, "db_instance_storage_type": { Type: schema.TypeString, Computed: true, }, "distribution_type": { Type: schema.TypeString, Computed: true, }, "effective_time": { Type: schema.TypeString, Computed: true, }, "encryption_key": { Type: schema.TypeString, Computed: true, }, "engine": { Type: schema.TypeString, Computed: true, }, "engine_version": { Type: schema.TypeString, Computed: true, }, "expire_time": { Type: schema.TypeString, Computed: true, }, "guard_db_instance_id": { Type: schema.TypeString, Computed: true, }, "lock_mode": { Type: schema.TypeString, Computed: true, }, "lock_reason": { Type: schema.TypeString, Computed: true, }, "maintain_time": { Type: schema.TypeString, Computed: true, }, "master_instance_id": { Type: schema.TypeString, Computed: true, }, "max_delay_time": { Type: schema.TypeString, Computed: true, }, "network_type": { Type: schema.TypeString, Computed: true, }, "payment_type": { Type: schema.TypeString, Computed: true, }, "period": { Type: schema.TypeString, Computed: true, }, "port": { Type: schema.TypeString, Computed: true, }, "private_ip_address": { Type: schema.TypeString, Computed: true, }, "read_write_splitting_classic_expired_days": { Type: schema.TypeInt, Computed: true, }, "read_write_splitting_private_ip_address": { Type: schema.TypeString, Computed: true, }, "record_total": { Type: schema.TypeInt, Computed: true, }, "region_id": { Type: schema.TypeString, Computed: true, }, "resource_group_id": { Type: schema.TypeString, Computed: true, }, "resource_type": { Type: schema.TypeString, Computed: true, }, "retain_classic": { Type: schema.TypeString, Computed: true, }, "role_arn": { Type: schema.TypeString, Computed: true, }, "security_ip_list": { Type: schema.TypeString, Computed: true, }, "security_ip_mode": { Type: schema.TypeString, Computed: true, }, "sql_collector_status": { Type: schema.TypeString, Computed: true, }, "status": { Type: schema.TypeString, Computed: true, }, "table_meta": { Type: schema.TypeString, Computed: true, }, "tags": { Type: schema.TypeList, Computed: true, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "tag_key": { Type: schema.TypeString, Computed: true, }, "tag_value": { Type: schema.TypeString, Computed: true, }, }, }, }, "temp_db_instance_id": { Type: schema.TypeString, Computed: true, }, "used_time": { Type: schema.TypeString, Computed: true, }, "vswitch_id": { Type: schema.TypeString, Computed: true, }, "vpc_cloud_instance_id": { Type: schema.TypeString, Computed: true, }, "vpc_id": { Type: schema.TypeString, Computed: true, }, "weight": { Type: schema.TypeString, Computed: true, }, "zone_id_slave_one": { Type: schema.TypeString, Computed: true, }, "zone_id_slave_two": { Type: schema.TypeString, Computed: true, }, }, }, }, }, } } func dataSourceAlibabacloudStackPolardbDbInstancesRead(d *schema.ResourceData, meta interface{}) error { client := meta.(*connectivity.AlibabacloudStackClient) request := client.NewCommonRequest("POST", "polardb", "2024-01-30", "DescribeDBInstances", "") PolardbDescribedbinstancesResponse := PolardbDescribedbinstancesResponse{} if v, ok := d.GetOk("db_instance_type"); ok { request.QueryParams["DBInstanceType"] = v.(string) } if v, ok := d.GetOk("db_instance_class"); ok { request.QueryParams["DBInstanceClass"] = v.(string) } if v, ok := d.GetOk("db_instance_id"); ok { request.QueryParams["DBInstanceId"] = v.(string) } if v, ok := d.GetOk("engine"); ok { request.QueryParams["Engine"] = v.(string) } if v, ok := d.GetOk("engine_version"); ok { request.QueryParams["EngineVersion"] = v.(string) } if v, ok := d.GetOk("network_type"); ok { request.QueryParams["InstanceNetworkType"] = v.(string) } if v, ok := d.GetOk("page_number"); ok { request.QueryParams["PageNumber"] = strconv.Itoa(v.(int)) } if v, ok := d.GetOk("page_size"); ok { request.QueryParams["PageSize"] = strconv.Itoa(v.(int)) } if v, ok := d.GetOk("payment_type"); ok { request.QueryParams["PayType"] = v.(string) } if v, ok := d.GetOk("region_id"); ok { request.QueryParams["RegionId"] = v.(string) } else { return fmt.Errorf("RegionId is required") } if v, ok := d.GetOk("resource_group_id"); ok { request.QueryParams["ResourceGroupId"] = v.(string) } if v, ok := d.GetOk("status"); ok { request.QueryParams["DBInstanceStatus"] = v.(string) } if v, ok := d.GetOk("vswitch_id"); ok { request.QueryParams["VSwitchId"] = v.(string) } if v, ok := d.GetOk("vpc_id"); ok { request.QueryParams["VpcId"] = v.(string) } bresponse, err := client.ProcessCommonRequest(request) if err != nil { if bresponse == nil { return errmsgs.WrapErrorf(err, "Process Common Request Failed") } errmsg := errmsgs.GetBaseResponseErrorMessage(bresponse.BaseResponse) return errmsgs.WrapErrorf(err, errmsgs.RequestV1ErrorMsg, "alibabacloudstack_polardb_db_instance", "DescribeDBInstances", request.GetActionName(), errmsgs.AlibabacloudStackSdkGoERROR, errmsg) } err = json.Unmarshal(bresponse.GetHttpContentBytes(), &PolardbDescribedbinstancesResponse) if err != nil { return errmsgs.WrapErrorf(err, errmsgs.DefaultErrorMsg, "alibabacloudstack_polardb_db_instance", "DescribeDBInstances", errmsgs.AlibabacloudStackSdkGoERROR) } var ids []string datas := make([]interface{}, 0) for _, data := range PolardbDescribedbinstancesResponse.Items.DBInstance { i := map[string]interface{}{ "category": data.Category, "db_instance_description": data.DBInstanceDescription, "db_instance_type": data.DBInstanceType, "db_instance_class": data.DBInstanceClass, "db_instance_id": data.DBInstanceId, "db_instance_net_type": data.DBInstanceNetType, "db_instance_storage_type": data.DBInstanceStorageType, "engine": data.Engine, "engine_version": data.EngineVersion, "expire_time": data.ExpireTime, "guard_db_instance_id": data.GuardDBInstanceId, "lock_mode": data.LockMode, "lock_reason": data.LockReason, "master_instance_id": data.MasterInstanceId, "network_type": data.InstanceNetworkType, "payment_type": data.PayType, "region_id": data.RegionId, "resource_group_id": data.ResourceGroupId, "status": data.DBInstanceStatus, "temp_db_instance_id": data.TempDBInstanceId, "vswitch_id": data.VSwitchId, "vpc_cloud_instance_id": data.VpcCloudInstanceId, "vpc_id": data.VpcId, } datas = append(datas, i) db_instance_id := data.DBInstanceId ids = append(ids, db_instance_id) } d.SetId(dataResourceIdHash(ids)) if err := d.Set("db_instances", datas); err != nil { return err } if err := d.Set("ids", ids); err != nil { return err } if output, ok := d.GetOk("output_file"); ok && output.(string) != "" { writeToFile(output.(string), datas) } return nil } type PolardbDescribedbinstancesResponse struct { Items struct { DBInstance []struct { SlaveZones struct { SlaveZone []struct { ZoneId string `json:"ZoneId"` } `json:"SlaveZone"` } `json:"SlaveZones"` ReadOnlyDBInstanceIds struct { ReadOnlyDBInstanceId []struct { DBInstanceId string `json:"DBInstanceId"` } `json:"ReadOnlyDBInstanceId"` } `json:"ReadOnlyDBInstanceIds"` InsId int `json:"InsId"` DBInstanceId string `json:"DBInstanceId"` DBInstanceDescription string `json:"DBInstanceDescription"` PayType string `json:"PayType"` DBInstanceType string `json:"DBInstanceType"` RegionId string `json:"RegionId"` ExpireTime string `json:"ExpireTime"` DestroyTime string `json:"DestroyTime"` DBInstanceStatus string `json:"DBInstanceStatus"` Engine string `json:"Engine"` DBInstanceNetType interface{} `json:"DBInstanceNetType"` ConnectionMode string `json:"ConnectionMode"` LockMode string `json:"LockMode"` Category string `json:"Category"` DBInstanceStorageType string `json:"DBInstanceStorageType"` DBInstanceClass string `json:"DBInstanceClass"` InstanceNetworkType string `json:"InstanceNetworkType"` VpcCloudInstanceId string `json:"VpcCloudInstanceId"` LockReason string `json:"LockReason"` ZoneId string `json:"ZoneId"` MutriORsignle bool `json:"MutriORsignle"` CreateTime string `json:"CreateTime"` EngineVersion string `json:"EngineVersion"` GuardDBInstanceId string `json:"GuardDBInstanceId"` TempDBInstanceId string `json:"TempDBInstanceId"` MasterInstanceId string `json:"MasterInstanceId"` VpcId string `json:"VpcId"` VSwitchId string `json:"VSwitchId"` ReplicateId string `json:"ReplicateId"` ResourceGroupId string `json:"ResourceGroupId"` DispenseMode string `json:"DispenseMode"` MasterZone string `json:"MasterZone"` CpuType string `json:"CpuType"` Vip string `json:"Vip"` Vip_v6 string `json:"Vip_v6"` Vport string `json:"Vport"` } `json:"DBInstance"` } `json:"Items"` RequestId string `json:"RequestId"` PageNumber int `json:"PageNumber"` TotalRecordCount int `json:"TotalRecordCount"` PageRecordCount int `json:"PageRecordCount"` }