|
| 1 | +/* |
| 2 | +Use this data source to query dayu eip rules |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_dayu_eip" "test" { |
| 8 | + resource_id="bgpip-000004xg" |
| 9 | +} |
| 10 | +``` |
| 11 | +*/ |
| 12 | +package tencentcloud |
| 13 | + |
| 14 | +import ( |
| 15 | + "context" |
| 16 | + |
| 17 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 18 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 19 | +) |
| 20 | + |
| 21 | +func dataSourceTencentCloudDayuEip() *schema.Resource { |
| 22 | + return &schema.Resource{ |
| 23 | + Read: dataSourceTencentCloudDayuEipRead, |
| 24 | + Schema: map[string]*schema.Schema{ |
| 25 | + "resource_id": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Required: true, |
| 28 | + Description: "Id of the resource.", |
| 29 | + }, |
| 30 | + "bind_status": { |
| 31 | + Type: schema.TypeList, |
| 32 | + Elem: &schema.Schema{ |
| 33 | + Type: schema.TypeString, |
| 34 | + ValidateFunc: validateAllowedStringValue(DDOS_EIP_BIND_STATUS), |
| 35 | + }, |
| 36 | + Optional: true, |
| 37 | + Description: "The binding state of the instance, value range [BINDING, BIND, UNBINDING, UNBIND], default is [BINDING, BIND, UNBINDING, UNBIND].", |
| 38 | + }, |
| 39 | + "offset": { |
| 40 | + Type: schema.TypeInt, |
| 41 | + Optional: true, |
| 42 | + Default: 0, |
| 43 | + Description: "The page start offset, default is `0`.", |
| 44 | + }, |
| 45 | + "limit": { |
| 46 | + Type: schema.TypeInt, |
| 47 | + Optional: true, |
| 48 | + Default: 10, |
| 49 | + Description: "The number of pages, default is `10`.", |
| 50 | + }, |
| 51 | + "result_output_file": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Optional: true, |
| 54 | + Description: "Used to save results.", |
| 55 | + }, |
| 56 | + "list": { |
| 57 | + Type: schema.TypeList, |
| 58 | + Computed: true, |
| 59 | + Description: "A list of layer 4 rules. Each element contains the following attributes:", |
| 60 | + Elem: &schema.Resource{ |
| 61 | + Schema: map[string]*schema.Schema{ |
| 62 | + "eip_list": { |
| 63 | + Type: schema.TypeList, |
| 64 | + Required: true, |
| 65 | + Elem: &schema.Schema{ |
| 66 | + Type: schema.TypeString, |
| 67 | + Description: "", |
| 68 | + }, |
| 69 | + }, |
| 70 | + "instance_id": { |
| 71 | + Type: schema.TypeString, |
| 72 | + Optional: true, |
| 73 | + Description: "ID of the resource instance.", |
| 74 | + }, |
| 75 | + "eip_bound_rsc_ins": { |
| 76 | + Type: schema.TypeString, |
| 77 | + Optional: true, |
| 78 | + Description: "The ID of the resource instance for the binding.", |
| 79 | + }, |
| 80 | + "eip_bound_rsc_eni": { |
| 81 | + Type: schema.TypeString, |
| 82 | + Required: true, |
| 83 | + Description: "The ID of the bound ENI.", |
| 84 | + }, |
| 85 | + "eip_bound_rsc_vip": { |
| 86 | + Type: schema.TypeString, |
| 87 | + Required: true, |
| 88 | + Description: "Bind the resource intranet IP.", |
| 89 | + }, |
| 90 | + "created_time": { |
| 91 | + Type: schema.TypeString, |
| 92 | + Required: true, |
| 93 | + Description: "The created time of resource.", |
| 94 | + }, |
| 95 | + "expired_time": { |
| 96 | + Type: schema.TypeString, |
| 97 | + Required: true, |
| 98 | + Description: "The expired time of resource.", |
| 99 | + }, |
| 100 | + "modify_time": { |
| 101 | + Type: schema.TypeString, |
| 102 | + Required: true, |
| 103 | + Description: "The modify time of resource.", |
| 104 | + }, |
| 105 | + "protection_status": { |
| 106 | + Type: schema.TypeString, |
| 107 | + Required: true, |
| 108 | + Description: "The protection status of the asset instance.", |
| 109 | + }, |
| 110 | + "eip_address_status": { |
| 111 | + Type: schema.TypeString, |
| 112 | + Required: true, |
| 113 | + Description: "Eip PUBLIC IP status.", |
| 114 | + }, |
| 115 | + "region": { |
| 116 | + Type: schema.TypeString, |
| 117 | + Required: true, |
| 118 | + Description: "The region where the asset instance is located.", |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + }, |
| 123 | + }, |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func dataSourceTencentCloudDayuEipRead(d *schema.ResourceData, meta interface{}) error { |
| 128 | + defer logElapsed("data_source.tencentcloud_dayu_l4_rules.read")() |
| 129 | + |
| 130 | + logId := getLogId(contextNil) |
| 131 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 132 | + |
| 133 | + resourceId := d.Get("resource_id").(string) |
| 134 | + var bindStatus []string |
| 135 | + if v, ok := d.GetOk("bind_status"); ok { |
| 136 | + tmpBindStatusList := v.([]interface{}) |
| 137 | + for _, tmpBindStatus := range tmpBindStatusList { |
| 138 | + bindStatus = append(bindStatus, tmpBindStatus.(string)) |
| 139 | + } |
| 140 | + } else { |
| 141 | + bindStatus = DDOS_EIP_BIND_STATUS |
| 142 | + } |
| 143 | + offset := d.Get("offset").(int) |
| 144 | + limit := d.Get("limit").(int) |
| 145 | + |
| 146 | + antiddosService := AntiddosService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 147 | + result, err := antiddosService.DescribeListBGPIPInstances(ctx, resourceId, bindStatus, offset, limit) |
| 148 | + if err != nil { |
| 149 | + return err |
| 150 | + } |
| 151 | + resultList := make([]map[string]interface{}, 0) |
| 152 | + for _, eipItem := range result { |
| 153 | + tmpEipInfo := make(map[string]interface{}) |
| 154 | + eipList := make([]string, 0) |
| 155 | + for _, eip := range eipItem.InstanceDetail.EipList { |
| 156 | + eipList = append(eipList, *eip) |
| 157 | + } |
| 158 | + tmpEipInfo["eip_list"] = eipList |
| 159 | + tmpEipInfo["instance_id"] = *eipItem.InstanceDetail.InstanceId |
| 160 | + tmpEipInfo["region"] = *eipItem.Region.Region |
| 161 | + tmpEipInfo["eip_bound_rsc_ins"] = *eipItem.EipAddressInfo.EipBoundRscIns |
| 162 | + tmpEipInfo["eip_bound_rsc_eni"] = *eipItem.EipAddressInfo.EipBoundRscEni |
| 163 | + tmpEipInfo["eip_bound_rsc_vip"] = *eipItem.EipAddressInfo.EipBoundRscVip |
| 164 | + tmpEipInfo["eip_address_status"] = *eipItem.EipAddressStatus |
| 165 | + tmpEipInfo["protection_status"] = *eipItem.Status |
| 166 | + tmpEipInfo["created_time"] = *eipItem.CreatedTime |
| 167 | + tmpEipInfo["expired_time"] = *eipItem.ExpiredTime |
| 168 | + tmpEipInfo["modify_time"] = *eipItem.EipAddressInfo.ModifyTime |
| 169 | + |
| 170 | + resultList = append(resultList, tmpEipInfo) |
| 171 | + } |
| 172 | + ids := make([]string, 0, len(resultList)) |
| 173 | + for _, listItem := range resultList { |
| 174 | + ids = append(ids, listItem["instance_id"].(string)) |
| 175 | + } |
| 176 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 177 | + _ = d.Set("list", resultList) |
| 178 | + output, ok := d.GetOk("result_output_file") |
| 179 | + if ok && output.(string) != "" { |
| 180 | + return writeToFile(output.(string), resultList) |
| 181 | + } |
| 182 | + return nil |
| 183 | + |
| 184 | +} |
0 commit comments