|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of tcr images |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_tcr_images" "images" { |
| 8 | + registry_id = "tcr-xxx" |
| 9 | + namespace_name = "ns" |
| 10 | + repository_name = "repo" |
| 11 | + image_version = "v1" |
| 12 | + digest = "sha256:xxxxx" |
| 13 | + exact_match = false |
| 14 | + } |
| 15 | +``` |
| 16 | +*/ |
| 17 | +package tencentcloud |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 24 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 25 | + tcr "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcr/v20190924" |
| 26 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 27 | +) |
| 28 | + |
| 29 | +func dataSourceTencentCloudTcrImages() *schema.Resource { |
| 30 | + return &schema.Resource{ |
| 31 | + Read: dataSourceTencentCloudTcrImagesRead, |
| 32 | + Schema: map[string]*schema.Schema{ |
| 33 | + "registry_id": { |
| 34 | + Required: true, |
| 35 | + Type: schema.TypeString, |
| 36 | + Description: "instance id.", |
| 37 | + }, |
| 38 | + |
| 39 | + "namespace_name": { |
| 40 | + Required: true, |
| 41 | + Type: schema.TypeString, |
| 42 | + Description: "namespace name.", |
| 43 | + }, |
| 44 | + |
| 45 | + "repository_name": { |
| 46 | + Required: true, |
| 47 | + Type: schema.TypeString, |
| 48 | + Description: "repository name.", |
| 49 | + }, |
| 50 | + |
| 51 | + "image_version": { |
| 52 | + Optional: true, |
| 53 | + Type: schema.TypeString, |
| 54 | + Description: "image version name, default is fuzzy match.", |
| 55 | + }, |
| 56 | + |
| 57 | + "digest": { |
| 58 | + Optional: true, |
| 59 | + Type: schema.TypeString, |
| 60 | + Description: "specify image digest for lookup.", |
| 61 | + }, |
| 62 | + |
| 63 | + "exact_match": { |
| 64 | + Optional: true, |
| 65 | + Type: schema.TypeBool, |
| 66 | + Description: "specifies whether it is an exact match, true is an exact match, and not filled is a fuzzy match.", |
| 67 | + }, |
| 68 | + |
| 69 | + "image_info_list": { |
| 70 | + Computed: true, |
| 71 | + Type: schema.TypeList, |
| 72 | + Description: "container image information list.", |
| 73 | + Elem: &schema.Resource{ |
| 74 | + Schema: map[string]*schema.Schema{ |
| 75 | + "digest": { |
| 76 | + Type: schema.TypeString, |
| 77 | + Computed: true, |
| 78 | + Description: "hash value.", |
| 79 | + }, |
| 80 | + "size": { |
| 81 | + Type: schema.TypeInt, |
| 82 | + Computed: true, |
| 83 | + Description: "image size (unit: byte).", |
| 84 | + }, |
| 85 | + "image_version": { |
| 86 | + Type: schema.TypeString, |
| 87 | + Computed: true, |
| 88 | + Description: "tag name.", |
| 89 | + }, |
| 90 | + "update_time": { |
| 91 | + Type: schema.TypeString, |
| 92 | + Computed: true, |
| 93 | + Description: "update time.", |
| 94 | + }, |
| 95 | + "kind": { |
| 96 | + Type: schema.TypeString, |
| 97 | + Computed: true, |
| 98 | + Description: "product type,note: this field may return null, indicating that no valid value can be obtained.", |
| 99 | + }, |
| 100 | + "kms_signature": { |
| 101 | + Type: schema.TypeString, |
| 102 | + Computed: true, |
| 103 | + Description: "kms signature information,note: this field may return null, indicating that no valid value can be obtained.", |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + |
| 109 | + "result_output_file": { |
| 110 | + Type: schema.TypeString, |
| 111 | + Optional: true, |
| 112 | + Description: "Used to save results.", |
| 113 | + }, |
| 114 | + }, |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +func dataSourceTencentCloudTcrImagesRead(d *schema.ResourceData, meta interface{}) error { |
| 119 | + defer logElapsed("data_source.tencentcloud_tcr_images.read")() |
| 120 | + defer inconsistentCheck(d, meta)() |
| 121 | + |
| 122 | + var ( |
| 123 | + logId = getLogId(contextNil) |
| 124 | + ctx = context.WithValue(context.TODO(), logIdKey, logId) |
| 125 | + registryId string |
| 126 | + namespaceName string |
| 127 | + repoName string |
| 128 | + ) |
| 129 | + |
| 130 | + paramMap := make(map[string]interface{}) |
| 131 | + if v, ok := d.GetOk("registry_id"); ok { |
| 132 | + paramMap["registry_id"] = helper.String(v.(string)) |
| 133 | + registryId = v.(string) |
| 134 | + } |
| 135 | + |
| 136 | + if v, ok := d.GetOk("namespace_name"); ok { |
| 137 | + paramMap["namespace_name"] = helper.String(v.(string)) |
| 138 | + namespaceName = v.(string) |
| 139 | + } |
| 140 | + |
| 141 | + if v, ok := d.GetOk("repository_name"); ok { |
| 142 | + paramMap["repository_name"] = helper.String(v.(string)) |
| 143 | + repoName = v.(string) |
| 144 | + } |
| 145 | + |
| 146 | + if v, ok := d.GetOk("image_version"); ok { |
| 147 | + paramMap["image_version"] = helper.String(v.(string)) |
| 148 | + } |
| 149 | + |
| 150 | + if v, ok := d.GetOk("digest"); ok { |
| 151 | + paramMap["digest"] = helper.String(v.(string)) |
| 152 | + } |
| 153 | + |
| 154 | + if v, _ := d.GetOk("exact_match"); v != nil { |
| 155 | + paramMap["exact_match"] = helper.Bool(v.(bool)) |
| 156 | + } |
| 157 | + |
| 158 | + service := TCRService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 159 | + |
| 160 | + var imageInfoList []*tcr.TcrImageInfo |
| 161 | + |
| 162 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 163 | + result, e := service.DescribeTcrImagesByFilter(ctx, paramMap) |
| 164 | + if e != nil { |
| 165 | + return retryError(e) |
| 166 | + } |
| 167 | + imageInfoList = result |
| 168 | + return nil |
| 169 | + }) |
| 170 | + if err != nil { |
| 171 | + return err |
| 172 | + } |
| 173 | + |
| 174 | + ids := make([]string, 0, len(imageInfoList)) |
| 175 | + tmpList := make([]map[string]interface{}, 0, len(imageInfoList)) |
| 176 | + |
| 177 | + if imageInfoList != nil { |
| 178 | + for _, tcrImageInfo := range imageInfoList { |
| 179 | + tcrImageInfoMap := map[string]interface{}{} |
| 180 | + |
| 181 | + if tcrImageInfo.Digest != nil { |
| 182 | + tcrImageInfoMap["digest"] = tcrImageInfo.Digest |
| 183 | + } |
| 184 | + |
| 185 | + if tcrImageInfo.Size != nil { |
| 186 | + tcrImageInfoMap["size"] = tcrImageInfo.Size |
| 187 | + } |
| 188 | + |
| 189 | + if tcrImageInfo.ImageVersion != nil { |
| 190 | + tcrImageInfoMap["image_version"] = tcrImageInfo.ImageVersion |
| 191 | + } |
| 192 | + |
| 193 | + if tcrImageInfo.UpdateTime != nil { |
| 194 | + tcrImageInfoMap["update_time"] = tcrImageInfo.UpdateTime |
| 195 | + } |
| 196 | + |
| 197 | + if tcrImageInfo.Kind != nil { |
| 198 | + tcrImageInfoMap["kind"] = tcrImageInfo.Kind |
| 199 | + } |
| 200 | + |
| 201 | + if tcrImageInfo.KmsSignature != nil { |
| 202 | + tcrImageInfoMap["kms_signature"] = tcrImageInfo.KmsSignature |
| 203 | + } |
| 204 | + |
| 205 | + ids = append(ids, strings.Join([]string{registryId, namespaceName, repoName, *tcrImageInfo.ImageVersion}, FILED_SP)) |
| 206 | + tmpList = append(tmpList, tcrImageInfoMap) |
| 207 | + } |
| 208 | + |
| 209 | + _ = d.Set("image_info_list", tmpList) |
| 210 | + } |
| 211 | + |
| 212 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 213 | + output, ok := d.GetOk("result_output_file") |
| 214 | + if ok && output.(string) != "" { |
| 215 | + if e := writeToFile(output.(string), tmpList); e != nil { |
| 216 | + return e |
| 217 | + } |
| 218 | + } |
| 219 | + return nil |
| 220 | +} |
0 commit comments