-
Notifications
You must be signed in to change notification settings - Fork 135
/
resource_tc_scf_namespace.go
130 lines (103 loc) · 3.77 KB
/
resource_tc_scf_namespace.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package scf
import (
"context"
"log"
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func ResourceTencentCloudScfNamespace() *schema.Resource {
return &schema.Resource{
Create: resourceTencentCloudScfNamespaceCreate,
Read: resourceTencentCloudScfNamespaceRead,
Update: resourceTencentCloudScfNamespaceUpdate,
Delete: resourceTencentCloudScfNamespaceDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"namespace": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Name of the SCF namespace.",
},
"description": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "Description of the SCF namespace.",
},
// computed
"create_time": {
Type: schema.TypeString,
Computed: true,
Description: "SCF namespace creation time.",
},
"modify_time": {
Type: schema.TypeString,
Computed: true,
Description: "SCF namespace last modified time.",
},
"type": {
Type: schema.TypeString,
Computed: true,
Description: "SCF namespace type.",
},
},
}
}
func resourceTencentCloudScfNamespaceCreate(d *schema.ResourceData, m interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_scf_namespace.create")()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
service := ScfService{client: m.(tccommon.ProviderMeta).GetAPIV3Conn()}
namespace := d.Get("namespace").(string)
desc := d.Get("description").(string)
if err := service.CreateNamespace(ctx, namespace, desc); err != nil {
log.Printf("[CRITAL]%s create namespace failed: %+v", logId, err)
return err
}
d.SetId(namespace)
return resourceTencentCloudScfNamespaceRead(d, m)
}
func resourceTencentCloudScfNamespaceRead(d *schema.ResourceData, m interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_scf_namespace.read")()
defer tccommon.InconsistentCheck(d, m)()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
service := ScfService{client: m.(tccommon.ProviderMeta).GetAPIV3Conn()}
id := d.Id()
namespace, err := service.DescribeNamespace(ctx, id)
if err != nil {
log.Printf("[CRITAL]%s read namespace failed: %+v", logId, err)
return err
}
if namespace == nil {
d.SetId("")
return nil
}
_ = d.Set("namespace", namespace.Name)
_ = d.Set("description", namespace.Description)
_ = d.Set("create_time", namespace.AddTime)
_ = d.Set("modify_time", namespace.ModTime)
_ = d.Set("type", namespace.Type)
return nil
}
func resourceTencentCloudScfNamespaceUpdate(d *schema.ResourceData, m interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_scf_namespace.update")()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
service := ScfService{client: m.(tccommon.ProviderMeta).GetAPIV3Conn()}
if err := service.ModifyNamespace(ctx, d.Id(), d.Get("description").(string)); err != nil {
log.Printf("[CRITAL]%s update namespace description failed: %+v", logId, err)
return err
}
return resourceTencentCloudScfNamespaceRead(d, m)
}
func resourceTencentCloudScfNamespaceDelete(d *schema.ResourceData, m interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_scf_namespace.delete")()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
service := ScfService{client: m.(tccommon.ProviderMeta).GetAPIV3Conn()}
return service.DeleteNamespace(ctx, d.Id())
}