|
| 1 | +package ucloud |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 7 | + "github.com/ucloud/ucloud-sdk-go/services/label" |
| 8 | + "github.com/ucloud/ucloud-sdk-go/ucloud" |
| 9 | +) |
| 10 | + |
| 11 | +func dataSourceUCloudLabelResources() *schema.Resource { |
| 12 | + return &schema.Resource{ |
| 13 | + Read: dataSourceUCloudLabelResourcesRead, |
| 14 | + Schema: map[string]*schema.Schema{ |
| 15 | + "key": { |
| 16 | + Type: schema.TypeString, |
| 17 | + Required: true, |
| 18 | + }, |
| 19 | + "value": { |
| 20 | + Type: schema.TypeString, |
| 21 | + Required: true, |
| 22 | + }, |
| 23 | + "resource_types": { |
| 24 | + Type: schema.TypeList, |
| 25 | + Required: true, |
| 26 | + Elem: &schema.Schema{ |
| 27 | + Type: schema.TypeString, |
| 28 | + }, |
| 29 | + }, |
| 30 | + "project_ids": { |
| 31 | + Type: schema.TypeList, |
| 32 | + Required: true, |
| 33 | + Elem: &schema.Schema{ |
| 34 | + Type: schema.TypeString, |
| 35 | + }, |
| 36 | + }, |
| 37 | + "output_file": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Optional: true, |
| 40 | + }, |
| 41 | + "total_count": { |
| 42 | + Type: schema.TypeInt, |
| 43 | + Computed: true, |
| 44 | + }, |
| 45 | + "resources": { |
| 46 | + Type: schema.TypeList, |
| 47 | + Computed: true, |
| 48 | + Elem: &schema.Resource{ |
| 49 | + Schema: map[string]*schema.Schema{ |
| 50 | + "id": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Computed: true, |
| 53 | + }, |
| 54 | + "name": { |
| 55 | + Type: schema.TypeString, |
| 56 | + Computed: true, |
| 57 | + }, |
| 58 | + "type": { |
| 59 | + Type: schema.TypeString, |
| 60 | + Computed: true, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func dataSourceUCloudLabelResourcesRead(d *schema.ResourceData, meta interface{}) error { |
| 70 | + client := meta.(*UCloudClient) |
| 71 | + conn := client.labelconn |
| 72 | + limit := 100 |
| 73 | + offset := 0 |
| 74 | + resourcesData := make([]map[string]interface{}, 0) |
| 75 | + ids := make([]string, 0) |
| 76 | + for { |
| 77 | + listResourcesReq := conn.NewListResourcesByLabelsRequest() |
| 78 | + listResourcesReq.ResourceTypes = interfaceSliceToStringSlice(d.Get("resource_types").([]interface{})) |
| 79 | + listResourcesReq.ProjectIds = interfaceSliceToStringSlice(d.Get("project_ids").([]interface{})) |
| 80 | + listResourcesReq.Limit = ucloud.Int(limit) |
| 81 | + listResourcesReq.Offset = ucloud.Int(offset) |
| 82 | + listResourcesReq.Labels = []label.ListResourcesByLabelsParamLabels{ |
| 83 | + { |
| 84 | + Key: ucloud.String(d.Get("key").(string)), |
| 85 | + Value: ucloud.String(d.Get("value").(string)), |
| 86 | + }, |
| 87 | + } |
| 88 | + listResourcesResp, err := conn.ListResourcesByLabels(listResourcesReq) |
| 89 | + if err != nil { |
| 90 | + return fmt.Errorf("error on reading resources list, %s", err) |
| 91 | + } |
| 92 | + |
| 93 | + if len(listResourcesResp.Resources) < 1 { |
| 94 | + break |
| 95 | + } |
| 96 | + for _, resource := range listResourcesResp.Resources { |
| 97 | + ids = append(ids, resource.ResourceId) |
| 98 | + resourcesData = append(resourcesData, map[string]interface{}{ |
| 99 | + "id": resource.ResourceId, |
| 100 | + "name": resource.ResourceName, |
| 101 | + "type": resource.ResourceType, |
| 102 | + }) |
| 103 | + } |
| 104 | + if len(listResourcesResp.Resources) < limit { |
| 105 | + break |
| 106 | + } |
| 107 | + offset = offset + limit |
| 108 | + } |
| 109 | + d.SetId(hashStringArray(ids)) |
| 110 | + d.Set("total_count", len(resourcesData)) |
| 111 | + if err := d.Set("resources", resourcesData); err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + |
| 115 | + if outputFile, ok := d.GetOk("output_file"); ok && outputFile.(string) != "" { |
| 116 | + writeToFile(outputFile.(string), resourcesData) |
| 117 | + } |
| 118 | + return nil |
| 119 | +} |
0 commit comments