forked from aliyun/terraform-provider-alicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_alicloud_log_project.go
117 lines (101 loc) · 3.28 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
package alicloud
import (
"fmt"
"time"
"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": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
}
}
func resourceAlicloudLogProjectCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
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 fmt.Errorf("CreateProject got an error: %#v.", err)
}
project, _ := raw.(*sls.LogProject)
d.SetId(project.Name)
return resourceAlicloudLogProjectRead(d, meta)
}
func resourceAlicloudLogProjectRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
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 fmt.Errorf("GetProject got an error: %#v.", err)
}
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.(*aliyunclient.AliyunClient)
//
// d.Partial(true)
//
// if d.HasChange("description") {
// if err := client.logconn.UpdateProject(d.Id(), d.Get("description").(string)); err != nil {
// return fmt.Errorf("UpdateProject got an error: %#v", err)
// }
// d.SetPartial("description")
// }
//
// d.Partial(false)
//
// 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{ProjectNotExist}) {
return resource.NonRetryableError(fmt.Errorf("Deleting log project got an error: %#v", err))
}
}
raw, err := client.WithLogClient(func(slsClient *sls.Client) (interface{}, error) {
return slsClient.CheckProjectExist(d.Id())
})
if err != nil {
return resource.NonRetryableError(fmt.Errorf("While deleting log project, checking project existing got an error: %#v.", err))
}
exist, _ := raw.(bool)
if !exist {
return nil
}
return resource.RetryableError(fmt.Errorf("Deleting log project %s timeout.", d.Id()))
})
}