Skip to content

Commit 1ffa890

Browse files
authored
Add TCR resources (#555)
Add TCR resources
1 parent 4907291 commit 1ffa890

File tree

62 files changed

+8873
-58
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+8873
-58
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
FEATURES:
44

5+
* **New Resource**: `tencentcloud_tcr_instance`
6+
* **New Resource**: `tencentcloud_tcr_token`
7+
* **New Resource**: `tencentcloud_tcr_namespace`
8+
* **New Resource**: `tencentcloud_tcr_repository`
9+
* **New Data Source**: `tencentcloud_tcr_instances`
10+
* **New Data Source**: `tencentcloud_tcr_tokens`
11+
* **New Data Source**: `tencentcloud_tcr_namespaces`
12+
* **New Data Source**: `tencentcloud_tcr_repositories`
513
* **New Resource**: `tencentcloud_cos_bucket_policy`
614

715
ENHANCEMENTS:
@@ -36,6 +44,7 @@ ENHANCEMENTS:
3644
* Resource: `tencentcloud_elasticsearch_instance` support `node_type` and `disk_size` modification.
3745
* Data Source: `tencentcloud_instance_types` add argument `exclude_sold_out` to support filtering sold out instance types.
3846
* Data Source: `tencentcloud_sqlserver_instances` add new argument `tags`.
47+
* Data Source: `tencentcloud_instance_types` add argument `exclude_sold_out` to support filtering sold out instance types.
3948

4049
BUG FIXES:
4150

examples/tencentcloud-tcr/main.tf

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
resource "tencentcloud_tcr_instance" "example" {
2+
name = "testacctcrinstance"
3+
instance_type = "standard"
4+
delete_bucket = true
5+
6+
tags ={
7+
test = "test"
8+
}
9+
}
10+
11+
resource "tencentcloud_tcr_token" "example" {
12+
instance_id = tencentcloud_tcr_instance.example.id
13+
description = "test"
14+
enable = false
15+
}
16+
17+
resource "tencentcloud_tcr_namespace" "example" {
18+
instance_id = tencentcloud_tcr_instance.example.id
19+
name = "test"
20+
is_public = false
21+
}
22+
23+
resource "tencentcloud_tcr_repository" "example" {
24+
instance_id = tencentcloud_tcr_instance.example.id
25+
namespace_name = tencentcloud_tcr_namespace.example.name
26+
name = "test"
27+
brief_desc = "example"
28+
description = "long example"
29+
}
30+
31+
data "tencentcloud_tcr_instances" "example" {
32+
name = tencentcloud_tcr_instance.example.name
33+
}
34+
35+
data "tencentcloud_tcr_tokens" "example" {
36+
instance_id = tencentcloud_tcr_token.example.instance_id
37+
}
38+
39+
data "tencentcloud_tcr_namespaces" "example" {
40+
instance_id = tencentcloud_tcr_namespace.example.instance_id
41+
}
42+
43+
data "tencentcloud_tcr_repositories" "example" {
44+
instance_id = tencentcloud_tcr_repository.example.instance_id
45+
namespace_name = tencentcloud_tcr_namespace.example.name
46+
}

examples/tencentcloud-tcr/version.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
terraform {
2+
required_version = ">= 0.12"
3+
}

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ require (
1515
github.com/mattn/go-colorable v0.1.6 // indirect
1616
github.com/mitchellh/go-homedir v1.1.0
1717
github.com/pkg/errors v0.9.1
18-
github.com/tencentcloud/tencentcloud-sdk-go v1.0.50
18+
github.com/tencentcloud/tencentcloud-sdk-go v1.0.59
1919
github.com/yangwenmai/ratelimit v0.0.0-20180104140304-44221c2292e1
2020
github.com/zclconf/go-cty v1.4.2 // indirect
2121
golang.org/x/sys v0.0.0-20200523222454-059865788121 // indirect
22+
golang.org/x/tools v0.0.0-20200502202811-ed308ab3e770
2223
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,8 @@ github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s
441441
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
442442
github.com/tdakkota/asciicheck v0.0.0-20200416190851-d7f85be797a2 h1:Xr9gkxfOP0KQWXKNqmwe8vEeSUiUj4Rlee9CMVX2ZUQ=
443443
github.com/tdakkota/asciicheck v0.0.0-20200416190851-d7f85be797a2/go.mod h1:yHp0ai0Z9gUljN3o0xMhYJnH/IcvkdTBOX2fmJ93JEM=
444-
github.com/tencentcloud/tencentcloud-sdk-go v1.0.50 h1:KAIRXHVSVMvRX3Dl5gvrcVqCxBv7fDLvHahU0F3GNsc=
445-
github.com/tencentcloud/tencentcloud-sdk-go v1.0.50/go.mod h1:asUz5BPXxgoPGaRgZaVm1iGcUAuHyYUo1nXqKa83cvI=
444+
github.com/tencentcloud/tencentcloud-sdk-go v1.0.59 h1:puW/EDFi2+iohSMqUUGXeKiJMq8MrJZdt56stDAsxBs=
445+
github.com/tencentcloud/tencentcloud-sdk-go v1.0.59/go.mod h1:asUz5BPXxgoPGaRgZaVm1iGcUAuHyYUo1nXqKa83cvI=
446446
github.com/tetafro/godot v0.3.7 h1:+mecr7RKrUKB5UQ1gwqEMn13sDKTyDR8KNIquB9mm+8=
447447
github.com/tetafro/godot v0.3.7/go.mod h1:/7NLHhv08H1+8DNj0MElpAACw1ajsCuf3TKNQxA5S+0=
448448
github.com/timakin/bodyclose v0.0.0-20190930140734-f7f2e9bca95e h1:RumXZ56IrCj4CL+g1b9OL/oH0QnsF976bC8xQFYUD5Q=

tencentcloud/connectivity/client.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
sts "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sts/v20180813"
3636
tag "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tag/v20180813"
3737
tcaplusdb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcaplusdb/v20190823"
38+
tcr "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcr/v20190924"
3839
tke "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke/v20180525"
3940
vod "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vod/v20180717"
4041
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
@@ -61,6 +62,7 @@ type TencentCloudClient struct {
6162
tagConn *tag.Client
6263
mongodbConn *mongodb.Client
6364
tkeConn *tke.Client
65+
tcrConn *tcr.Client
6466
camConn *cam.Client
6567
stsConn *sts.Client
6668
gaapConn *gaap.Client
@@ -503,3 +505,16 @@ func (me *TencentCloudClient) UseAPIGatewayClient() *apigateway.Client {
503505

504506
return me.apiGatewayConn
505507
}
508+
509+
// UseTCRClient returns apigateway client for service
510+
func (me *TencentCloudClient) UseTCRClient() *tcr.Client {
511+
if me.tcrConn != nil {
512+
return me.tcrConn
513+
}
514+
515+
cpf := me.NewClientProfile(300)
516+
me.tcrConn, _ = tcr.NewClient(me.Credential, me.Region, cpf)
517+
me.tcrConn.WithHttpTransport(&LogRoundTripper{})
518+
519+
return me.tcrConn
520+
}
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
7+
)
8+
9+
var testDataTCRInstancesNameAll = "data.tencentcloud_tcr_instances.all_test"
10+
11+
func TestAccTencentCloudDataTCRInstances(t *testing.T) {
12+
resource.Test(t, resource.TestCase{
13+
PreCheck: func() { testAccPreCheck(t) },
14+
Providers: testAccProviders,
15+
CheckDestroy: testAccCheckTCRInstanceDestroy,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccTencentCloudDataTCRInstancesBasic,
19+
Check: resource.ComposeAggregateTestCheckFunc(
20+
testAccCheckTCRInstanceExists("tencentcloud_tcr_instance.mytcr_instance"),
21+
resource.TestCheckResourceAttrSet(testDataTCRInstancesNameAll, "instance_list.0.id"),
22+
resource.TestCheckResourceAttr(testDataTCRInstancesNameAll, "instance_list.0.instance_type", "standard"),
23+
resource.TestCheckResourceAttrSet(testDataTCRInstancesNameAll, "instance_list.0.internal_end_point"),
24+
resource.TestCheckResourceAttrSet(testDataTCRInstancesNameAll, "instance_list.0.public_domain"),
25+
resource.TestCheckResourceAttrSet(testDataTCRInstancesNameAll, "instance_list.0.status"),
26+
resource.TestCheckResourceAttr(testDataTCRInstancesNameAll, "instance_list.0.tags.test", "test"),
27+
),
28+
},
29+
},
30+
})
31+
}
32+
33+
const testAccTencentCloudDataTCRInstancesBasic = `
34+
resource "tencentcloud_tcr_instance" "mytcr_instance" {
35+
name = "testacctcrinstance"
36+
instance_type = "standard"
37+
delete_bucket = true
38+
39+
tags ={
40+
test = "test"
41+
}
42+
}
43+
44+
data "tencentcloud_tcr_instances" "all_test" {
45+
name = tencentcloud_tcr_instance.mytcr_instance.name
46+
}
47+
48+
`

0 commit comments

Comments
 (0)