|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of TCR instances. |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_tcr_instances" "name" { |
| 8 | + name = "test" |
| 9 | +} |
| 10 | +``` |
| 11 | +*/ |
| 12 | +package tencentcloud |
| 13 | + |
| 14 | +import ( |
| 15 | + "context" |
| 16 | + "fmt" |
| 17 | + "log" |
| 18 | + |
| 19 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 20 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 21 | + tcr "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcr/v20190924" |
| 22 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 23 | +) |
| 24 | + |
| 25 | +func dataSourceTencentCloudTCRInstances() *schema.Resource { |
| 26 | + return &schema.Resource{ |
| 27 | + Read: dataSourceTencentCloudTCRInstancesRead, |
| 28 | + |
| 29 | + Schema: map[string]*schema.Schema{ |
| 30 | + "name": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Optional: true, |
| 33 | + Description: "Name of the TCR instance to query.", |
| 34 | + }, |
| 35 | + "instance_id": { |
| 36 | + Type: schema.TypeString, |
| 37 | + Optional: true, |
| 38 | + Description: "Id of the TCR instance to query.", |
| 39 | + }, |
| 40 | + "result_output_file": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Optional: true, |
| 43 | + Description: "Used to save results.", |
| 44 | + }, |
| 45 | + |
| 46 | + // Computed values |
| 47 | + "instance_list": { |
| 48 | + Type: schema.TypeList, |
| 49 | + Computed: true, |
| 50 | + Description: "Information list of the dedicated TCR instances.", |
| 51 | + Elem: &schema.Resource{ |
| 52 | + Schema: map[string]*schema.Schema{ |
| 53 | + "id": { |
| 54 | + Type: schema.TypeString, |
| 55 | + Computed: true, |
| 56 | + Description: "Id of the TCR instance.", |
| 57 | + }, |
| 58 | + "name": { |
| 59 | + Type: schema.TypeString, |
| 60 | + Computed: true, |
| 61 | + Description: "Name of TCR instance.", |
| 62 | + }, |
| 63 | + "status": { |
| 64 | + Type: schema.TypeString, |
| 65 | + Computed: true, |
| 66 | + Description: "Status of the TCR instance.", |
| 67 | + }, |
| 68 | + "public_domain": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Computed: true, |
| 71 | + Description: "Public address for access of the TCR instance.", |
| 72 | + }, |
| 73 | + "internal_end_point": { |
| 74 | + Type: schema.TypeString, |
| 75 | + Computed: true, |
| 76 | + Description: "Internal address for access of the TCR instance.", |
| 77 | + }, |
| 78 | + "instance_type": { |
| 79 | + Type: schema.TypeString, |
| 80 | + Computed: true, |
| 81 | + Description: "Instance type.", |
| 82 | + }, |
| 83 | + "tags": { |
| 84 | + Type: schema.TypeMap, |
| 85 | + Computed: true, |
| 86 | + Description: "Tags of the TCR instance.", |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func dataSourceTencentCloudTCRInstancesRead(d *schema.ResourceData, meta interface{}) error { |
| 96 | + defer logElapsed("data_source.tencentcloud_tcr_instances.read")() |
| 97 | + |
| 98 | + logId := getLogId(contextNil) |
| 99 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 100 | + |
| 101 | + var name, instanceId string |
| 102 | + var filters = make([]*tcr.Filter, 0) |
| 103 | + if v, ok := d.GetOk("name"); ok { |
| 104 | + name = v.(string) |
| 105 | + filters = append(filters, &tcr.Filter{Name: helper.String("RegistryName"), Values: []*string{&name}}) |
| 106 | + } |
| 107 | + |
| 108 | + if v, ok := d.GetOk("instance_id"); ok { |
| 109 | + instanceId = v.(string) |
| 110 | + } |
| 111 | + |
| 112 | + if instanceId == "" && name == "" { |
| 113 | + return fmt.Errorf("instance_id or name must be set at least one.") |
| 114 | + } |
| 115 | + tcrService := TCRService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 116 | + var outErr, inErr error |
| 117 | + instances, outErr := tcrService.DescribeTCRInstances(ctx, instanceId, filters) |
| 118 | + if outErr != nil { |
| 119 | + outErr = resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 120 | + instances, inErr = tcrService.DescribeTCRInstances(ctx, instanceId, filters) |
| 121 | + if inErr != nil { |
| 122 | + return retryError(inErr) |
| 123 | + } |
| 124 | + return nil |
| 125 | + }) |
| 126 | + } |
| 127 | + |
| 128 | + if outErr != nil { |
| 129 | + return outErr |
| 130 | + } |
| 131 | + |
| 132 | + ids := make([]string, 0, len(instances)) |
| 133 | + instanceList := make([]map[string]interface{}, 0, len(instances)) |
| 134 | + for _, ins := range instances { |
| 135 | + mapping := map[string]interface{}{ |
| 136 | + "id": ins.RegistryId, |
| 137 | + "name": ins.RegistryName, |
| 138 | + "status": ins.Status, |
| 139 | + "public_domain": ins.PublicDomain, |
| 140 | + "instance_type": ins.RegistryType, |
| 141 | + "internal_end_point": ins.InternalEndpoint, |
| 142 | + } |
| 143 | + tags := make(map[string]string, len(ins.TagSpecification.Tags)) |
| 144 | + for _, tag := range ins.TagSpecification.Tags { |
| 145 | + tags[*tag.Key] = *tag.Value |
| 146 | + } |
| 147 | + mapping["tags"] = tags |
| 148 | + instanceList = append(instanceList, mapping) |
| 149 | + ids = append(ids, *ins.RegistryId) |
| 150 | + } |
| 151 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 152 | + if e := d.Set("instance_list", instanceList); e != nil { |
| 153 | + log.Printf("[CRITAL]%s provider set TCR instance list fail, reason:%s\n", logId, e) |
| 154 | + return e |
| 155 | + } |
| 156 | + |
| 157 | + output, ok := d.GetOk("result_output_file") |
| 158 | + if ok && output.(string) != "" { |
| 159 | + if e := writeToFile(output.(string), instanceList); e != nil { |
| 160 | + return e |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + return nil |
| 165 | + |
| 166 | +} |
0 commit comments