|
| 1 | +// Package alicloud. This file is generated automatically. Please do not modify it manually, thank you! |
| 2 | +package alicloud |
| 3 | + |
| 4 | +import ( |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/PaesslerAG/jsonpath" |
| 9 | + util "github.com/alibabacloud-go/tea-utils/service" |
| 10 | + "github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 13 | +) |
| 14 | + |
| 15 | +func dataSourceAliCloudVpcIpv6CidrBlocks() *schema.Resource { |
| 16 | + return &schema.Resource{ |
| 17 | + Read: dataSourceAliCloudVpcIpv6CidrBlockRead, |
| 18 | + Schema: map[string]*schema.Schema{ |
| 19 | + "ids": { |
| 20 | + Type: schema.TypeList, |
| 21 | + Optional: true, |
| 22 | + ForceNew: true, |
| 23 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 24 | + Computed: true, |
| 25 | + }, |
| 26 | + "vpc_id": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Required: true, |
| 29 | + ForceNew: true, |
| 30 | + }, |
| 31 | + "blocks": { |
| 32 | + Type: schema.TypeList, |
| 33 | + Computed: true, |
| 34 | + Elem: &schema.Resource{ |
| 35 | + Schema: map[string]*schema.Schema{ |
| 36 | + "ipv6_cidr_block": { |
| 37 | + Type: schema.TypeString, |
| 38 | + Computed: true, |
| 39 | + }, |
| 40 | + "id": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Computed: true, |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | + }, |
| 47 | + "output_file": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Optional: true, |
| 50 | + ForceNew: true, |
| 51 | + }, |
| 52 | + }, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func dataSourceAliCloudVpcIpv6CidrBlockRead(d *schema.ResourceData, meta interface{}) error { |
| 57 | + client := meta.(*connectivity.AliyunClient) |
| 58 | + |
| 59 | + var objects []map[string]interface{} |
| 60 | + |
| 61 | + idsMap := make(map[string]string) |
| 62 | + if v, ok := d.GetOk("ids"); ok { |
| 63 | + for _, vv := range v.([]interface{}) { |
| 64 | + if vv == nil { |
| 65 | + continue |
| 66 | + } |
| 67 | + idsMap[vv.(string)] = vv.(string) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + var request map[string]interface{} |
| 72 | + var response map[string]interface{} |
| 73 | + var query map[string]interface{} |
| 74 | + action := "DescribeVpcAttribute" |
| 75 | + var err error |
| 76 | + request = make(map[string]interface{}) |
| 77 | + query = make(map[string]interface{}) |
| 78 | + request["RegionId"] = client.RegionId |
| 79 | + if v, ok := d.GetOk("vpc_id"); ok { |
| 80 | + request["VpcId"] = v |
| 81 | + } |
| 82 | + request["VpcId"] = d.Get("vpc_id") |
| 83 | + runtime := util.RuntimeOptions{} |
| 84 | + runtime.SetAutoretry(true) |
| 85 | + wait := incrementalWait(3*time.Second, 5*time.Second) |
| 86 | + err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError { |
| 87 | + response, err = client.RpcPost("Vpc", "2016-04-28", action, query, request, true) |
| 88 | + |
| 89 | + if err != nil { |
| 90 | + if IsExpectedErrors(err, []string{"LastTokenProcessing", "OperationConflict", "SystemBusy", "ServiceUnavailable", "IncorrectStatus"}) || NeedRetry(err) { |
| 91 | + wait() |
| 92 | + return resource.RetryableError(err) |
| 93 | + } |
| 94 | + return resource.NonRetryableError(err) |
| 95 | + } |
| 96 | + addDebug(action, response, request) |
| 97 | + return nil |
| 98 | + }) |
| 99 | + if err != nil { |
| 100 | + return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) |
| 101 | + } |
| 102 | + |
| 103 | + resp, _ := jsonpath.Get("$.Ipv6CidrBlocks.Ipv6CidrBlock[*]", response) |
| 104 | + |
| 105 | + result, _ := resp.([]interface{}) |
| 106 | + for _, v := range result { |
| 107 | + item := v.(map[string]interface{}) |
| 108 | + id := fmt.Sprint(request["VpcId"], "#", item["Ipv6CidrBlock"]) |
| 109 | + if len(idsMap) > 0 { |
| 110 | + if _, ok := idsMap[id]; !ok { |
| 111 | + continue |
| 112 | + } |
| 113 | + } |
| 114 | + objects = append(objects, item) |
| 115 | + } |
| 116 | + |
| 117 | + ids := make([]string, 0) |
| 118 | + s := make([]map[string]interface{}, 0) |
| 119 | + for _, objectRaw := range objects { |
| 120 | + mapping := map[string]interface{}{} |
| 121 | + |
| 122 | + mapping["id"] = fmt.Sprint(request["VpcId"], "#", objectRaw["Ipv6CidrBlock"]) |
| 123 | + |
| 124 | + mapping["ipv6_cidr_block"] = objectRaw["Ipv6CidrBlock"] |
| 125 | + |
| 126 | + ids = append(ids, fmt.Sprint(mapping["id"])) |
| 127 | + s = append(s, mapping) |
| 128 | + } |
| 129 | + |
| 130 | + d.SetId(dataResourceIdHash(ids)) |
| 131 | + if err := d.Set("ids", ids); err != nil { |
| 132 | + return WrapError(err) |
| 133 | + } |
| 134 | + |
| 135 | + if err := d.Set("blocks", s); err != nil { |
| 136 | + return WrapError(err) |
| 137 | + } |
| 138 | + |
| 139 | + if output, ok := d.GetOk("output_file"); ok && output.(string) != "" { |
| 140 | + writeToFile(output.(string), s) |
| 141 | + } |
| 142 | + return nil |
| 143 | +} |
0 commit comments