Skip to content

Commit d7729d3

Browse files
tongyimingmikatong
andauthored
support snapshot (#1693)
* support snapshot * add changelog * update --------- Co-authored-by: mikatong <mikatong@tencent.com>
1 parent b9de032 commit d7729d3

11 files changed

+538
-0
lines changed

.changelog/1693.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```release-note:new-resource
2+
tencentcloud_lighthouse_snapshot
3+
```
4+
5+
```release-note:new-resource
6+
tencentcloud_lighthouse_apply_instance_snapshot
7+
```

tencentcloud/basic_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,7 @@ const (
981981
defaultLighthoustDiskId = "lhdisk-do4p4hz6"
982982
defaultLighthouseBackupDiskId = "lhdisk-cwodsc4q"
983983
defaultLighthouseBackUpId = "lhbak-bpum3ygx"
984+
defaultLighthouseSnapshotId = "lhsnap-9jaw9m17"
984985
)
985986

986987
const DefaultLighthoustVariables = `
@@ -999,4 +1000,8 @@ variable "lighthouse_backup_disk_id" {
9991000
variable "lighthouse_backup_id" {
10001001
default = "` + defaultLighthouseBackUpId + `"
10011002
}
1003+
1004+
variable "lighthouse_snapshot_id" {
1005+
default = "` + defaultLighthouseSnapshotId + `"
1006+
}
10021007
`

tencentcloud/provider.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,8 @@ TencentCloud Lighthouse(Lighthouse)
767767
tencentcloud_lighthouse_apply_disk_backup
768768
tencentcloud_lighthouse_disk_attachment
769769
tencentcloud_lighthouse_key_pair
770+
tencentcloud_lighthouse_snapshot
771+
tencentcloud_lighthouse_apply_instance_snapshot
770772
771773
Data Source
772774
tencentcloud_lighthouse_firewall_rules_template
@@ -1918,6 +1920,8 @@ func Provider() terraform.ResourceProvider {
19181920
"tencentcloud_lighthouse_apply_disk_backup": resourceTencentCloudLighthouseApplyDiskBackup(),
19191921
"tencentcloud_lighthouse_disk_attachment": resourceTencentCloudLighthouseDiskAttachment(),
19201922
"tencentcloud_lighthouse_key_pair": resourceTencentCloudLighthouseKeyPair(),
1923+
"tencentcloud_lighthouse_snapshot": resourceTencentCloudLighthouseSnapshot(),
1924+
"tencentcloud_lighthouse_apply_instance_snapshot": resourceTencentCloudLighthouseApplyInstanceSnapshot(),
19211925
},
19221926

19231927
ConfigureFunc: providerConfigure,
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
Provides a resource to create a lighthouse apply_instance_snapshot
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_lighthouse_apply_instance_snapshot" "apply_instance_snapshot" {
8+
instance_id = "lhins-123456"
9+
snapshot_id = "lhsnap-123456"
10+
}
11+
```
12+
*/
13+
package tencentcloud
14+
15+
import (
16+
"log"
17+
"time"
18+
19+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
20+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
21+
lighthouse "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lighthouse/v20200324"
22+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
23+
)
24+
25+
func resourceTencentCloudLighthouseApplyInstanceSnapshot() *schema.Resource {
26+
return &schema.Resource{
27+
Create: resourceTencentCloudLighthouseApplyInstanceSnapshotCreate,
28+
Read: resourceTencentCloudLighthouseApplyInstanceSnapshotRead,
29+
Delete: resourceTencentCloudLighthouseApplyInstanceSnapshotDelete,
30+
31+
Schema: map[string]*schema.Schema{
32+
"instance_id": {
33+
Required: true,
34+
ForceNew: true,
35+
Type: schema.TypeString,
36+
Description: "Instance ID.",
37+
},
38+
39+
"snapshot_id": {
40+
Required: true,
41+
ForceNew: true,
42+
Type: schema.TypeString,
43+
Description: "Snapshot ID.",
44+
},
45+
},
46+
}
47+
}
48+
49+
func resourceTencentCloudLighthouseApplyInstanceSnapshotCreate(d *schema.ResourceData, meta interface{}) error {
50+
defer logElapsed("resource.tencentcloud_lighthouse_apply_instance_snapshot.create")()
51+
defer inconsistentCheck(d, meta)()
52+
53+
logId := getLogId(contextNil)
54+
55+
var (
56+
request = lighthouse.NewApplyInstanceSnapshotRequest()
57+
snapshotId string
58+
instanceId string
59+
)
60+
if v, ok := d.GetOk("instance_id"); ok {
61+
instanceId = v.(string)
62+
request.InstanceId = helper.String(instanceId)
63+
}
64+
65+
if v, ok := d.GetOk("snapshot_id"); ok {
66+
snapshotId = v.(string)
67+
request.SnapshotId = helper.String(snapshotId)
68+
}
69+
70+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
71+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseLighthouseClient().ApplyInstanceSnapshot(request)
72+
if e != nil {
73+
return retryError(e)
74+
} else {
75+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
76+
}
77+
return nil
78+
})
79+
if err != nil {
80+
log.Printf("[CRITAL]%s operate lighthouse applyInstanceSnapshot failed, reason:%+v", logId, err)
81+
return err
82+
}
83+
84+
d.SetId(instanceId + FILED_SP + snapshotId)
85+
86+
service := LightHouseService{client: meta.(*TencentCloudClient).apiV3Conn}
87+
88+
conf := BuildStateChangeConf([]string{}, []string{"SUCCESS"}, 20*readRetryTimeout, time.Second, service.LighthouseApplySnapshotStateRefreshFunc(snapshotId, []string{}))
89+
90+
if _, e := conf.WaitForState(); e != nil {
91+
return e
92+
}
93+
94+
return resourceTencentCloudLighthouseApplyInstanceSnapshotRead(d, meta)
95+
}
96+
97+
func resourceTencentCloudLighthouseApplyInstanceSnapshotRead(d *schema.ResourceData, meta interface{}) error {
98+
defer logElapsed("resource.tencentcloud_lighthouse_apply_instance_snapshot.read")()
99+
defer inconsistentCheck(d, meta)()
100+
101+
return nil
102+
}
103+
104+
func resourceTencentCloudLighthouseApplyInstanceSnapshotDelete(d *schema.ResourceData, meta interface{}) error {
105+
defer logElapsed("resource.tencentcloud_lighthouse_apply_instance_snapshot.delete")()
106+
defer inconsistentCheck(d, meta)()
107+
108+
return nil
109+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudLighthouseApplyInstanceSnapshotResource_basic(t *testing.T) {
10+
resource.Test(t, resource.TestCase{
11+
PreCheck: func() {
12+
testAccPreCheckCommon(t, ACCOUNT_TYPE_PREPAY)
13+
},
14+
Providers: testAccProviders,
15+
Steps: []resource.TestStep{
16+
{
17+
Config: testAccLighthouseApplyInstanceSnapshot,
18+
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttrSet("tencentcloud_lighthouse_apply_instance_snapshot.apply_instance_snapshot", "id")),
19+
},
20+
},
21+
})
22+
}
23+
24+
const testAccLighthouseApplyInstanceSnapshot = DefaultLighthoustVariables + `
25+
resource "tencentcloud_lighthouse_apply_instance_snapshot" "apply_instance_snapshot" {
26+
instance_id = var.lighthouse_id
27+
snapshot_id = var.lighthouse_snapshot_id
28+
}
29+
`
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
/*
2+
Provides a resource to create a lighthouse snapshot
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_lighthouse_snapshot" "snapshot" {
8+
instance_id = "lhins-acd1234"
9+
snapshot_name = "snap_20200903"
10+
}
11+
```
12+
*/
13+
package tencentcloud
14+
15+
import (
16+
"context"
17+
"log"
18+
"time"
19+
20+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
21+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
22+
lighthouse "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lighthouse/v20200324"
23+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
24+
)
25+
26+
func resourceTencentCloudLighthouseSnapshot() *schema.Resource {
27+
return &schema.Resource{
28+
Create: resourceTencentCloudLighthouseSnapshotCreate,
29+
Read: resourceTencentCloudLighthouseSnapshotRead,
30+
Update: resourceTencentCloudLighthouseSnapshotUpdate,
31+
Delete: resourceTencentCloudLighthouseSnapshotDelete,
32+
33+
Schema: map[string]*schema.Schema{
34+
"instance_id": {
35+
Required: true,
36+
ForceNew: true,
37+
Type: schema.TypeString,
38+
Description: "ID of the instance for which to create a snapshot.",
39+
},
40+
41+
"snapshot_name": {
42+
Optional: true,
43+
Type: schema.TypeString,
44+
Description: "Snapshot name, which can contain up to 60 characters.",
45+
},
46+
},
47+
}
48+
}
49+
50+
func resourceTencentCloudLighthouseSnapshotCreate(d *schema.ResourceData, meta interface{}) error {
51+
defer logElapsed("resource.tencentcloud_lighthouse_snapshot.create")()
52+
defer inconsistentCheck(d, meta)()
53+
54+
logId := getLogId(contextNil)
55+
56+
var (
57+
request = lighthouse.NewCreateInstanceSnapshotRequest()
58+
response = lighthouse.NewCreateInstanceSnapshotResponse()
59+
snapshotId string
60+
)
61+
request.InstanceId = helper.String(d.Get("instance_id").(string))
62+
63+
if v, ok := d.GetOk("snapshot_name"); ok {
64+
request.SnapshotName = helper.String(v.(string))
65+
}
66+
67+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
68+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseLighthouseClient().CreateInstanceSnapshot(request)
69+
if e != nil {
70+
return retryError(e)
71+
} else {
72+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
73+
}
74+
response = result
75+
return nil
76+
})
77+
if err != nil {
78+
log.Printf("[CRITAL]%s create lighthouse snapshot failed, reason:%+v", logId, err)
79+
return err
80+
}
81+
82+
snapshotId = *response.Response.SnapshotId
83+
d.SetId(snapshotId)
84+
85+
service := LightHouseService{client: meta.(*TencentCloudClient).apiV3Conn}
86+
87+
conf := BuildStateChangeConf([]string{}, []string{"NORMAL"}, 20*readRetryTimeout, time.Second, service.LighthouseSnapshotStateRefreshFunc(d.Id(), []string{}))
88+
89+
if _, e := conf.WaitForState(); e != nil {
90+
return e
91+
}
92+
93+
return resourceTencentCloudLighthouseSnapshotRead(d, meta)
94+
}
95+
96+
func resourceTencentCloudLighthouseSnapshotRead(d *schema.ResourceData, meta interface{}) error {
97+
defer logElapsed("resource.tencentcloud_lighthouse_snapshot.read")()
98+
defer inconsistentCheck(d, meta)()
99+
100+
logId := getLogId(contextNil)
101+
102+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
103+
104+
service := LightHouseService{client: meta.(*TencentCloudClient).apiV3Conn}
105+
106+
snapshotId := d.Id()
107+
108+
snapshot, err := service.DescribeLighthouseSnapshotById(ctx, snapshotId)
109+
if err != nil {
110+
return err
111+
}
112+
113+
if snapshot == nil {
114+
d.SetId("")
115+
log.Printf("[WARN]%s resource `LighthouseSnapshot` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
116+
return nil
117+
}
118+
119+
if snapshot.SnapshotName != nil {
120+
_ = d.Set("snapshot_name", snapshot.SnapshotName)
121+
}
122+
123+
return nil
124+
}
125+
126+
func resourceTencentCloudLighthouseSnapshotUpdate(d *schema.ResourceData, meta interface{}) error {
127+
defer logElapsed("resource.tencentcloud_lighthouse_snapshot.update")()
128+
defer inconsistentCheck(d, meta)()
129+
130+
logId := getLogId(contextNil)
131+
132+
request := lighthouse.NewModifySnapshotAttributeRequest()
133+
134+
snapshotId := d.Id()
135+
136+
request.SnapshotId = &snapshotId
137+
138+
if d.HasChange("snapshot_name") {
139+
if v, ok := d.GetOk("snapshot_name"); ok {
140+
request.SnapshotName = helper.String(v.(string))
141+
}
142+
}
143+
144+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
145+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseLighthouseClient().ModifySnapshotAttribute(request)
146+
if e != nil {
147+
return retryError(e)
148+
} else {
149+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
150+
}
151+
return nil
152+
})
153+
if err != nil {
154+
log.Printf("[CRITAL]%s update lighthouse snapshot failed, reason:%+v", logId, err)
155+
return err
156+
}
157+
158+
return resourceTencentCloudLighthouseSnapshotRead(d, meta)
159+
}
160+
161+
func resourceTencentCloudLighthouseSnapshotDelete(d *schema.ResourceData, meta interface{}) error {
162+
defer logElapsed("resource.tencentcloud_lighthouse_snapshot.delete")()
163+
defer inconsistentCheck(d, meta)()
164+
165+
logId := getLogId(contextNil)
166+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
167+
168+
service := LightHouseService{client: meta.(*TencentCloudClient).apiV3Conn}
169+
snapshotId := d.Id()
170+
171+
if err := service.DeleteLighthouseSnapshotById(ctx, snapshotId); err != nil {
172+
return err
173+
}
174+
175+
return nil
176+
}

0 commit comments

Comments
 (0)