Skip to content

Commit 5bbbd69

Browse files
authored
feat(teo): [125978298] add new resource (#3449)
* add * add
1 parent ef92a16 commit 5bbbd69

File tree

9 files changed

+483
-0
lines changed

9 files changed

+483
-0
lines changed

.changelog/3449.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-resource
2+
tencentcloud_teo_content_identifier
3+
```

tencentcloud/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1891,6 +1891,7 @@ func Provider() *schema.Provider {
18911891
"tencentcloud_teo_dns_record": teo.ResourceTencentCloudTeoDnsRecord(),
18921892
"tencentcloud_teo_bind_security_template": teo.ResourceTencentCloudTeoBindSecurityTemplate(),
18931893
"tencentcloud_teo_plan": teo.ResourceTencentCloudTeoPlan(),
1894+
"tencentcloud_teo_content_identifier": teo.ResourceTencentCloudTeoContentIdentifier(),
18941895
"tencentcloud_tcm_mesh": tcm.ResourceTencentCloudTcmMesh(),
18951896
"tencentcloud_tcm_cluster_attachment": tcm.ResourceTencentCloudTcmClusterAttachment(),
18961897
"tencentcloud_tcm_prometheus_attachment": tcm.ResourceTencentCloudTcmPrometheusAttachment(),

tencentcloud/provider.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,6 +1513,7 @@ tencentcloud_teo_security_ip_group
15131513
tencentcloud_teo_security_policy_config
15141514
tencentcloud_teo_dns_record
15151515
tencentcloud_teo_bind_security_template
1516+
tencentcloud_teo_content_identifier
15161517

15171518
TencentCloud ServiceMesh(TCM)
15181519
Data Source
Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
package teo
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
teov20220901 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/teo/v20220901"
11+
12+
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
13+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
14+
)
15+
16+
func ResourceTencentCloudTeoContentIdentifier() *schema.Resource {
17+
return &schema.Resource{
18+
Create: resourceTencentCloudTeoContentIdentifierCreate,
19+
Read: resourceTencentCloudTeoContentIdentifierRead,
20+
Update: resourceTencentCloudTeoContentIdentifierUpdate,
21+
Delete: resourceTencentCloudTeoContentIdentifierDelete,
22+
Importer: &schema.ResourceImporter{
23+
State: schema.ImportStatePassthrough,
24+
},
25+
Schema: map[string]*schema.Schema{
26+
"description": {
27+
Type: schema.TypeString,
28+
Required: true,
29+
Description: "Description of the content identifier, length limit of up to 20 characters.",
30+
},
31+
32+
"plan_id": {
33+
Type: schema.TypeString,
34+
Required: true,
35+
Description: "Target plan id to be bound, available only for the enterprise edition. <li>if there is already a plan under your account, go to [plan management](https://console.cloud.tencent.com/edgeone/package) to get the plan id and directly bind the content identifier to the plan;</li><li>if you do not have a plan to bind, please purchase an enterprise edition plan first.</li>.",
36+
},
37+
38+
"tags": {
39+
Type: schema.TypeList,
40+
Optional: true,
41+
Description: "Tags of the content identifier. this parameter is used for authority control. to create tags, go to the [tag console](https://console.cloud.tencent.com/tag/taglist).",
42+
Elem: &schema.Resource{
43+
Schema: map[string]*schema.Schema{
44+
"tag_key": {
45+
Type: schema.TypeString,
46+
Required: true,
47+
Description: "The tag key.\nNote: This field may return null, indicating that no valid values can be obtained.",
48+
},
49+
"tag_value": {
50+
Type: schema.TypeString,
51+
Required: true,
52+
Description: "The tag value.\nNote: This field may return null, indicating that no valid values can be obtained.",
53+
},
54+
},
55+
},
56+
},
57+
58+
// computed
59+
"content_id": {
60+
Type: schema.TypeString,
61+
Computed: true,
62+
Description: "Content identifier ID.",
63+
},
64+
65+
"created_on": {
66+
Type: schema.TypeString,
67+
Computed: true,
68+
Description: "Creation time, which is in Coordinated Universal Time (UTC) and follows the ISO 8601 date and time format..",
69+
},
70+
71+
"modified_on": {
72+
Type: schema.TypeString,
73+
Computed: true,
74+
Description: "The time of the latest update, in Coordinated Universal Time (UTC), following the ISO 8601 date and time format..",
75+
},
76+
},
77+
}
78+
}
79+
80+
func resourceTencentCloudTeoContentIdentifierCreate(d *schema.ResourceData, meta interface{}) error {
81+
defer tccommon.LogElapsed("resource.tencentcloud_teo_content_identifier.create")()
82+
defer tccommon.InconsistentCheck(d, meta)()
83+
84+
var (
85+
logId = tccommon.GetLogId(tccommon.ContextNil)
86+
ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
87+
request = teov20220901.NewCreateContentIdentifierRequest()
88+
response = teov20220901.NewCreateContentIdentifierResponse()
89+
)
90+
91+
if v, ok := d.GetOk("description"); ok {
92+
request.Description = helper.String(v.(string))
93+
}
94+
95+
if v, ok := d.GetOk("plan_id"); ok {
96+
request.PlanId = helper.String(v.(string))
97+
}
98+
99+
if v, ok := d.GetOk("tags"); ok {
100+
for _, item := range v.([]interface{}) {
101+
tagsMap := item.(map[string]interface{})
102+
tag := teov20220901.Tag{}
103+
if v, ok := tagsMap["tag_key"].(string); ok && v != "" {
104+
tag.TagKey = helper.String(v)
105+
}
106+
107+
if v, ok := tagsMap["tag_value"].(string); ok && v != "" {
108+
tag.TagValue = helper.String(v)
109+
}
110+
111+
request.Tags = append(request.Tags, &tag)
112+
}
113+
}
114+
115+
reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
116+
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTeoV20220901Client().CreateContentIdentifierWithContext(ctx, request)
117+
if e != nil {
118+
return tccommon.RetryError(e)
119+
} else {
120+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
121+
}
122+
123+
if result == nil || result.Response == nil {
124+
return resource.NonRetryableError(fmt.Errorf("Create teo content identifier failed, Response is nil."))
125+
}
126+
127+
response = result
128+
return nil
129+
})
130+
131+
if reqErr != nil {
132+
log.Printf("[CRITAL]%s create teo content identifier failed, reason:%+v", logId, reqErr)
133+
return reqErr
134+
}
135+
136+
if response.Response.ContentId == nil {
137+
return fmt.Errorf("ContentId is nil.")
138+
}
139+
140+
d.SetId(*response.Response.ContentId)
141+
return resourceTencentCloudTeoContentIdentifierRead(d, meta)
142+
}
143+
144+
func resourceTencentCloudTeoContentIdentifierRead(d *schema.ResourceData, meta interface{}) error {
145+
defer tccommon.LogElapsed("resource.tencentcloud_teo_content_identifier.read")()
146+
defer tccommon.InconsistentCheck(d, meta)()
147+
148+
var (
149+
logId = tccommon.GetLogId(tccommon.ContextNil)
150+
ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
151+
service = TeoService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
152+
contentId = d.Id()
153+
)
154+
155+
respData, err := service.DescribeTeoContentIdentifierById(ctx, contentId)
156+
if err != nil {
157+
return err
158+
}
159+
160+
if respData == nil {
161+
log.Printf("[WARN]%s resource `teo_content_identifier` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
162+
d.SetId("")
163+
return nil
164+
}
165+
166+
if respData.Description != nil {
167+
_ = d.Set("description", respData.Description)
168+
}
169+
170+
if respData.PlanId != nil {
171+
_ = d.Set("plan_id", respData.PlanId)
172+
}
173+
174+
if respData.Tags != nil {
175+
tagsList := make([]map[string]interface{}, 0, len(respData.Tags))
176+
for _, tags := range respData.Tags {
177+
tagsMap := map[string]interface{}{}
178+
if tags.TagKey != nil {
179+
tagsMap["tag_key"] = tags.TagKey
180+
}
181+
182+
if tags.TagValue != nil {
183+
tagsMap["tag_value"] = tags.TagValue
184+
}
185+
186+
tagsList = append(tagsList, tagsMap)
187+
}
188+
189+
_ = d.Set("tags", tagsList)
190+
}
191+
192+
if respData.ContentId != nil {
193+
_ = d.Set("content_id", respData.ContentId)
194+
}
195+
196+
if respData.CreatedOn != nil {
197+
_ = d.Set("created_on", respData.CreatedOn)
198+
}
199+
200+
if respData.ModifiedOn != nil {
201+
_ = d.Set("modified_on", respData.ModifiedOn)
202+
}
203+
204+
return nil
205+
}
206+
207+
func resourceTencentCloudTeoContentIdentifierUpdate(d *schema.ResourceData, meta interface{}) error {
208+
defer tccommon.LogElapsed("resource.tencentcloud_teo_content_identifier.update")()
209+
defer tccommon.InconsistentCheck(d, meta)()
210+
211+
var (
212+
logId = tccommon.GetLogId(tccommon.ContextNil)
213+
ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
214+
contentId = d.Id()
215+
)
216+
217+
immutableArgs := []string{"plan_id", "tags"}
218+
for _, v := range immutableArgs {
219+
if d.HasChange(v) {
220+
return fmt.Errorf("argument `%s` cannot be changed", v)
221+
}
222+
}
223+
224+
if d.HasChange("description") {
225+
request := teov20220901.NewModifyContentIdentifierRequest()
226+
if v, ok := d.GetOk("description"); ok {
227+
request.Description = helper.String(v.(string))
228+
}
229+
230+
request.ContentId = &contentId
231+
reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
232+
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTeoV20220901Client().ModifyContentIdentifierWithContext(ctx, request)
233+
if e != nil {
234+
return tccommon.RetryError(e)
235+
} else {
236+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
237+
}
238+
239+
return nil
240+
})
241+
242+
if reqErr != nil {
243+
log.Printf("[CRITAL]%s update teo content identifier failed, reason:%+v", logId, reqErr)
244+
return reqErr
245+
}
246+
}
247+
248+
return resourceTencentCloudTeoContentIdentifierRead(d, meta)
249+
}
250+
251+
func resourceTencentCloudTeoContentIdentifierDelete(d *schema.ResourceData, meta interface{}) error {
252+
defer tccommon.LogElapsed("resource.tencentcloud_teo_content_identifier.delete")()
253+
defer tccommon.InconsistentCheck(d, meta)()
254+
255+
var (
256+
logId = tccommon.GetLogId(tccommon.ContextNil)
257+
ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
258+
request = teov20220901.NewDeleteContentIdentifierRequest()
259+
contentId = d.Id()
260+
)
261+
262+
request.ContentId = &contentId
263+
reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
264+
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTeoV20220901Client().DeleteContentIdentifierWithContext(ctx, request)
265+
if e != nil {
266+
return tccommon.RetryError(e)
267+
} else {
268+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
269+
}
270+
271+
return nil
272+
})
273+
274+
if reqErr != nil {
275+
log.Printf("[CRITAL]%s delete teo content identifier failed, reason:%+v", logId, reqErr)
276+
return reqErr
277+
}
278+
279+
return nil
280+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Provides a resource to create a TEO content identifier
2+
3+
Example Usage
4+
5+
```hcl
6+
resource "tencentcloud_teo_content_identifier" "example" {
7+
plan_id = "edgeone-6bzvsgjkfa9g"
8+
description = "example"
9+
tags {
10+
tag_key = "tagKey"
11+
tag_value = "tagValue"
12+
}
13+
}
14+
```
15+
16+
Import
17+
18+
TEO content identifier can be imported using the id, e.g.
19+
20+
```
21+
terraform import tencentcloud_teo_content_identifier.example eocontent-3dy8iyfq8dba
22+
```
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package teo_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
8+
tcacctest "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest"
9+
)
10+
11+
func TestAccTencentCloudTeoContentIdentifierResource_basic(t *testing.T) {
12+
t.Parallel()
13+
resource.Test(t, resource.TestCase{
14+
PreCheck: func() {
15+
tcacctest.AccPreCheck(t)
16+
},
17+
Providers: tcacctest.AccProviders,
18+
Steps: []resource.TestStep{
19+
{
20+
Config: testAccTeoContentIdentifier,
21+
Check: resource.ComposeTestCheckFunc(
22+
resource.TestCheckResourceAttrSet("tencentcloud_teo_content_identifier.example", "id"),
23+
resource.TestCheckResourceAttrSet("tencentcloud_teo_content_identifier.example", "plan_id"),
24+
resource.TestCheckResourceAttrSet("tencentcloud_teo_content_identifier.example", "description"),
25+
),
26+
},
27+
{
28+
Config: testAccTeoContentIdentifierUpdate,
29+
Check: resource.ComposeTestCheckFunc(
30+
resource.TestCheckResourceAttrSet("tencentcloud_teo_content_identifier.example", "id"),
31+
resource.TestCheckResourceAttrSet("tencentcloud_teo_content_identifier.example", "plan_id"),
32+
resource.TestCheckResourceAttrSet("tencentcloud_teo_content_identifier.example", "description"),
33+
),
34+
},
35+
{
36+
ResourceName: "tencentcloud_teo_content_identifier.example",
37+
ImportState: true,
38+
ImportStateVerify: true,
39+
},
40+
},
41+
})
42+
}
43+
44+
const testAccTeoContentIdentifier = `
45+
resource "tencentcloud_teo_content_identifier" "example" {
46+
plan_id = "edgeone-3bzvsgjkfw6g"
47+
description = "example"
48+
tags {
49+
tag_key = "tagKey"
50+
tag_value = "tagValue"
51+
}
52+
}
53+
`
54+
55+
const testAccTeoContentIdentifierUpdate = `
56+
resource "tencentcloud_teo_content_identifier" "example" {
57+
plan_id = "edgeone-3bzvsgjkfw6g"
58+
description = "example update"
59+
tags {
60+
tag_key = "tagKey"
61+
tag_value = "tagValue"
62+
}
63+
}
64+
`

0 commit comments

Comments
 (0)