Skip to content

Commit 9497804

Browse files
tongyimingmikatong
andauthored
support lighthouse blueprint (#1613)
* support lighthouse blueprint * add changelog --------- Co-authored-by: mikatong <mikatong@tencent.com>
1 parent ab25f1c commit 9497804

File tree

7 files changed

+368
-0
lines changed

7 files changed

+368
-0
lines changed

.changelog/1613.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_lighthouse_blueprint
3+
```

tencentcloud/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,7 @@ Cloud Log Service(CLS)
714714
TencentCloud Lighthouse(Lighthouse)
715715
Resource
716716
tencentcloud_lighthouse_instance
717+
tencentcloud_lighthouse_blueprint
717718
718719
TencentCloud Elastic Microservice(TEM)
719720
Resource
@@ -1765,6 +1766,7 @@ func Provider() terraform.ResourceProvider {
17651766
"tencentcloud_chdfs_mount_point": resourceTencentCloudChdfsMountPoint(),
17661767
"tencentcloud_chdfs_mount_point_attachment": resourceTencentCloudChdfsMountPointAttachment(),
17671768
"tencentcloud_mdl_stream_live_input": resourceTencentCloudMdlStreamLiveInput(),
1769+
"tencentcloud_lighthouse_blueprint": resourceTencentCloudLighthouseBlueprint(),
17681770
},
17691771

17701772
ConfigureFunc: providerConfigure,
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
/*
2+
Provides a resource to create a lighthouse blueprint
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_lighthouse_blueprint" "blueprint" {
8+
blueprint_name = "blueprint_name_test"
9+
description = "blueprint_description_test"
10+
instance_id = "lhins-xxxxxx"
11+
}
12+
```
13+
14+
Import
15+
16+
lighthouse blueprint can be imported using the id, e.g.
17+
18+
```
19+
terraform import tencentcloud_lighthouse_blueprint.blueprint blueprint_id
20+
```
21+
*/
22+
package tencentcloud
23+
24+
import (
25+
"context"
26+
"log"
27+
"time"
28+
29+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
30+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
31+
lighthouse "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lighthouse/v20200324"
32+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
33+
)
34+
35+
func resourceTencentCloudLighthouseBlueprint() *schema.Resource {
36+
return &schema.Resource{
37+
Create: resourceTencentCloudLighthouseBlueprintCreate,
38+
Read: resourceTencentCloudLighthouseBlueprintRead,
39+
Update: resourceTencentCloudLighthouseBlueprintUpdate,
40+
Delete: resourceTencentCloudLighthouseBlueprintDelete,
41+
Importer: &schema.ResourceImporter{
42+
State: schema.ImportStatePassthrough,
43+
},
44+
Schema: map[string]*schema.Schema{
45+
"blueprint_name": {
46+
Required: true,
47+
Type: schema.TypeString,
48+
Description: "Blueprint name, which can contain up to 60 characters.",
49+
},
50+
51+
"description": {
52+
Optional: true,
53+
Type: schema.TypeString,
54+
Description: "Blueprint description, which can contain up to 60 characters.",
55+
},
56+
57+
"instance_id": {
58+
Optional: true,
59+
Type: schema.TypeString,
60+
Description: "ID of the instance for which to make a blueprint.",
61+
},
62+
},
63+
}
64+
}
65+
66+
func resourceTencentCloudLighthouseBlueprintCreate(d *schema.ResourceData, meta interface{}) error {
67+
defer logElapsed("resource.tencentcloud_lighthouse_blueprint.create")()
68+
defer inconsistentCheck(d, meta)()
69+
70+
logId := getLogId(contextNil)
71+
72+
var (
73+
request = lighthouse.NewCreateBlueprintRequest()
74+
response = lighthouse.NewCreateBlueprintResponse()
75+
blueprintId string
76+
)
77+
if v, ok := d.GetOk("blueprint_name"); ok {
78+
request.BlueprintName = helper.String(v.(string))
79+
}
80+
81+
if v, ok := d.GetOk("description"); ok {
82+
request.Description = helper.String(v.(string))
83+
}
84+
85+
if v, ok := d.GetOk("instance_id"); ok {
86+
request.InstanceId = helper.String(v.(string))
87+
}
88+
89+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
90+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseLighthouseClient().CreateBlueprint(request)
91+
if e != nil {
92+
return retryError(e)
93+
} else {
94+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
95+
}
96+
response = result
97+
return nil
98+
})
99+
if err != nil {
100+
log.Printf("[CRITAL]%s create lighthouse blueprint failed, reason:%+v", logId, err)
101+
return err
102+
}
103+
104+
blueprintId = *response.Response.BlueprintId
105+
d.SetId(blueprintId)
106+
107+
service := LightHouseService{client: meta.(*TencentCloudClient).apiV3Conn}
108+
109+
conf := BuildStateChangeConf([]string{}, []string{"NORMAL"}, 10*readRetryTimeout, time.Second, service.LighthouseBlueprintStateRefreshFunc(d.Id(), []string{}))
110+
111+
if _, e := conf.WaitForState(); e != nil {
112+
return e
113+
}
114+
115+
return resourceTencentCloudLighthouseBlueprintRead(d, meta)
116+
}
117+
118+
func resourceTencentCloudLighthouseBlueprintRead(d *schema.ResourceData, meta interface{}) error {
119+
defer logElapsed("resource.tencentcloud_lighthouse_blueprint.read")()
120+
defer inconsistentCheck(d, meta)()
121+
122+
logId := getLogId(contextNil)
123+
124+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
125+
126+
service := LightHouseService{client: meta.(*TencentCloudClient).apiV3Conn}
127+
128+
blueprintId := d.Id()
129+
130+
blueprint, err := service.DescribeLighthouseBlueprintById(ctx, blueprintId)
131+
if err != nil {
132+
return err
133+
}
134+
135+
if blueprint == nil {
136+
d.SetId("")
137+
log.Printf("[WARN]%s resource `LighthouseBlueprint` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
138+
return nil
139+
}
140+
141+
if blueprint.BlueprintName != nil {
142+
_ = d.Set("blueprint_name", blueprint.BlueprintName)
143+
}
144+
145+
if blueprint.Description != nil {
146+
_ = d.Set("description", blueprint.Description)
147+
}
148+
149+
return nil
150+
}
151+
152+
func resourceTencentCloudLighthouseBlueprintUpdate(d *schema.ResourceData, meta interface{}) error {
153+
defer logElapsed("resource.tencentcloud_lighthouse_blueprint.update")()
154+
defer inconsistentCheck(d, meta)()
155+
156+
logId := getLogId(contextNil)
157+
158+
request := lighthouse.NewModifyBlueprintAttributeRequest()
159+
160+
blueprintId := d.Id()
161+
162+
request.BlueprintId = &blueprintId
163+
164+
if d.HasChange("blueprint_name") {
165+
if v, ok := d.GetOk("blueprint_name"); ok {
166+
request.BlueprintName = helper.String(v.(string))
167+
}
168+
}
169+
170+
if d.HasChange("description") {
171+
if v, ok := d.GetOk("description"); ok {
172+
request.Description = helper.String(v.(string))
173+
}
174+
}
175+
176+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
177+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseLighthouseClient().ModifyBlueprintAttribute(request)
178+
if e != nil {
179+
return retryError(e)
180+
} else {
181+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
182+
}
183+
return nil
184+
})
185+
if err != nil {
186+
log.Printf("[CRITAL]%s update lighthouse blueprint failed, reason:%+v", logId, err)
187+
return err
188+
}
189+
190+
return resourceTencentCloudLighthouseBlueprintRead(d, meta)
191+
}
192+
193+
func resourceTencentCloudLighthouseBlueprintDelete(d *schema.ResourceData, meta interface{}) error {
194+
defer logElapsed("resource.tencentcloud_lighthouse_blueprint.delete")()
195+
defer inconsistentCheck(d, meta)()
196+
197+
logId := getLogId(contextNil)
198+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
199+
200+
service := LightHouseService{client: meta.(*TencentCloudClient).apiV3Conn}
201+
blueprintId := d.Id()
202+
203+
if err := service.DeleteLighthouseBlueprintById(ctx, blueprintId); err != nil {
204+
return err
205+
}
206+
207+
return nil
208+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudLighthouseBlueprintResource_basic(t *testing.T) {
10+
t.Parallel()
11+
resource.Test(t, resource.TestCase{
12+
PreCheck: func() { testAccPreCheckCommon(t, ACCOUNT_TYPE_PREPAY) },
13+
Providers: testAccProviders,
14+
Steps: []resource.TestStep{
15+
{
16+
Config: testAccLighthouseBlueprint,
17+
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttrSet("tencentcloud_lighthouse_blueprint.blueprint", "id")),
18+
},
19+
{
20+
ResourceName: "tencentcloud_lighthouse_blueprint.blueprint",
21+
ImportState: true,
22+
ImportStateVerify: true,
23+
ImportStateVerifyIgnore: []string{"instance_id"},
24+
},
25+
},
26+
})
27+
}
28+
29+
const testAccLighthouseBlueprint = `
30+
31+
resource "tencentcloud_lighthouse_blueprint" "blueprint" {
32+
blueprint_name = "blueprint_name_test"
33+
description = "blueprint_description_test"
34+
instance_id = "lhins-hwe21u91"
35+
}
36+
37+
`

tencentcloud/service_tencentcloud_lighthouse.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"log"
66

7+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
78
lighthouse "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lighthouse/v20200324"
89
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/connectivity"
910
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
@@ -117,3 +118,70 @@ func (me *LightHouseService) IsolateLighthouseInstanceById(ctx context.Context,
117118

118119
return
119120
}
121+
122+
func (me *LightHouseService) LighthouseBlueprintStateRefreshFunc(blueprintId string, failStates []string) resource.StateRefreshFunc {
123+
return func() (interface{}, string, error) {
124+
ctx := contextNil
125+
126+
object, err := me.DescribeLighthouseBlueprintById(ctx, blueprintId)
127+
128+
if err != nil {
129+
return nil, "", err
130+
}
131+
132+
return object, helper.PString(object.BlueprintState), nil
133+
}
134+
}
135+
136+
func (me *LightHouseService) DeleteLighthouseBlueprintById(ctx context.Context, blueprintId string) (errRet error) {
137+
logId := getLogId(ctx)
138+
139+
request := lighthouse.NewDeleteBlueprintsRequest()
140+
request.BlueprintIds = []*string{&blueprintId}
141+
142+
defer func() {
143+
if errRet != nil {
144+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error())
145+
}
146+
}()
147+
148+
ratelimit.Check(request.GetAction())
149+
150+
response, err := me.client.UseLighthouseClient().DeleteBlueprints(request)
151+
if err != nil {
152+
errRet = err
153+
return
154+
}
155+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
156+
157+
return
158+
}
159+
160+
func (me *LightHouseService) DescribeLighthouseBlueprintById(ctx context.Context, blueprintId string) (blueprint *lighthouse.Blueprint, errRet error) {
161+
logId := getLogId(ctx)
162+
163+
request := lighthouse.NewDescribeBlueprintsRequest()
164+
request.BlueprintIds = []*string{&blueprintId}
165+
166+
defer func() {
167+
if errRet != nil {
168+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error())
169+
}
170+
}()
171+
172+
ratelimit.Check(request.GetAction())
173+
174+
response, err := me.client.UseLighthouseClient().DescribeBlueprints(request)
175+
if err != nil {
176+
errRet = err
177+
return
178+
}
179+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
180+
181+
if len(response.Response.BlueprintSet) < 1 {
182+
return
183+
}
184+
185+
blueprint = response.Response.BlueprintSet[0]
186+
return
187+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
subcategory: "TencentCloud Lighthouse(Lighthouse)"
3+
layout: "tencentcloud"
4+
page_title: "TencentCloud: tencentcloud_lighthouse_blueprint"
5+
sidebar_current: "docs-tencentcloud-resource-lighthouse_blueprint"
6+
description: |-
7+
Provides a resource to create a lighthouse blueprint
8+
---
9+
10+
# tencentcloud_lighthouse_blueprint
11+
12+
Provides a resource to create a lighthouse blueprint
13+
14+
## Example Usage
15+
16+
```hcl
17+
resource "tencentcloud_lighthouse_blueprint" "blueprint" {
18+
blueprint_name = "blueprint_name_test"
19+
description = "blueprint_description_test"
20+
instance_id = "lhins-xxxxxx"
21+
}
22+
```
23+
24+
## Argument Reference
25+
26+
The following arguments are supported:
27+
28+
* `blueprint_name` - (Required, String) Blueprint name, which can contain up to 60 characters.
29+
* `description` - (Optional, String) Blueprint description, which can contain up to 60 characters.
30+
* `instance_id` - (Optional, String) ID of the instance for which to make a blueprint.
31+
32+
## Attributes Reference
33+
34+
In addition to all arguments above, the following attributes are exported:
35+
36+
* `id` - ID of the resource.
37+
38+
39+
40+
## Import
41+
42+
lighthouse blueprint can be imported using the id, e.g.
43+
44+
```
45+
terraform import tencentcloud_lighthouse_blueprint.blueprint blueprint_id
46+
```
47+

0 commit comments

Comments
 (0)