Skip to content

Commit 9dc6168

Browse files
authored
support tcr general and operation resource (#1707)
* support resource: tencentcloud_tcr_customized_domain * add changelog * Support tcr general and operation resources. * passed the e2e case * passed the e2e case
1 parent 715c9e5 commit 9dc6168

20 files changed

+2092
-0
lines changed

.changelog/1707.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
```release-note:new-resource
2+
tencentcloud_tcr_customized_domain
3+
```
4+
5+
```release-note:new-resource
6+
tencentcloud_tcr_immutable_tag_rule
7+
```
8+
9+
```release-note:new-resource
10+
tencentcloud_tcr_delete_image_operation
11+
```
12+
13+
```release-note:new-resource
14+
tencentcloud_tcr_create_image_signature_operation
15+
```
16+
17+
```release-note:new-data-source
18+
tencentcloud_tcr_images
19+
```

tencentcloud/basic_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,10 @@ variable "cam_user_basic" {
550550

551551
// TCR Service
552552
const defaultTCRInstanceName = "keep-tcr-instance"
553+
const defaultTCRInstanceId = "tcr-e79o580i"
553554
const defaultTCRNamespace = "keep-tcr-namespace"
554555
const defaultTCRRepoName = "keep-tcr-repo"
556+
const defaultTCRSSL = "0a5zD3cN"
555557

556558
const defaultTCRInstanceVar = `
557559
variable "tcr_name" {
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
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+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package tencentcloud
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
8+
)
9+
10+
const testObjectName = "data.tencentcloud_tcr_images.images"
11+
12+
func TestAccTencentCloudTcrImagesDataSource_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(testAccTcrImagesDataSource_id, defaultTCRInstanceId, defaultTCRNamespace, defaultTCRRepoName),
22+
Check: resource.ComposeTestCheckFunc(
23+
resource.TestCheckResourceAttrSet(testObjectName, "id"),
24+
resource.TestCheckResourceAttr(testObjectName, "registry_id", defaultTCRInstanceId),
25+
resource.TestCheckResourceAttr(testObjectName, "namespace_name", defaultTCRNamespace),
26+
resource.TestCheckResourceAttr(testObjectName, "repository_name", defaultTCRRepoName),
27+
resource.TestCheckResourceAttrSet(testObjectName, "image_info_list.#"),
28+
),
29+
},
30+
},
31+
})
32+
}
33+
34+
func TestAccTencentCloudTcrImagesDataSource_exact(t *testing.T) {
35+
t.Parallel()
36+
resource.Test(t, resource.TestCase{
37+
PreCheck: func() {
38+
testAccPreCheck(t)
39+
},
40+
Providers: testAccProviders,
41+
Steps: []resource.TestStep{
42+
{
43+
Config: fmt.Sprintf(testAccTcrImagesDataSource_exact, defaultTCRInstanceId, defaultTCRNamespace, defaultTCRRepoName),
44+
Check: resource.ComposeTestCheckFunc(
45+
resource.TestCheckResourceAttrSet(testObjectName, "id"),
46+
resource.TestCheckResourceAttr(testObjectName, "registry_id", defaultTCRInstanceId),
47+
resource.TestCheckResourceAttr(testObjectName, "namespace_name", defaultTCRNamespace),
48+
resource.TestCheckResourceAttr(testObjectName, "repository_name", defaultTCRRepoName),
49+
resource.TestCheckResourceAttr(testObjectName, "exact_match", "true"),
50+
resource.TestCheckResourceAttrSet(testObjectName, "image_info_list.#"),
51+
),
52+
},
53+
},
54+
})
55+
}
56+
57+
func TestAccTencentCloudTcrImagesDataSource_exact_version(t *testing.T) {
58+
t.Parallel()
59+
resource.Test(t, resource.TestCase{
60+
PreCheck: func() {
61+
testAccPreCheck(t)
62+
},
63+
Providers: testAccProviders,
64+
Steps: []resource.TestStep{
65+
{
66+
Config: fmt.Sprintf(testAccTcrImagesDataSource_exact_version, defaultTCRInstanceId, defaultTCRNamespace, defaultTCRRepoName),
67+
Check: resource.ComposeTestCheckFunc(
68+
resource.TestCheckResourceAttrSet(testObjectName, "id"),
69+
resource.TestCheckResourceAttr(testObjectName, "registry_id", defaultTCRInstanceId),
70+
resource.TestCheckResourceAttr(testObjectName, "namespace_name", defaultTCRNamespace),
71+
resource.TestCheckResourceAttr(testObjectName, "repository_name", defaultTCRRepoName),
72+
resource.TestCheckResourceAttr(testObjectName, "image_version", "v1"),
73+
resource.TestCheckResourceAttr(testObjectName, "exact_match", "true"),
74+
resource.TestCheckResourceAttrSet(testObjectName, "image_info_list.#"),
75+
),
76+
},
77+
},
78+
})
79+
}
80+
81+
const testAccTcrImagesDataSource_id = `
82+
83+
data "tencentcloud_tcr_images" "images" {
84+
registry_id = "%s"
85+
namespace_name = "%s"
86+
repository_name = "%s"
87+
}
88+
89+
`
90+
91+
const testAccTcrImagesDataSource_exact = `
92+
93+
data "tencentcloud_tcr_images" "images" {
94+
registry_id = "%s"
95+
namespace_name = "%s"
96+
repository_name = "%s"
97+
exact_match = true
98+
}
99+
100+
`
101+
102+
const testAccTcrImagesDataSource_exact_version = `
103+
104+
data "tencentcloud_tcr_images" "images" {
105+
registry_id = "%s"
106+
namespace_name = "%s"
107+
repository_name = "%s"
108+
image_version = "v1"
109+
exact_match = true
110+
}
111+
112+
`

0 commit comments

Comments
 (0)