Skip to content

Commit 2015f03

Browse files
authored
support tcr: data source (#1760)
* support tcr: 2 data source * add changelog * fix gofmt
1 parent 188766a commit 2015f03

12 files changed

+678
-16
lines changed

.changelog/1760.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```release-note:new-data-source
2+
tencentcloud_tcr_image_manifests
3+
```
4+
5+
```release-note:new-data-source
6+
tencentcloud_tcr_tag_retention_execution_tasks
7+
```
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/*
2+
Use this data source to query detailed information of tcr image_manifests
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_tcr_image_manifests" "image_manifests" {
8+
registry_id = "%s"
9+
namespace_name = "%s"
10+
repository_name = "%s"
11+
image_version = "v1"
12+
}
13+
```
14+
*/
15+
package tencentcloud
16+
17+
import (
18+
"context"
19+
20+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
21+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
22+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
23+
)
24+
25+
func dataSourceTencentCloudTcrImageManifests() *schema.Resource {
26+
return &schema.Resource{
27+
Read: dataSourceTencentCloudTcrImageManifestsRead,
28+
Schema: map[string]*schema.Schema{
29+
"registry_id": {
30+
Required: true,
31+
Type: schema.TypeString,
32+
Description: "instance ID.",
33+
},
34+
35+
"namespace_name": {
36+
Required: true,
37+
Type: schema.TypeString,
38+
Description: "namespace name.",
39+
},
40+
41+
"repository_name": {
42+
Required: true,
43+
Type: schema.TypeString,
44+
Description: "mirror warehouse name.",
45+
},
46+
47+
"image_version": {
48+
Required: true,
49+
Type: schema.TypeString,
50+
Description: "mirror version.",
51+
},
52+
53+
"manifest": {
54+
Computed: true,
55+
Type: schema.TypeString,
56+
Description: "Manifest information of the image.",
57+
},
58+
59+
"config": {
60+
Computed: true,
61+
Type: schema.TypeString,
62+
Description: "configuration information of the image.",
63+
},
64+
65+
"result_output_file": {
66+
Type: schema.TypeString,
67+
Optional: true,
68+
Description: "Used to save results.",
69+
},
70+
},
71+
}
72+
}
73+
74+
func dataSourceTencentCloudTcrImageManifestsRead(d *schema.ResourceData, meta interface{}) error {
75+
defer logElapsed("data_source.tencentcloud_tcr_image_manifests.read")()
76+
defer inconsistentCheck(d, meta)()
77+
78+
logId := getLogId(contextNil)
79+
80+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
81+
var (
82+
registryId string
83+
namespaceName string
84+
repositoryName string
85+
imageVersion string
86+
)
87+
88+
paramMap := make(map[string]interface{})
89+
if v, ok := d.GetOk("registry_id"); ok {
90+
paramMap["registry_id"] = helper.String(v.(string))
91+
registryId = v.(string)
92+
}
93+
94+
if v, ok := d.GetOk("namespace_name"); ok {
95+
paramMap["namespace_name"] = helper.String(v.(string))
96+
namespaceName = v.(string)
97+
}
98+
99+
if v, ok := d.GetOk("repository_name"); ok {
100+
paramMap["repository_name"] = helper.String(v.(string))
101+
repositoryName = v.(string)
102+
}
103+
104+
if v, ok := d.GetOk("image_version"); ok {
105+
paramMap["image_version"] = helper.String(v.(string))
106+
imageVersion = v.(string)
107+
}
108+
109+
service := TCRService{client: meta.(*TencentCloudClient).apiV3Conn}
110+
111+
var (
112+
config *string
113+
manifest *string
114+
)
115+
116+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
117+
result, e := service.DescribeTcrImageManifestsByFilter(ctx, paramMap)
118+
if e != nil {
119+
return retryError(e)
120+
}
121+
config = result.Config
122+
manifest = result.Manifest
123+
return nil
124+
})
125+
if err != nil {
126+
return err
127+
}
128+
129+
if manifest != nil {
130+
_ = d.Set("manifest", manifest)
131+
}
132+
133+
if config != nil {
134+
_ = d.Set("config", config)
135+
}
136+
137+
tmpList := []map[string]interface{}{
138+
{
139+
"manifest": manifest,
140+
"config": config,
141+
},
142+
}
143+
144+
d.SetId(helper.DataResourceIdsHash([]string{registryId, namespaceName, repositoryName, imageVersion}))
145+
output, ok := d.GetOk("result_output_file")
146+
if ok && output.(string) != "" {
147+
if e := writeToFile(output.(string), tmpList); e != nil {
148+
return e
149+
}
150+
}
151+
return nil
152+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package tencentcloud
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
)
9+
10+
const testTcrImageManifestsObjectName = "data.tencentcloud_tcr_image_manifests.image_manifests"
11+
12+
func TestAccTencentCloudTcrImageManifestsDataSource_basic(t *testing.T) {
13+
t.Parallel()
14+
resource.Test(t, resource.TestCase{
15+
PreCheck: func() {
16+
testAccPreCheck(t)
17+
},
18+
Providers: testAccProviders,
19+
Steps: []resource.TestStep{
20+
{
21+
Config: fmt.Sprintf(testAccTcrImageManifestsDataSource, defaultTCRInstanceId, defaultTCRNamespace, defaultTCRRepoName),
22+
Check: resource.ComposeTestCheckFunc(
23+
testAccCheckTencentCloudDataSourceID(testTcrImageManifestsObjectName),
24+
resource.TestCheckResourceAttrSet(testTcrImageManifestsObjectName, "id"),
25+
resource.TestCheckResourceAttr(testTcrImageManifestsObjectName, "registry_id", defaultTCRInstanceId),
26+
resource.TestCheckResourceAttr(testTcrImageManifestsObjectName, "namespace_name", defaultTCRNamespace),
27+
resource.TestCheckResourceAttr(testTcrImageManifestsObjectName, "repository_name", defaultTCRRepoName),
28+
),
29+
},
30+
},
31+
})
32+
}
33+
34+
const testAccTcrImageManifestsDataSource = `
35+
36+
data "tencentcloud_tcr_image_manifests" "image_manifests" {
37+
registry_id = "%s"
38+
namespace_name = "%s"
39+
repository_name = "%s"
40+
image_version = "v1"
41+
}
42+
43+
`

0 commit comments

Comments
 (0)