alicloud/data_source_alicloud_bastionhost_hosts.go (275 lines of code) (raw):

package alicloud import ( "fmt" "regexp" "time" "github.com/PaesslerAG/jsonpath" "github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/helper/validation" ) func dataSourceAlicloudBastionhostHosts() *schema.Resource { return &schema.Resource{ Read: dataSourceAlicloudBastionhostHostsRead, Schema: map[string]*schema.Schema{ "host_address": { Type: schema.TypeString, Optional: true, ForceNew: true, }, "ids": { Type: schema.TypeList, Optional: true, ForceNew: true, Elem: &schema.Schema{Type: schema.TypeString}, Computed: true, }, "name_regex": { Type: schema.TypeString, Optional: true, ValidateFunc: validation.ValidateRegexp, ForceNew: true, }, "names": { Type: schema.TypeList, Elem: &schema.Schema{Type: schema.TypeString}, Computed: true, }, "host_name": { Type: schema.TypeString, Optional: true, ForceNew: true, }, "instance_id": { Type: schema.TypeString, Required: true, ForceNew: true, }, "os_type": { Type: schema.TypeString, Optional: true, ForceNew: true, ValidateFunc: validation.StringInSlice([]string{"Linux", "Windows"}, false), }, "source": { Type: schema.TypeString, Optional: true, ForceNew: true, ValidateFunc: validation.StringInSlice([]string{"Ecs", "Local", "Rds"}, false), }, "source_instance_id": { Type: schema.TypeString, Optional: true, ForceNew: true, }, "source_instance_state": { Type: schema.TypeString, Optional: true, ForceNew: true, }, "output_file": { Type: schema.TypeString, Optional: true, }, "hosts": { Type: schema.TypeList, Computed: true, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "active_address_type": { Type: schema.TypeString, Computed: true, }, "comment": { Type: schema.TypeString, Computed: true, }, "id": { Type: schema.TypeString, Computed: true, }, "host_id": { Type: schema.TypeString, Computed: true, }, "host_name": { Type: schema.TypeString, Computed: true, }, "host_private_address": { Type: schema.TypeString, Computed: true, }, "host_public_address": { Type: schema.TypeString, Computed: true, }, "instance_id": { Type: schema.TypeString, Computed: true, }, "os_type": { Type: schema.TypeString, Computed: true, }, "protocols": { Type: schema.TypeList, Computed: true, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "host_finger_print": { Type: schema.TypeString, Computed: true, }, "port": { Type: schema.TypeInt, Computed: true, }, "protocol_name": { Type: schema.TypeString, Computed: true, }, }, }, }, "source": { Type: schema.TypeString, Computed: true, }, "source_instance_id": { Type: schema.TypeString, Computed: true, }, }, }, }, "enable_details": { Type: schema.TypeBool, Optional: true, Default: false, }, }, } } func dataSourceAlicloudBastionhostHostsRead(d *schema.ResourceData, meta interface{}) error { client := meta.(*connectivity.AliyunClient) action := "ListHosts" request := make(map[string]interface{}) if v, ok := d.GetOk("host_address"); ok { request["HostAddress"] = v } if v, ok := d.GetOk("host_name"); ok { request["HostName"] = v } request["InstanceId"] = d.Get("instance_id") if v, ok := d.GetOk("os_type"); ok { request["OSType"] = v } request["RegionId"] = client.RegionId if v, ok := d.GetOk("source"); ok { request["Source"] = v } if v, ok := d.GetOk("source_instance_id"); ok { request["SourceInstanceId"] = v } if v, ok := d.GetOk("source_instance_state"); ok { request["SourceInstanceState"] = v } request["PageSize"] = PageSizeLarge request["PageNumber"] = 1 var objects []map[string]interface{} var hostNameRegex *regexp.Regexp if v, ok := d.GetOk("name_regex"); ok { r, err := regexp.Compile(v.(string)) if err != nil { return WrapError(err) } hostNameRegex = r } idsMap := make(map[string]string) if v, ok := d.GetOk("ids"); ok { for _, vv := range v.([]interface{}) { if vv == nil { continue } idsMap[vv.(string)] = vv.(string) } } var response map[string]interface{} var err error for { wait := incrementalWait(3*time.Second, 3*time.Second) err = resource.Retry(5*time.Minute, func() *resource.RetryError { response, err = client.RpcPost("Yundun-bastionhost", "2019-12-09", action, nil, request, true) 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 WrapErrorf(err, DataDefaultErrorMsg, "alicloud_bastionhost_hosts", action, AlibabaCloudSdkGoERROR) } resp, err := jsonpath.Get("$.Hosts", response) if err != nil { return WrapErrorf(err, FailedGetAttributeMsg, action, "$.Hosts", response) } result, _ := resp.([]interface{}) for _, v := range result { item := v.(map[string]interface{}) if hostNameRegex != nil && !hostNameRegex.MatchString(fmt.Sprint(item["HostName"])) { continue } if len(idsMap) > 0 { if _, ok := idsMap[fmt.Sprint(item["HostId"])]; !ok { continue } } objects = append(objects, item) } if len(result) < PageSizeLarge { break } request["PageNumber"] = request["PageNumber"].(int) + 1 } ids := make([]string, 0) names := make([]interface{}, 0) s := make([]map[string]interface{}, 0) for _, object := range objects { mapping := map[string]interface{}{ "active_address_type": object["ActiveAddressType"], "comment": object["Comment"], "id": fmt.Sprint(object["HostId"]), "host_id": fmt.Sprint(object["HostId"]), "host_name": object["HostName"], "host_private_address": object["HostPrivateAddress"], "host_public_address": object["HostPublicAddress"], "instance_id": request["InstanceId"], "os_type": object["OSType"], "source": object["Source"], "source_instance_id": object["SourceInstanceId"], } ids = append(ids, fmt.Sprint(mapping["id"])) names = append(names, object["HostName"]) s = append(s, mapping) } d.SetId(dataResourceIdHash(ids)) if err := d.Set("ids", ids); err != nil { return WrapError(err) } if err := d.Set("names", names); err != nil { return WrapError(err) } if err := d.Set("hosts", s); err != nil { return WrapError(err) } if output, ok := d.GetOk("output_file"); ok && output.(string) != "" { writeToFile(output.(string), s) } return nil }