forked from aliyun/terraform-provider-alicloud
/
resource_alicloud_log_project.go
129 lines (113 loc) · 4 KB
/
resource_alicloud_log_project.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
package alicloud
import (
"fmt"
"time"
sls "github.com/aliyun/aliyun-log-go-sdk"
"github.com/terraform-providers/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAlicloudLogProject() *schema.Resource {
return &schema.Resource{
Create: resourceAlicloudLogProjectCreate,
Read: resourceAlicloudLogProjectRead,
Update: resourceAlicloudLogProjectUpdate,
Delete: resourceAlicloudLogProjectDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
},
}
}
func resourceAlicloudLogProjectCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
invoker := NewInvoker()
invoker.AddCatcher(SlsClientTimeoutCatcher)
if err := invoker.Run(func() error {
raw, err := client.WithLogClient(func(slsClient *sls.Client) (interface{}, error) {
return slsClient.CreateProject(d.Get("name").(string), d.Get("description").(string))
})
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, "log_project", "CreateProject", AliyunLogGoSdkERROR)
}
project, _ := raw.(*sls.LogProject)
d.SetId(project.Name)
return nil
}); err != nil {
return WrapError(err)
}
return resourceAlicloudLogProjectRead(d, meta)
}
func resourceAlicloudLogProjectRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
invoker := NewInvoker()
invoker.AddCatcher(SlsClientTimeoutCatcher)
return invoker.Run(func() error {
raw, err := client.WithLogClient(func(slsClient *sls.Client) (interface{}, error) {
return slsClient.GetProject(d.Id())
})
if err != nil {
if IsExceptedError(err, ProjectNotExist) {
d.SetId("")
return nil
}
return WrapErrorf(err, DefaultErrorMsg, "log_project", "GetProject", AliyunLogGoSdkERROR)
}
project, _ := raw.(*sls.LogProject)
d.Set("name", project.Name)
d.Set("description", project.Description)
return nil
})
}
func resourceAlicloudLogProjectUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
if d.HasChange("description") {
_, err := client.WithLogClient(func(slsClient *sls.Client) (interface{}, error) {
return slsClient.UpdateProject(d.Get("name").(string), d.Get("description").(string))
})
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, "log_project", "UpdateProject", AliyunLogGoSdkERROR)
}
}
return resourceAlicloudLogProjectRead(d, meta)
}
func resourceAlicloudLogProjectDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
return resource.Retry(3*time.Minute, func() *resource.RetryError {
_, err := client.WithLogClient(func(slsClient *sls.Client) (interface{}, error) {
return nil, slsClient.DeleteProject(d.Id())
})
if err != nil {
if IsExceptedErrors(err, []string{LogClientTimeout, LogRequestTimeout}) {
return resource.RetryableError(BuildWrapError("DeleteProject", d.Id(), AliyunLogGoSdkERROR, err, ""))
}
if !IsExceptedErrors(err, []string{ProjectNotExist}) {
return resource.NonRetryableError(BuildWrapError("DeleteProject", d.Id(), AliyunLogGoSdkERROR, err, ""))
}
}
raw, err := client.WithLogClient(func(slsClient *sls.Client) (interface{}, error) {
return slsClient.CheckProjectExist(d.Id())
})
if err != nil {
if IsExceptedErrors(err, []string{LogClientTimeout}) {
return resource.RetryableError(BuildWrapError("CheckProjectExist", d.Id(), AliyunLogGoSdkERROR, err, ""))
}
return resource.NonRetryableError(BuildWrapError("CheckProjectExist", d.Id(), AliyunLogGoSdkERROR, err, ""))
}
exist, _ := raw.(bool)
if !exist {
return nil
}
return resource.RetryableError(BuildWrapError("DeleteProject", d.Id(), ProviderERROR, fmt.Errorf("Timeout"), ""))
})
}