This repository has been archived by the owner on Jul 17, 2020. It is now read-only.
/
resource_tc_scf_namespace.go
147 lines (116 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
Provide a resource to create a SCF namespace.
Example Usage
```hcl
resource "tencentcloud_scf_namespace" "foo" {
namespace = "ci-test-scf"
}
```
Import
SCF namespace can be imported, e.g.
```
$ terraform import tencentcloud_scf_function.test default
```
*/
package tencentcloud
import (
"context"
"log"
"github.com/hashicorp/terraform-plugin-sdk/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 logElapsed("resource.tencentcloud_scf_namespace.create")()
logId := getLogId(contextNil)
ctx := context.WithValue(context.TODO(), logIdKey, logId)
service := ScfService{client: m.(*TencentCloudClient).apiV3Conn}
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 logElapsed("resource.tencentcloud_scf_namespace.read")()
defer inconsistentCheck(d, m)()
logId := getLogId(contextNil)
ctx := context.WithValue(context.TODO(), logIdKey, logId)
service := ScfService{client: m.(*TencentCloudClient).apiV3Conn}
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 logElapsed("resource.tencentcloud_scf_namespace.update")()
logId := getLogId(contextNil)
ctx := context.WithValue(context.TODO(), logIdKey, logId)
service := ScfService{client: m.(*TencentCloudClient).apiV3Conn}
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 logElapsed("resource.tencentcloud_scf_namespace.delete")()
logId := getLogId(contextNil)
ctx := context.WithValue(context.TODO(), logIdKey, logId)
service := ScfService{client: m.(*TencentCloudClient).apiV3Conn}
return service.DeleteNamespace(ctx, d.Id())
}